2 * ============LICENSE_START=======================================================
3 * feature-session-persistence
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.drools.persistence;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
25 import static org.mockito.Mockito.doThrow;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.never;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
32 import java.util.HashMap;
34 import javax.persistence.EntityManager;
35 import javax.persistence.EntityManagerFactory;
36 import javax.persistence.Persistence;
37 import javax.transaction.HeuristicMixedException;
38 import javax.transaction.HeuristicRollbackException;
39 import javax.transaction.NotSupportedException;
40 import javax.transaction.RollbackException;
41 import javax.transaction.Status;
42 import javax.transaction.SystemException;
43 import javax.transaction.UserTransaction;
44 import org.junit.AfterClass;
45 import org.junit.Before;
46 import org.junit.BeforeClass;
47 import org.junit.Test;
48 import org.onap.policy.drools.persistence.EntityMgrTrans.EntityMgrException;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 public class EntityMgrTransTest {
54 private static final Logger logger = LoggerFactory.getLogger(PersistenceFeatureTest.class);
56 private static UserTransaction savetrans;
58 private UserTransaction trans;
59 private EntityManager mgr;
62 * Configure properties for JTA.
65 public static void setUpBeforeClass() {
66 System.setProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir", "target/tm");
67 System.setProperty("ObjectStoreEnvironmentBean.objectStoreDir", "target/tm");
69 savetrans = EntityMgrTrans.getUserTrans();
73 public static void tearDownAfterClass() {
74 EntityMgrTrans.setUserTrans(savetrans);
78 * Creates a mock transaction and entity manager. Resets the "userTrans" field of the
81 * @throws Exception if an error occurs
84 public void setUp() throws Exception {
85 trans = mock(UserTransaction.class);
86 mgr = mock(EntityManager.class);
88 when(trans.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE);
90 EntityMgrTrans.setUserTrans(trans);
94 * Verifies that the constructor starts a transaction, but does not do anything extra
95 * before being closed.
97 * @throws Exception if an error occurs
100 public void testEntityMgrTrans_Inactive() throws Exception {
101 final EntityMgrTrans emt = new EntityMgrTrans(mgr);
103 // verify that transaction was started
104 verify(trans).begin();
105 verify(mgr).joinTransaction();
107 // verify not closed, committed, or rolled back yet
108 verify(trans, never()).commit();
109 verify(trans, never()).rollback();
110 verify(mgr, never()).close();
116 * Verifies that the constructor does not start a transaction, because one is already
119 * @throws Exception if an error occurs
122 public void testEntityMgrTrans_Active() throws Exception {
124 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
126 final EntityMgrTrans emt = new EntityMgrTrans(mgr);
128 // verify that transaction was not re-started started
129 verify(trans, never()).begin();
131 // verify that transaction was joined
132 verify(mgr).joinTransaction();
134 // verify not closed, committed, or rolled back yet
135 verify(trans, never()).commit();
136 verify(trans, never()).rollback();
137 verify(mgr, never()).close();
142 @Test(expected = EntityMgrException.class)
143 public void testEntityMgrTrans_RtEx() throws Exception {
145 doThrow(new IllegalArgumentException("expected exception")).when(trans).begin();
147 try (EntityMgrTrans emt = new EntityMgrTrans(mgr)) {
152 @Test(expected = EntityMgrException.class)
153 public void testEntityMgrTrans_NotSuppEx() throws Exception {
155 doThrow(new NotSupportedException("expected exception")).when(trans).begin();
157 try (EntityMgrTrans emt = new EntityMgrTrans(mgr)) {
162 @Test(expected = EntityMgrException.class)
163 public void testEntityMgrTrans_SysEx() throws Exception {
165 doThrow(new SystemException("expected exception")).when(trans).begin();
167 try (EntityMgrTrans emt = new EntityMgrTrans(mgr)) {
173 * Verifies that the transaction is not rolled back, but the manager is closed when a
174 * transaction is already active.
177 public void testClose_Active() throws Exception {
178 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
180 EntityMgrTrans emt = new EntityMgrTrans(mgr);
183 // closed and rolled back, but not committed
184 verify(trans, never()).commit();
185 verify(trans, never()).rollback();
190 * Verifies that the transaction is rolled back and the manager is closed when a
191 * transaction is begun by the constructor.
194 public void testClose_Begun() throws Exception {
195 EntityMgrTrans emt = new EntityMgrTrans(mgr);
199 // closed and rolled back, but not committed
200 verify(trans, never()).commit();
201 verify(trans).rollback();
206 * Verifies that the manager is closed, but that the transaction is <i>not</i> rolled
207 * back when no transaction is active.
210 public void testClose_Inactive() throws Exception {
211 when(trans.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION);
213 EntityMgrTrans emt = new EntityMgrTrans(mgr);
217 // closed, but not committed or rolled back
219 verify(trans, never()).commit();
220 verify(trans, never()).rollback();
223 @Test(expected = EntityMgrException.class)
224 public void testClose_IllStateEx() throws Exception {
226 doThrow(new IllegalStateException("expected exception")).when(trans).rollback();
228 try (EntityMgrTrans emt = new EntityMgrTrans(mgr)) {
233 @Test(expected = EntityMgrException.class)
234 public void testClose_SecEx() throws Exception {
236 doThrow(new SecurityException("expected exception")).when(trans).rollback();
238 try (EntityMgrTrans emt = new EntityMgrTrans(mgr)) {
243 @Test(expected = EntityMgrException.class)
244 public void testClose_SysEx() throws Exception {
246 doThrow(new SystemException("expected exception")).when(trans).rollback();
248 try (EntityMgrTrans emt = new EntityMgrTrans(mgr)) {
254 * Verifies that the manager is closed and the transaction rolled back when "try"
255 * block exits normally and a transaction is active.
258 public void testClose_TryWithoutExcept_Active() throws Exception {
259 try (EntityMgrTrans emt = new EntityMgrTrans(mgr)) {
263 // closed and rolled back, but not committed
264 verify(trans, never()).commit();
265 verify(trans).rollback();
270 * Verifies that the manager is closed, but that the transaction is <i>not</i> rolled
271 * back when "try" block exits normally and no transaction is active.
274 public void testClose_TryWithoutExcept_Inactive() throws Exception {
276 when(trans.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION);
278 try (EntityMgrTrans emt = new EntityMgrTrans(mgr)) {
282 // closed, but not rolled back or committed
283 verify(trans, never()).commit();
284 verify(trans, never()).rollback();
289 * Verifies that the manager is closed and the transaction rolled back when "try"
290 * block throws an exception and a transaction is active.
293 public void testClose_TryWithExcept_Active() throws Exception {
295 try (EntityMgrTrans emt = new EntityMgrTrans(mgr)) {
296 throw new SystemException("expected exception");
299 } catch (Exception e) {
300 logger.trace("expected exception", e);
303 // closed and rolled back, but not committed
304 verify(trans, never()).commit();
305 verify(trans).rollback();
310 * Verifies that the manager is closed, but that the transaction is <i>not</i> rolled
311 * back when "try" block throws an exception and no transaction is active.
314 public void testClose_TryWithExcept_Inactive() throws Exception {
316 when(trans.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION);
319 try (EntityMgrTrans emt = new EntityMgrTrans(mgr)) {
320 throw new SystemException("expected exception");
323 } catch (Exception e) {
324 logger.trace("expected exception", e);
327 // closed, but not rolled back or committed
328 verify(trans, never()).commit();
329 verify(trans, never()).rollback();
334 * Verifies that commit() only commits, and that the subsequent close() does not
338 public void testCommit() throws Exception {
339 EntityMgrTrans emt = new EntityMgrTrans(mgr);
343 when(trans.getStatus()).thenReturn(Status.STATUS_COMMITTED);
345 // committed, but not closed or rolled back
346 verify(trans).commit();
347 verify(trans, never()).rollback();
348 verify(mgr, never()).close();
350 // closed, but not re-committed
353 verify(trans, times(1)).commit();
358 * Verifies that commit() does nothing, and that the subsequent close() does not
359 * re-commit when a transaction is already active.
362 public void testCommit_Active() throws Exception {
363 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
365 EntityMgrTrans emt = new EntityMgrTrans(mgr);
369 // nothing happened yet
370 verify(trans, never()).commit();
371 verify(trans, never()).rollback();
372 verify(mgr, never()).close();
374 // closed, but not re-committed
377 // still no commit or rollback
378 verify(trans, never()).commit();
379 verify(trans, never()).rollback();
383 @Test(expected = EntityMgrException.class)
384 public void testCommit_SecEx() throws Exception {
386 doThrow(new SecurityException("expected exception")).when(trans).commit();
388 try (EntityMgrTrans emt = new EntityMgrTrans(mgr)) {
393 @Test(expected = EntityMgrException.class)
394 public void testCommit_IllStateEx() throws Exception {
396 doThrow(new IllegalStateException("expected exception")).when(trans).commit();
398 try (EntityMgrTrans emt = new EntityMgrTrans(mgr)) {
403 @Test(expected = EntityMgrException.class)
404 public void testCommit_RbEx() throws Exception {
406 doThrow(new RollbackException("expected exception")).when(trans).commit();
408 try (EntityMgrTrans emt = new EntityMgrTrans(mgr)) {
413 @Test(expected = EntityMgrException.class)
414 public void testCommit_HmEx() throws Exception {
416 doThrow(new HeuristicMixedException("expected exception")).when(trans).commit();
418 try (EntityMgrTrans emt = new EntityMgrTrans(mgr)) {
423 @Test(expected = EntityMgrException.class)
424 public void testCommit_HrbEx() throws Exception {
426 doThrow(new HeuristicRollbackException("expected exception")).when(trans).commit();
428 try (EntityMgrTrans emt = new EntityMgrTrans(mgr)) {
433 @Test(expected = EntityMgrException.class)
434 public void testCommit_SysEx() throws Exception {
436 doThrow(new SystemException("expected exception")).when(trans).commit();
438 try (EntityMgrTrans emt = new EntityMgrTrans(mgr)) {
444 * Verifies that rollback() only rolls back, and that the subsequent close() does not
448 public void testRollback() throws Exception {
449 EntityMgrTrans emt = new EntityMgrTrans(mgr);
453 when(trans.getStatus()).thenReturn(Status.STATUS_ROLLEDBACK);
455 // rolled back, but not closed or committed
456 verify(trans, never()).commit();
457 verify(trans).rollback();
458 verify(mgr, never()).close();
460 // closed, but not re-rolled back
463 // still no commit or rollback
464 verify(trans, never()).commit();
465 verify(trans).rollback();
470 * Verifies that rollback() does nothing, and that the subsequent close() does not
471 * re-roll back when a transaction is already active.
474 public void testRollback_Active() throws Exception {
475 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
476 EntityMgrTrans emt = new EntityMgrTrans(mgr);
481 verify(trans, never()).commit();
482 verify(trans, never()).rollback();
483 verify(mgr, never()).close();
487 // still no commit or rollback
488 verify(trans, never()).commit();
489 verify(trans, never()).rollback();
493 @Test(expected = EntityMgrException.class)
494 public void testRollback_IllStateEx() throws Exception {
496 doThrow(new IllegalStateException("expected exception")).when(trans).rollback();
498 try (EntityMgrTrans emt = new EntityMgrTrans(mgr)) {
503 @Test(expected = EntityMgrException.class)
504 public void testRollback_SecEx() throws Exception {
506 doThrow(new SecurityException("expected exception")).when(trans).rollback();
508 try (EntityMgrTrans emt = new EntityMgrTrans(mgr)) {
513 @Test(expected = EntityMgrException.class)
514 public void testRollback_SysEx() throws Exception {
516 doThrow(new SystemException("expected exception")).when(trans).rollback();
518 try (EntityMgrTrans emt = new EntityMgrTrans(mgr)) {
524 public void testEntityMgrException() {
525 SecurityException secex = new SecurityException("expected exception");
526 EntityMgrException ex = new EntityMgrException(secex);
528 assertEquals(secex, ex.getCause());
533 * Tests using real (i.e., not mocked) Persistence classes.
536 public void testReal() {
537 EntityMgrTrans.setUserTrans(savetrans);
539 Map<String, Object> propMap = new HashMap<>();
541 propMap.put("javax.persistence.jdbc.driver", "org.h2.Driver");
542 propMap.put("javax.persistence.jdbc.url", "jdbc:h2:mem:EntityMgrTransTest");
544 EntityManagerFactory emf = Persistence.createEntityManagerFactory("junitDroolsSessionEntityPU", propMap);
546 try (EntityMgrTrans trans1 = new EntityMgrTrans(emf.createEntityManager())) {
548 // nest a transaction - should still be OK
550 try (EntityMgrTrans trans2 = new EntityMgrTrans(emf.createEntityManager())) {
554 } catch (Exception e) {
555 logger.info("persistence error", e);
557 fail("persistence error");