Upgrade Java 17 in xacml-pdp
[policy/xacml-pdp.git] / applications / common / src / test / java / org / onap / policy / pdp / xacml / application / common / operationshistory / CountRecentOperationsPipTest.java
index 9a0eb6d..e564cd9 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.
@@ -19,9 +20,8 @@
 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;
@@ -32,6 +32,9 @@ 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.Persistence;
+import jakarta.persistence.Query;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.sql.Date;
@@ -41,18 +44,18 @@ 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.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);
 
@@ -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
@@ -161,7 +154,7 @@ public class CountRecentOperationsPipTest {
 
     @Test
     public void testConfigure_DbException() throws Exception {
-        properties.put("javax.persistence.jdbc.url", "invalid");
+        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("cl-foobar-1", "vnf-1", "SUCCESS");
         //
         // 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
@@ -233,7 +233,7 @@ public class CountRecentOperationsPipTest {
     @Test
     public void testStringToChronoUnit() throws PIPException {
         // not configured yet
-        Dbao newEntry = createEntry("cl-foobar-1", "vnf-1", "SUCCESS");
+        OperationsHistory newEntry = createEntry("cl-foobar-1", "vnf-1", "SUCCESS");
         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,11 +271,11 @@ public class CountRecentOperationsPipTest {
         return ((Number) value.getValue()).longValue();
     }
 
-    private Dbao createEntry(String cl, String target, String outcome) {
+    private OperationsHistory createEntry(String cl, String target, String outcome) {
         //
         // Create entry
         //
-        Dbao newEntry = new Dbao();
+        OperationsHistory newEntry = new OperationsHistory();
         newEntry.setClosedLoopName(cl);
         newEntry.setTarget(target);
         newEntry.setOutcome(outcome);