Upgrade Java 17 in xacml-pdp
[policy/xacml-pdp.git] / applications / common / src / test / java / org / onap / policy / pdp / xacml / application / common / operationshistory / GetOperationOutcomePipTest.java
index f4ed1a3..bd53789 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.assertThat;
 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.junit.Assert.assertNotEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+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.Persistence;
 import java.io.FileInputStream;
 import java.lang.reflect.Method;
-import java.sql.Date;
 import java.time.Instant;
+import java.util.Date;
 import java.util.Properties;
 import java.util.UUID;
-import javax.persistence.EntityManager;
-import javax.persistence.Persistence;
 import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.onap.policy.guard.OperationsHistory;
+import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -52,11 +61,13 @@ public class GetOperationOutcomePipTest {
     private Properties properties;
     private GetOperationOutcomePip pipEngine;
 
-    @Mock
-    private PIPRequest pipRequest;
+    private final PIPRequest pipRequest = mock(PIPRequest.class);
 
-    @Mock
-    private PIPFinder pipFinder;
+    private final PIPFinder pipFinder = mock(PIPFinder.class);
+
+    private final PIPResponse resp1 = mock(PIPResponse.class);
+
+    private final Status okStatus = mock(Status.class);
 
     /**
      * Create an instance of our engine and also the persistence
@@ -104,8 +115,6 @@ public class GetOperationOutcomePipTest {
      */
     @Before
     public void setupEngine() throws Exception {
-        MockitoAnnotations.initMocks(this);
-
         when(pipRequest.getIssuer()).thenReturn("urn:org:onap:xacml:guard:tw:1:hour");
         //
         // Create instance
@@ -124,7 +133,6 @@ public class GetOperationOutcomePipTest {
         pipEngine.configure("issuer", properties);
         LOGGER.info("PIP configured now creating our entity manager");
         LOGGER.info("properties {}", properties);
-
     }
 
     @Test
@@ -134,7 +142,7 @@ public class GetOperationOutcomePipTest {
 
     @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();
@@ -152,51 +160,76 @@ public class GetOperationOutcomePipTest {
         assertEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pipEngine.getAttributes(pipRequest, pipFinder));
     }
 
+    @Test
+    public void testGetAttributes() throws Exception {
+        //
+        //
+        //
+        when(pipRequest.getIssuer()).thenReturn(ToscaDictionary.GUARD_ISSUER_PREFIX + "clname:clfoo");
+        when(pipFinder.getMatchingAttributes(any(), eq(pipEngine))).thenReturn(resp1);
+        when(resp1.getStatus()).thenReturn(okStatus);
+        when(okStatus.isOk()).thenReturn(true);
+
+        assertNotEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pipEngine.getAttributes(pipRequest, pipFinder));
+
+        pipEngine.shutdown();
+
+        assertThatExceptionOfType(PIPException.class).isThrownBy(() -> pipEngine.getAttributes(pipRequest, pipFinder))
+            .withMessageContaining("Engine is shutdown");
+    }
+
     @Test
     public void testGetOutcomeFromDb() throws Exception {
         //
         // Use reflection to run getCountFromDB
         //
         Method method = GetOperationOutcomePip.class.getDeclaredMethod("doDatabaseQuery",
-                                                                       String.class,
                                                                        String.class);
         method.setAccessible(true);
         //
+        // Test pipEngine
+        //
+        String outcome = (String) method.invoke(pipEngine, "testcl1");
+        assertThat(outcome).isNull();
+        //
         // Insert entry
         //
-        insertEntry("testcl1", "testtarget1", "1");
+        insertEntry("testcl1", "testtarget1", "Started");
         //
         // Test pipEngine
         //
-        String outcome = (String) method.invoke(pipEngine, "testcl1", "testtarget1");
+        outcome = (String) method.invoke(pipEngine, "testcl1");
         //
-        // outcome should be "1"
+        // outcome should be "In_Progress"
         //
-        assertEquals("1", outcome);
+        assertEquals("In_Progress", outcome);
         //
         // Insert more entries
         //
-        insertEntry("testcl1", "testtarget1", "2");
-        insertEntry("testcl2", "testtarget2", "3");
-        insertEntry("testcl1", "testtarget2", "4");
+        insertEntry("testcl2", "testtarget1", "Success");
+        insertEntry("testcl3", "testtarget2", "Failed");
         //
         // Test pipEngine
         //
-        outcome = (String) method.invoke(pipEngine, "testcl1", "testtarget1");
-        assertEquals("2", outcome);
+        outcome = (String) method.invoke(pipEngine, "testcl2");
+        assertEquals("Complete", outcome);
+
+        outcome = (String) method.invoke(pipEngine, "testcl3");
+        assertEquals("Complete", outcome);
 
-        outcome = (String) method.invoke(pipEngine, "testcl2", "testtarget2");
-        assertEquals("3", outcome);
+        //
+        // Shut it down
+        //
+        pipEngine.shutdown();
 
-        outcome = (String) method.invoke(pipEngine, "testcl1", "testtarget2");
-        assertEquals("4", outcome);
+        assertThat(method.invoke(pipEngine, "testcl1")).isNull();
     }
 
     private void insertEntry(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);