aboutsummaryrefslogtreecommitdiffstats
path: root/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/EntityMgrTransTest.java
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2018-02-14 12:52:31 -0500
committerJim Hahn <jrh3@att.com>2018-02-14 16:14:17 -0500
commitc83e35bab5aa44f01cb7a9089701ef963ee0c131 (patch)
treed76dfcdb85ecdde91202da22053876ab57f46204 /feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/EntityMgrTransTest.java
parentc5d5a9058d47eca9d4ac90308514ff8f1f9d0ca3 (diff)
Replace bitronix and eclipselink in persistence
Replaced bitronix transaction manager, which is not intended for production, with jboss transaction manager. Eliminated eclipselink so that only hibernate is used for persistence for both JPA and drools-persistence. Added more test cases to EntityMgrTrans to provide coverage for various exception types. Moved object store to features/session-persistence/jta. Wrapped RuntimeException in specific type. Modified test to throw specific exception type. Converted GenSchema from an @Test to a main(). Logged caught exceptions in junit tests. Change-Id: I4b02efc8da43d20b2dbb3c0b25adc382e80474ec Issue-ID: POLICY-191 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/EntityMgrTransTest.java')
-rw-r--r--feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/EntityMgrTransTest.java335
1 files changed, 274 insertions, 61 deletions
diff --git a/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/EntityMgrTransTest.java b/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/EntityMgrTransTest.java
index 0165b1e4..9c9a30b3 100644
--- a/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/EntityMgrTransTest.java
+++ b/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/EntityMgrTransTest.java
@@ -25,37 +25,69 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.doThrow;
import javax.persistence.EntityManager;
-import javax.persistence.EntityTransaction;
+import javax.transaction.HeuristicMixedException;
+import javax.transaction.HeuristicRollbackException;
+import javax.transaction.NotSupportedException;
+import javax.transaction.RollbackException;
+import javax.transaction.Status;
+import javax.transaction.SystemException;
+import javax.transaction.UserTransaction;
+import org.junit.AfterClass;
import org.junit.Before;
+import org.junit.BeforeClass;
import org.junit.Test;
-import org.onap.policy.drools.persistence.EntityMgrTrans;
+import org.onap.policy.drools.persistence.EntityMgrTrans.EntityMgrException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
public class EntityMgrTransTest {
-
- private EntityTransaction trans;
+
+ private static final Logger logger = LoggerFactory.getLogger(PersistenceFeatureTest.class);
+
+ private static UserTransaction savetrans;
+
+ private UserTransaction trans;
private EntityManager mgr;
+ @BeforeClass
+ public static void setUpBeforeClass() {
+ System.setProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir", "target/tm");
+ System.setProperty("ObjectStoreEnvironmentBean.objectStoreDir", "target/tm");
+
+ savetrans = EntityMgrTrans.getUserTrans();
+ }
+
+ @AfterClass
+ public static void tearDownAfterClass() {
+ EntityMgrTrans.setUserTrans(savetrans);
+ }
+
@Before
public void setUp() throws Exception {
- trans = mock(EntityTransaction.class);
+ trans = mock(UserTransaction.class);
mgr = mock(EntityManager.class);
-
- when(mgr.getTransaction()).thenReturn(trans);
- }
-
+ EntityMgrTrans.setUserTrans(trans);
+ }
/**
* Verifies that the constructor starts a transaction, but does not do
* anything extra before being closed.
+ *
+ * @throws Exception
*/
@Test
- public void testEntityMgrTrans() {
+ public void testEntityMgrTrans() throws Exception {
+
+ when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
+
EntityMgrTrans t = new EntityMgrTrans(mgr);
-
+
// verify that transaction was started
verify(trans).begin();
@@ -63,20 +95,50 @@ public class EntityMgrTransTest {
verify(trans, never()).commit();
verify(trans, never()).rollback();
verify(mgr, never()).close();
-
+
t.close();
}
+ @Test(expected = EntityMgrException.class)
+ public void testEntityMgrTrans_RtEx() throws Exception {
+
+ doThrow(new IllegalArgumentException("expected exception")).when(trans).begin();
+
+ try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
+
+ }
+ }
+
+ @Test(expected = EntityMgrException.class)
+ public void testEntityMgrTrans_NotSuppEx() throws Exception {
+
+ doThrow(new NotSupportedException("expected exception")).when(trans).begin();
+
+ try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
+
+ }
+ }
+
+ @Test(expected = EntityMgrException.class)
+ public void testEntityMgrTrans_SysEx() throws Exception {
+
+ doThrow(new SystemException("expected exception")).when(trans).begin();
+
+ try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
+
+ }
+ }
+
/**
- * Verifies that the transaction is rolled back and the manager is
- * closed when and a transaction is active.
+ * Verifies that the transaction is rolled back and the manager is closed
+ * when and a transaction is active.
*/
@Test
- public void testClose_Active() {
+ public void testClose_Active() throws Exception {
EntityMgrTrans t = new EntityMgrTrans(mgr);
- when(trans.isActive()).thenReturn(true);
-
+ when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
+
t.close();
// closed and rolled back, but not committed
@@ -87,14 +149,14 @@ public class EntityMgrTransTest {
/**
* Verifies that the manager is closed, but that the transaction is
- * <i>not</i> rolled back and when and no transaction is active.
+ * <i>not</i> rolled back when and no transaction is active.
*/
@Test
- public void testClose_Inactive() {
+ public void testClose_Inactive() throws Exception {
EntityMgrTrans t = new EntityMgrTrans(mgr);
- when(trans.isActive()).thenReturn(false);
-
+ when(trans.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION);
+
t.close();
// closed, but not committed or rolled back
@@ -103,16 +165,49 @@ public class EntityMgrTransTest {
verify(trans, never()).rollback();
}
+ @Test(expected = EntityMgrException.class)
+ public void testClose_IllStateEx() throws Exception {
+
+ when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
+ doThrow(new IllegalStateException("expected exception")).when(trans).rollback();
+
+ try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
+
+ }
+ }
+
+ @Test(expected = EntityMgrException.class)
+ public void testClose_SecEx() throws Exception {
+
+ when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
+ doThrow(new SecurityException("expected exception")).when(trans).rollback();
+
+ try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
+
+ }
+ }
+
+ @Test(expected = EntityMgrException.class)
+ public void testClose_SysEx() throws Exception {
+
+ when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
+ doThrow(new SystemException("expected exception")).when(trans).rollback();
+
+ try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
+
+ }
+ }
+
/**
- * Verifies that the manager is closed and the transaction rolled back
- * when "try" block exits normally and a transaction is active.
+ * Verifies that the manager is closed and the transaction rolled back when
+ * "try" block exits normally and a transaction is active.
*/
@Test
- public void testClose_TryWithoutExcept_Active() {
- when(trans.isActive()).thenReturn(true);
-
- try(EntityMgrTrans t = new EntityMgrTrans(mgr)) {
-
+ public void testClose_TryWithoutExcept_Active() throws Exception {
+ when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
+
+ try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
+
}
// closed and rolled back, but not committed
@@ -123,15 +218,16 @@ public class EntityMgrTransTest {
/**
* Verifies that the manager is closed, but that the transaction is
- * <i>not</i> rolled back when "try" block exits normally and no
- * transaction is active.
+ * <i>not</i> rolled back when "try" block exits normally and no transaction
+ * is active.
*/
@Test
- public void testClose_TryWithoutExcept_Inactive() {
- when(trans.isActive()).thenReturn(false);
-
- try(EntityMgrTrans t = new EntityMgrTrans(mgr)) {
-
+ public void testClose_TryWithoutExcept_Inactive() throws Exception {
+
+ when(trans.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION);
+
+ try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
+
}
// closed, but not rolled back or committed
@@ -141,19 +237,21 @@ public class EntityMgrTransTest {
}
/**
- * Verifies that the manager is closed and the transaction rolled back
- * when "try" block throws an exception and a transaction is active.
+ * Verifies that the manager is closed and the transaction rolled back when
+ * "try" block throws an exception and a transaction is active.
*/
@Test
- public void testClose_TryWithExcept_Active() {
- when(trans.isActive()).thenReturn(true);
-
+ public void testClose_TryWithExcept_Active() throws Exception {
+
+ when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
+
try {
- try(EntityMgrTrans t = new EntityMgrTrans(mgr)) {
- throw new Exception("expected exception");
+ try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
+ throw new SystemException("expected exception");
}
-
+
} catch (Exception e) {
+ logger.trace("expected exception", e);
}
// closed and rolled back, but not committed
@@ -168,15 +266,17 @@ public class EntityMgrTransTest {
* transaction is active.
*/
@Test
- public void testClose_TryWithExcept_Inactive() {
- when(trans.isActive()).thenReturn(false);
-
+ public void testClose_TryWithExcept_Inactive() throws Exception {
+
+ when(trans.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION);
+
try {
- try(EntityMgrTrans t = new EntityMgrTrans(mgr)) {
- throw new Exception("expected exception");
+ try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
+ throw new SystemException("expected exception");
}
-
+
} catch (Exception e) {
+ logger.trace("expected exception", e);
}
// closed, but not rolled back or committed
@@ -186,20 +286,23 @@ public class EntityMgrTransTest {
}
/**
- * Verifies that commit() only commits, and that the subsequent close()
- * does not re-commit.
+ * Verifies that commit() only commits, and that the subsequent close() does
+ * not re-commit.
*/
@Test
- public void testCommit() {
+ public void testCommit() throws Exception {
EntityMgrTrans t = new EntityMgrTrans(mgr);
-
+ when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
+
t.commit();
-
+
+ when(trans.getStatus()).thenReturn(Status.STATUS_COMMITTED);
+
// committed, but not closed or rolled back
verify(trans).commit();
verify(trans, never()).rollback();
verify(mgr, never()).close();
-
+
// closed, but not re-committed
t.close();
@@ -207,21 +310,90 @@ public class EntityMgrTransTest {
verify(mgr).close();
}
+ @Test(expected = EntityMgrException.class)
+ public void testCommit_SecEx() throws Exception {
+
+ when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
+ doThrow(new SecurityException("expected exception")).when(trans).commit();
+
+ try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
+ t.commit();
+ }
+ }
+
+ @Test(expected = EntityMgrException.class)
+ public void testCommit_IllStateEx() throws Exception {
+
+ when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
+ doThrow(new IllegalStateException("expected exception")).when(trans).commit();
+
+ try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
+ t.commit();
+ }
+ }
+
+ @Test(expected = EntityMgrException.class)
+ public void testCommit_RbEx() throws Exception {
+
+ when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
+ doThrow(new RollbackException("expected exception")).when(trans).commit();
+
+ try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
+ t.commit();
+ }
+ }
+
+ @Test(expected = EntityMgrException.class)
+ public void testCommit_HmEx() throws Exception {
+
+ when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
+ doThrow(new HeuristicMixedException("expected exception")).when(trans).commit();
+
+ try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
+ t.commit();
+ }
+ }
+
+ @Test(expected = EntityMgrException.class)
+ public void testCommit_HrbEx() throws Exception {
+
+ when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
+ doThrow(new HeuristicRollbackException("expected exception")).when(trans).commit();
+
+ try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
+ t.commit();
+ }
+ }
+
+ @Test(expected = EntityMgrException.class)
+ public void testCommit_SysEx() throws Exception {
+
+ when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
+ doThrow(new SystemException("expected exception")).when(trans).commit();
+
+ try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
+ t.commit();
+ }
+ }
+
/**
- * Verifies that rollback() only rolls back, and that the subsequent
- * close() does not re-roll back.
+ * Verifies that rollback() only rolls back, and that the subsequent close()
+ * does not re-roll back.
*/
@Test
- public void testRollback() {
+ public void testRollback() throws Exception {
EntityMgrTrans t = new EntityMgrTrans(mgr);
-
+ when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
+
t.rollback();
-
+
+ when(trans.getStatus()).thenReturn(Status.STATUS_ROLLEDBACK);
+
// rolled back, but not closed or committed
verify(trans, never()).commit();
verify(trans).rollback();
verify(mgr, never()).close();
-
+
// closed, but not re-rolled back
t.close();
@@ -229,4 +401,45 @@ public class EntityMgrTransTest {
verify(mgr).close();
}
+ @Test(expected = EntityMgrException.class)
+ public void testRollback_IllStateEx() throws Exception {
+
+ when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
+ doThrow(new IllegalStateException("expected exception")).when(trans).rollback();
+
+ try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
+ t.rollback();
+ }
+ }
+
+ @Test(expected = EntityMgrException.class)
+ public void testRollback_SecEx() throws Exception {
+
+ when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
+ doThrow(new SecurityException("expected exception")).when(trans).rollback();
+
+ try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
+ t.rollback();
+ }
+ }
+
+ @Test(expected = EntityMgrException.class)
+ public void testRollback_SysEx() throws Exception {
+
+ when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
+ doThrow(new SystemException("expected exception")).when(trans).rollback();
+
+ try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
+ t.rollback();
+ }
+ }
+
+ @Test
+ public void testEntityMgrException() {
+ SecurityException secex = new SecurityException("expected exception");
+ EntityMgrException ex = new EntityMgrException(secex);
+
+ assertEquals(secex, ex.getCause());
+
+ }
}