2 * ============LICENSE_START=======================================================
3 * feature-session-persistence
4 * ================================================================================
5 * Copyright (C) 2017 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.mockito.Mockito.mock;
24 import static org.mockito.Mockito.never;
25 import static org.mockito.Mockito.times;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 import static org.junit.Assert.assertEquals;
29 import static org.mockito.Mockito.doThrow;
31 import javax.persistence.EntityManager;
32 import javax.transaction.HeuristicMixedException;
33 import javax.transaction.HeuristicRollbackException;
34 import javax.transaction.NotSupportedException;
35 import javax.transaction.RollbackException;
36 import javax.transaction.Status;
37 import javax.transaction.SystemException;
38 import javax.transaction.UserTransaction;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.onap.policy.drools.persistence.EntityMgrTrans.EntityMgrException;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 public class EntityMgrTransTest {
50 private static final Logger logger = LoggerFactory.getLogger(PersistenceFeatureTest.class);
52 private static UserTransaction savetrans;
54 private UserTransaction trans;
55 private EntityManager mgr;
58 public static void setUpBeforeClass() {
59 System.setProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir", "target/tm");
60 System.setProperty("ObjectStoreEnvironmentBean.objectStoreDir", "target/tm");
62 savetrans = EntityMgrTrans.getUserTrans();
66 public static void tearDownAfterClass() {
67 EntityMgrTrans.setUserTrans(savetrans);
71 public void setUp() throws Exception {
72 trans = mock(UserTransaction.class);
73 mgr = mock(EntityManager.class);
75 EntityMgrTrans.setUserTrans(trans);
79 * Verifies that the constructor starts a transaction, but does not do
80 * anything extra before being closed.
85 public void testEntityMgrTrans() throws Exception {
87 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
89 EntityMgrTrans t = new EntityMgrTrans(mgr);
91 // verify that transaction was started
92 verify(trans).begin();
94 // verify not closed, committed, or rolled back yet
95 verify(trans, never()).commit();
96 verify(trans, never()).rollback();
97 verify(mgr, never()).close();
102 @Test(expected = EntityMgrException.class)
103 public void testEntityMgrTrans_RtEx() throws Exception {
105 doThrow(new IllegalArgumentException("expected exception")).when(trans).begin();
107 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
112 @Test(expected = EntityMgrException.class)
113 public void testEntityMgrTrans_NotSuppEx() throws Exception {
115 doThrow(new NotSupportedException("expected exception")).when(trans).begin();
117 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
122 @Test(expected = EntityMgrException.class)
123 public void testEntityMgrTrans_SysEx() throws Exception {
125 doThrow(new SystemException("expected exception")).when(trans).begin();
127 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
133 * Verifies that the transaction is rolled back and the manager is closed
134 * when and a transaction is active.
137 public void testClose_Active() throws Exception {
138 EntityMgrTrans t = new EntityMgrTrans(mgr);
140 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
144 // closed and rolled back, but not committed
145 verify(trans, never()).commit();
146 verify(trans).rollback();
151 * Verifies that the manager is closed, but that the transaction is
152 * <i>not</i> rolled back when and no transaction is active.
155 public void testClose_Inactive() throws Exception {
156 EntityMgrTrans t = new EntityMgrTrans(mgr);
158 when(trans.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION);
162 // closed, but not committed or rolled back
164 verify(trans, never()).commit();
165 verify(trans, never()).rollback();
168 @Test(expected = EntityMgrException.class)
169 public void testClose_IllStateEx() throws Exception {
171 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
172 doThrow(new IllegalStateException("expected exception")).when(trans).rollback();
174 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
179 @Test(expected = EntityMgrException.class)
180 public void testClose_SecEx() throws Exception {
182 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
183 doThrow(new SecurityException("expected exception")).when(trans).rollback();
185 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
190 @Test(expected = EntityMgrException.class)
191 public void testClose_SysEx() throws Exception {
193 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
194 doThrow(new SystemException("expected exception")).when(trans).rollback();
196 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
202 * Verifies that the manager is closed and the transaction rolled back when
203 * "try" block exits normally and a transaction is active.
206 public void testClose_TryWithoutExcept_Active() throws Exception {
207 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
209 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
213 // closed and rolled back, but not committed
214 verify(trans, never()).commit();
215 verify(trans).rollback();
220 * Verifies that the manager is closed, but that the transaction is
221 * <i>not</i> rolled back when "try" block exits normally and no transaction
225 public void testClose_TryWithoutExcept_Inactive() throws Exception {
227 when(trans.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION);
229 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
233 // closed, but not rolled back or committed
234 verify(trans, never()).commit();
235 verify(trans, never()).rollback();
240 * Verifies that the manager is closed and the transaction rolled back when
241 * "try" block throws an exception and a transaction is active.
244 public void testClose_TryWithExcept_Active() throws Exception {
246 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
249 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
250 throw new SystemException("expected exception");
253 } catch (Exception e) {
254 logger.trace("expected exception", e);
257 // closed and rolled back, but not committed
258 verify(trans, never()).commit();
259 verify(trans).rollback();
264 * Verifies that the manager is closed, but that the transaction is
265 * <i>not</i> rolled back when "try" block throws an exception and no
266 * transaction is active.
269 public void testClose_TryWithExcept_Inactive() throws Exception {
271 when(trans.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION);
274 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
275 throw new SystemException("expected exception");
278 } catch (Exception e) {
279 logger.trace("expected exception", e);
282 // closed, but not rolled back or committed
283 verify(trans, never()).commit();
284 verify(trans, never()).rollback();
289 * Verifies that commit() only commits, and that the subsequent close() does
293 public void testCommit() throws Exception {
294 EntityMgrTrans t = new EntityMgrTrans(mgr);
295 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
299 when(trans.getStatus()).thenReturn(Status.STATUS_COMMITTED);
301 // committed, but not closed or rolled back
302 verify(trans).commit();
303 verify(trans, never()).rollback();
304 verify(mgr, never()).close();
306 // closed, but not re-committed
309 verify(trans, times(1)).commit();
313 @Test(expected = EntityMgrException.class)
314 public void testCommit_SecEx() throws Exception {
316 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
317 doThrow(new SecurityException("expected exception")).when(trans).commit();
319 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
324 @Test(expected = EntityMgrException.class)
325 public void testCommit_IllStateEx() throws Exception {
327 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
328 doThrow(new IllegalStateException("expected exception")).when(trans).commit();
330 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
335 @Test(expected = EntityMgrException.class)
336 public void testCommit_RbEx() throws Exception {
338 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
339 doThrow(new RollbackException("expected exception")).when(trans).commit();
341 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
346 @Test(expected = EntityMgrException.class)
347 public void testCommit_HmEx() throws Exception {
349 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
350 doThrow(new HeuristicMixedException("expected exception")).when(trans).commit();
352 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
357 @Test(expected = EntityMgrException.class)
358 public void testCommit_HrbEx() throws Exception {
360 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
361 doThrow(new HeuristicRollbackException("expected exception")).when(trans).commit();
363 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
368 @Test(expected = EntityMgrException.class)
369 public void testCommit_SysEx() throws Exception {
371 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
372 doThrow(new SystemException("expected exception")).when(trans).commit();
374 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
380 * Verifies that rollback() only rolls back, and that the subsequent close()
381 * does not re-roll back.
384 public void testRollback() throws Exception {
385 EntityMgrTrans t = new EntityMgrTrans(mgr);
386 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
390 when(trans.getStatus()).thenReturn(Status.STATUS_ROLLEDBACK);
392 // rolled back, but not closed or committed
393 verify(trans, never()).commit();
394 verify(trans).rollback();
395 verify(mgr, never()).close();
397 // closed, but not re-rolled back
400 verify(trans, times(1)).rollback();
404 @Test(expected = EntityMgrException.class)
405 public void testRollback_IllStateEx() throws Exception {
407 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
408 doThrow(new IllegalStateException("expected exception")).when(trans).rollback();
410 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
415 @Test(expected = EntityMgrException.class)
416 public void testRollback_SecEx() throws Exception {
418 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
419 doThrow(new SecurityException("expected exception")).when(trans).rollback();
421 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
426 @Test(expected = EntityMgrException.class)
427 public void testRollback_SysEx() throws Exception {
429 when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
430 doThrow(new SystemException("expected exception")).when(trans).rollback();
432 try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
438 public void testEntityMgrException() {
439 SecurityException secex = new SecurityException("expected exception");
440 EntityMgrException ex = new EntityMgrException(secex);
442 assertEquals(secex, ex.getCause());