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
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.Matchers.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;
33 import javax.persistence.EntityManager;
34 import javax.persistence.EntityManagerFactory;
35 import javax.persistence.EntityTransaction;
36 import javax.persistence.Persistence;
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;
46 public class JpaDroolsSessionConnectorTest {
48 private EntityManagerFactory emf;
49 private JpaDroolsSessionConnector conn;
52 public static void setUpBeforeClass() {
53 System.setProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir", "target/tm");
54 System.setProperty("ObjectStoreEnvironmentBean.objectStoreDir", "target/tm");
60 * @throws Exception exception
63 public void setUp() throws Exception {
64 Map<String, Object> propMap = new HashMap<>();
66 propMap.put("javax.persistence.jdbc.driver", "org.h2.Driver");
67 propMap.put("javax.persistence.jdbc.url", "jdbc:h2:mem:JpaDroolsSessionConnectorTest");
69 emf = Persistence.createEntityManagerFactory("junitDroolsSessionEntityPU", propMap);
71 conn = new JpaDroolsSessionConnector(emf);
75 public void tearDown() {
76 // this will cause the memory db to be dropped
81 public void testGet() {
83 * Load up the DB with some data.
86 addSession("nameA", 10);
87 addSession("nameY", 20);
90 * Now test the functionality.
94 assertNull(conn.get("unknown"));
96 assertEquals("{name=nameA, id=10}", conn.get("nameA").toString());
98 assertEquals("{name=nameY, id=20}", conn.get("nameY").toString());
101 @Test(expected = IllegalArgumentException.class)
102 public void testGet_NewEx() {
103 EntityManagerFactory emf = mock(EntityManagerFactory.class);
104 EntityManager em = mock(EntityManager.class);
106 when(emf.createEntityManager()).thenReturn(em);
107 when(em.find(any(), any())).thenThrow(new IllegalArgumentException("expected exception"));
109 conn = new JpaDroolsSessionConnector(emf);
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);
119 when(emf.createEntityManager()).thenReturn(em);
120 when(em.getTransaction()).thenReturn(tr);
121 when(em.find(any(), any())).thenThrow(new IllegalArgumentException("expected exception"));
123 new JpaDroolsSessionConnector(emf).get("xyz");
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);
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();
137 new JpaDroolsSessionConnector(emf).get("xyz");
141 public void testReplace_Existing() {
142 addSession("nameA", 10);
144 DroolsSessionEntity sess = new DroolsSessionEntity("nameA", 30);
148 // id should be changed
149 assertEquals(sess.toString(), conn.get("nameA").toString());
153 public void testReplace_New() {
154 DroolsSessionEntity sess = new DroolsSessionEntity("nameA", 30);
158 assertEquals(sess.toString(), conn.get("nameA").toString());
162 public void testAdd() {
163 DroolsSessionEntity sess = new DroolsSessionEntity("nameA", 30);
167 assertEquals(sess.toString(), conn.get("nameA").toString());
171 public void testUpdate() {
172 addSession("nameA", 10);
174 DroolsSessionEntity sess = new DroolsSessionEntity("nameA", 30);
178 // id should be changed
179 assertEquals("{name=nameA, id=30}", conn.get("nameA").toString());
183 * Adds a session to the DB.
185 * @param sessnm session name
186 * @param sessid session id
188 private void addSession(String sessnm, int sessid) {
189 EntityManager em = emf.createEntityManager();
191 try (EntityMgrTrans trans = new EntityMgrTrans(em)) {
192 DroolsSessionEntity ent = new DroolsSessionEntity();
194 ent.setSessionName(sessnm);
195 ent.setSessionId(sessid);