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;
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 javax.persistence.EntityManager;
33 import javax.transaction.HeuristicMixedException;
34 import javax.transaction.HeuristicRollbackException;
35 import javax.transaction.NotSupportedException;
36 import javax.transaction.RollbackException;
37 import javax.transaction.Status;
38 import javax.transaction.SystemException;
39 import javax.transaction.UserTransaction;
41 import org.junit.AfterClass;
42 import org.junit.Before;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.onap.policy.drools.persistence.EntityMgrTrans.EntityMgrException;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 public class EntityMgrTransTest {
51 private static final Logger logger = LoggerFactory.getLogger(PersistenceFeatureTest.class);
53 private static UserTransaction savetrans;
55 private UserTransaction trans;
56 private EntityManager mgr;
59 * Setup before the class.
63 public static void setUpBeforeClass() {
64 System.setProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir", "target/tm");
65 System.setProperty("ObjectStoreEnvironmentBean.objectStoreDir", "target/tm");
67 savetrans = EntityMgrTrans.getUserTrans();
71 public static void tearDownAfterClass() {
72 EntityMgrTrans.setUserTrans(savetrans);
78 * @throws Exception exception
81 public void setUp() throws Exception {
82 trans = mock(UserTransaction.class);
83 mgr = mock(EntityManager.class);
85 EntityMgrTrans.setUserTrans(trans);
89 * Verifies that the constructor starts a transaction, but does not do anything extra before being
92 * @throws Exception exception
95 public void testEntityMgrTrans() throws Exception {
97 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
99 final EntityMgrTrans newTrans = new EntityMgrTrans(mgr);
101 // verify that transaction was started
102 verify(trans).begin();
104 // verify not closed, committed, or rolled back yet
105 verify(trans, never()).commit();
106 verify(trans, never()).rollback();
107 verify(mgr, never()).close();
112 @Test(expected = EntityMgrException.class)
113 public void testEntityMgrTrans_RtEx() throws Exception {
115 doThrow(new IllegalArgumentException("expected exception")).when(trans).begin();
117 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
122 @Test(expected = EntityMgrException.class)
123 public void testEntityMgrTrans_NotSuppEx() throws Exception {
125 doThrow(new NotSupportedException("expected exception")).when(trans).begin();
127 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
132 @Test(expected = EntityMgrException.class)
133 public void testEntityMgrTrans_SysEx() throws Exception {
135 doThrow(new SystemException("expected exception")).when(trans).begin();
137 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
143 * Verifies that the transaction is rolled back and the manager is closed when and a transaction
147 public void testClose_Active() throws Exception {
148 EntityMgrTrans newTrans = new EntityMgrTrans(mgr);
150 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
154 // closed and rolled back, but not committed
155 verify(trans, never()).commit();
156 verify(trans).rollback();
161 * Verifies that the manager is closed, but that the transaction is <i>not</i> rolled back when
162 * and no transaction is active.
165 public void testClose_Inactive() throws Exception {
166 EntityMgrTrans newTrans = new EntityMgrTrans(mgr);
168 when(trans.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION);
172 // closed, but not committed or rolled back
174 verify(trans, never()).commit();
175 verify(trans, never()).rollback();
178 @Test(expected = EntityMgrException.class)
179 public void testClose_IllStateEx() throws Exception {
181 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
182 doThrow(new IllegalStateException("expected exception")).when(trans).rollback();
184 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
189 @Test(expected = EntityMgrException.class)
190 public void testClose_SecEx() throws Exception {
192 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
193 doThrow(new SecurityException("expected exception")).when(trans).rollback();
195 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
200 @Test(expected = EntityMgrException.class)
201 public void testClose_SysEx() throws Exception {
203 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
204 doThrow(new SystemException("expected exception")).when(trans).rollback();
206 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
212 * Verifies that the manager is closed and the transaction rolled back when "try" block exits
213 * normally and a transaction is active.
216 public void testClose_TryWithoutExcept_Active() throws Exception {
217 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
219 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
223 // closed and rolled back, but not committed
224 verify(trans, never()).commit();
225 verify(trans).rollback();
230 * Verifies that the manager is closed, but that the transaction is <i>not</i> rolled back when
231 * "try" block exits normally and no transaction is active.
234 public void testClose_TryWithoutExcept_Inactive() throws Exception {
236 when(trans.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION);
238 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
242 // closed, but not rolled back or committed
243 verify(trans, never()).commit();
244 verify(trans, never()).rollback();
249 * Verifies that the manager is closed and the transaction rolled back when "try" block throws an
250 * exception and a transaction is active.
253 public void testClose_TryWithExcept_Active() throws Exception {
255 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
258 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
259 throw new SystemException("expected exception");
262 } catch (Exception e) {
263 logger.trace("expected exception", e);
266 // closed and rolled back, but not committed
267 verify(trans, never()).commit();
268 verify(trans).rollback();
273 * Verifies that the manager is closed, but that the transaction is <i>not</i> rolled back when
274 * "try" block throws an exception and no transaction is active.
277 public void testClose_TryWithExcept_Inactive() throws Exception {
279 when(trans.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION);
282 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
283 throw new SystemException("expected exception");
286 } catch (Exception e) {
287 logger.trace("expected exception", e);
290 // closed, but not rolled back or committed
291 verify(trans, never()).commit();
292 verify(trans, never()).rollback();
296 /** Verifies that commit() only commits, and that the subsequent close() does not re-commit. */
298 public void testCommit() throws Exception {
299 EntityMgrTrans newTrans = new EntityMgrTrans(mgr);
300 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
304 when(trans.getStatus()).thenReturn(Status.STATUS_COMMITTED);
306 // committed, but not closed or rolled back
307 verify(trans).commit();
308 verify(trans, never()).rollback();
309 verify(mgr, never()).close();
311 // closed, but not re-committed
314 verify(trans, times(1)).commit();
318 @Test(expected = EntityMgrException.class)
319 public void testCommit_SecEx() throws Exception {
321 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
322 doThrow(new SecurityException("expected exception")).when(trans).commit();
324 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
329 @Test(expected = EntityMgrException.class)
330 public void testCommit_IllStateEx() throws Exception {
332 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
333 doThrow(new IllegalStateException("expected exception")).when(trans).commit();
335 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
340 @Test(expected = EntityMgrException.class)
341 public void testCommit_RbEx() throws Exception {
343 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
344 doThrow(new RollbackException("expected exception")).when(trans).commit();
346 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
351 @Test(expected = EntityMgrException.class)
352 public void testCommit_HmEx() throws Exception {
354 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
355 doThrow(new HeuristicMixedException("expected exception")).when(trans).commit();
357 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
362 @Test(expected = EntityMgrException.class)
363 public void testCommit_HrbEx() throws Exception {
365 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
366 doThrow(new HeuristicRollbackException("expected exception")).when(trans).commit();
368 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
373 @Test(expected = EntityMgrException.class)
374 public void testCommit_SysEx() throws Exception {
376 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
377 doThrow(new SystemException("expected exception")).when(trans).commit();
379 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
385 * Verifies that rollback() only rolls back, and that the subsequent close() does not re-roll
389 public void testRollback() throws Exception {
390 EntityMgrTrans newTrans = new EntityMgrTrans(mgr);
391 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
395 when(trans.getStatus()).thenReturn(Status.STATUS_ROLLEDBACK);
397 // rolled back, but not closed or committed
398 verify(trans, never()).commit();
399 verify(trans).rollback();
400 verify(mgr, never()).close();
402 // closed, but not re-rolled back
405 verify(trans, times(1)).rollback();
409 @Test(expected = EntityMgrException.class)
410 public void testRollback_IllStateEx() throws Exception {
412 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
413 doThrow(new IllegalStateException("expected exception")).when(trans).rollback();
415 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
420 @Test(expected = EntityMgrException.class)
421 public void testRollback_SecEx() throws Exception {
423 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
424 doThrow(new SecurityException("expected exception")).when(trans).rollback();
426 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
431 @Test(expected = EntityMgrException.class)
432 public void testRollback_SysEx() throws Exception {
434 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
435 doThrow(new SystemException("expected exception")).when(trans).rollback();
437 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
443 public void testEntityMgrException() {
444 SecurityException secex = new SecurityException("expected exception");
445 EntityMgrException ex = new EntityMgrException(secex);
447 assertEquals(secex, ex.getCause());