2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Bell Canada. All rights reserved.
4 * Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
5 * Modifications Copyright (C) 2024 Nordix Foundation
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.actor.cds;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertSame;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
29 import java.util.concurrent.CompletableFuture;
30 import java.util.concurrent.ExecutionException;
31 import java.util.concurrent.TimeUnit;
32 import java.util.concurrent.TimeoutException;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.extension.ExtendWith;
36 import org.mockito.junit.jupiter.MockitoExtension;
37 import org.onap.ccsdk.cds.controllerblueprints.common.api.EventType;
38 import org.onap.ccsdk.cds.controllerblueprints.common.api.Status;
39 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
40 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
41 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
43 @ExtendWith(MockitoExtension.class)
44 class GrpcActorServiceManagerTest {
46 CdsActorServiceManager manager;
47 CompletableFuture<OperationOutcome> future;
48 ExecutionServiceOutput output;
54 void setUp() throws Exception {
55 future = new CompletableFuture<>();
56 manager = new CdsActorServiceManager(new OperationOutcome(), future);
60 void testOnMessageSuccess() throws InterruptedException, ExecutionException, TimeoutException {
62 Status status = Status.newBuilder().setEventType(EventType.EVENT_COMPONENT_EXECUTED).build();
63 output = ExecutionServiceOutput.newBuilder().setStatus(status).build();
64 manager.onMessage(output);
65 OperationOutcome outcome = future.get(2, TimeUnit.SECONDS);
66 assertEquals(OperationResult.SUCCESS, outcome.getResult());
67 assertSame(output, outcome.getResponse());
71 void testOnMessageProcessing() throws InterruptedException, ExecutionException, TimeoutException {
73 Status status = Status.newBuilder().setEventType(EventType.EVENT_COMPONENT_PROCESSING).build();
74 output = ExecutionServiceOutput.newBuilder().setStatus(status).build();
75 manager.onMessage(output);
76 assertThatThrownBy(() -> future.get(200, TimeUnit.MILLISECONDS)).isInstanceOf(TimeoutException.class);
77 assertFalse(future.isDone());
81 void testOnMessageFailure() throws InterruptedException, ExecutionException, TimeoutException {
83 Status status = Status.newBuilder().setEventType(EventType.EVENT_COMPONENT_FAILURE).build();
84 output = ExecutionServiceOutput.newBuilder().setStatus(status).build();
85 manager.onMessage(output);
86 OperationOutcome outcome = future.get(2, TimeUnit.SECONDS);
87 assertEquals(OperationResult.FAILURE, outcome.getResult());
88 assertSame(output, outcome.getResponse());
92 void testOnError() throws InterruptedException, ExecutionException, TimeoutException {
94 Exception exception = new Exception("something failed");
95 manager.onError(exception);
96 assertTrue(future.isCompletedExceptionally());