2  * ============LICENSE_START=======================================================
 
   3  * Copyright (C) 2020 Bell Canada. All rights reserved.
 
   4  * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
 
   5  * ================================================================================
 
   6  * Licensed under the Apache License, Version 2.0 (the "License");
 
   7  * you may not use this file except in compliance with the License.
 
   8  * You may obtain a copy of the License at
 
  10  *      http://www.apache.org/licenses/LICENSE-2.0
 
  12  * Unless required by applicable law or agreed to in writing, software
 
  13  * distributed under the License is distributed on an "AS IS" BASIS,
 
  14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  15  * See the License for the specific language governing permissions and
 
  16  * limitations under the License.
 
  17  * ============LICENSE_END=========================================================
 
  20 package org.onap.policy.controlloop.actor.cds;
 
  22 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
  23 import static org.junit.Assert.assertEquals;
 
  24 import static org.junit.Assert.assertFalse;
 
  25 import static org.junit.Assert.assertSame;
 
  26 import static org.junit.Assert.assertTrue;
 
  28 import java.util.concurrent.CompletableFuture;
 
  29 import java.util.concurrent.ExecutionException;
 
  30 import java.util.concurrent.TimeUnit;
 
  31 import java.util.concurrent.TimeoutException;
 
  32 import org.junit.Before;
 
  33 import org.junit.Test;
 
  34 import org.mockito.MockitoAnnotations;
 
  35 import org.onap.ccsdk.cds.controllerblueprints.common.api.EventType;
 
  36 import org.onap.ccsdk.cds.controllerblueprints.common.api.Status;
 
  37 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
 
  38 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
 
  39 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
 
  41 public class GrpcActorServiceManagerTest {
 
  43     CdsActorServiceManager manager;
 
  44     CompletableFuture<OperationOutcome> future;
 
  45     ExecutionServiceOutput output;
 
  51     public void setUp() throws Exception {
 
  52         MockitoAnnotations.initMocks(this);
 
  53         future = new CompletableFuture<>();
 
  54         manager = new CdsActorServiceManager(new OperationOutcome(), future);
 
  58     public void testOnMessageSuccess() throws InterruptedException, ExecutionException, TimeoutException {
 
  60         Status status = Status.newBuilder().setEventType(EventType.EVENT_COMPONENT_EXECUTED).build();
 
  61         output = ExecutionServiceOutput.newBuilder().setStatus(status).build();
 
  62         manager.onMessage(output);
 
  63         OperationOutcome outcome = future.get(2, TimeUnit.SECONDS);
 
  64         assertEquals(OperationResult.SUCCESS, outcome.getResult());
 
  65         assertSame(output, outcome.getResponse());
 
  69     public void testOnMessageProcessing() throws InterruptedException, ExecutionException, TimeoutException {
 
  71         Status status = Status.newBuilder().setEventType(EventType.EVENT_COMPONENT_PROCESSING).build();
 
  72         output = ExecutionServiceOutput.newBuilder().setStatus(status).build();
 
  73         manager.onMessage(output);
 
  74         assertThatThrownBy(() -> future.get(200, TimeUnit.MILLISECONDS)).isInstanceOf(TimeoutException.class);
 
  75         assertFalse(future.isDone());
 
  79     public void testOnMessageFailure() throws InterruptedException, ExecutionException, TimeoutException {
 
  81         Status status = Status.newBuilder().setEventType(EventType.EVENT_COMPONENT_FAILURE).build();
 
  82         output = ExecutionServiceOutput.newBuilder().setStatus(status).build();
 
  83         manager.onMessage(output);
 
  84         OperationOutcome outcome = future.get(2, TimeUnit.SECONDS);
 
  85         assertEquals(OperationResult.FAILURE, outcome.getResult());
 
  86         assertSame(output, outcome.getResponse());
 
  90     public void testOnError() throws InterruptedException, ExecutionException, TimeoutException {
 
  92         Exception exception = new Exception("something failed");
 
  93         manager.onError(exception);
 
  94         assertTrue(future.isCompletedExceptionally());