aba1d80eb4031d25ca6d87e79255cd3fb159716b
[policy/drools-pdp.git] /
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.drools.persistence;
22
23 import static org.junit.Assert.assertEquals;
24
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;
31
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;
40
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;
48
49 public class EntityMgrTransTest {
50
51     private static final Logger logger = LoggerFactory.getLogger(PersistenceFeatureTest.class);
52
53     private static UserTransaction savetrans;
54
55     private UserTransaction trans;
56     private EntityManager mgr;
57
58     /**
59      * Setup before the class.
60      * 
61      */
62     @BeforeClass
63     public static void setUpBeforeClass() {
64         System.setProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir", "target/tm");
65         System.setProperty("ObjectStoreEnvironmentBean.objectStoreDir", "target/tm");
66
67         savetrans = EntityMgrTrans.getUserTrans();
68     }
69
70     @AfterClass
71     public static void tearDownAfterClass() {
72         EntityMgrTrans.setUserTrans(savetrans);
73     }
74
75     /**
76      * Setup.
77      * 
78      * @throws Exception exception
79      */
80     @Before
81     public void setUp() throws Exception {
82         trans = mock(UserTransaction.class);
83         mgr = mock(EntityManager.class);
84
85         EntityMgrTrans.setUserTrans(trans);
86     }
87
88     /**
89      * Verifies that the constructor starts a transaction, but does not do anything extra before being
90      * closed.
91      *
92      * @throws Exception exception
93      */
94     @Test
95     public void testEntityMgrTrans() throws Exception {
96
97         when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
98
99         final EntityMgrTrans newTrans = new EntityMgrTrans(mgr);
100
101         // verify that transaction was started
102         verify(trans).begin();
103
104         // verify not closed, committed, or rolled back yet
105         verify(trans, never()).commit();
106         verify(trans, never()).rollback();
107         verify(mgr, never()).close();
108
109         newTrans.close();
110     }
111
112     @Test(expected = EntityMgrException.class)
113     public void testEntityMgrTrans_RtEx() throws Exception {
114
115         doThrow(new IllegalArgumentException("expected exception")).when(trans).begin();
116
117         try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
118             // Empty
119         }
120     }
121
122     @Test(expected = EntityMgrException.class)
123     public void testEntityMgrTrans_NotSuppEx() throws Exception {
124
125         doThrow(new NotSupportedException("expected exception")).when(trans).begin();
126
127         try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
128             // Empty
129         }
130     }
131
132     @Test(expected = EntityMgrException.class)
133     public void testEntityMgrTrans_SysEx() throws Exception {
134
135         doThrow(new SystemException("expected exception")).when(trans).begin();
136
137         try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
138             // Empty
139         }
140     }
141
142     /**
143      * Verifies that the transaction is rolled back and the manager is closed when and a transaction
144      * is active.
145      */
146     @Test
147     public void testClose_Active() throws Exception {
148         EntityMgrTrans newTrans = new EntityMgrTrans(mgr);
149
150         when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
151
152         newTrans.close();
153
154         // closed and rolled back, but not committed
155         verify(trans, never()).commit();
156         verify(trans).rollback();
157         verify(mgr).close();
158     }
159
160     /**
161      * Verifies that the manager is closed, but that the transaction is <i>not</i> rolled back when
162      * and no transaction is active.
163      */
164     @Test
165     public void testClose_Inactive() throws Exception {
166         EntityMgrTrans newTrans = new EntityMgrTrans(mgr);
167
168         when(trans.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION);
169
170         newTrans.close();
171
172         // closed, but not committed or rolled back
173         verify(mgr).close();
174         verify(trans, never()).commit();
175         verify(trans, never()).rollback();
176     }
177
178     @Test(expected = EntityMgrException.class)
179     public void testClose_IllStateEx() throws Exception {
180
181         when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
182         doThrow(new IllegalStateException("expected exception")).when(trans).rollback();
183
184         try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
185             // Empty
186         }
187     }
188
189     @Test(expected = EntityMgrException.class)
190     public void testClose_SecEx() throws Exception {
191
192         when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
193         doThrow(new SecurityException("expected exception")).when(trans).rollback();
194
195         try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
196             // Empty
197         }
198     }
199
200     @Test(expected = EntityMgrException.class)
201     public void testClose_SysEx() throws Exception {
202
203         when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
204         doThrow(new SystemException("expected exception")).when(trans).rollback();
205
206         try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
207             // Empty
208         }
209     }
210
211     /**
212      * Verifies that the manager is closed and the transaction rolled back when "try" block exits
213      * normally and a transaction is active.
214      */
215     @Test
216     public void testClose_TryWithoutExcept_Active() throws Exception {
217         when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
218
219         try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
220             // Empty
221         }
222
223         // closed and rolled back, but not committed
224         verify(trans, never()).commit();
225         verify(trans).rollback();
226         verify(mgr).close();
227     }
228
229     /**
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.
232      */
233     @Test
234     public void testClose_TryWithoutExcept_Inactive() throws Exception {
235
236         when(trans.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION);
237
238         try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
239             // Empty
240         }
241
242         // closed, but not rolled back or committed
243         verify(trans, never()).commit();
244         verify(trans, never()).rollback();
245         verify(mgr).close();
246     }
247
248     /**
249      * Verifies that the manager is closed and the transaction rolled back when "try" block throws an
250      * exception and a transaction is active.
251      */
252     @Test
253     public void testClose_TryWithExcept_Active() throws Exception {
254
255         when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
256
257         try {
258             try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
259                 throw new SystemException("expected exception");
260             }
261
262         } catch (Exception e) {
263             logger.trace("expected exception", e);
264         }
265
266         // closed and rolled back, but not committed
267         verify(trans, never()).commit();
268         verify(trans).rollback();
269         verify(mgr).close();
270     }
271
272     /**
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.
275      */
276     @Test
277     public void testClose_TryWithExcept_Inactive() throws Exception {
278
279         when(trans.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION);
280
281         try {
282             try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
283                 throw new SystemException("expected exception");
284             }
285
286         } catch (Exception e) {
287             logger.trace("expected exception", e);
288         }
289
290         // closed, but not rolled back or committed
291         verify(trans, never()).commit();
292         verify(trans, never()).rollback();
293         verify(mgr).close();
294     }
295
296     /** Verifies that commit() only commits, and that the subsequent close() does not re-commit. */
297     @Test
298     public void testCommit() throws Exception {
299         EntityMgrTrans newTrans = new EntityMgrTrans(mgr);
300         when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
301
302         newTrans.commit();
303
304         when(trans.getStatus()).thenReturn(Status.STATUS_COMMITTED);
305
306         // committed, but not closed or rolled back
307         verify(trans).commit();
308         verify(trans, never()).rollback();
309         verify(mgr, never()).close();
310
311         // closed, but not re-committed
312         newTrans.close();
313
314         verify(trans, times(1)).commit();
315         verify(mgr).close();
316     }
317
318     @Test(expected = EntityMgrException.class)
319     public void testCommit_SecEx() throws Exception {
320
321         when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
322         doThrow(new SecurityException("expected exception")).when(trans).commit();
323
324         try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
325             t.commit();
326         }
327     }
328
329     @Test(expected = EntityMgrException.class)
330     public void testCommit_IllStateEx() throws Exception {
331
332         when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
333         doThrow(new IllegalStateException("expected exception")).when(trans).commit();
334
335         try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
336             t.commit();
337         }
338     }
339
340     @Test(expected = EntityMgrException.class)
341     public void testCommit_RbEx() throws Exception {
342
343         when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
344         doThrow(new RollbackException("expected exception")).when(trans).commit();
345
346         try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
347             t.commit();
348         }
349     }
350
351     @Test(expected = EntityMgrException.class)
352     public void testCommit_HmEx() throws Exception {
353
354         when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
355         doThrow(new HeuristicMixedException("expected exception")).when(trans).commit();
356
357         try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
358             t.commit();
359         }
360     }
361
362     @Test(expected = EntityMgrException.class)
363     public void testCommit_HrbEx() throws Exception {
364
365         when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
366         doThrow(new HeuristicRollbackException("expected exception")).when(trans).commit();
367
368         try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
369             t.commit();
370         }
371     }
372
373     @Test(expected = EntityMgrException.class)
374     public void testCommit_SysEx() throws Exception {
375
376         when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
377         doThrow(new SystemException("expected exception")).when(trans).commit();
378
379         try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
380             t.commit();
381         }
382     }
383
384     /**
385      * Verifies that rollback() only rolls back, and that the subsequent close() does not re-roll
386      * back.
387      */
388     @Test
389     public void testRollback() throws Exception {
390         EntityMgrTrans newTrans = new EntityMgrTrans(mgr);
391         when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
392
393         newTrans.rollback();
394
395         when(trans.getStatus()).thenReturn(Status.STATUS_ROLLEDBACK);
396
397         // rolled back, but not closed or committed
398         verify(trans, never()).commit();
399         verify(trans).rollback();
400         verify(mgr, never()).close();
401
402         // closed, but not re-rolled back
403         newTrans.close();
404
405         verify(trans, times(1)).rollback();
406         verify(mgr).close();
407     }
408
409     @Test(expected = EntityMgrException.class)
410     public void testRollback_IllStateEx() throws Exception {
411
412         when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
413         doThrow(new IllegalStateException("expected exception")).when(trans).rollback();
414
415         try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
416             t.rollback();
417         }
418     }
419
420     @Test(expected = EntityMgrException.class)
421     public void testRollback_SecEx() throws Exception {
422
423         when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
424         doThrow(new SecurityException("expected exception")).when(trans).rollback();
425
426         try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
427             t.rollback();
428         }
429     }
430
431     @Test(expected = EntityMgrException.class)
432     public void testRollback_SysEx() throws Exception {
433
434         when(trans.getStatus()).thenReturn(Status.STATUS_ACTIVE);
435         doThrow(new SystemException("expected exception")).when(trans).rollback();
436
437         try (EntityMgrTrans t = new EntityMgrTrans(mgr)) {
438             t.rollback();
439         }
440     }
441
442     @Test
443     public void testEntityMgrException() {
444         SecurityException secex = new SecurityException("expected exception");
445         EntityMgrException ex = new EntityMgrException(secex);
446
447         assertEquals(secex, ex.getCause());
448     }
449 }