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;
29 import javax.persistence.EntityManager;
30 import javax.persistence.EntityTransaction;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.drools.persistence.EntityMgrTrans;
36 public class EntityMgrTransTest {
38 private EntityTransaction trans;
39 private EntityManager mgr;
42 public void setUp() throws Exception {
43 trans = mock(EntityTransaction.class);
44 mgr = mock(EntityManager.class);
46 when(mgr.getTransaction()).thenReturn(trans);
52 * Verifies that the constructor starts a transaction, but does not do
53 * anything extra before being closed.
56 public void testEntityMgrTrans() {
57 EntityMgrTrans t = new EntityMgrTrans(mgr);
59 // verify that transaction was started
60 verify(trans).begin();
62 // verify not closed, committed, or rolled back yet
63 verify(trans, never()).commit();
64 verify(trans, never()).rollback();
65 verify(mgr, never()).close();
71 * Verifies that the transaction is rolled back and the manager is
72 * closed when and a transaction is active.
75 public void testClose_Active() {
76 EntityMgrTrans t = new EntityMgrTrans(mgr);
78 when(trans.isActive()).thenReturn(true);
82 // closed and rolled back, but not committed
83 verify(trans, never()).commit();
84 verify(trans).rollback();
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.
93 public void testClose_Inactive() {
94 EntityMgrTrans t = new EntityMgrTrans(mgr);
96 when(trans.isActive()).thenReturn(false);
100 // closed, but not committed or rolled back
102 verify(trans, never()).commit();
103 verify(trans, never()).rollback();
107 * Verifies that the manager is closed and the transaction rolled back
108 * when "try" block exits normally and a transaction is active.
111 public void testClose_TryWithoutExcept_Active() {
112 when(trans.isActive()).thenReturn(true);
114 try(EntityMgrTrans t = new EntityMgrTrans(mgr)) {
118 // closed and rolled back, but not committed
119 verify(trans, never()).commit();
120 verify(trans).rollback();
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.
130 public void testClose_TryWithoutExcept_Inactive() {
131 when(trans.isActive()).thenReturn(false);
133 try(EntityMgrTrans t = new EntityMgrTrans(mgr)) {
137 // closed, but not rolled back or committed
138 verify(trans, never()).commit();
139 verify(trans, never()).rollback();
144 * Verifies that the manager is closed and the transaction rolled back
145 * when "try" block throws an exception and a transaction is active.
148 public void testClose_TryWithExcept_Active() {
149 when(trans.isActive()).thenReturn(true);
152 try(EntityMgrTrans t = new EntityMgrTrans(mgr)) {
153 throw new Exception("expected exception");
156 } catch (Exception e) {
159 // closed and rolled back, but not committed
160 verify(trans, never()).commit();
161 verify(trans).rollback();
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.
171 public void testClose_TryWithExcept_Inactive() {
172 when(trans.isActive()).thenReturn(false);
175 try(EntityMgrTrans t = new EntityMgrTrans(mgr)) {
176 throw new Exception("expected exception");
179 } catch (Exception e) {
182 // closed, but not rolled back or committed
183 verify(trans, never()).commit();
184 verify(trans, never()).rollback();
189 * Verifies that commit() only commits, and that the subsequent close()
190 * does not re-commit.
193 public void testCommit() {
194 EntityMgrTrans t = new EntityMgrTrans(mgr);
198 // committed, but not closed or rolled back
199 verify(trans).commit();
200 verify(trans, never()).rollback();
201 verify(mgr, never()).close();
203 // closed, but not re-committed
206 verify(trans, times(1)).commit();
211 * Verifies that rollback() only rolls back, and that the subsequent
212 * close() does not re-roll back.
215 public void testRollback() {
216 EntityMgrTrans t = new EntityMgrTrans(mgr);
220 // rolled back, but not closed or committed
221 verify(trans, never()).commit();
222 verify(trans).rollback();
223 verify(mgr, never()).close();
225 // closed, but not re-rolled back
228 verify(trans, times(1)).rollback();