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 / SoOperationTest.java
index 871d370..e364246 100644 (file)
 
 package org.onap.policy.controlloop.actor.so;
 
+import static org.assertj.core.api.Assertions.assertThatCode;
 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;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+import java.time.LocalDateTime;
+import java.time.Month;
 import java.util.Collections;
 import java.util.List;
-import java.util.Map;
 import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ForkJoinPool;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.BiConsumer;
 import java.util.function.Consumer;
 import java.util.function.Supplier;
 import org.junit.Before;
 import org.junit.Test;
 import org.onap.aai.domain.yang.CloudRegion;
 import org.onap.aai.domain.yang.GenericVnf;
+import org.onap.aai.domain.yang.ModelVer;
 import org.onap.aai.domain.yang.ServiceInstance;
 import org.onap.aai.domain.yang.Tenant;
 import org.onap.policy.aai.AaiCqResponse;
+import org.onap.policy.common.utils.coder.Coder;
 import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.controlloop.ControlLoopOperation;
 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
+import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
 import org.onap.policy.controlloop.policy.PolicyResult;
 import org.onap.policy.so.SoModelInfo;
 import org.onap.policy.so.SoRequest;
 import org.onap.policy.so.SoRequestInfo;
-import org.onap.policy.so.SoRequestParameters;
-import org.onap.policy.so.SoRequestReferences;
 import org.onap.policy.so.SoRequestStatus;
 import org.onap.policy.so.SoResponse;
 
 public class SoOperationTest extends BasicSoOperation {
 
+    private static final String VF_COUNT_KEY = SoConstants.VF_COUNT_PREFIX
+                    + "[my-model-customization-id][my-model-invariant-id][my-model-version-id]";
+
+    private static final List<String> PROP_NAMES = Collections.emptyList();
+
+    private static final String VERSION_ID = "1.2.3";
+
     private SoOperation oper;
 
     /**
@@ -70,112 +79,113 @@ public class SoOperationTest extends BasicSoOperation {
 
         initConfig();
 
-        oper = new SoOperation(params, config) {};
+        oper = new SoOperation(params, config, PROP_NAMES) {};
     }
 
     @Test
-    public void testConstructor_testGetWaitMsGet() {
+    public void testConstructor() {
         assertEquals(DEFAULT_ACTOR, oper.getActorName());
         assertEquals(DEFAULT_OPERATION, oper.getName());
         assertSame(config, oper.getConfig());
-        assertEquals(1000 * WAIT_SEC_GETS, oper.getWaitMsGet());
+        assertTrue(oper.isUsePolling());
+
+        // check when Target is null
+        params = params.toBuilder().target(null).build();
+        assertThatIllegalArgumentException().isThrownBy(() -> new SoOperation(params, config, PROP_NAMES) {})
+                        .withMessageContaining("Target information");
+    }
+
+    @Test
+    public void testValidateTarget() {
+        // check when various fields are null
+        verifyNotNull("modelCustomizationId", target::getModelCustomizationId, target::setModelCustomizationId);
+        verifyNotNull("modelInvariantId", target::getModelInvariantId, target::setModelInvariantId);
+        verifyNotNull("modelVersionId", target::getModelVersionId, target::setModelVersionId);
+
+        // verify it's still valid
+        assertThatCode(() -> new VfModuleCreate(params, config)).doesNotThrowAnyException();
+    }
+
+    private void verifyNotNull(String expectedText, Supplier<String> getter, Consumer<String> setter) {
+        String originalValue = getter.get();
+
+        // try with null
+        setter.accept(null);
+        assertThatIllegalArgumentException().isThrownBy(() -> new VfModuleCreate(params, config))
+                        .withMessageContaining(expectedText);
+
+        setter.accept(originalValue);
     }
 
     @Test
     public void testStartPreprocessorAsync() {
-        AtomicBoolean guardStarted = new AtomicBoolean();
-
-        oper = new SoOperation(params, config) {
-            @Override
-            protected CompletableFuture<OperationOutcome> startGuardAsync() {
-                guardStarted.set(true);
-                return super.startGuardAsync();
-            }
-        };
-
-        assertNull(oper.startPreprocessorAsync());
-        assertTrue(guardStarted.get());
+        assertNotNull(oper.startPreprocessorAsync());
     }
 
     @Test
-    public void testPostProcess() throws Exception {
-        // completed
-        CompletableFuture<OperationOutcome> future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
-        assertTrue(future2.isDone());
-        assertSame(outcome, future2.get());
-        assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+    public void testObtainVfCount_testGetVfCount_testSetVfCount() throws Exception {
+        // insert CQ data so it's there for the check
+        context.setProperty(AaiCqResponse.CONTEXT_KEY, makeCqResponse());
 
-        // failed
-        response.getRequest().getRequestStatus().setRequestState(SoOperation.FAILED);
-        future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
-        assertTrue(future2.isDone());
-        assertSame(outcome, future2.get());
-        assertEquals(PolicyResult.FAILURE, outcome.getResult());
+        // shouldn't actually need to do anything
+        assertNull(oper.obtainVfCount());
 
-        // no request id in the response
-        response.getRequestReferences().setRequestId(null);
-        response.getRequest().getRequestStatus().setRequestState("unknown");
-        assertThatIllegalArgumentException()
-                        .isThrownBy(() -> oper.postProcessResponse(outcome, PATH, rawResponse, response))
-                        .withMessage("missing request ID in response");
-        response.getRequestReferences().setRequestId(REQ_ID.toString());
+        // verify that the count was stored
+        Integer vfcount = context.getProperty(VF_COUNT_KEY);
+        assertEquals(VF_COUNT, vfcount);
+        assertEquals(VF_COUNT.intValue(), oper.getVfCount());
 
-        // status = 500
-        when(rawResponse.getStatus()).thenReturn(500);
+        // change the count and then verify that it isn't overwritten by another call
+        oper.setVfCount(VF_COUNT + 1);
 
-        // null request reference
-        SoRequestReferences ref = response.getRequestReferences();
-        response.setRequestReferences(null);
-        assertThatIllegalArgumentException()
-                        .isThrownBy(() -> oper.postProcessResponse(outcome, PATH, rawResponse, response))
-                        .withMessage("missing request ID in response");
-        response.setRequestReferences(ref);
+        assertNull(oper.obtainVfCount());
+        vfcount = context.getProperty(VF_COUNT_KEY);
+        assertEquals(VF_COUNT + 1, vfcount.intValue());
+        assertEquals(VF_COUNT + 1, oper.getVfCount());
     }
 
     /**
-     * Tests postProcess() when the "get" is repeated a couple of times.
+     * Tests the VF Count methods when properties are being used.
+     * @throws Exception if an error occurs
      */
     @Test
-    public void testPostProcessRepeated_testResetGetCount() throws Exception {
-        /*
-         * Two failures and then a success - should result in two "get" calls.
-         *
-         * Note: getStatus() is invoked twice during each call, so have to double up the
-         * return values.
-         */
-        when(rawResponse.getStatus()).thenReturn(500, 500, 500, 500, 200, 200);
-
-        when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse));
-
-        // use a real executor
-        params = params.toBuilder().executor(ForkJoinPool.commonPool()).build();
-
-        oper = new SoOperation(params, config) {
-            @Override
-            public long getWaitMsGet() {
-                return 1;
-            }
-        };
-
-        CompletableFuture<OperationOutcome> future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
-
-        assertSame(outcome, future2.get(500, TimeUnit.SECONDS));
-        assertEquals(PolicyResult.SUCCESS, outcome.getResult());
-        assertEquals(2, oper.getGetCount());
+    public void testGetVfCount_testSetVfCount_ViaProperties() throws Exception {
+        oper.setProperty(OperationProperties.DATA_VF_COUNT, VF_COUNT);
 
-        /*
-         * repeat - this time, the "get" operations will be exhausted, so it should fail
-         */
-        when(rawResponse.getStatus()).thenReturn(500);
+        // verify that the count was stored
+        assertEquals(VF_COUNT.intValue(), oper.getVfCount());
 
-        future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
+        oper.setVfCount(VF_COUNT + 1);
+
+        int count = oper.getProperty(OperationProperties.DATA_VF_COUNT);
+        assertEquals(VF_COUNT + 1, count);
+        assertEquals(VF_COUNT + 1, oper.getVfCount());
+    }
+
+    /**
+     * Tests obtainVfCount() when it actually has to query.
+     */
+    @Test
+    public void testObtainVfCountQuery() throws Exception {
+        CompletableFuture<OperationOutcome> future2 = oper.obtainVfCount();
+        assertNotNull(future2);
+        assertTrue(executor.runAll(100));
 
-        assertSame(outcome, future2.get(5, TimeUnit.SECONDS));
-        assertEquals(PolicyResult.FAILURE_TIMEOUT, outcome.getResult());
-        assertEquals(MAX_GETS + 1, oper.getGetCount());
+        // not done yet
+        assertFalse(future2.isDone());
 
-        oper.resetGetCount();
-        assertEquals(0, oper.getGetCount());
+        provideCqResponse(makeCqResponse());
+
+        assertTrue(executor.runAll(100));
+        assertTrue(future2.isDone());
+        assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
+
+        // verify that the count was stored
+        Integer vfcount = context.getProperty(VF_COUNT_KEY);
+        assertEquals(VF_COUNT, vfcount);
+
+        // repeat - shouldn't need to do anything now
+        assertNull(oper.obtainVfCount());
     }
 
     @Test
@@ -213,6 +223,7 @@ public class SoOperationTest extends BasicSoOperation {
 
         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
         assertEquals("200 " + ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
+        assertSame(response, outcome.getResponse());
 
         // failure case
         when(rawResponse.getStatus()).thenReturn(500);
@@ -220,6 +231,7 @@ public class SoOperationTest extends BasicSoOperation {
 
         assertEquals(PolicyResult.FAILURE, outcome.getResult());
         assertEquals("500 " + ControlLoopOperation.FAILED_MSG, outcome.getMessage());
+        assertSame(response, outcome.getResponse());
     }
 
     @Test
@@ -236,9 +248,8 @@ public class SoOperationTest extends BasicSoOperation {
 
         // try with null target
         params = params.toBuilder().target(null).build();
-        oper = new SoOperation(params, config) {};
-
-        assertThatIllegalArgumentException().isThrownBy(() -> oper.prepareSoModelInfo()).withMessage("missing Target");
+        assertThatIllegalArgumentException().isThrownBy(() -> new SoOperation(params, config, PROP_NAMES) {})
+                        .withMessageContaining("missing Target");
     }
 
     private void verifyMissingModelInfo(Supplier<String> getter, Consumer<String> setter) {
@@ -260,8 +271,7 @@ public class SoOperationTest extends BasicSoOperation {
     @Test
     public void testBuildRequestParameters() throws CoderException {
         // valid data
-        SoRequestParameters reqParams = oper.buildRequestParameters();
-        verifyRequest("reqparams.json", reqParams);
+        verifyRequest("reqparams.json", oper.buildRequestParameters().get());
 
         // invalid json
         params.getPayload().put(SoOperation.REQ_PARAM_NM, "{invalid json");
@@ -270,19 +280,18 @@ public class SoOperationTest extends BasicSoOperation {
 
         // missing data
         params.getPayload().remove(SoOperation.REQ_PARAM_NM);
-        assertNull(oper.buildRequestParameters());
+        assertTrue(oper.buildRequestParameters().isEmpty());
 
         // null payload
         params = params.toBuilder().payload(null).build();
-        oper = new SoOperation(params, config) {};
-        assertNull(oper.buildRequestParameters());
+        oper = new SoOperation(params, config, PROP_NAMES) {};
+        assertTrue(oper.buildRequestParameters().isEmpty());
     }
 
     @Test
     public void testBuildConfigurationParameters() {
         // valid data
-        List<Map<String, String>> result = oper.buildConfigurationParameters();
-        assertEquals(List.of(Collections.emptyMap()), result);
+        assertEquals(List.of(Collections.emptyMap()), oper.buildConfigurationParameters().get());
 
         // invalid json
         params.getPayload().put(SoOperation.CONFIG_PARAM_NM, "{invalid json");
@@ -291,63 +300,136 @@ public class SoOperationTest extends BasicSoOperation {
 
         // missing data
         params.getPayload().remove(SoOperation.CONFIG_PARAM_NM);
-        assertNull(oper.buildConfigurationParameters());
+        assertTrue(oper.buildConfigurationParameters().isEmpty());
 
         // null payload
         params = params.toBuilder().payload(null).build();
-        oper = new SoOperation(params, config) {};
-        assertNull(oper.buildConfigurationParameters());
+        oper = new SoOperation(params, config, PROP_NAMES) {};
+        assertTrue(oper.buildConfigurationParameters().isEmpty());
     }
 
     @Test
-    public void testGetVnfItem() {
-        // missing data
+    public void testGetItem() {
         AaiCqResponse cq = mock(AaiCqResponse.class);
-        assertThatIllegalArgumentException().isThrownBy(() -> oper.getVnfItem(cq, oper.prepareSoModelInfo()))
-                        .withMessage("missing generic VNF");
+        params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
 
-        // valid data
-        GenericVnf vnf = new GenericVnf();
-        when(cq.getGenericVnfByVfModuleModelInvariantId(MODEL_INVAR_ID)).thenReturn(vnf);
-        assertSame(vnf, oper.getVnfItem(cq, oper.prepareSoModelInfo()));
+        // in neither property nor custom query
+        assertThatIllegalArgumentException().isThrownBy(() -> oper.getItem("propA", cq2 -> null, "not found"))
+                        .withMessage("not found");
+
+        // only in custom query
+        assertEquals("valueB", oper.getItem("propB", cq2 -> "valueB", "failureB"));
+
+        // both - should choose the property
+        oper.setProperty("propC", "valueC");
+        assertEquals("valueC", oper.getItem("propC", cq2 -> "valueC2", "failureC"));
+
+        // both - should choose the property, even if it's null
+        oper.setProperty("propD", null);
+        assertNull(oper.getItem("propD", cq2 -> "valueD2", "failureD"));
     }
 
     @Test
-    public void testGetServiceInstance() {
-        // missing data
-        AaiCqResponse cq = mock(AaiCqResponse.class);
-        assertThatIllegalArgumentException().isThrownBy(() -> oper.getServiceInstance(cq))
-                        .withMessage("missing VNF Service Item");
+    public void testGetVnfItem() {
+        // @formatter:off
+        verifyItems(OperationProperties.AAI_VNF, GenericVnf::new,
+            (cq, instance) -> when(cq.getGenericVnfByVfModuleModelInvariantId(MODEL_INVAR_ID)).thenReturn(instance),
+            () -> oper.getVnfItem(oper.prepareSoModelInfo()),
+            "missing generic VNF");
+        // @formatter:on
+    }
 
-        // valid data
-        ServiceInstance instance = new ServiceInstance();
-        when(cq.getServiceInstance()).thenReturn(instance);
-        assertSame(instance, oper.getServiceInstance(cq));
+    @Test
+    public void testGetServiceInstance() {
+        // @formatter:off
+        verifyItems(OperationProperties.AAI_SERVICE, ServiceInstance::new,
+            (cq, instance) -> when(cq.getServiceInstance()).thenReturn(instance),
+            () -> oper.getServiceInstance(),
+            "missing VNF Service Item");
+        // @formatter:on
     }
 
     @Test
     public void testGetDefaultTenant() {
-        // missing data
-        AaiCqResponse cq = mock(AaiCqResponse.class);
-        assertThatIllegalArgumentException().isThrownBy(() -> oper.getDefaultTenant(cq))
-                        .withMessage("missing Tenant Item");
+        // @formatter:off
+        verifyItems(OperationProperties.AAI_DEFAULT_TENANT, Tenant::new,
+            (cq, tenant) -> when(cq.getDefaultTenant()).thenReturn(tenant),
+            () -> oper.getDefaultTenant(),
+            "missing Default Tenant Item");
+        // @formatter:on
+    }
 
-        // valid data
-        Tenant tenant = new Tenant();
-        when(cq.getDefaultTenant()).thenReturn(tenant);
-        assertSame(tenant, oper.getDefaultTenant(cq));
+    @Test
+    public void testGetVnfModel() {
+        GenericVnf vnf = new GenericVnf();
+        vnf.setModelVersionId(VERSION_ID);
+
+        // @formatter:off
+        verifyItems(OperationProperties.AAI_VNF_MODEL, ModelVer::new,
+            (cq, model) -> when(cq.getModelVerByVersionId(VERSION_ID)).thenReturn(model),
+            () -> oper.getVnfModel(vnf),
+            "missing generic VNF Model");
+        // @formatter:on
+    }
+
+    @Test
+    public void testGetServiceModel() {
+        ServiceInstance service = new ServiceInstance();
+        service.setModelVersionId(VERSION_ID);
+
+        // @formatter:off
+        verifyItems(OperationProperties.AAI_SERVICE_MODEL, ModelVer::new,
+            (cq, model) -> when(cq.getModelVerByVersionId(VERSION_ID)).thenReturn(model),
+            () -> oper.getServiceModel(service),
+            "missing Service Model");
+        // @formatter:on
     }
 
     @Test
     public void testGetDefaultCloudRegion() {
-        // missing data
+        // @formatter:off
+        verifyItems(OperationProperties.AAI_DEFAULT_CLOUD_REGION, CloudRegion::new,
+            (cq, region) -> when(cq.getDefaultCloudRegion()).thenReturn(region),
+            () -> oper.getDefaultCloudRegion(),
+            "missing Default Cloud Region");
+        // @formatter:on
+    }
+
+    private <T> void verifyItems(String propName, Supplier<T> maker, BiConsumer<AaiCqResponse, T> setter,
+                    Supplier<T> getter, String errmsg) {
+
         AaiCqResponse cq = mock(AaiCqResponse.class);
-        assertThatIllegalArgumentException().isThrownBy(() -> oper.getDefaultCloudRegion(cq))
-                        .withMessage("missing Cloud Region");
+        params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
 
-        // valid data
-        CloudRegion region = new CloudRegion();
-        when(cq.getDefaultCloudRegion()).thenReturn(region);
-        assertSame(region, oper.getDefaultCloudRegion(cq));
+        // in neither property nor custom query
+        assertThatIllegalArgumentException().isThrownBy(getter::get).withMessage(errmsg);
+
+        // only in custom query
+        final T item1 = maker.get();
+        setter.accept(cq, item1);
+        assertSame(item1, getter.get());
+
+        // both - should choose the property
+        final T item2 = maker.get();
+        oper.setProperty(propName, item2);
+        assertSame(item2, getter.get());
+
+        // both - should choose the property, even if it's null
+        oper.setProperty(propName, null);
+        assertNull(getter.get());
+    }
+
+    @Test
+    public void testGetCoder() throws CoderException {
+        Coder opcoder = oper.getCoder();
+
+        // ensure we can decode an SO timestamp
+        String json = "{'request':{'finishTime':'Fri, 15 May 2020 12:14:21 GMT'}}";
+        SoResponse resp = opcoder.decode(json.replace('\'', '"'), SoResponse.class);
+
+        LocalDateTime tfinish = resp.getRequest().getFinishTime();
+        assertNotNull(tfinish);
+        assertEquals(2020, tfinish.getYear());
+        assertEquals(Month.MAY, tfinish.getMonth());
     }
 }