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
 
  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.Test;
 
  41 import org.onap.policy.drools.persistence.DroolsSessionEntity;
 
  42 import org.onap.policy.drools.persistence.EntityMgrTrans;
 
  43 import org.onap.policy.drools.persistence.JpaDroolsSessionConnector;
 
  45 public class JpaDroolsSessionConnectorTest {
 
  47         private EntityManagerFactory emf;
 
  48         private JpaDroolsSessionConnector conn;
 
  52         public void setUp() throws Exception {
 
  53                 Map<String, Object> propMap = new HashMap<>();
 
  55                 propMap.put("javax.persistence.jdbc.driver", "org.h2.Driver");
 
  56                 propMap.put("javax.persistence.jdbc.url",
 
  57                                                 "jdbc:h2:mem:JpaDroolsSessionConnectorTest");
 
  59                 emf = Persistence.createEntityManagerFactory(
 
  60                                                                 "junitDroolsSessionEntityPU", propMap);
 
  62                 conn = new JpaDroolsSessionConnector(emf);
 
  66         public void tearDown() {
 
  67                 // this will cause the memory db to be dropped
 
  72         public void testGet() {
 
  74                  * Load up the DB with some data.
 
  77                 addSession("nameA", 10);
 
  78                 addSession("nameY", 20);
 
  82                  * Now test the functionality.
 
  86                 assertNull( conn.get("unknown"));
 
  88                 assertEquals("{name=nameA, id=10}",
 
  89                                                 conn.get("nameA").toString());
 
  91                 assertEquals("{name=nameY, id=20}",
 
  92                                                 conn.get("nameY").toString());
 
  95         @Test(expected = RuntimeException.class)
 
  96         public void testGet_NewEx() {
 
  97                 EntityManagerFactory emf = mock(EntityManagerFactory.class);
 
  98                 EntityManager em = mock(EntityManager.class);
 
 100                 when(emf.createEntityManager()).thenReturn(em);
 
 101                 when(em.getTransaction()).thenThrow(new RuntimeException("expected exception"));
 
 103                 conn = new JpaDroolsSessionConnector(emf);
 
 107         @Test(expected = RuntimeException.class)
 
 108         public void testGet_FindEx() {
 
 109                 EntityManagerFactory emf = mock(EntityManagerFactory.class);
 
 110                 EntityManager em = mock(EntityManager.class);
 
 111                 EntityTransaction tr = mock(EntityTransaction.class);
 
 113                 when(emf.createEntityManager()).thenReturn(em);
 
 114                 when(em.getTransaction()).thenReturn(tr);
 
 115                 when(em.find(any(), any())).thenThrow(new RuntimeException("expected exception"));
 
 117                 new JpaDroolsSessionConnector(emf).get("xyz");
 
 120         @Test(expected = RuntimeException.class)
 
 121         public void testGet_FindEx_CloseEx() {
 
 122                 EntityManagerFactory emf = mock(EntityManagerFactory.class);
 
 123                 EntityManager em = mock(EntityManager.class);
 
 124                 EntityTransaction tr = mock(EntityTransaction.class);
 
 126                 when(emf.createEntityManager()).thenReturn(em);
 
 127                 when(em.getTransaction()).thenReturn(tr);
 
 128                 when(em.find(any(), any())).thenThrow(new RuntimeException("expected exception"));
 
 129                 doThrow(new RuntimeException("expected exception #2")).when(em).close();
 
 131                 new JpaDroolsSessionConnector(emf).get("xyz");
 
 135         public void testReplace_Existing() {
 
 136                 addSession("nameA", 10);
 
 138                 DroolsSessionEntity sess =
 
 139                                 new DroolsSessionEntity("nameA", 30);
 
 143                 // id should be changed
 
 144                 assertEquals(sess.toString(),
 
 145                                                 conn.get("nameA").toString());
 
 149         public void testReplace_New() {
 
 150                 DroolsSessionEntity sess =
 
 151                                 new DroolsSessionEntity("nameA", 30);
 
 155                 assertEquals(sess.toString(),
 
 156                                                 conn.get("nameA").toString());
 
 160         public void testAdd() {
 
 161                 DroolsSessionEntity sess =
 
 162                                 new DroolsSessionEntity("nameA", 30);
 
 166                 assertEquals(sess.toString(),
 
 167                                                 conn.get("nameA").toString());
 
 171         public void testUpdate() {
 
 172                 addSession("nameA", 10);
 
 174                 DroolsSessionEntity sess =
 
 175                                 new DroolsSessionEntity("nameA", 30);
 
 179                 // id should be changed
 
 180                 assertEquals("{name=nameA, id=30}",
 
 181                                                 conn.get("nameA").toString());
 
 186          * Adds a session to the DB.
 
 187          * @param sessnm        session name
 
 188          * @param sessid        session id
 
 190         private void addSession(String sessnm, int sessid) {
 
 191                 EntityManager em = emf.createEntityManager();
 
 193                 try(EntityMgrTrans trans = new EntityMgrTrans(em)) {
 
 194                         DroolsSessionEntity ent = new DroolsSessionEntity();
 
 196                         ent.setSessionName(sessnm);
 
 197                         ent.setSessionId(sessid);