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.assertTrue;
27 import java.util.concurrent.CompletableFuture;
28 import java.util.concurrent.ExecutionException;
29 import java.util.concurrent.TimeUnit;
30 import java.util.concurrent.TimeoutException;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.MockitoAnnotations;
34 import org.onap.ccsdk.cds.controllerblueprints.common.api.EventType;
35 import org.onap.ccsdk.cds.controllerblueprints.common.api.Status;
36 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
37 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
38 import org.onap.policy.controlloop.policy.PolicyResult;
40 public class GrpcActorServiceManagerTest {
42 CdsActorServiceManager manager;
43 CompletableFuture<OperationOutcome> future;
44 ExecutionServiceOutput output;
50 public void setUp() throws Exception {
51 MockitoAnnotations.initMocks(this);
52 future = new CompletableFuture<>();
53 manager = new CdsActorServiceManager(new OperationOutcome(), future);
57 public void testOnMessageSuccess() throws InterruptedException, ExecutionException, TimeoutException {
59 Status status = Status.newBuilder().setEventType(EventType.EVENT_COMPONENT_EXECUTED).build();
60 output = ExecutionServiceOutput.newBuilder().setStatus(status).build();
61 manager.onMessage(output);
62 assertEquals(PolicyResult.SUCCESS, future.get(2, TimeUnit.SECONDS).getResult());
63 assertTrue(future.isDone());
67 public void testOnMessageProcessing() throws InterruptedException, ExecutionException, TimeoutException {
69 Status status = Status.newBuilder().setEventType(EventType.EVENT_COMPONENT_PROCESSING).build();
70 output = ExecutionServiceOutput.newBuilder().setStatus(status).build();
71 manager.onMessage(output);
72 assertThatThrownBy(() -> future.get(200, TimeUnit.MILLISECONDS)).isInstanceOf(TimeoutException.class);
73 assertFalse(future.isDone());
77 public void testOnMessageFailure() throws InterruptedException, ExecutionException, TimeoutException {
79 Status status = Status.newBuilder().setEventType(EventType.EVENT_COMPONENT_FAILURE).build();
80 output = ExecutionServiceOutput.newBuilder().setStatus(status).build();
81 manager.onMessage(output);
82 assertEquals(PolicyResult.FAILURE, future.get(2, TimeUnit.SECONDS).getResult());
83 assertTrue(future.isDone());
87 public void testOnError() throws InterruptedException, ExecutionException, TimeoutException {
89 Exception exception = new Exception("something failed");
90 manager.onError(exception);
91 assertTrue(future.isCompletedExceptionally());
92 assertTrue(future.isDone());