7350a7f713ddaf0e26e88188ae466c5903ce6e79
[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.verify;
26
27 import javax.persistence.EntityManager;
28
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.policy.drools.persistence.EntityMgrCloser;
32
33 public class EntityMgrCloserTest {
34         
35         private EntityManager mgr;
36         
37
38         @Before
39         public void setUp() throws Exception {
40                 mgr = mock(EntityManager.class);
41         }
42
43
44         /**
45          * Verifies that the constructor does not do anything extra before
46          * being closed.
47          */
48         @Test
49         public void testEntityMgrCloser() {
50                 EntityMgrCloser c = new EntityMgrCloser(mgr);
51
52                 // verify not closed yet
53                 verify(mgr, never()).close();
54                 
55                 c.close();
56         }
57         
58         /**
59          * Verifies that the manager gets closed when close() is invoked.
60          */
61         @Test
62         public void testClose() {
63                 EntityMgrCloser c = new EntityMgrCloser(mgr);
64                 
65                 c.close();
66                 
67                 // should be closed
68                 verify(mgr).close();
69         }
70
71         /**
72          * Ensures that the manager gets closed when "try" block exits normally.
73          */
74         @Test
75         public void testClose_TryWithoutExcept() {
76                 try(EntityMgrCloser c = new EntityMgrCloser(mgr)) {
77                         
78                 }
79                 
80                 verify(mgr).close();
81         }
82
83         /**
84          * Ensures that the manager gets closed when "try" block throws an
85          * exception.
86          */
87         @Test
88         public void testClose_TryWithExcept() {
89                 try {
90                         try(EntityMgrCloser c = new EntityMgrCloser(mgr)) {
91                                 throw new Exception("expected exception");
92                         }
93                         
94                 } catch (Exception e) {
95                 }
96                 
97                 verify(mgr).close();
98         }
99
100 }