Custom Query Code
[policy/models.git] / models-interactions / model-actors / actor.vfc / src / test / java / org / onap / policy / controlloop / actor / vfc / VfcActorServiceProviderTest.java
index 80ec347..8d5d5fa 100644 (file)
@@ -27,12 +27,16 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.fail;
 
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 import java.util.Objects;
 import java.util.UUID;
-
+import org.apache.commons.io.IOUtils;
+import org.eclipse.persistence.exceptions.JAXBException;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.onap.policy.aai.AaiCqResponse;
 import org.onap.policy.aai.AaiGetVnfResponse;
 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
 import org.onap.policy.controlloop.ControlLoopOperation;
@@ -73,27 +77,34 @@ public class VfcActorServiceProviderTest {
         onset.getAai().put("generic-vnf.vnf-id", "dorothy.gale.1939");
         assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, null, null, null));
 
-        assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666", "AAI", "AAI"));
+        assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666",
+                "AAI", "AAI"));
 
         UUID requestId = UUID.randomUUID();
         onset.setRequestId(requestId);
-        assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666", "AAI", "AAI"));
+        assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666",
+                "AAI", "AAI"));
 
         onset.getAai().put("generic-vnf.vnf-name", "Dorothy");
-        assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666", "AAI", null));
+        assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666",
+                "AAI", null));
 
-        assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666", "AAI", "AAI"));
+        assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666",
+                "AAI", "AAI"));
 
         onset.getAai().put("service-instance.service-instance-id", "");
-        assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666", "AAI", "AAI"));
+        assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666",
+                "AAI", "AAI"));
 
-        assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse(), "http://localhost:6666", "AAI", "AAI"));
+        assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse(),
+                "http://localhost:6666", "AAI", "AAI"));
 
         policy.setRecipe("Restart");
-        assertNotNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse(), "http://localhost:6666", "AAI", "AAI"));
+        assertNotNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse(),
+                "http://localhost:6666", "AAI", "AAI"));
 
-        VfcRequest request =
-                VfcActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse(), "http://localhost:6666", "AAI", "AAI");
+        VfcRequest request = VfcActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse(),
+                "http://localhost:6666", "AAI", "AAI");
 
         assertEquals(requestId, Objects.requireNonNull(request).getRequestId());
         assertEquals("dorothy.gale.1939", request.getHealRequest().getVnfInstanceId());
@@ -110,4 +121,58 @@ public class VfcActorServiceProviderTest {
         assertEquals("VM", sp.recipeTargets("Restart").get(0));
         assertEquals(0, sp.recipePayloads("Restart").size());
     }
+
+    @Test
+    public void testConstructRequestCq() throws IOException, JAXBException {
+        VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
+        ControlLoopOperation operation = new ControlLoopOperation();
+
+        Policy policy = new Policy();
+        policy.setRecipe("GoToOz");
+
+        assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
+
+        onset.getAai().put("generic-vnf.vnf-id", "dorothy.gale.1939");
+        assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
+
+
+        UUID requestId = UUID.randomUUID();
+        onset.setRequestId(requestId);
+        assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
+
+        onset.getAai().put("generic-vnf.vnf-name", "Dorothy");
+        assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
+
+
+        onset.getAai().put("service-instance.service-instance-id", "");
+        assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
+
+        assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy,
+                loadAaiResponse("aai/AaiCqResponse.json")));
+
+        policy.setRecipe("Restart");
+        assertNotNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy,
+                loadAaiResponse("aai/AaiCqResponse.json")));
+
+        VfcRequest request = VfcActorServiceProvider.constructRequestCq(onset, operation, policy,
+                loadAaiResponse("aai/AaiCqResponse.json"));
+
+        assertEquals(requestId, Objects.requireNonNull(request).getRequestId());
+        assertEquals("dorothy.gale.1939", request.getHealRequest().getVnfInstanceId());
+        assertEquals("restartvm", request.getHealRequest().getAdditionalParams().getAction());
+    }
+
+    /**
+     * Reads an AAI vserver named-query response from a file.
+     *
+     * @param fileName name of the file containing the JSON response
+     * @return output from the AAI vserver named-query
+     * @throws IOException if the file cannot be read
+     * @throws JAXBException throws JAXBException
+     */
+    private AaiCqResponse loadAaiResponse(String fileName) throws IOException, JAXBException {
+        String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
+        return new AaiCqResponse(resp);
+    }
+
 }