Remove AAF from xacml-pdp
[policy/xacml-pdp.git] / applications / common / src / test / java / org / onap / policy / pdp / xacml / application / common / operationshistory / CountRecentOperationsPipTest.java
index 9a0eb6d..c38ab71 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2023 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 package org.onap.policy.pdp.xacml.application.common.operationshistory;
 
 import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
 import static org.junit.Assert.assertEquals;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.when;
 
 import com.att.research.xacml.api.Attribute;
 import com.att.research.xacml.api.AttributeValue;
-import com.att.research.xacml.api.Status;
 import com.att.research.xacml.api.pip.PIPException;
 import com.att.research.xacml.api.pip.PIPFinder;
 import com.att.research.xacml.api.pip.PIPRequest;
 import com.att.research.xacml.api.pip.PIPResponse;
 import com.att.research.xacml.std.pip.StdPIPResponse;
+import jakarta.persistence.EntityManager;
+import jakarta.persistence.EntityManagerFactory;
+import jakarta.persistence.Persistence;
+import jakarta.persistence.Query;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.sql.Date;
@@ -41,18 +44,19 @@ import java.util.LinkedList;
 import java.util.Properties;
 import java.util.Queue;
 import java.util.UUID;
-import javax.persistence.EntityManager;
-import javax.persistence.Persistence;
-import javax.persistence.Query;
+import org.junit.After;
 import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.onap.policy.guard.OperationsHistory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+@RunWith(MockitoJUnitRunner.class)
 public class CountRecentOperationsPipTest {
     private static final Logger LOGGER = LoggerFactory.getLogger(CountRecentOperationsPipTest.class);
 
@@ -61,6 +65,7 @@ public class CountRecentOperationsPipTest {
     private static final String TARGET = "my-target";
     private static final String TEST_PROPERTIES = "src/test/resources/test.properties";
 
+    private static EntityManagerFactory emf;
     private static EntityManager em;
 
     @Mock
@@ -78,9 +83,6 @@ public class CountRecentOperationsPipTest {
     @Mock
     private PIPResponse resp3;
 
-    @Mock
-    private Status okStatus;
-
     private Properties properties;
     private Queue<PIPResponse> responses;
     private Queue<String> attributes;
@@ -107,11 +109,12 @@ public class CountRecentOperationsPipTest {
         //
         String persistenceUnit = CountRecentOperationsPip.ISSUER_NAME + ".persistenceunit";
         LOGGER.info("persistenceunit {}", persistenceUnit);
-        em = Persistence.createEntityManagerFactory(props2.getProperty(persistenceUnit), props2).createEntityManager();
+        emf = Persistence.createEntityManagerFactory(props2.getProperty(persistenceUnit), props2);
+        em = emf.createEntityManager();
         //
         //
         //
-        LOGGER.info("Configured own entity manager", em.toString());
+        LOGGER.info("Configured own entity manager {}", em.toString());
     }
 
     /**
@@ -131,8 +134,6 @@ public class CountRecentOperationsPipTest {
      */
     @Before
     public void setUp() throws Exception {
-        MockitoAnnotations.initMocks(this);
-
         when(pipRequest.getIssuer()).thenReturn("urn:org:onap:xacml:guard:tw:1:hour");
 
         pipEngine = new MyPip();
@@ -142,16 +143,8 @@ public class CountRecentOperationsPipTest {
             properties.load(is);
         }
 
-        when(pipFinder.getMatchingAttributes(any(), eq(pipEngine))).thenReturn(resp1, resp2, resp3);
-
         responses = new LinkedList<>(Arrays.asList(resp1, resp2, resp3));
         attributes = new LinkedList<>(Arrays.asList(ACTOR, RECIPE, TARGET));
-
-        when(resp1.getStatus()).thenReturn(okStatus);
-        when(resp2.getStatus()).thenReturn(okStatus);
-        when(resp3.getStatus()).thenReturn(okStatus);
-
-        when(okStatus.isOk()).thenReturn(true);
     }
 
     @Test
@@ -160,8 +153,8 @@ public class CountRecentOperationsPipTest {
     }
 
     @Test
-    public void testConfigure_DbException() throws Exception {
-        properties.put("javax.persistence.jdbc.url", "invalid");
+    public void testConfigure_DbException() {
+        properties.put("jakarta.persistence.jdbc.url", "invalid");
         assertThatCode(() ->
             pipEngine.configure("issuer", properties)
         ).doesNotThrowAnyException();
@@ -197,6 +190,13 @@ public class CountRecentOperationsPipTest {
         assertEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pipEngine.getAttributes(pipRequest, pipFinder));
     }
 
+    @Test
+    public void testShutdown() {
+        pipEngine.shutdown();
+        assertThatExceptionOfType(PIPException.class).isThrownBy(() -> pipEngine.getAttributes(pipRequest, pipFinder))
+            .withMessageContaining("Engine is shutdown");
+    }
+
     @Test
     public void testGetCountFromDb() throws Exception {
         //
@@ -208,7 +208,7 @@ public class CountRecentOperationsPipTest {
         //
         // create entry
         //
-        Dbao newEntry = createEntry("cl-foobar-1", "vnf-1", "SUCCESS");
+        OperationsHistory newEntry = createEntry();
         //
         // No entries yet
         //
@@ -222,7 +222,7 @@ public class CountRecentOperationsPipTest {
         //
         // Directly check ground truth
         //
-        Query queryCount = em.createNativeQuery("select count(*) as numops from operationshistory").setParameter(1, 1);
+        Query queryCount = em.createNativeQuery("select count(*) as numops from operationshistory");
         LOGGER.info("{} entries", queryCount.getSingleResult());
         //
         // Should count 1 entry now
@@ -231,9 +231,9 @@ public class CountRecentOperationsPipTest {
     }
 
     @Test
-    public void testStringToChronoUnit() throws PIPException {
+    public void testStringToChronosUnit() throws PIPException {
         // not configured yet
-        Dbao newEntry = createEntry("cl-foobar-1", "vnf-1", "SUCCESS");
+        OperationsHistory newEntry = createEntry();
         assertEquals(-1, getCount(newEntry));
 
         // now configure it
@@ -258,7 +258,7 @@ public class CountRecentOperationsPipTest {
         assertEquals(-1, getCount(newEntry));
     }
 
-    private long getCount(Dbao newEntry) throws PIPException {
+    private long getCount(OperationsHistory newEntry) throws PIPException {
         responses = new LinkedList<>(Arrays.asList(resp1, resp2, resp3));
         attributes = new LinkedList<>(
                         Arrays.asList(newEntry.getActor(), newEntry.getOperation(), newEntry.getTarget()));
@@ -271,14 +271,14 @@ public class CountRecentOperationsPipTest {
         return ((Number) value.getValue()).longValue();
     }
 
-    private Dbao createEntry(String cl, String target, String outcome) {
+    private OperationsHistory createEntry() {
         //
         // Create entry
         //
-        Dbao newEntry = new Dbao();
-        newEntry.setClosedLoopName(cl);
-        newEntry.setTarget(target);
-        newEntry.setOutcome(outcome);
+        OperationsHistory newEntry = new OperationsHistory();
+        newEntry.setClosedLoopName("cl-foobar-1");
+        newEntry.setTarget("vnf-1");
+        newEntry.setOutcome("SUCCESS");
         newEntry.setActor("Controller");
         newEntry.setOperation("operationA");
         newEntry.setStarttime(Date.from(Instant.now().minusMillis(20000)));