- /**
- * Tests startPreprocessor() when the preprocessor returns a failure.
- */
- @Test
- public void testStartPreprocessorFailure() {
- oper.setPreProc(CompletableFuture.completedFuture(makeFailure()));
-
- verifyRun("testStartPreprocessorFailure", 1, 0, PolicyResult.FAILURE_GUARD);
- }
-
- /**
- * Tests startPreprocessor() when the preprocessor throws an exception.
- */
- @Test
- public void testStartPreprocessorException() {
- // arrange for the preprocessor to throw an exception
- oper.setPreProc(CompletableFuture.failedFuture(new IllegalStateException(EXPECTED_EXCEPTION)));
-
- verifyRun("testStartPreprocessorException", 1, 0, PolicyResult.FAILURE_GUARD);
- }
-
- /**
- * Tests startPreprocessor() when the pipeline is not running.
- */
- @Test
- public void testStartPreprocessorNotRunning() {
- // arrange for the preprocessor to return success, which will be ignored
- // oper.setGuard(CompletableFuture.completedFuture(makeSuccess()));
-
- oper.start().cancel(false);
- assertTrue(executor.runAll(MAX_REQUESTS));
-
- assertNull(opstart);
- assertNull(opend);
-
- assertEquals(0, numStart);
- assertEquals(0, oper.getCount());
- assertEquals(0, numEnd);
- }
-
- /**
- * Tests startPreprocessor() when the preprocessor <b>builder</b> throws an exception.
- */
- @Test
- public void testStartPreprocessorBuilderException() {
- oper = new MyOper() {
- @Override
- protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
- throw new IllegalStateException(EXPECTED_EXCEPTION);
- }
- };
-
- assertThatIllegalStateException().isThrownBy(() -> oper.start());
-
- // should be nothing in the queue
- assertEquals(0, executor.getQueueLength());
- }
-
- @Test
- public void testStartPreprocessorAsync() {
- assertNull(oper.startPreprocessorAsync());
- }
-
- @Test
- 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);
- }
-
- /**
- * Tests startGuardAsync() when preprocessing is disabled.
- */
- @Test
- public void testStartGuardAsyncDisabled() {
- params = params.toBuilder().preprocessed(true).build();
- assertNull(new MyOper().startGuardAsync());
- }
-
- @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());
- }
-