2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
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.so;
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertSame;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.when;
32 import java.util.Collections;
33 import java.util.List;
35 import java.util.concurrent.CompletableFuture;
36 import java.util.concurrent.ForkJoinPool;
37 import java.util.concurrent.TimeUnit;
38 import java.util.concurrent.atomic.AtomicBoolean;
39 import java.util.function.Consumer;
40 import java.util.function.Supplier;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.onap.aai.domain.yang.CloudRegion;
44 import org.onap.aai.domain.yang.GenericVnf;
45 import org.onap.aai.domain.yang.ServiceInstance;
46 import org.onap.aai.domain.yang.Tenant;
47 import org.onap.policy.aai.AaiCqResponse;
48 import org.onap.policy.common.utils.coder.CoderException;
49 import org.onap.policy.controlloop.ControlLoopOperation;
50 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
51 import org.onap.policy.controlloop.policy.PolicyResult;
52 import org.onap.policy.so.SoModelInfo;
53 import org.onap.policy.so.SoRequest;
54 import org.onap.policy.so.SoRequestInfo;
55 import org.onap.policy.so.SoRequestParameters;
56 import org.onap.policy.so.SoRequestReferences;
57 import org.onap.policy.so.SoRequestStatus;
58 import org.onap.policy.so.SoResponse;
60 public class SoOperationTest extends BasicSoOperation {
62 private SoOperation oper;
68 public void setUp() throws Exception {
73 oper = new SoOperation(params, soOperator) {};
77 public void testConstructor_testGetWaitMsGet() {
78 assertEquals(DEFAULT_ACTOR, oper.getActorName());
79 assertEquals(DEFAULT_OPERATION, oper.getName());
80 assertSame(soOperator, oper.getOperator());
81 assertEquals(1000 * WAIT_SEC_GETS, oper.getWaitMsGet());
85 public void testStartPreprocessorAsync() {
86 AtomicBoolean guardStarted = new AtomicBoolean();
88 oper = new SoOperation(params, soOperator) {
90 protected CompletableFuture<OperationOutcome> startGuardAsync() {
91 guardStarted.set(true);
92 return super.startGuardAsync();
96 assertNull(oper.startPreprocessorAsync());
97 assertTrue(guardStarted.get());
101 public void testPostProcess() throws Exception {
103 CompletableFuture<OperationOutcome> future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
104 assertTrue(future2.isDone());
105 assertSame(outcome, future2.get());
106 assertEquals(PolicyResult.SUCCESS, outcome.getResult());
109 response.getRequest().getRequestStatus().setRequestState(SoOperation.FAILED);
110 future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
111 assertTrue(future2.isDone());
112 assertSame(outcome, future2.get());
113 assertEquals(PolicyResult.FAILURE, outcome.getResult());
115 // no request id in the response
116 response.getRequestReferences().setRequestId(null);
117 response.getRequest().getRequestStatus().setRequestState("unknown");
118 assertThatIllegalArgumentException()
119 .isThrownBy(() -> oper.postProcessResponse(outcome, PATH, rawResponse, response))
120 .withMessage("missing request ID in response");
121 response.getRequestReferences().setRequestId(REQ_ID.toString());
124 when(rawResponse.getStatus()).thenReturn(500);
126 // null request reference
127 SoRequestReferences ref = response.getRequestReferences();
128 response.setRequestReferences(null);
129 assertThatIllegalArgumentException()
130 .isThrownBy(() -> oper.postProcessResponse(outcome, PATH, rawResponse, response))
131 .withMessage("missing request ID in response");
132 response.setRequestReferences(ref);
136 * Tests postProcess() when the "get" is repeated a couple of times.
139 public void testPostProcessRepeated_testResetGetCount() throws Exception {
141 * Two failures and then a success - should result in two "get" calls.
143 * Note: getStatus() is invoked twice during each call, so have to double up the
146 when(rawResponse.getStatus()).thenReturn(500, 500, 500, 500, 200, 200);
148 when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse));
150 // use a real executor
151 params = params.toBuilder().executor(ForkJoinPool.commonPool()).build();
153 oper = new SoOperation(params, soOperator) {
155 public long getWaitMsGet() {
160 CompletableFuture<OperationOutcome> future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
162 assertSame(outcome, future2.get(5, TimeUnit.SECONDS));
163 assertEquals(PolicyResult.SUCCESS, outcome.getResult());
164 assertEquals(2, oper.getGetCount());
167 * repeat - this time, the "get" operations will be exhausted, so it should fail
169 when(rawResponse.getStatus()).thenReturn(500);
171 future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
173 assertSame(outcome, future2.get(5, TimeUnit.SECONDS));
174 assertEquals(PolicyResult.FAILURE_TIMEOUT, outcome.getResult());
175 assertEquals(MAX_GETS + 1, oper.getGetCount());
177 oper.resetGetCount();
178 assertEquals(0, oper.getGetCount());
182 public void testGetRequestState() {
183 SoResponse resp = new SoResponse();
184 assertNull(oper.getRequestState(resp));
186 SoRequest req = new SoRequest();
187 resp.setRequest(req);
188 assertNull(oper.getRequestState(resp));
190 SoRequestStatus status = new SoRequestStatus();
191 req.setRequestStatus(status);
192 assertNull(oper.getRequestState(resp));
194 status.setRequestState("my-state");
195 assertEquals("my-state", oper.getRequestState(resp));
199 public void testIsSuccess() {
202 assertTrue(oper.isSuccess(rawResponse, response));
204 when(rawResponse.getStatus()).thenReturn(500);
205 assertTrue(oper.isSuccess(rawResponse, response));
209 public void testSetOutcome() {
211 when(rawResponse.getStatus()).thenReturn(200);
212 assertSame(outcome, oper.setOutcome(outcome, PolicyResult.SUCCESS, rawResponse, response));
214 assertEquals(PolicyResult.SUCCESS, outcome.getResult());
215 assertEquals("200 " + ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
218 when(rawResponse.getStatus()).thenReturn(500);
219 assertSame(outcome, oper.setOutcome(outcome, PolicyResult.FAILURE, rawResponse, response));
221 assertEquals(PolicyResult.FAILURE, outcome.getResult());
222 assertEquals("500 " + ControlLoopOperation.FAILED_MSG, outcome.getMessage());
226 public void testPrepareSoModelInfo() throws CoderException {
227 verifyMissingModelInfo(target::getModelCustomizationId, target::setModelCustomizationId);
228 verifyMissingModelInfo(target::getModelInvariantId, target::setModelInvariantId);
229 verifyMissingModelInfo(target::getModelName, target::setModelName);
230 verifyMissingModelInfo(target::getModelVersion, target::setModelVersion);
231 verifyMissingModelInfo(target::getModelVersionId, target::setModelVersionId);
234 SoModelInfo info = oper.prepareSoModelInfo();
235 verifyRequest("model.json", info);
237 // try with null target
238 params = params.toBuilder().target(null).build();
239 oper = new SoOperation(params, soOperator) {};
241 assertThatIllegalArgumentException().isThrownBy(() -> oper.prepareSoModelInfo()).withMessage("missing Target");
244 private void verifyMissingModelInfo(Supplier<String> getter, Consumer<String> setter) {
245 String original = getter.get();
248 assertThatIllegalArgumentException().isThrownBy(() -> oper.prepareSoModelInfo())
249 .withMessage("missing VF Module model");
251 setter.accept(original);
255 public void testConstructRequestInfo() throws CoderException {
256 SoRequestInfo info = oper.constructRequestInfo();
257 verifyRequest("reqinfo.json", info);
261 public void testBuildRequestParameters() throws CoderException {
263 SoRequestParameters reqParams = oper.buildRequestParameters();
264 verifyRequest("reqparams.json", reqParams);
267 params.getPayload().put(SoOperation.REQ_PARAM_NM, "{invalid json");
268 assertThatIllegalArgumentException().isThrownBy(() -> oper.buildRequestParameters())
269 .withMessage("invalid payload value: " + SoOperation.REQ_PARAM_NM);
272 params.getPayload().remove(SoOperation.REQ_PARAM_NM);
273 assertNull(oper.buildRequestParameters());
276 params = params.toBuilder().payload(null).build();
277 oper = new SoOperation(params, soOperator) {};
278 assertNull(oper.buildRequestParameters());
282 public void testBuildConfigurationParameters() {
284 List<Map<String, String>> result = oper.buildConfigurationParameters();
285 assertEquals(List.of(Collections.emptyMap()), result);
288 params.getPayload().put(SoOperation.CONFIG_PARAM_NM, "{invalid json");
289 assertThatIllegalArgumentException().isThrownBy(() -> oper.buildConfigurationParameters())
290 .withMessage("invalid payload value: " + SoOperation.CONFIG_PARAM_NM);
293 params.getPayload().remove(SoOperation.CONFIG_PARAM_NM);
294 assertNull(oper.buildConfigurationParameters());
297 params = params.toBuilder().payload(null).build();
298 oper = new SoOperation(params, soOperator) {};
299 assertNull(oper.buildConfigurationParameters());
303 public void testGetVnfItem() {
305 AaiCqResponse cq = mock(AaiCqResponse.class);
306 assertThatIllegalArgumentException().isThrownBy(() -> oper.getVnfItem(cq, oper.prepareSoModelInfo()))
307 .withMessage("missing generic VNF");
310 GenericVnf vnf = new GenericVnf();
311 when(cq.getGenericVnfByVfModuleModelInvariantId(MODEL_INVAR_ID)).thenReturn(vnf);
312 assertSame(vnf, oper.getVnfItem(cq, oper.prepareSoModelInfo()));
316 public void testGetServiceInstance() {
318 AaiCqResponse cq = mock(AaiCqResponse.class);
319 assertThatIllegalArgumentException().isThrownBy(() -> oper.getServiceInstance(cq))
320 .withMessage("missing VNF Service Item");
323 ServiceInstance instance = new ServiceInstance();
324 when(cq.getServiceInstance()).thenReturn(instance);
325 assertSame(instance, oper.getServiceInstance(cq));
329 public void testGetDefaultTenant() {
331 AaiCqResponse cq = mock(AaiCqResponse.class);
332 assertThatIllegalArgumentException().isThrownBy(() -> oper.getDefaultTenant(cq))
333 .withMessage("missing Tenant Item");
336 Tenant tenant = new Tenant();
337 when(cq.getDefaultTenant()).thenReturn(tenant);
338 assertSame(tenant, oper.getDefaultTenant(cq));
342 public void testGetDefaultCloudRegion() {
344 AaiCqResponse cq = mock(AaiCqResponse.class);
345 assertThatIllegalArgumentException().isThrownBy(() -> oper.getDefaultCloudRegion(cq))
346 .withMessage("missing Cloud Region");
349 CloudRegion region = new CloudRegion();
350 when(cq.getDefaultCloudRegion()).thenReturn(region);
351 assertSame(region, oper.getDefaultCloudRegion(cq));