Changes for Checkstyle 8.32
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / impl / OperationPartialTest.java
index 67ac27c..e2ed2d9 100644 (file)
@@ -21,7 +21,6 @@
 package org.onap.policy.controlloop.actorserviceprovider.impl;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatCode;
 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
@@ -29,10 +28,15 @@ 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.verify;
+import static org.mockito.Mockito.when;
 
 import ch.qos.logback.classic.Logger;
 import java.time.Instant;
+import java.util.ArrayDeque;
 import java.util.Arrays;
+import java.util.Deque;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
@@ -41,12 +45,10 @@ import java.util.UUID;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CompletionException;
 import java.util.concurrent.ExecutionException;
-import java.util.concurrent.Executor;
 import java.util.concurrent.ForkJoinPool;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
-import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Consumer;
 import java.util.function.Supplier;
@@ -57,6 +59,9 @@ import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
 import org.onap.policy.common.utils.coder.Coder;
@@ -66,10 +71,14 @@ import org.onap.policy.common.utils.test.log.logback.ExtractAppender;
 import org.onap.policy.common.utils.time.PseudoExecutor;
 import org.onap.policy.controlloop.ControlLoopOperation;
 import org.onap.policy.controlloop.VirtualControlLoopEvent;
+import org.onap.policy.controlloop.actorserviceprovider.ActorService;
 import org.onap.policy.controlloop.actorserviceprovider.Operation;
 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
+import org.onap.policy.controlloop.actorserviceprovider.Operator;
 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.OperatorConfig;
+import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
 import org.onap.policy.controlloop.policy.PolicyResult;
 import org.slf4j.LoggerFactory;
 
@@ -83,6 +92,7 @@ public class OperationPartialTest {
     private static final String OPERATION = "my-operation";
     private static final String MY_SINK = "my-sink";
     private static final String MY_SOURCE = "my-source";
+    private static final String MY_TARGET_ENTITY = "my-entity";
     private static final String TEXT = "my-text";
     private static final int TIMEOUT = 1000;
     private static final UUID REQ_ID = UUID.randomUUID();
@@ -96,6 +106,15 @@ public class OperationPartialTest {
     private static final Logger logger = (Logger) LoggerFactory.getLogger(OperationPartial.class);
     private static final ExtractAppender appender = new ExtractAppender();
 
+    @Mock
+    private ActorService service;
+    @Mock
+    private Actor guardActor;
+    @Mock
+    private Operator guardOperator;
+    @Mock
+    private Operation guardOperation;
+
     private VirtualControlLoopEvent event;
     private ControlLoopEventContext context;
     private PseudoExecutor executor;
@@ -111,14 +130,17 @@ public class OperationPartialTest {
     private OperationOutcome opstart;
     private OperationOutcome opend;
 
-    private OperatorPartial operator;
+    private Deque<OperationOutcome> starts;
+    private Deque<OperationOutcome> ends;
+
+    private OperatorConfig config;
 
     /**
      * Attaches the appender to the logger.
      */
     @BeforeClass
     public static void setUpBeforeClass() throws Exception {
-        /**
+        /*
          * Attach appender to the logger.
          */
         appender.setContext(logger.getLoggerContext());
@@ -140,6 +162,8 @@ public class OperationPartialTest {
      */
     @Before
     public void setUp() {
+        MockitoAnnotations.initMocks(this);
+
         event = new VirtualControlLoopEvent();
         event.setRequestId(REQ_ID);
 
@@ -147,23 +171,15 @@ public class OperationPartialTest {
         executor = new PseudoExecutor();
 
         params = ControlLoopOperationParams.builder().completeCallback(this::completer).context(context)
-                        .executor(executor).actor(ACTOR).operation(OPERATION).timeoutSec(TIMEOUT)
-                        .startCallback(this::starter).targetEntity(MY_SINK).build();
+                        .executor(executor).actorService(service).actor(ACTOR).operation(OPERATION).timeoutSec(TIMEOUT)
+                        .startCallback(this::starter).targetEntity(MY_TARGET_ENTITY).build();
 
-        operator = new OperatorPartial(ACTOR, OPERATION) {
-            @Override
-            public Executor getBlockingExecutor() {
-                return executor;
-            }
+        when(service.getActor(OperationPartial.GUARD_ACTOR_NAME)).thenReturn(guardActor);
+        when(guardActor.getOperator(OperationPartial.GUARD_OPERATION_NAME)).thenReturn(guardOperator);
+        when(guardOperator.buildOperation(any())).thenReturn(guardOperation);
+        when(guardOperation.start()).thenReturn(CompletableFuture.completedFuture(makeSuccess()));
 
-            @Override
-            public Operation buildOperation(ControlLoopOperationParams params) {
-                return null;
-            }
-        };
-
-        operator.configure(null);
-        operator.start();
+        config = new OperatorConfig(executor);
 
         oper = new MyOper();
 
@@ -171,6 +187,9 @@ public class OperationPartialTest {
 
         opstart = null;
         opend = null;
+
+        starts = new ArrayDeque<>(10);
+        ends = new ArrayDeque<>(10);
     }
 
     @Test
@@ -197,53 +216,11 @@ public class OperationPartialTest {
         assertNull(future.get(5, TimeUnit.SECONDS));
     }
 
-    /**
-     * Exercises the doXxx() methods.
-     */
-    @Test
-    public void testDoXxx() {
-        assertThatCode(() -> operator.doConfigure(null)).doesNotThrowAnyException();
-        assertThatCode(() -> operator.doStart()).doesNotThrowAnyException();
-        assertThatCode(() -> operator.doStop()).doesNotThrowAnyException();
-        assertThatCode(() -> operator.doShutdown()).doesNotThrowAnyException();
-
-    }
-
     @Test
     public void testStart() {
         verifyRun("testStart", 1, 1, PolicyResult.SUCCESS);
     }
 
-    /**
-     * Tests startOperation() when the operator is not running.
-     */
-    @Test
-    public void testStartNotRunning() {
-        // stop the operator
-        operator.stop();
-
-        assertThatIllegalStateException().isThrownBy(() -> oper.start());
-    }
-
-    /**
-     * Tests startOperation() when the operation has a preprocessor.
-     */
-    @Test
-    public void testStartWithPreprocessor() {
-        AtomicInteger count = new AtomicInteger();
-
-        CompletableFuture<OperationOutcome> preproc = CompletableFuture.supplyAsync(() -> {
-            count.incrementAndGet();
-            return makeSuccess();
-        }, executor);
-
-        oper.setGuard(preproc);
-
-        verifyRun("testStartWithPreprocessor_testStartPreprocessor", 1, 1, PolicyResult.SUCCESS);
-
-        assertEquals(1, count.get());
-    }
-
     /**
      * Tests start() with multiple running requests.
      */
@@ -269,7 +246,7 @@ public class OperationPartialTest {
      */
     @Test
     public void testStartPreprocessorFailure() {
-        oper.setGuard(CompletableFuture.completedFuture(makeFailure()));
+        oper.setPreProc(CompletableFuture.completedFuture(makeFailure()));
 
         verifyRun("testStartPreprocessorFailure", 1, 0, PolicyResult.FAILURE_GUARD);
     }
@@ -280,7 +257,7 @@ public class OperationPartialTest {
     @Test
     public void testStartPreprocessorException() {
         // arrange for the preprocessor to throw an exception
-        oper.setGuard(CompletableFuture.failedFuture(new IllegalStateException(EXPECTED_EXCEPTION)));
+        oper.setPreProc(CompletableFuture.failedFuture(new IllegalStateException(EXPECTED_EXCEPTION)));
 
         verifyRun("testStartPreprocessorException", 1, 0, PolicyResult.FAILURE_GUARD);
     }
@@ -291,7 +268,7 @@ public class OperationPartialTest {
     @Test
     public void testStartPreprocessorNotRunning() {
         // arrange for the preprocessor to return success, which will be ignored
-        oper.setGuard(CompletableFuture.completedFuture(makeSuccess()));
+        // oper.setGuard(CompletableFuture.completedFuture(makeSuccess()));
 
         oper.start().cancel(false);
         assertTrue(executor.runAll(MAX_REQUESTS));
@@ -328,8 +305,43 @@ public class OperationPartialTest {
     }
 
     @Test
-    public void testStartGuardAsync() {
-        assertNull(oper.startGuardAsync());
+    public void testStartGuardAsync() throws Exception {
+        CompletableFuture<OperationOutcome> future = oper.startGuardAsync();
+        assertTrue(future.isDone());
+        assertEquals(PolicyResult.SUCCESS, future.get().getResult());
+
+        // verify the parameters that were passed
+        ArgumentCaptor<ControlLoopOperationParams> paramsCaptor =
+                        ArgumentCaptor.forClass(ControlLoopOperationParams.class);
+        verify(guardOperator).buildOperation(paramsCaptor.capture());
+
+        params = paramsCaptor.getValue();
+        assertEquals(OperationPartial.GUARD_ACTOR_NAME, params.getActor());
+        assertEquals(OperationPartial.GUARD_OPERATION_NAME, params.getOperation());
+        assertNull(params.getRetry());
+        assertNull(params.getTimeoutSec());
+
+        Map<String, Object> payload = params.getPayload();
+        assertNotNull(payload);
+
+        assertEquals(oper.makeGuardPayload(), payload);
+    }
+
+    @Test
+    public void testMakeGuardPayload() {
+        Map<String, Object> payload = oper.makeGuardPayload();
+        assertSame(REQ_ID, payload.get("requestId"));
+
+        // request id changes, so remove it
+        payload.remove("requestId");
+
+        assertEquals("{actor=my-actor, operation=my-operation, target=my-entity}", payload.toString());
+
+        // repeat, but with closed loop name
+        event.setClosedLoopControlName("my-loop");
+        payload = oper.makeGuardPayload();
+        payload.remove("requestId");
+        assertEquals("{actor=my-actor, operation=my-operation, target=my-entity, clname=my-loop}", payload.toString());
     }
 
     @Test
@@ -342,6 +354,8 @@ public class OperationPartialTest {
 
     @Test
     public void testIsSuccess() {
+        assertFalse(oper.isSuccess(null));
+
         OperationOutcome outcome = new OperationOutcome();
 
         outcome.setResult(PolicyResult.SUCCESS);
@@ -392,7 +406,7 @@ public class OperationPartialTest {
         /*
          * Use an operation that doesn't override doOperation().
          */
-        OperationPartial oper2 = new OperationPartial(params, operator) {};
+        OperationPartial oper2 = new OperationPartial(params, config) {};
 
         oper2.start();
         assertTrue(executor.runAll(MAX_REQUESTS));
@@ -509,15 +523,15 @@ public class OperationPartialTest {
         // arrange to return null from doOperation()
         oper = new MyOper() {
             @Override
-            protected OperationOutcome doOperation(int attempt, OperationOutcome operation) {
+            protected OperationOutcome doOperation(int attempt, OperationOutcome outcome) {
 
                 // update counters
-                super.doOperation(attempt, operation);
+                super.doOperation(attempt, outcome);
                 return null;
             }
         };
 
-        verifyRun("testSetRetryFlag_testRetryOnFailure_NullOutcome", 1, 1, PolicyResult.FAILURE, null, noop());
+        verifyRun("testSetRetryFlag_testRetryOnFailure_NullOutcome", 1, 1, PolicyResult.FAILURE, noop());
     }
 
     @Test
@@ -577,8 +591,8 @@ public class OperationPartialTest {
      * Tests handleFailure() when the outcome is a success.
      */
     @Test
-    public void testHandlePreprocessorFailureTrue() {
-        oper.setGuard(CompletableFuture.completedFuture(makeSuccess()));
+    public void testHandlePreprocessorFailureSuccess() {
+        oper.setPreProc(CompletableFuture.completedFuture(makeSuccess()));
         verifyRun("testHandlePreprocessorFailureTrue", 1, 1, PolicyResult.SUCCESS);
     }
 
@@ -586,8 +600,8 @@ public class OperationPartialTest {
      * Tests handleFailure() when the outcome is <i>not</i> a success.
      */
     @Test
-    public void testHandlePreprocessorFailureFalse() throws Exception {
-        oper.setGuard(CompletableFuture.completedFuture(makeFailure()));
+    public void testHandlePreprocessorFailureFailed() throws Exception {
+        oper.setPreProc(CompletableFuture.completedFuture(makeFailure()));
         verifyRun("testHandlePreprocessorFailureFalse", 1, 0, PolicyResult.FAILURE_GUARD);
     }
 
@@ -596,9 +610,8 @@ public class OperationPartialTest {
      */
     @Test
     public void testHandlePreprocessorFailureNull() throws Exception {
-        // arrange to return null from the preprocessor
-        oper.setGuard(CompletableFuture.completedFuture(null));
-
+        // arrange to return a null outcome from the preprocessor
+        oper.setPreProc(CompletableFuture.completedFuture(null));
         verifyRun("testHandlePreprocessorFailureNull", 1, 0, PolicyResult.FAILURE_GUARD);
     }
 
@@ -1014,6 +1027,10 @@ public class OperationPartialTest {
         assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
 
+        oper.setOutcome(outcome, PolicyResult.SUCCESS);
+        assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
+        assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+
         for (PolicyResult result : FAILURE_RESULTS) {
             outcome = new OperationOutcome();
             oper.setOutcome(outcome, result);
@@ -1101,7 +1118,7 @@ public class OperationPartialTest {
     @Test
     public void testGetRetryWait() {
         // need an operator that doesn't override the retry time
-        OperationPartial oper2 = new OperationPartial(params, operator) {};
+        OperationPartial oper2 = new OperationPartial(params, config) {};
         assertEquals(OperationPartial.DEFAULT_RETRY_WAIT_MS, oper2.getRetryWaitMs());
     }
 
@@ -1121,11 +1138,13 @@ public class OperationPartialTest {
         ++numStart;
         tstart = oper.getStart();
         opstart = oper;
+        starts.add(oper);
     }
 
     private void completer(OperationOutcome oper) {
         ++numEnd;
         opend = oper;
+        ends.add(oper);
     }
 
     /**
@@ -1164,10 +1183,7 @@ public class OperationPartialTest {
     private void verifyRun(String testName, int expectedCallbacks, int expectedOperations,
                     PolicyResult expectedResult) {
 
-        String expectedSubRequestId =
-                        (expectedResult == PolicyResult.FAILURE_EXCEPTION ? null : String.valueOf(expectedOperations));
-
-        verifyRun(testName, expectedCallbacks, expectedOperations, expectedResult, expectedSubRequestId, noop());
+        verifyRun(testName, expectedCallbacks, expectedOperations, expectedResult, noop());
     }
 
     /**
@@ -1177,13 +1193,18 @@ public class OperationPartialTest {
      * @param expectedCallbacks number of callbacks expected
      * @param expectedOperations number of operation invocations expected
      * @param expectedResult expected outcome
-     * @param expectedSubRequestId expected sub request ID
      * @param manipulator function to modify the future returned by
      *        {@link OperationPartial#start(ControlLoopOperationParams)} before the tasks
      *        in the executor are run
      */
     private void verifyRun(String testName, int expectedCallbacks, int expectedOperations, PolicyResult expectedResult,
-                    String expectedSubRequestId, Consumer<CompletableFuture<OperationOutcome>> manipulator) {
+                    Consumer<CompletableFuture<OperationOutcome>> manipulator) {
+
+        tstart = null;
+        opstart = null;
+        opend = null;
+        starts.clear();
+        ends.clear();
 
         CompletableFuture<OperationOutcome> future = oper.start();
 
@@ -1204,14 +1225,28 @@ public class OperationPartialTest {
 
             try {
                 assertTrue(future.isDone());
-                assertSame(testName, opend, future.get());
+                assertEquals(testName, opend, future.get());
+
+                // "start" is never final
+                for (OperationOutcome outcome : starts) {
+                    assertFalse(testName, outcome.isFinalOutcome());
+                }
+
+                // only the last "complete" is final
+                assertTrue(testName, ends.removeLast().isFinalOutcome());
+
+                for (OperationOutcome outcome : ends) {
+                    assertFalse(outcome.isFinalOutcome());
+                }
 
             } catch (InterruptedException | ExecutionException e) {
                 throw new IllegalStateException(e);
             }
 
             if (expectedOperations > 0) {
-                assertEquals(testName, expectedSubRequestId, opend.getSubRequestId());
+                assertNotNull(testName, oper.getSubRequestId());
+                assertEquals(testName + " op start", oper.getSubRequestId(), opstart.getSubRequestId());
+                assertEquals(testName + " op end", oper.getSubRequestId(), opend.getSubRequestId());
             }
         }
 
@@ -1224,7 +1259,7 @@ public class OperationPartialTest {
     private void setOperCoderException() {
         oper = new MyOper() {
             @Override
-            protected Coder makeCoder() {
+            protected Coder getCoder() {
                 return new StandardCoder() {
                     @Override
                     public String encode(Object object, boolean pretty) throws CoderException {
@@ -1248,16 +1283,14 @@ public class OperationPartialTest {
 
         @Setter
         private boolean genException;
-
         @Setter
         private int maxFailures = 0;
-
         @Setter
-        private CompletableFuture<OperationOutcome> guard;
+        private CompletableFuture<OperationOutcome> preProc;
 
 
         public MyOper() {
-            super(OperationPartialTest.this.params, operator);
+            super(OperationPartialTest.this.params, config);
         }
 
         @Override
@@ -1278,11 +1311,6 @@ public class OperationPartialTest {
             return operation;
         }
 
-        @Override
-        protected CompletableFuture<OperationOutcome> startGuardAsync() {
-            return (guard != null ? guard : super.startGuardAsync());
-        }
-
         @Override
         protected long getRetryWaitMs() {
             /*
@@ -1291,5 +1319,10 @@ public class OperationPartialTest {
              */
             return 0L;
         }
+
+        @Override
+        protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
+            return (preProc != null ? preProc : super.startPreprocessorAsync());
+        }
     }
 }