2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.policy.controlloop.actorserviceprovider;
 
  23 import static org.junit.Assert.assertEquals;
 
  24 import static org.junit.Assert.assertSame;
 
  25 import static org.junit.Assert.assertTrue;
 
  27 import java.util.UUID;
 
  28 import java.util.concurrent.CompletableFuture;
 
  29 import java.util.concurrent.atomic.AtomicReference;
 
  30 import org.junit.Before;
 
  31 import org.junit.Test;
 
  32 import org.onap.policy.controlloop.VirtualControlLoopEvent;
 
  33 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
 
  34 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
 
  35 import org.onap.policy.controlloop.policy.PolicyResult;
 
  37 public class AsyncResponseHandlerTest {
 
  39     private static final String ACTOR = "my-actor";
 
  40     private static final String OPERATION = "my-operation";
 
  41     private static final UUID REQ_ID = UUID.randomUUID();
 
  42     private static final String TEXT = "some text";
 
  44     private VirtualControlLoopEvent event;
 
  45     private ControlLoopEventContext context;
 
  46     private ControlLoopOperationParams params;
 
  47     private OperationOutcome outcome;
 
  48     private MyHandler handler;
 
  51      * Initializes all fields, including {@link #handler}.
 
  55         event = new VirtualControlLoopEvent();
 
  56         event.setRequestId(REQ_ID);
 
  58         context = new ControlLoopEventContext(event);
 
  59         params = ControlLoopOperationParams.builder().actor(ACTOR).operation(OPERATION).context(context).build();
 
  60         outcome = params.makeOutcome();
 
  62         handler = new MyHandler(params, outcome);
 
  66     public void testAsyncResponseHandler_testGetParams_testGetOutcome() {
 
  67         assertSame(params, handler.getParams());
 
  68         assertSame(outcome, handler.getOutcome());
 
  72     public void testHandle() {
 
  73         CompletableFuture<String> future = new CompletableFuture<>();
 
  74         handler.handle(future).complete(outcome);
 
  76         assertTrue(future.isCancelled());
 
  80     public void testCompleted() throws Exception {
 
  81         CompletableFuture<OperationOutcome> result = handler.handle(new CompletableFuture<>());
 
  82         handler.completed(TEXT);
 
  83         assertTrue(result.isDone());
 
  84         assertSame(outcome, result.get());
 
  85         assertEquals(PolicyResult.FAILURE_RETRIES, outcome.getResult());
 
  86         assertEquals(TEXT, outcome.getMessage());
 
  90      * Tests completed() when doCompleted() throws an exception.
 
  93     public void testCompletedException() throws Exception {
 
  94         IllegalStateException except = new IllegalStateException();
 
  96         outcome = params.makeOutcome();
 
  97         handler = new MyHandler(params, outcome) {
 
  99             protected OperationOutcome doComplete(String rawResponse) {
 
 104         CompletableFuture<OperationOutcome> result = handler.handle(new CompletableFuture<>());
 
 105         handler.completed(TEXT);
 
 106         assertTrue(result.isCompletedExceptionally());
 
 108         AtomicReference<Throwable> thrown = new AtomicReference<>();
 
 109         result.whenComplete((unused, thrown2) -> thrown.set(thrown2));
 
 111         assertSame(except, thrown.get());
 
 115     public void testFailed() throws Exception {
 
 116         IllegalStateException except = new IllegalStateException();
 
 118         CompletableFuture<OperationOutcome> result = handler.handle(new CompletableFuture<>());
 
 119         handler.failed(except);
 
 121         assertTrue(result.isDone());
 
 122         assertSame(outcome, result.get());
 
 123         assertEquals(PolicyResult.FAILURE_GUARD, outcome.getResult());
 
 127      * Tests failed() when doFailed() throws an exception.
 
 130     public void testFailedException() throws Exception {
 
 131         IllegalStateException except = new IllegalStateException();
 
 133         outcome = params.makeOutcome();
 
 134         handler = new MyHandler(params, outcome) {
 
 136             protected OperationOutcome doFailed(Throwable thrown) {
 
 141         CompletableFuture<OperationOutcome> result = handler.handle(new CompletableFuture<>());
 
 142         handler.failed(except);
 
 143         assertTrue(result.isCompletedExceptionally());
 
 145         AtomicReference<Throwable> thrown = new AtomicReference<>();
 
 146         result.whenComplete((unused, thrown2) -> thrown.set(thrown2));
 
 148         assertSame(except, thrown.get());
 
 151     private class MyHandler extends AsyncResponseHandler<String> {
 
 153         public MyHandler(ControlLoopOperationParams params, OperationOutcome outcome) {
 
 154             super(params, outcome);
 
 158         protected OperationOutcome doComplete(String rawResponse) {
 
 159             OperationOutcome outcome = getOutcome();
 
 160             outcome.setResult(PolicyResult.FAILURE_RETRIES);
 
 161             outcome.setMessage(rawResponse);
 
 166         protected OperationOutcome doFailed(Throwable thrown) {
 
 167             OperationOutcome outcome = getOutcome();
 
 168             outcome.setResult(PolicyResult.FAILURE_GUARD);