Remove Target and TargetType
[policy/models.git] / models-interactions / model-actors / actor.so / src / test / java / org / onap / policy / controlloop / actor / so / VfModuleCreateTest.java
index 918559a..f1741d6 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.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ForkJoinPool;
@@ -50,9 +53,13 @@ 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.OperationResult;
 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
-import org.onap.policy.controlloop.policy.PolicyResult;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingParams;
 import org.onap.policy.so.SoRequest;
+import org.onap.policy.so.SoResponse;
 
 public class VfModuleCreateTest extends BasicSoOperation {
     private static final String MODEL_NAME2 = "my-model-name-B";
@@ -76,6 +83,7 @@ public class VfModuleCreateTest extends BasicSoOperation {
         destroyAfterClass();
     }
 
+    @Override
     @Before
     public void setUp() throws Exception {
         super.setUp();
@@ -87,15 +95,63 @@ public class VfModuleCreateTest extends BasicSoOperation {
      */
     @Test
     public void testSuccess() throws Exception {
-        SoParams opParams = SoParams.builder().clientName(MY_CLIENT).path("serviceInstantiation/v7/serviceInstances")
-                        .pathGet("orchestrationRequests/v5/").build();
-        config = new SoConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
+        HttpPollingParams opParams = HttpPollingParams.builder().clientName(MY_CLIENT)
+                        .path("serviceInstantiation/v7/serviceInstances").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 VfModuleCreate(params, config);
 
         outcome = oper.start().get();
-        assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+        assertEquals(OperationResult.SUCCESS, outcome.getResult());
+        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("serviceInstantiation/v7/serviceInstances").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 VfModuleCreate(params, config);
+
+        // set the properties
+        ServiceInstance instance = new ServiceInstance();
+        instance.setServiceInstanceId(SVC_INSTANCE_ID);
+        oper.setProperty(OperationProperties.AAI_SERVICE, instance);
+
+        ModelVer modelVers = new ModelVer();
+        modelVers.setModelName(MODEL_NAME2);
+        modelVers.setModelVersion(MODEL_VERS2);
+
+        oper.setProperty(OperationProperties.AAI_SERVICE_MODEL, modelVers);
+        oper.setProperty(OperationProperties.AAI_VNF_MODEL, modelVers);
+
+        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(OperationResult.SUCCESS, outcome.getResult());
+        assertTrue(outcome.getResponse() instanceof SoResponse);
+
+        int count = oper.getProperty(OperationProperties.DATA_VF_COUNT);
+        assertEquals(VF_COUNT + 1, count);
     }
 
     @Test
@@ -104,11 +160,26 @@ public class VfModuleCreateTest extends BasicSoOperation {
         assertEquals(VfModuleCreate.NAME, oper.getName());
 
         // verify that target validation is done
-        params = params.toBuilder().target(null).build();
+        params = params.toBuilder().targetType(null).build();
         assertThatIllegalArgumentException().isThrownBy(() -> new VfModuleCreate(params, config))
                         .withMessageContaining("Target information");
     }
 
+    @Test
+    public void testGetPropertyNames() {
+        // @formatter:off
+        assertThat(oper.getPropertyNames()).isEqualTo(
+                        List.of(
+                            OperationProperties.AAI_SERVICE,
+                            OperationProperties.AAI_SERVICE_MODEL,
+                            OperationProperties.AAI_VNF,
+                            OperationProperties.AAI_VNF_MODEL,
+                            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
@@ -129,6 +200,15 @@ public class VfModuleCreateTest extends BasicSoOperation {
         assertTrue(guardStarted.get());
     }
 
+    /**
+     * Tests startPreprocessorAsync(), when preprocessing is disabled.
+     */
+    @Test
+    public void testStartPreprocessorAsyncDisabled() {
+        params = params.toBuilder().preprocessed(true).build();
+        assertNull(new VfModuleCreate(params, config).startPreprocessorAsync());
+    }
+
     @Test
     public void testStartGuardAsync() throws Exception {
         // remove CQ data so it's forced to query
@@ -141,7 +221,7 @@ public class VfModuleCreateTest extends BasicSoOperation {
         provideCqResponse(makeCqResponse());
         assertTrue(executor.runAll(100));
         assertTrue(future2.isDone());
-        assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
+        assertEquals(OperationResult.SUCCESS, future2.get().getResult());
     }
 
     @Test
@@ -177,7 +257,7 @@ public class VfModuleCreateTest extends BasicSoOperation {
 
         oper = new VfModuleCreate(params, config) {
             @Override
-            public long getWaitMsGet() {
+            protected long getPollWaitMs() {
                 return 1;
             }
         };
@@ -185,16 +265,20 @@ public class VfModuleCreateTest extends BasicSoOperation {
         CompletableFuture<OperationOutcome> future2 = oper.start();
 
         outcome = future2.get(5, TimeUnit.SECONDS);
-        assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+        assertEquals(OperationResult.SUCCESS, outcome.getResult());
+
+        SoResponse resp = outcome.getResponse();
+        assertNotNull(resp);
+        assertEquals(REQ_ID.toString(), resp.getRequestReferences().getRequestId());
 
         assertEquals(origCount + 1, oper.getVfCount());
     }
 
     /**
-     * Tests startOperationAsync() when "get" operations are required.
+     * Tests startOperationAsync() when polling is required.
      */
     @Test
-    public void testStartOperationAsyncWithGets() throws Exception {
+    public void testStartOperationAsyncWithPolling() throws Exception {
         when(rawResponse.getStatus()).thenReturn(500, 500, 500, 500, 200, 200);
 
         when(client.post(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
@@ -205,7 +289,7 @@ public class VfModuleCreateTest extends BasicSoOperation {
 
         oper = new VfModuleCreate(params, config) {
             @Override
-            public long getWaitMsGet() {
+            public long getPollWaitMs() {
                 return 1;
             }
         };
@@ -213,7 +297,7 @@ public class VfModuleCreateTest extends BasicSoOperation {
         CompletableFuture<OperationOutcome> future2 = oper.start();
 
         outcome = future2.get(5, TimeUnit.SECONDS);
-        assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+        assertEquals(OperationResult.SUCCESS, outcome.getResult());
     }
 
     @Test