From 3b50594851f420c6acb3d293c019f4376d274be2 Mon Sep 17 00:00:00 2001
From: srosse <none@none>
Date: Tue, 19 Nov 2013 15:18:29 +0100
Subject: [PATCH] OO-868: reload a reference of the authentication before
 deleting it, add unit tests

---
 .../basesecurity/BaseSecurityManager.java     | 12 +++++-
 .../basesecurity/BaseSecurityManagerTest.java | 37 +++++++++++++++++++
 2 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/olat/basesecurity/BaseSecurityManager.java b/src/main/java/org/olat/basesecurity/BaseSecurityManager.java
index bca22741ee5..aca0b643b4d 100644
--- a/src/main/java/org/olat/basesecurity/BaseSecurityManager.java
+++ b/src/main/java/org/olat/basesecurity/BaseSecurityManager.java
@@ -36,6 +36,7 @@ import java.util.Locale;
 import java.util.Map;
 import java.util.UUID;
 
+import javax.persistence.EntityNotFoundException;
 import javax.persistence.LockModeType;
 import javax.persistence.TemporalType;
 import javax.persistence.TypedQuery;
@@ -62,8 +63,8 @@ import org.olat.core.id.User;
 import org.olat.core.id.UserConstants;
 import org.olat.core.logging.AssertException;
 import org.olat.core.manager.BasicManager;
-import org.olat.core.util.Encoder.Algorithm;
 import org.olat.core.util.Encoder;
+import org.olat.core.util.Encoder.Algorithm;
 import org.olat.core.util.Util;
 import org.olat.core.util.coordinate.CoordinatorManager;
 import org.olat.core.util.coordinate.SyncerCallback;
@@ -1541,7 +1542,14 @@ public class BaseSecurityManager extends BasicManager implements BaseSecurity {
 	 */
 	@Override
 	public void deleteAuthentication(Authentication auth) {
-		dbInstance.getCurrentEntityManager().remove(auth);
+		if(auth == null || auth.getKey() == null) return;//nothing to do
+		try {
+			AuthenticationImpl authRef = dbInstance.getCurrentEntityManager()
+					.getReference(AuthenticationImpl.class, auth.getKey());
+			dbInstance.getCurrentEntityManager().remove(authRef);
+		} catch (EntityNotFoundException e) {
+			logError("", e);
+		}
 	}
 
 	/**
diff --git a/src/test/java/org/olat/basesecurity/BaseSecurityManagerTest.java b/src/test/java/org/olat/basesecurity/BaseSecurityManagerTest.java
index 7110c9d2c7f..8ca595a80ec 100644
--- a/src/test/java/org/olat/basesecurity/BaseSecurityManagerTest.java
+++ b/src/test/java/org/olat/basesecurity/BaseSecurityManagerTest.java
@@ -40,6 +40,7 @@ import org.olat.core.id.Identity;
 import org.olat.core.id.Roles;
 import org.olat.core.id.User;
 import org.olat.core.id.UserConstants;
+import org.olat.core.util.Encoder;
 import org.olat.resource.OLATResource;
 import org.olat.test.JunitTestHelper;
 import org.olat.test.OlatTestCase;
@@ -897,4 +898,40 @@ public class BaseSecurityManagerTest extends OlatTestCase {
 		dbInstance.getCurrentEntityManager().merge(sgmsi);
 		dbInstance.commitAndCloseSession();	
 	}
+	
+	@Test
+	public void deleteAuthentication() {
+		Identity identity = JunitTestHelper.createAndPersistIdentityAsUser("auth-del-" + UUID.randomUUID().toString());
+		Authentication auth = securityManager.createAndPersistAuthentication(identity, "del-test", identity.getName(), "secret", Encoder.Algorithm.sha512);
+		dbInstance.commitAndCloseSession();
+		Assert.assertNotNull(auth);
+		
+		//reload and check
+		Authentication reloadedAuth = securityManager.findAuthentication(identity, "del-test");
+		Assert.assertNotNull(reloadedAuth);
+		Assert.assertEquals(auth, reloadedAuth);
+		dbInstance.commitAndCloseSession();
+		
+		//delete
+		securityManager.deleteAuthentication(auth);
+	}
+	
+	@Test
+	public void deleteAuthentication_checkTransactionSurvive() {
+		Identity identity = JunitTestHelper.createAndPersistIdentityAsUser("auth-del-" + UUID.randomUUID().toString());
+		Authentication auth = securityManager.createAndPersistAuthentication(identity, "del-test", identity.getName(), "secret", Encoder.Algorithm.sha512);
+		dbInstance.commitAndCloseSession();
+		Assert.assertNotNull(auth);
+		
+		//delete
+		securityManager.deleteAuthentication(auth);
+		dbInstance.commitAndCloseSession();
+		
+		//delete deleted auth
+		securityManager.deleteAuthentication(auth);
+		//check that the transaction is not in "rollback" mode
+		Identity reloadedId = securityManager.loadIdentityByKey(identity.getKey());
+		Assert.assertEquals(identity, reloadedId);
+		dbInstance.commitAndCloseSession();
+	}
 }
-- 
GitLab