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;
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;
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;
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;
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();
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;
private OperationOutcome opstart;
private OperationOutcome opend;
+ private Deque<OperationOutcome> starts;
+ private Deque<OperationOutcome> ends;
+
private OperatorConfig config;
/**
*/
@Before
public void setUp() {
+ MockitoAnnotations.initMocks(this);
+
event = new VirtualControlLoopEvent();
event.setRequestId(REQ_ID);
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();
+
+ 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()));
config = new OperatorConfig(executor);
opstart = null;
opend = null;
+
+ starts = new ArrayDeque<>(10);
+ ends = new ArrayDeque<>(10);
}
@Test
verifyRun("testStart", 1, 1, PolicyResult.SUCCESS);
}
- /**
- * 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.
*/
*/
@Test
public void testStartPreprocessorFailure() {
- oper.setGuard(CompletableFuture.completedFuture(makeFailure()));
+ oper.setPreProc(CompletableFuture.completedFuture(makeFailure()));
verifyRun("testStartPreprocessorFailure", 1, 0, PolicyResult.FAILURE_GUARD);
}
@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);
}
@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));
}
@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
@Test
public void testIsSuccess() {
+ assertFalse(oper.isSuccess(null));
+
OperationOutcome outcome = new OperationOutcome();
outcome.setResult(PolicyResult.SUCCESS);
// 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
* 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);
}
* 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);
}
*/
@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);
}
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);
++numStart;
tstart = oper.getStart();
opstart = oper;
+ starts.add(oper);
}
private void completer(OperationOutcome oper) {
++numEnd;
opend = oper;
+ ends.add(oper);
}
/**
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());
}
/**
* @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();
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());
}
}
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 {
@Setter
private boolean genException;
-
@Setter
private int maxFailures = 0;
-
@Setter
- private CompletableFuture<OperationOutcome> guard;
+ private CompletableFuture<OperationOutcome> preProc;
public MyOper() {
return operation;
}
- @Override
- protected CompletableFuture<OperationOutcome> startGuardAsync() {
- return (guard != null ? guard : super.startGuardAsync());
- }
-
@Override
protected long getRetryWaitMs() {
/*
*/
return 0L;
}
+
+ @Override
+ protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
+ return (preProc != null ? preProc : super.startPreprocessorAsync());
+ }
}
}