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