908b05c08e9f85799831c442c45cdcfea1044ea2
[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 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.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.policy.PolicyResult;
40
41 public class GrpcActorServiceManagerTest {
42
43     CdsActorServiceManager manager;
44     CompletableFuture<OperationOutcome> future;
45     ExecutionServiceOutput output;
46
47     /**
48      * Sets up the fields.
49      */
50     @Before
51     public void setUp() throws Exception {
52         MockitoAnnotations.initMocks(this);
53         future = new CompletableFuture<>();
54         manager = new CdsActorServiceManager(new OperationOutcome(), future);
55     }
56
57     @Test
58     public void testOnMessageSuccess() throws InterruptedException, ExecutionException, TimeoutException {
59
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(PolicyResult.SUCCESS, outcome.getResult());
65         assertSame(output, outcome.getResponse());
66     }
67
68     @Test
69     public void testOnMessageProcessing() throws InterruptedException, ExecutionException, TimeoutException {
70
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());
76     }
77
78     @Test
79     public void testOnMessageFailure() throws InterruptedException, ExecutionException, TimeoutException {
80
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(PolicyResult.FAILURE, outcome.getResult());
86         assertSame(output, outcome.getResponse());
87     }
88
89     @Test
90     public void testOnError() throws InterruptedException, ExecutionException, TimeoutException {
91
92         Exception exception = new Exception("something failed");
93         manager.onError(exception);
94         assertTrue(future.isCompletedExceptionally());
95     }
96 }