Upgrade and clean up dependencies
[policy/models.git] / models-interactions / model-actors / actor.appclcm / src / test / java / org / onap / policy / controlloop / actor / appclcm / AppcLcmOperationTest.java
index 8e6a603..5765e3c 100644 (file)
@@ -2,7 +2,8 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2020-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.controlloop.actor.appclcm;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotEquals;
 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.Mockito.mock;
-import static org.mockito.Mockito.when;
 
 import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.stream.Collectors;
-import org.apache.commons.lang3.tuple.Pair;
+import org.junit.After;
+import org.junit.AfterClass;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnitRunner;
 import org.onap.policy.appclcm.AppcLcmBody;
 import org.onap.policy.appclcm.AppcLcmCommonHeader;
 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
 import org.onap.policy.appclcm.AppcLcmOutput;
 import org.onap.policy.appclcm.AppcLcmResponseStatus;
+import org.onap.policy.common.endpoints.event.comm.TopicSink;
+import org.onap.policy.common.endpoints.event.comm.TopicSource;
 import org.onap.policy.common.utils.coder.Coder;
 import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.common.utils.coder.StandardCoder;
 import org.onap.policy.controlloop.ControlLoopOperation;
 import org.onap.policy.controlloop.actor.test.BasicBidirectionalTopicOperation;
-import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
-import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
+import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
+import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
 import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperation.Status;
-import org.onap.policy.controlloop.policy.PolicyResult;
-import org.onap.policy.controlloop.policy.Target;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
+import org.onap.policy.simulators.AppcLcmTopicServer;
+import org.onap.policy.simulators.TopicServer;
 
-public class AppcLcmOperationTest extends BasicBidirectionalTopicOperation {
+@RunWith(MockitoJUnitRunner.class)
+public class AppcLcmOperationTest extends BasicBidirectionalTopicOperation<AppcLcmDmaapWrapper> {
 
     private static final String EXPECTED_EXCEPTION = "expected exception";
     private static final String PAYLOAD_KEY1 = "key-A";
@@ -68,6 +79,16 @@ public class AppcLcmOperationTest extends BasicBidirectionalTopicOperation {
     private AppcLcmDmaapWrapper response;
     private AppcLcmOperation oper;
 
+    @BeforeClass
+    public static void setUpBeforeClass() throws Exception {
+        initBeforeClass(MY_SINK, MY_SOURCE);
+    }
+
+    @AfterClass
+    public static void tearDownAfterClass() {
+        destroyAfterClass();
+    }
+
     /**
      * Sets up.
      */
@@ -78,52 +99,56 @@ public class AppcLcmOperationTest extends BasicBidirectionalTopicOperation {
         response = makeResponse();
 
         oper = new AppcLcmOperation(params, config);
+        oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
     }
 
-    @Test
-    public void testConstructor() {
-        assertEquals(DEFAULT_ACTOR, oper.getActorName());
-        assertEquals(DEFAULT_OPERATION, oper.getName());
+    @After
+    public void tearDown() {
+        super.tearDownBasic();
+    }
 
-        // missing target entity
-        params = params.toBuilder().targetEntity("").build();
-        assertThatIllegalArgumentException().isThrownBy(() -> new AppcLcmOperation(params, config))
-                        .withMessage("missing targetEntity");
+    @Override
+    protected TopicServer<AppcLcmDmaapWrapper> makeServer(TopicSink sink, TopicSource source) {
+        return new AppcLcmTopicServer(sink, source);
     }
 
+    /**
+     * Tests "success" case with simulator.
+     */
     @Test
-    public void testStartPreprocessorAsync() throws Exception {
-        context = mock(ControlLoopEventContext.class);
-        when(context.getEvent()).thenReturn(event);
-        params = params.toBuilder().context(context).build();
+    public void testSuccess() throws Exception {
+        BidirectionalTopicParams opParams =
+                        BidirectionalTopicParams.builder().sinkTopic(MY_SINK).sourceTopic(MY_SOURCE).build();
+        config = new BidirectionalTopicConfig(blockingExecutor, opParams, topicMgr, AppcLcmOperation.SELECTOR_KEYS);
 
-        AtomicBoolean guardStarted = new AtomicBoolean();
+        params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
 
-        oper = new AppcLcmOperation(params, config) {
-            @Override
-            protected CompletableFuture<OperationOutcome> startGuardAsync() {
-                guardStarted.set(true);
-                return super.startGuardAsync();
-            }
-        };
+        oper = new AppcLcmOperation(params, config);
+        oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
 
-        CompletableFuture<OperationOutcome> future2 = oper.startPreprocessorAsync();
-        assertNotNull(future2);
-        assertFalse(future.isDone());
-        assertTrue(guardStarted.get());
+        outcome = oper.start().get();
+        assertEquals(OperationResult.SUCCESS, outcome.getResult());
+        assertTrue(outcome.getResponse() instanceof AppcLcmDmaapWrapper);
+    }
+
+    @Test
+    public void testConstructor() {
+        assertEquals(DEFAULT_ACTOR, oper.getActorName());
+        assertEquals(DEFAULT_OPERATION, oper.getName());
+    }
 
-        assertTrue(executor.runAll(100));
-        assertTrue(future2.isDone());
-        assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
+    @Test
+    public void testGetPropertyNames() {
+        assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_TARGET_ENTITY));
     }
 
     @Test
     public void testMakeRequest() {
-        Pair<String, AppcLcmDmaapWrapper> result = oper.makeRequest(2);
-        String subreq = result.getLeft();
+        oper.generateSubRequestId(2);
+        String subreq = oper.getSubRequestId();
         assertNotNull(subreq);
 
-        AppcLcmDmaapWrapper request = result.getRight();
+        AppcLcmDmaapWrapper request = oper.makeRequest(2);
         assertEquals("DefaultOperation", request.getBody().getInput().getAction());
 
         AppcLcmCommonHeader header = request.getBody().getInput().getCommonHeader();
@@ -134,11 +159,20 @@ public class AppcLcmOperationTest extends BasicBidirectionalTopicOperation {
 
         assertEquals("{vnf-id=my-target}", request.getBody().getInput().getActionIdentifiers().toString());
 
-        // a subsequent request should have a different sub-request id
-        result = oper.makeRequest(2);
-        assertNotEquals(subreq, result.getLeft());
-        assertNotNull(result.getLeft());
-        assertEquals(result.getLeft(), result.getRight().getBody().getInput().getCommonHeader().getSubRequestId());
+        request = oper.makeRequest(2);
+        assertEquals(subreq, request.getBody().getInput().getCommonHeader().getSubRequestId());
+    }
+
+    /**
+     * Tests makeRequest() when a property is missing.
+     */
+    @Test
+    public void testMakeRequestMissingProperty() throws Exception {
+        oper = new AppcLcmOperation(params, config);
+        oper.generateSubRequestId(1);
+
+        assertThatIllegalStateException().isThrownBy(() -> oper.makeRequest(1))
+                        .withMessageContaining("missing target entity");
     }
 
     @Test
@@ -146,14 +180,16 @@ public class AppcLcmOperationTest extends BasicBidirectionalTopicOperation {
         // only builds a payload for ConfigModify
         params = params.toBuilder().operation(AppcLcmConstants.OPERATION_CONFIG_MODIFY).build();
         oper = new AppcLcmOperation(params, config);
+        oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
 
-        AppcLcmDmaapWrapper req = oper.makeRequest(2).getRight();
+        oper.generateSubRequestId(2);
+        AppcLcmDmaapWrapper req = oper.makeRequest(2);
         assertEquals("{\"key-A\":\"value-A\"}", req.getBody().getInput().getPayload());
 
         // coder exception
         oper = new AppcLcmOperation(params, config) {
             @Override
-            protected Coder makeCoder() {
+            protected Coder getCoder() {
                 return new StandardCoder() {
                     @Override
                     public String encode(Object object) throws CoderException {
@@ -163,13 +199,17 @@ public class AppcLcmOperationTest extends BasicBidirectionalTopicOperation {
             }
         };
 
+        oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
+        oper.generateSubRequestId(2);
+
         assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest(2))
-                        .withMessage("Cannot convert payload");
+                        .withMessageContaining("Cannot convert payload");
     }
 
     @Test
     public void testGetExpectedKeyValues() {
-        AppcLcmDmaapWrapper request = oper.makeRequest(2).getRight();
+        oper.generateSubRequestId(2);
+        AppcLcmDmaapWrapper request = oper.makeRequest(2);
         assertEquals(Arrays.asList(request.getBody().getInput().getCommonHeader().getSubRequestId()),
                         oper.getExpectedKeyValues(50, request));
     }
@@ -205,24 +245,28 @@ public class AppcLcmOperationTest extends BasicBidirectionalTopicOperation {
 
     @Test
     public void testSetOutcome() {
-        oper.setOutcome(outcome, PolicyResult.SUCCESS, response);
-        assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+        oper.setOutcome(outcome, OperationResult.SUCCESS, response);
+        assertEquals(OperationResult.SUCCESS, outcome.getResult());
         assertEquals(MY_MESSAGE, outcome.getMessage());
+        assertSame(response, outcome.getResponse());
 
         // failure
-        oper.setOutcome(outcome, PolicyResult.FAILURE, response);
-        assertEquals(PolicyResult.FAILURE, outcome.getResult());
+        oper.setOutcome(outcome, OperationResult.FAILURE, response);
+        assertEquals(OperationResult.FAILURE, outcome.getResult());
         assertEquals(MY_MESSAGE, outcome.getMessage());
+        assertSame(response, outcome.getResponse());
 
         // null message
         response.getBody().getOutput().getStatus().setMessage(null);
-        oper.setOutcome(outcome, PolicyResult.SUCCESS, response);
+        oper.setOutcome(outcome, OperationResult.SUCCESS, response);
         assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
+        assertSame(response, outcome.getResponse());
 
         // null status
         response.getBody().getOutput().setStatus(null);
-        oper.setOutcome(outcome, PolicyResult.SUCCESS, response);
+        oper.setOutcome(outcome, OperationResult.SUCCESS, response);
         assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
+        assertSame(response, outcome.getResponse());
     }
 
     @Test
@@ -289,10 +333,10 @@ public class AppcLcmOperationTest extends BasicBidirectionalTopicOperation {
     protected void makeContext() {
         super.makeContext();
 
-        Target target = new Target();
-        target.setResourceID(RESOURCE_ID);
+        Map<String, String> targetEntities = new HashMap<>();
+        targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_RESOURCEID, RESOURCE_ID);
 
-        params = params.toBuilder().target(target).build();
+        params = params.toBuilder().targetEntityIds(targetEntities).build();
     }
 
     @Override