c1fe08a13aeb721769ce73d4643baa6cae08b0d2
[policy/drools-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * feature-session-persistence
4  * ================================================================================
5  * Copyright (C) 2017-2018, 2020 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.ArgumentMatchers.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 import javax.persistence.EntityManager;
33 import javax.persistence.EntityManagerFactory;
34 import javax.persistence.EntityTransaction;
35 import javax.persistence.Persistence;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40
41 public class JpaDroolsSessionConnectorTest {
42
43     private EntityManagerFactory emf;
44     private JpaDroolsSessionConnector conn;
45
46     @BeforeClass
47     public static void setUpBeforeClass() {
48         System.setProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir", "target/tm");
49         System.setProperty("ObjectStoreEnvironmentBean.objectStoreDir", "target/tm");
50     }
51
52     /**
53      * Setup.
54      *
55      * @throws Exception exception
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 session name
181      * @param sessid session id
182      */
183     private void addSession(String sessnm, int sessid) {
184         EntityManager em = emf.createEntityManager();
185
186         try (EntityMgrTrans trans = new EntityMgrTrans(em)) {
187             DroolsSessionEntity ent = new DroolsSessionEntity();
188
189             ent.setSessionName(sessnm);
190             ent.setSessionId(sessid);
191
192             em.persist(ent);
193
194             trans.commit();
195         }
196     }
197 }