Modify Actors to use properties when provided
[policy/models.git] / models-interactions / model-actors / actor.so / src / test / java / org / onap / policy / controlloop / actor / so / VfModuleDeleteTest.java
index f5d05a0..9335f01 100644 (file)
 
 package org.onap.policy.controlloop.actor.so;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
@@ -39,6 +41,7 @@ import java.net.http.HttpResponse;
 import java.net.http.HttpResponse.BodyHandlers;
 import java.nio.charset.StandardCharsets;
 import java.util.Base64;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ForkJoinPool;
@@ -63,8 +66,10 @@ import org.onap.policy.aai.AaiCqResponse;
 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
 import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
+import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
-import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingParams;
 import org.onap.policy.controlloop.policy.PolicyResult;
 import org.onap.policy.so.SoRequest;
 import org.onap.policy.so.SoResponse;
@@ -119,11 +124,12 @@ public class VfModuleDeleteTest extends BasicSoOperation {
      */
     @Test
     public void testSuccess() throws Exception {
-        SoParams opParams = SoParams.builder().clientName(MY_CLIENT).path("serviceInstances/v7")
-                        .pathGet("orchestrationRequests/v5/").maxGets(2).build();
-        config = new SoConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
+        HttpPollingParams opParams = HttpPollingParams.builder().clientName(MY_CLIENT).path("serviceInstances/v7")
+                        .pollPath("orchestrationRequests/v5/").maxPolls(2).build();
+        config = new HttpPollingConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
 
         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
+
         oper = new VfModuleDelete(params, config);
 
         outcome = oper.start().get();
@@ -131,6 +137,43 @@ public class VfModuleDeleteTest extends BasicSoOperation {
         assertTrue(outcome.getResponse() instanceof SoResponse);
     }
 
+    /**
+     * Tests "success" case with simulator, using properties instead of custom query data.
+     */
+    @Test
+    public void testSuccessViaProperties() throws Exception {
+        HttpPollingParams opParams = HttpPollingParams.builder().clientName(MY_CLIENT).path("serviceInstances/v7")
+                        .pollPath("orchestrationRequests/v5/").maxPolls(2).build();
+        config = new HttpPollingConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
+
+        params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).preprocessed(true).build();
+        params.getContext().removeProperty(AaiCqResponse.CONTEXT_KEY);
+
+        oper = new VfModuleDelete(params, config);
+
+        // set the properties
+        ServiceInstance instance = new ServiceInstance();
+        instance.setServiceInstanceId(SVC_INSTANCE_ID);
+        oper.setProperty(OperationProperties.AAI_SERVICE, instance);
+
+        GenericVnf vnf = new GenericVnf();
+        vnf.setVnfId(VNF_ID);
+        oper.setProperty(OperationProperties.AAI_VNF, vnf);
+
+        oper.setProperty(OperationProperties.AAI_DEFAULT_CLOUD_REGION, new CloudRegion());
+        oper.setProperty(OperationProperties.AAI_DEFAULT_TENANT, new Tenant());
+
+        oper.setProperty(OperationProperties.DATA_VF_COUNT, VF_COUNT);
+
+        // run the operation
+        outcome = oper.start().get();
+        assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+        assertTrue(outcome.getResponse() instanceof SoResponse);
+
+        int count = oper.getProperty(OperationProperties.DATA_VF_COUNT);
+        assertEquals(VF_COUNT - 1, count);
+    }
+
     @Test
     public void testConstructor() {
         assertEquals(DEFAULT_ACTOR, oper.getActorName());
@@ -138,10 +181,23 @@ public class VfModuleDeleteTest extends BasicSoOperation {
 
         // verify that target validation is done
         params = params.toBuilder().target(null).build();
-        assertThatIllegalArgumentException().isThrownBy(() -> new VfModuleDelete(params, config))
+        assertThatIllegalArgumentException().isThrownBy(() -> new MyOperation(params, config))
                         .withMessageContaining("Target information");
     }
 
+    @Test
+    public void testGetPropertyNames() {
+        // @formatter:off
+        assertThat(oper.getPropertyNames()).isEqualTo(
+                        List.of(
+                            OperationProperties.AAI_SERVICE,
+                            OperationProperties.AAI_VNF,
+                            OperationProperties.AAI_DEFAULT_CLOUD_REGION,
+                            OperationProperties.AAI_DEFAULT_TENANT,
+                            OperationProperties.DATA_VF_COUNT));
+        // @formatter:on
+    }
+
     @Test
     public void testStartPreprocessorAsync() throws Exception {
         // insert CQ data so it's there for the check
@@ -162,6 +218,15 @@ public class VfModuleDeleteTest extends BasicSoOperation {
         assertTrue(guardStarted.get());
     }
 
+    /**
+     * Tests startPreprocessorAsync(), when preprocessing is disabled.
+     */
+    @Test
+    public void testStartPreprocessorAsyncDisabled() {
+        params = params.toBuilder().preprocessed(true).build();
+        assertNull(new MyOperation(params, config).startPreprocessorAsync());
+    }
+
     @Test
     public void testStartGuardAsync() throws Exception {
         // remove CQ data so it's forced to query
@@ -208,7 +273,7 @@ public class VfModuleDeleteTest extends BasicSoOperation {
 
         oper = new MyOperation(params, config) {
             @Override
-            public long getWaitMsGet() {
+            public long getPollWaitMs() {
                 return 1;
             }
         };
@@ -226,10 +291,10 @@ public class VfModuleDeleteTest extends BasicSoOperation {
     }
 
     /**
-     * Tests startOperationAsync() when "get" operations are required.
+     * Tests startOperationAsync() when polling is required.
      */
     @Test
-    public void testStartOperationAsyncWithGets() throws Exception {
+    public void testStartOperationAsyncWithPolling() throws Exception {
 
         // indicate that the response was incomplete
         configureResponse(coder.encode(response).replace("COMPLETE", "incomplete"));
@@ -242,7 +307,7 @@ public class VfModuleDeleteTest extends BasicSoOperation {
 
         oper = new MyOperation(params, config) {
             @Override
-            public long getWaitMsGet() {
+            public long getPollWaitMs() {
                 return 1;
             }
         };
@@ -383,7 +448,7 @@ public class VfModuleDeleteTest extends BasicSoOperation {
     @Test
     public void testMakeHttpClient() {
         // must use a real operation to invoke this method
-        assertNotNull(new VfModuleDelete(params, config).makeHttpClient());
+        assertNotNull(new MyOperation(params, config).makeHttpClient());
     }
 
 
@@ -428,7 +493,7 @@ public class VfModuleDeleteTest extends BasicSoOperation {
 
     private class MyOperation extends VfModuleDelete {
 
-        public MyOperation(ControlLoopOperationParams params, HttpConfig config) {
+        public MyOperation(ControlLoopOperationParams params, HttpPollingConfig config) {
             super(params, config);
         }