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 b01fa70..c38ab71 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
- * Copyright (C) 2019 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.Matchers.any;
-import static org.mockito.Matchers.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;
@@ -40,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);
 
@@ -60,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
@@ -77,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;
@@ -106,11 +109,22 @@ 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());
+    }
+
+    /**
+     * Close the entity manager.
+     */
+    @AfterClass
+    public static void cleanup() {
+        if (em != null) {
+            em.close();
+        }
     }
 
     /**
@@ -120,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();
@@ -131,32 +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);
-    }
-
-    private Dbao createEntry(String cl, String target, String outcome) {
-        //
-        // Create entry
-        //
-        Dbao newEntry = new Dbao();
-        newEntry.setClosedLoopName(cl);
-        newEntry.setTarget(target);
-        newEntry.setOutcome(outcome);
-        newEntry.setActor("Controller");
-        newEntry.setOperation("operationA");
-        newEntry.setStarttime(Date.from(Instant.now().minusMillis(20000)));
-        newEntry.setEndtime(Date.from(Instant.now()));
-        newEntry.setRequestId(UUID.randomUUID().toString());
-        return newEntry;
     }
 
     @Test
@@ -165,9 +153,11 @@ public class CountRecentOperationsPipTest {
     }
 
     @Test
-    public void testConfigure_DbException() throws Exception {
-        properties.put("javax.persistence.jdbc.url", "invalid");
-        pipEngine.configure("issuer", properties);
+    public void testConfigure_DbException() {
+        properties.put("jakarta.persistence.jdbc.url", "invalid");
+        assertThatCode(() ->
+            pipEngine.configure("issuer", properties)
+        ).doesNotThrowAnyException();
     }
 
     @Test
@@ -200,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 {
         //
@@ -211,7 +208,7 @@ public class CountRecentOperationsPipTest {
         //
         // create entry
         //
-        Dbao newEntry = createEntry("cl-foobar-1", "vnf-1", "SUCCESS");
+        OperationsHistory newEntry = createEntry();
         //
         // No entries yet
         //
@@ -225,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
@@ -233,23 +230,10 @@ public class CountRecentOperationsPipTest {
         assertEquals(1, getCount(newEntry));
     }
 
-    private long getCount(Dbao newEntry) throws PIPException {
-        responses = new LinkedList<>(Arrays.asList(resp1, resp2, resp3));
-        attributes = new LinkedList<>(
-                        Arrays.asList(newEntry.getActor(), newEntry.getOperation(), newEntry.getTarget()));
-
-        PIPResponse result = pipEngine.getAttributes(pipRequest, pipFinder);
-
-        Attribute attr = result.getAttributes().iterator().next();
-        AttributeValue<?> value = attr.getValues().iterator().next();
-
-        return ((Number) value.getValue()).longValue();
-    }
-
     @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
@@ -274,14 +258,33 @@ public class CountRecentOperationsPipTest {
         assertEquals(-1, getCount(newEntry));
     }
 
-    /**
-     * Close the entity manager.
-     */
-    @AfterClass
-    public static void cleanup() {
-        if (em != null) {
-            em.close();
-        }
+    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()));
+
+        PIPResponse result = pipEngine.getAttributes(pipRequest, pipFinder);
+
+        Attribute attr = result.getAttributes().iterator().next();
+        AttributeValue<?> value = attr.getValues().iterator().next();
+
+        return ((Number) value.getValue()).longValue();
+    }
+
+    private OperationsHistory createEntry() {
+        //
+        // Create entry
+        //
+        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)));
+        newEntry.setEndtime(Date.from(Instant.now()));
+        newEntry.setRequestId(UUID.randomUUID().toString());
+        return newEntry;
     }
 
     private class MyPip extends CountRecentOperationsPip {