792e6f8b916fa354c9f84559e7bc8bd9bc822e01
[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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNull;
25 import static org.mockito.Matchers.any;
26 import static org.mockito.Mockito.doThrow;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29
30 import java.util.HashMap;
31 import java.util.Map;
32
33 import javax.persistence.EntityManager;
34 import javax.persistence.EntityManagerFactory;
35 import javax.persistence.EntityTransaction;
36 import javax.persistence.Persistence;
37
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.policy.drools.persistence.DroolsSessionEntity;
42 import org.onap.policy.drools.persistence.EntityMgrTrans;
43 import org.onap.policy.drools.persistence.JpaDroolsSessionConnector;
44
45 public class JpaDroolsSessionConnectorTest {
46         
47         private EntityManagerFactory emf;
48         private JpaDroolsSessionConnector conn;
49         
50
51         @Before
52         public void setUp() throws Exception {
53                 Map<String, Object> propMap = new HashMap<>();
54
55                 propMap.put("javax.persistence.jdbc.driver", "org.h2.Driver");
56                 propMap.put("javax.persistence.jdbc.url",
57                                                 "jdbc:h2:mem:JpaDroolsSessionConnectorTest");
58                 
59                 emf = Persistence.createEntityManagerFactory(
60                                                                 "junitDroolsSessionEntityPU", propMap);
61                 
62                 conn = new JpaDroolsSessionConnector(emf);
63         }
64         
65         @After
66         public void tearDown() {
67                 // this will cause the memory db to be dropped
68                 emf.close();
69         }
70
71         @Test
72         public void testGet() {
73                 /*
74                  * Load up the DB with some data.
75                  */
76                 
77                 addSession("nameA", 10);
78                 addSession("nameY", 20);
79                 
80                 
81                 /*
82                  * Now test the functionality.
83                  */
84                 
85                 // not found
86                 assertNull( conn.get("unknown"));
87                 
88                 assertEquals("{name=nameA, id=10}",
89                                                 conn.get("nameA").toString());
90                 
91                 assertEquals("{name=nameY, id=20}",
92                                                 conn.get("nameY").toString());
93         }
94         
95         @Test(expected = RuntimeException.class)
96         public void testGet_NewEx() {
97                 EntityManagerFactory emf = mock(EntityManagerFactory.class);
98                 EntityManager em = mock(EntityManager.class);
99                 
100                 when(emf.createEntityManager()).thenReturn(em);
101                 when(em.getTransaction()).thenThrow(new RuntimeException("expected exception"));
102
103                 conn = new JpaDroolsSessionConnector(emf);
104                 conn.get("xyz");
105         }
106         
107         @Test(expected = RuntimeException.class)
108         public void testGet_FindEx() {
109                 EntityManagerFactory emf = mock(EntityManagerFactory.class);
110                 EntityManager em = mock(EntityManager.class);
111                 EntityTransaction tr = mock(EntityTransaction.class);
112                 
113                 when(emf.createEntityManager()).thenReturn(em);
114                 when(em.getTransaction()).thenReturn(tr);
115                 when(em.find(any(), any())).thenThrow(new RuntimeException("expected exception"));
116
117                 new JpaDroolsSessionConnector(emf).get("xyz");
118         }
119         
120         @Test(expected = RuntimeException.class)
121         public void testGet_FindEx_CloseEx() {
122                 EntityManagerFactory emf = mock(EntityManagerFactory.class);
123                 EntityManager em = mock(EntityManager.class);
124                 EntityTransaction tr = mock(EntityTransaction.class);
125                 
126                 when(emf.createEntityManager()).thenReturn(em);
127                 when(em.getTransaction()).thenReturn(tr);
128                 when(em.find(any(), any())).thenThrow(new RuntimeException("expected exception"));
129                 doThrow(new RuntimeException("expected exception #2")).when(em).close();
130
131                 new JpaDroolsSessionConnector(emf).get("xyz");
132         }
133
134         @Test
135         public void testReplace_Existing() {
136                 addSession("nameA", 10);
137                 
138                 DroolsSessionEntity sess =
139                                 new DroolsSessionEntity("nameA", 30);
140                 
141                 conn.replace(sess);
142
143                 // id should be changed
144                 assertEquals(sess.toString(),
145                                                 conn.get("nameA").toString());
146         }
147
148         @Test
149         public void testReplace_New() {
150                 DroolsSessionEntity sess =
151                                 new DroolsSessionEntity("nameA", 30);
152                 
153                 conn.replace(sess);
154
155                 assertEquals(sess.toString(),
156                                                 conn.get("nameA").toString());
157         }
158
159         @Test
160         public void testAdd() {
161                 DroolsSessionEntity sess =
162                                 new DroolsSessionEntity("nameA", 30);
163                 
164                 conn.replace(sess);
165
166                 assertEquals(sess.toString(),
167                                                 conn.get("nameA").toString());
168         }
169
170         @Test
171         public void testUpdate() {
172                 addSession("nameA", 10);
173                 
174                 DroolsSessionEntity sess =
175                                 new DroolsSessionEntity("nameA", 30);
176                 
177                 conn.replace(sess);
178
179                 // id should be changed
180                 assertEquals("{name=nameA, id=30}",
181                                                 conn.get("nameA").toString());
182         }
183
184
185         /**
186          * Adds a session to the DB.
187          * @param sessnm        session name
188          * @param sessid        session id
189          */
190         private void addSession(String sessnm, int sessid) {
191                 EntityManager em = emf.createEntityManager();
192                 
193                 try(EntityMgrTrans trans = new EntityMgrTrans(em)) {
194                         DroolsSessionEntity ent = new DroolsSessionEntity();
195                         
196                         ent.setSessionName(sessnm);
197                         ent.setSessionId(sessid);
198                         
199                         em.persist(ent);
200                 
201                         trans.commit();
202                 }
203         }
204 }