Fix sonars from depeendency upgrade
[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  * ================================================================================
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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=========================================================
18  */
19
20 package org.onap.policy.controlloop.actor.cds;
21
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;
27
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.junit.runner.RunWith;
35 import org.mockito.junit.MockitoJUnitRunner;
36 import org.onap.ccsdk.cds.controllerblueprints.common.api.EventType;
37 import org.onap.ccsdk.cds.controllerblueprints.common.api.Status;
38 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
39 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
40 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
41
42 @RunWith(MockitoJUnitRunner.class)
43 public class GrpcActorServiceManagerTest {
44
45     CdsActorServiceManager manager;
46     CompletableFuture<OperationOutcome> future;
47     ExecutionServiceOutput output;
48
49     /**
50      * Sets up the fields.
51      */
52     @Before
53     public void setUp() throws Exception {
54         future = new CompletableFuture<>();
55         manager = new CdsActorServiceManager(new OperationOutcome(), future);
56     }
57
58     @Test
59     public void testOnMessageSuccess() throws InterruptedException, ExecutionException, TimeoutException {
60
61         Status status = Status.newBuilder().setEventType(EventType.EVENT_COMPONENT_EXECUTED).build();
62         output = ExecutionServiceOutput.newBuilder().setStatus(status).build();
63         manager.onMessage(output);
64         OperationOutcome outcome = future.get(2, TimeUnit.SECONDS);
65         assertEquals(OperationResult.SUCCESS, outcome.getResult());
66         assertSame(output, outcome.getResponse());
67     }
68
69     @Test
70     public void testOnMessageProcessing() throws InterruptedException, ExecutionException, TimeoutException {
71
72         Status status = Status.newBuilder().setEventType(EventType.EVENT_COMPONENT_PROCESSING).build();
73         output = ExecutionServiceOutput.newBuilder().setStatus(status).build();
74         manager.onMessage(output);
75         assertThatThrownBy(() -> future.get(200, TimeUnit.MILLISECONDS)).isInstanceOf(TimeoutException.class);
76         assertFalse(future.isDone());
77     }
78
79     @Test
80     public void testOnMessageFailure() throws InterruptedException, ExecutionException, TimeoutException {
81
82         Status status = Status.newBuilder().setEventType(EventType.EVENT_COMPONENT_FAILURE).build();
83         output = ExecutionServiceOutput.newBuilder().setStatus(status).build();
84         manager.onMessage(output);
85         OperationOutcome outcome = future.get(2, TimeUnit.SECONDS);
86         assertEquals(OperationResult.FAILURE, outcome.getResult());
87         assertSame(output, outcome.getResponse());
88     }
89
90     @Test
91     public void testOnError() throws InterruptedException, ExecutionException, TimeoutException {
92
93         Exception exception = new Exception("something failed");
94         manager.onError(exception);
95         assertTrue(future.isCompletedExceptionally());
96     }
97 }