0165b1e41f1efb72defb558e0e594959108c9d78
[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
29 import javax.persistence.EntityManager;
30 import javax.persistence.EntityTransaction;
31
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.drools.persistence.EntityMgrTrans;
35
36 public class EntityMgrTransTest {
37         
38         private EntityTransaction trans;
39         private EntityManager mgr;
40
41         @Before
42         public void setUp() throws Exception {
43                 trans = mock(EntityTransaction.class);
44                 mgr = mock(EntityManager.class);
45                 
46                 when(mgr.getTransaction()).thenReturn(trans);
47         }
48
49
50
51         /**
52          * Verifies that the constructor starts a transaction, but does not do
53          * anything extra before being closed.
54          */
55         @Test
56         public void testEntityMgrTrans() {
57                 EntityMgrTrans t = new EntityMgrTrans(mgr);
58                 
59                 // verify that transaction was started
60                 verify(trans).begin();
61
62                 // verify not closed, committed, or rolled back yet
63                 verify(trans, never()).commit();
64                 verify(trans, never()).rollback();
65                 verify(mgr, never()).close();
66                 
67                 t.close();
68         }
69
70         /**
71          * Verifies that the transaction is rolled back and the manager is
72          * closed when and a transaction is active.
73          */
74         @Test
75         public void testClose_Active() {
76                 EntityMgrTrans t = new EntityMgrTrans(mgr);
77
78                 when(trans.isActive()).thenReturn(true);
79                 
80                 t.close();
81
82                 // closed and rolled back, but not committed
83                 verify(trans, never()).commit();
84                 verify(trans).rollback();
85                 verify(mgr).close();
86         }
87
88         /**
89          * Verifies that the manager is closed, but that the transaction is
90          * <i>not</i> rolled back and when and no transaction is active.
91          */
92         @Test
93         public void testClose_Inactive() {
94                 EntityMgrTrans t = new EntityMgrTrans(mgr);
95
96                 when(trans.isActive()).thenReturn(false);
97                 
98                 t.close();
99
100                 // closed, but not committed or rolled back
101                 verify(mgr).close();
102                 verify(trans, never()).commit();
103                 verify(trans, never()).rollback();
104         }
105
106         /**
107          * Verifies that the manager is closed and the transaction rolled back
108          * when "try" block exits normally and a transaction is active.
109          */
110         @Test
111         public void testClose_TryWithoutExcept_Active() {
112                 when(trans.isActive()).thenReturn(true);
113                 
114                 try(EntityMgrTrans t = new EntityMgrTrans(mgr)) {
115                         
116                 }
117
118                 // closed and rolled back, but not committed
119                 verify(trans, never()).commit();
120                 verify(trans).rollback();
121                 verify(mgr).close();
122         }
123
124         /**
125          * Verifies that the manager is closed, but that the transaction is
126          * <i>not</i> rolled back when "try" block exits normally and no
127          * transaction is active.
128          */
129         @Test
130         public void testClose_TryWithoutExcept_Inactive() {
131                 when(trans.isActive()).thenReturn(false);
132                 
133                 try(EntityMgrTrans t = new EntityMgrTrans(mgr)) {
134                         
135                 }
136
137                 // closed, but not rolled back or committed
138                 verify(trans, never()).commit();
139                 verify(trans, never()).rollback();
140                 verify(mgr).close();
141         }
142
143         /**
144          * Verifies that the manager is closed and the transaction rolled back
145          * when "try" block throws an exception and a transaction is active.
146          */
147         @Test
148         public void testClose_TryWithExcept_Active() {
149                 when(trans.isActive()).thenReturn(true);
150                 
151                 try {
152                         try(EntityMgrTrans t = new EntityMgrTrans(mgr)) {
153                                 throw new Exception("expected exception");
154                         }
155                         
156                 } catch (Exception e) {
157                 }
158
159                 // closed and rolled back, but not committed
160                 verify(trans, never()).commit();
161                 verify(trans).rollback();
162                 verify(mgr).close();
163         }
164
165         /**
166          * Verifies that the manager is closed, but that the transaction is
167          * <i>not</i> rolled back when "try" block throws an exception and no
168          * transaction is active.
169          */
170         @Test
171         public void testClose_TryWithExcept_Inactive() {
172                 when(trans.isActive()).thenReturn(false);
173                 
174                 try {
175                         try(EntityMgrTrans t = new EntityMgrTrans(mgr)) {
176                                 throw new Exception("expected exception");
177                         }
178                         
179                 } catch (Exception e) {
180                 }
181
182                 // closed, but not rolled back or committed
183                 verify(trans, never()).commit();
184                 verify(trans, never()).rollback();
185                 verify(mgr).close();
186         }
187
188         /**
189          * Verifies that commit() only commits, and that the subsequent close()
190          * does not re-commit.
191          */
192         @Test
193         public void testCommit() {
194                 EntityMgrTrans t = new EntityMgrTrans(mgr);
195                 
196                 t.commit();
197                 
198                 // committed, but not closed or rolled back
199                 verify(trans).commit();
200                 verify(trans, never()).rollback();
201                 verify(mgr, never()).close();
202                 
203                 // closed, but not re-committed
204                 t.close();
205
206                 verify(trans, times(1)).commit();
207                 verify(mgr).close();
208         }
209
210         /**
211          * Verifies that rollback() only rolls back, and that the subsequent
212          * close() does not re-roll back.
213          */
214         @Test
215         public void testRollback() {
216                 EntityMgrTrans t = new EntityMgrTrans(mgr);
217                 
218                 t.rollback();
219                 
220                 // rolled back, but not closed or committed
221                 verify(trans, never()).commit();
222                 verify(trans).rollback();
223                 verify(mgr, never()).close();
224                 
225                 // closed, but not re-rolled back
226                 t.close();
227
228                 verify(trans, times(1)).rollback();
229                 verify(mgr).close();
230         }
231
232 }