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