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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.drools.persistence;
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;
30 import java.util.HashMap;
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;
41 public class JpaDroolsSessionConnectorTest {
43 private EntityManagerFactory emf;
44 private JpaDroolsSessionConnector conn;
47 public static void setUpBeforeClass() {
48 System.setProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir", "target/tm");
49 System.setProperty("ObjectStoreEnvironmentBean.objectStoreDir", "target/tm");
55 * @throws Exception exception
58 public void setUp() throws Exception {
59 Map<String, Object> propMap = new HashMap<>();
61 propMap.put("javax.persistence.jdbc.driver", "org.h2.Driver");
62 propMap.put("javax.persistence.jdbc.url", "jdbc:h2:mem:JpaDroolsSessionConnectorTest");
64 emf = Persistence.createEntityManagerFactory("junitDroolsSessionEntityPU", propMap);
66 conn = new JpaDroolsSessionConnector(emf);
70 public void tearDown() {
71 // this will cause the memory db to be dropped
76 public void testGet() {
78 * Load up the DB with some data.
81 addSession("nameA", 10);
82 addSession("nameY", 20);
85 * Now test the functionality.
89 assertNull(conn.get("unknown"));
91 assertEquals("{name=nameA, id=10}", conn.get("nameA").toString());
93 assertEquals("{name=nameY, id=20}", conn.get("nameY").toString());
96 @Test(expected = IllegalArgumentException.class)
97 public void testGet_NewEx() {
98 EntityManagerFactory emf = mock(EntityManagerFactory.class);
99 EntityManager em = mock(EntityManager.class);
101 when(emf.createEntityManager()).thenReturn(em);
102 when(em.find(any(), any())).thenThrow(new IllegalArgumentException("expected exception"));
104 conn = new JpaDroolsSessionConnector(emf);
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);
114 when(emf.createEntityManager()).thenReturn(em);
115 when(em.getTransaction()).thenReturn(tr);
116 when(em.find(any(), any())).thenThrow(new IllegalArgumentException("expected exception"));
118 new JpaDroolsSessionConnector(emf).get("xyz");
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);
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();
132 new JpaDroolsSessionConnector(emf).get("xyz");
136 public void testReplace_Existing() {
137 addSession("nameA", 10);
139 DroolsSessionEntity sess = new DroolsSessionEntity("nameA", 30);
143 // id should be changed
144 assertEquals(sess.toString(), conn.get("nameA").toString());
148 public void testReplace_New() {
149 DroolsSessionEntity sess = new DroolsSessionEntity("nameA", 30);
153 assertEquals(sess.toString(), conn.get("nameA").toString());
157 public void testAdd() {
158 DroolsSessionEntity sess = new DroolsSessionEntity("nameA", 30);
162 assertEquals(sess.toString(), conn.get("nameA").toString());
166 public void testUpdate() {
167 addSession("nameA", 10);
169 DroolsSessionEntity sess = new DroolsSessionEntity("nameA", 30);
173 // id should be changed
174 assertEquals("{name=nameA, id=30}", conn.get("nameA").toString());
178 * Adds a session to the DB.
180 * @param sessnm session name
181 * @param sessid session id
183 private void addSession(String sessnm, int sessid) {
184 EntityManager em = emf.createEntityManager();
186 try (EntityMgrTrans trans = new EntityMgrTrans(em)) {
187 DroolsSessionEntity ent = new DroolsSessionEntity();
189 ent.setSessionName(sessnm);
190 ent.setSessionId(sessid);