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