Convert models to JUnit 5
[policy/models.git] / models-interactions / model-actors / actor.cds / src / test / java / org / onap / policy / controlloop / actor / cds / GrpcActorServiceManagerTest.java
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.actor.cds;
22
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;
28
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;
42
43 @ExtendWith(MockitoExtension.class)
44  class GrpcActorServiceManagerTest {
45
46     CdsActorServiceManager manager;
47     CompletableFuture<OperationOutcome> future;
48     ExecutionServiceOutput output;
49
50     /**
51      * Sets up the fields.
52      */
53     @BeforeEach
54      void setUp() throws Exception {
55         future = new CompletableFuture<>();
56         manager = new CdsActorServiceManager(new OperationOutcome(), future);
57     }
58
59     @Test
60      void testOnMessageSuccess() throws InterruptedException, ExecutionException, TimeoutException {
61
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());
68     }
69
70     @Test
71      void testOnMessageProcessing() throws InterruptedException, ExecutionException, TimeoutException {
72
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());
78     }
79
80     @Test
81      void testOnMessageFailure() throws InterruptedException, ExecutionException, TimeoutException {
82
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());
89     }
90
91     @Test
92      void testOnError() throws InterruptedException, ExecutionException, TimeoutException {
93
94         Exception exception = new Exception("something failed");
95         manager.onError(exception);
96         assertTrue(future.isCompletedExceptionally());
97     }
98 }