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.assertThatIllegalArgumentException;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
32 import java.util.HashMap;
34 import java.util.UUID;
35 import java.util.concurrent.CompletableFuture;
36 import java.util.concurrent.CountDownLatch;
37 import java.util.concurrent.ExecutionException;
38 import java.util.concurrent.TimeUnit;
39 import java.util.concurrent.TimeoutException;
40 import java.util.concurrent.atomic.AtomicBoolean;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
46 import org.onap.policy.aai.AaiCqResponse;
47 import org.onap.policy.cds.client.CdsProcessorGrpcClient;
48 import org.onap.policy.cds.properties.CdsServerProperties;
49 import org.onap.policy.common.utils.time.PseudoExecutor;
50 import org.onap.policy.controlloop.VirtualControlLoopEvent;
51 import org.onap.policy.controlloop.actor.cds.constants.CdsActorConstants;
52 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
53 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
54 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
55 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
56 import org.onap.policy.controlloop.policy.PolicyResult;
58 public class GrpcOperationTest {
60 private static final String CDS_BLUEPRINT_NAME = "vfw-cds";
61 private static final String CDS_BLUEPRINT_VERSION = "1.0.0";
62 private static final UUID REQUEST_ID = UUID.randomUUID();
65 private CdsProcessorGrpcClient cdsClient;
66 private CdsServerProperties cdsProps;
67 private VirtualControlLoopEvent onset;
68 private PseudoExecutor executor;
69 private GrpcOperation operation;
75 public void setUp() throws Exception {
76 MockitoAnnotations.initMocks(this);
78 // Setup the CDS properties
79 cdsProps = new CdsServerProperties();
80 cdsProps.setHost("10.10.10.10");
81 cdsProps.setPort(2000);
82 cdsProps.setUsername("testUser");
83 cdsProps.setPassword("testPassword");
84 cdsProps.setTimeout(1);
87 when(cdsClient.sendRequest(any(ExecutionServiceInput.class))).thenReturn(mock(CountDownLatch.class));
90 onset = new VirtualControlLoopEvent();
91 onset.setRequestId(REQUEST_ID);
94 executor = new PseudoExecutor();
98 public void testStartPreprocessorAsync() throws InterruptedException, ExecutionException, TimeoutException {
100 CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
101 ControlLoopEventContext context = mock(ControlLoopEventContext.class);
102 when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(future2);
103 when(context.getEvent()).thenReturn(onset);
105 AtomicBoolean guardStarted = new AtomicBoolean();
107 ControlLoopOperationParams params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
108 .operation(GrpcOperation.NAME).context(context).actorService(new ActorService())
109 .targetEntity("entity").build();
110 GrpcConfig config = new GrpcConfig(executor, cdsProps);
112 operation = new GrpcOperation(params, config) {
114 protected CompletableFuture<OperationOutcome> startGuardAsync() {
115 guardStarted.set(true);
120 CompletableFuture<OperationOutcome> future3 = operation.startPreprocessorAsync();
121 assertNotNull(future3);
122 assertTrue(guardStarted.get());
123 verify(context).obtain(eq(AaiCqResponse.CONTEXT_KEY), any());
125 future2.complete(params.makeOutcome());
126 assertTrue(executor.runAll(100));
127 assertEquals(PolicyResult.SUCCESS, future3.get(2, TimeUnit.SECONDS).getResult());
128 assertTrue(future3.isDone());
132 public void testStartOperationAsync() throws Exception {
134 ControlLoopEventContext context = new ControlLoopEventContext(onset);
135 verifyOperation(context);
139 public void testStartOperationAsyncWithAdditionalParams() throws Exception {
141 Map<String, String> additionalParams = new HashMap<>();
142 additionalParams.put("test", "additionalParams");
143 onset.setAdditionalEventParams(additionalParams);
144 ControlLoopEventContext context = new ControlLoopEventContext(onset);
145 verifyOperation(context);
149 public void testStartOperationAsyncError() throws Exception {
151 ControlLoopEventContext context = new ControlLoopEventContext(onset);
152 ControlLoopOperationParams params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
153 .operation(GrpcOperation.NAME).context(context).actorService(new ActorService())
154 .targetEntity("entity").build();
156 GrpcConfig config = new GrpcConfig(executor, cdsProps);
157 operation = new GrpcOperation(params, config);
158 assertThatIllegalArgumentException().isThrownBy(() -> operation.startOperationAsync(1, params.makeOutcome()));
161 private void verifyOperation(ControlLoopEventContext context) {
163 Map<String, Object> payloadMap = Map.of(CdsActorConstants.KEY_CBA_NAME, CDS_BLUEPRINT_NAME,
164 CdsActorConstants.KEY_CBA_VERSION, CDS_BLUEPRINT_VERSION, "data",
165 "{\"mapInfo\":{\"key\":\"val\"},\"arrayInfo\":[\"one\",\"two\"],\"paramInfo\":\"val\"}");
167 ControlLoopOperationParams params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
168 .operation(GrpcOperation.NAME).context(context).actorService(new ActorService())
169 .targetEntity("entity").payload(payloadMap).build();
171 GrpcConfig config = new GrpcConfig(executor, cdsProps);
172 operation = new GrpcOperation(params, config);
173 assertEquals(1000, operation.getTimeoutMs(null));
174 assertEquals(1000, operation.getTimeoutMs(0));
175 assertEquals(2000, operation.getTimeoutMs(2));
176 operation.generateSubRequestId(1);
177 CompletableFuture<OperationOutcome> future3 = operation.startOperationAsync(1, params.makeOutcome());
178 assertNotNull(future3);