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.assertThatCode;
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertSame;
30 import static org.junit.Assert.assertTrue;
31 import static org.mockito.ArgumentMatchers.any;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.when;
35 import java.time.LocalDateTime;
36 import java.time.Month;
37 import java.util.Collections;
38 import java.util.List;
39 import java.util.concurrent.CompletableFuture;
40 import java.util.concurrent.ForkJoinPool;
41 import java.util.concurrent.TimeUnit;
42 import java.util.function.Consumer;
43 import java.util.function.Supplier;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.onap.aai.domain.yang.CloudRegion;
47 import org.onap.aai.domain.yang.GenericVnf;
48 import org.onap.aai.domain.yang.ServiceInstance;
49 import org.onap.aai.domain.yang.Tenant;
50 import org.onap.policy.aai.AaiCqResponse;
51 import org.onap.policy.common.utils.coder.Coder;
52 import org.onap.policy.common.utils.coder.CoderException;
53 import org.onap.policy.controlloop.ControlLoopOperation;
54 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
55 import org.onap.policy.controlloop.policy.PolicyResult;
56 import org.onap.policy.so.SoModelInfo;
57 import org.onap.policy.so.SoRequest;
58 import org.onap.policy.so.SoRequestInfo;
59 import org.onap.policy.so.SoRequestReferences;
60 import org.onap.policy.so.SoRequestStatus;
61 import org.onap.policy.so.SoResponse;
63 public class SoOperationTest extends BasicSoOperation {
65 private static final String VF_COUNT_KEY = SoConstants.VF_COUNT_PREFIX
66 + "[my-model-customization-id][my-model-invariant-id][my-model-version-id]";
67 private SoOperation oper;
73 public void setUp() throws Exception {
78 oper = new SoOperation(params, config) {};
82 public void testConstructor_testGetWaitMsGet() {
83 assertEquals(DEFAULT_ACTOR, oper.getActorName());
84 assertEquals(DEFAULT_OPERATION, oper.getName());
85 assertSame(config, oper.getConfig());
86 assertEquals(1000 * WAIT_SEC_GETS, oper.getWaitMsGet());
88 // check when Target is null
89 params = params.toBuilder().target(null).build();
90 assertThatIllegalArgumentException().isThrownBy(() -> new SoOperation(params, config) {})
91 .withMessageContaining("Target information");
95 public void testValidateTarget() {
96 // check when various fields are null
97 verifyNotNull("modelCustomizationId", target::getModelCustomizationId, target::setModelCustomizationId);
98 verifyNotNull("modelInvariantId", target::getModelInvariantId, target::setModelInvariantId);
99 verifyNotNull("modelVersionId", target::getModelVersionId, target::setModelVersionId);
101 // verify it's still valid
102 assertThatCode(() -> new VfModuleCreate(params, config)).doesNotThrowAnyException();
105 private void verifyNotNull(String expectedText, Supplier<String> getter, Consumer<String> setter) {
106 String originalValue = getter.get();
110 assertThatIllegalArgumentException().isThrownBy(() -> new VfModuleCreate(params, config))
111 .withMessageContaining(expectedText);
113 setter.accept(originalValue);
117 public void testStartPreprocessorAsync() {
118 assertNotNull(oper.startPreprocessorAsync());
122 public void testObtainVfCount_testGetVfCount_testSetVfCount() throws Exception {
123 // insert CQ data so it's there for the check
124 context.setProperty(AaiCqResponse.CONTEXT_KEY, makeCqResponse());
126 // shouldn't actually need to do anything
127 assertNull(oper.obtainVfCount());
129 // verify that the count was stored
130 Integer vfcount = context.getProperty(VF_COUNT_KEY);
131 assertEquals(VF_COUNT, vfcount);
132 assertEquals(VF_COUNT.intValue(), oper.getVfCount());
134 // change the count and then verify that it isn't overwritten by another call
135 oper.setVfCount(VF_COUNT + 1);
137 assertNull(oper.obtainVfCount());
138 vfcount = context.getProperty(VF_COUNT_KEY);
139 assertEquals(VF_COUNT + 1, vfcount.intValue());
140 assertEquals(VF_COUNT + 1, oper.getVfCount());
144 * Tests obtainVfCount() when it actually has to query.
147 public void testObtainVfCountQuery() throws Exception {
148 CompletableFuture<OperationOutcome> future2 = oper.obtainVfCount();
149 assertNotNull(future2);
150 assertTrue(executor.runAll(100));
153 assertFalse(future2.isDone());
155 provideCqResponse(makeCqResponse());
157 assertTrue(executor.runAll(100));
158 assertTrue(future2.isDone());
159 assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
161 // verify that the count was stored
162 Integer vfcount = context.getProperty(VF_COUNT_KEY);
163 assertEquals(VF_COUNT, vfcount);
165 // repeat - shouldn't need to do anything now
166 assertNull(oper.obtainVfCount());
170 public void testPostProcess() throws Exception {
172 oper.generateSubRequestId(2);
173 assertNull(oper.getSubRequestId());
174 CompletableFuture<OperationOutcome> future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
175 assertTrue(future2.isDone());
176 assertSame(outcome, future2.get());
177 assertEquals(PolicyResult.SUCCESS, outcome.getResult());
178 assertNotNull(oper.getSubRequestId());
181 oper.generateSubRequestId(2);
182 assertNull(oper.getSubRequestId());
183 response.getRequest().getRequestStatus().setRequestState(SoOperation.FAILED);
184 future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
185 assertTrue(future2.isDone());
186 assertSame(outcome, future2.get());
187 assertEquals(PolicyResult.FAILURE, outcome.getResult());
188 assertNotNull(oper.getSubRequestId());
190 // no request id in the response
191 oper.generateSubRequestId(2);
192 assertNull(oper.getSubRequestId());
193 response.getRequestReferences().setRequestId(null);
194 response.getRequest().getRequestStatus().setRequestState("unknown");
195 assertThatIllegalArgumentException()
196 .isThrownBy(() -> oper.postProcessResponse(outcome, PATH, rawResponse, response))
197 .withMessage("missing request ID in response");
198 response.getRequestReferences().setRequestId(REQ_ID.toString());
201 when(rawResponse.getStatus()).thenReturn(500);
203 // null request reference
204 SoRequestReferences ref = response.getRequestReferences();
205 response.setRequestReferences(null);
206 assertThatIllegalArgumentException()
207 .isThrownBy(() -> oper.postProcessResponse(outcome, PATH, rawResponse, response))
208 .withMessage("missing request ID in response");
209 response.setRequestReferences(ref);
213 * Tests postProcess() when the "get" is repeated a couple of times.
216 public void testPostProcessRepeated_testResetGetCount() throws Exception {
218 * Two failures and then a success - should result in two "get" calls.
220 * Note: getStatus() is invoked twice during each call, so have to double up the
223 when(rawResponse.getStatus()).thenReturn(500, 500, 500, 500, 200, 200);
225 when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse));
227 // use a real executor
228 params = params.toBuilder().executor(ForkJoinPool.commonPool()).build();
230 oper = new SoOperation(params, config) {
232 public long getWaitMsGet() {
237 CompletableFuture<OperationOutcome> future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
239 assertSame(outcome, future2.get(5, TimeUnit.SECONDS));
240 assertEquals(PolicyResult.SUCCESS, outcome.getResult());
241 assertEquals(2, oper.getGetCount());
244 * repeat - this time, the "get" operations will be exhausted, so it should fail
246 when(rawResponse.getStatus()).thenReturn(500);
248 future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
250 assertSame(outcome, future2.get(5, TimeUnit.SECONDS));
251 assertEquals(PolicyResult.FAILURE_TIMEOUT, outcome.getResult());
252 assertEquals(MAX_GETS + 1, oper.getGetCount());
254 oper.resetGetCount();
255 assertEquals(0, oper.getGetCount());
256 assertNull(oper.getSubRequestId());
260 public void testGetRequestState() {
261 SoResponse resp = new SoResponse();
262 assertNull(oper.getRequestState(resp));
264 SoRequest req = new SoRequest();
265 resp.setRequest(req);
266 assertNull(oper.getRequestState(resp));
268 SoRequestStatus status = new SoRequestStatus();
269 req.setRequestStatus(status);
270 assertNull(oper.getRequestState(resp));
272 status.setRequestState("my-state");
273 assertEquals("my-state", oper.getRequestState(resp));
277 public void testIsSuccess() {
280 assertTrue(oper.isSuccess(rawResponse, response));
282 when(rawResponse.getStatus()).thenReturn(500);
283 assertTrue(oper.isSuccess(rawResponse, response));
287 public void testSetOutcome() {
289 when(rawResponse.getStatus()).thenReturn(200);
290 assertSame(outcome, oper.setOutcome(outcome, PolicyResult.SUCCESS, rawResponse, response));
292 assertEquals(PolicyResult.SUCCESS, outcome.getResult());
293 assertEquals("200 " + ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
296 when(rawResponse.getStatus()).thenReturn(500);
297 assertSame(outcome, oper.setOutcome(outcome, PolicyResult.FAILURE, rawResponse, response));
299 assertEquals(PolicyResult.FAILURE, outcome.getResult());
300 assertEquals("500 " + ControlLoopOperation.FAILED_MSG, outcome.getMessage());
304 public void testPrepareSoModelInfo() throws CoderException {
305 verifyMissingModelInfo(target::getModelCustomizationId, target::setModelCustomizationId);
306 verifyMissingModelInfo(target::getModelInvariantId, target::setModelInvariantId);
307 verifyMissingModelInfo(target::getModelName, target::setModelName);
308 verifyMissingModelInfo(target::getModelVersion, target::setModelVersion);
309 verifyMissingModelInfo(target::getModelVersionId, target::setModelVersionId);
312 SoModelInfo info = oper.prepareSoModelInfo();
313 verifyRequest("model.json", info);
315 // try with null target
316 params = params.toBuilder().target(null).build();
317 assertThatIllegalArgumentException().isThrownBy(() -> new SoOperation(params, config) {})
318 .withMessageContaining("missing Target");
321 private void verifyMissingModelInfo(Supplier<String> getter, Consumer<String> setter) {
322 String original = getter.get();
325 assertThatIllegalArgumentException().isThrownBy(() -> oper.prepareSoModelInfo())
326 .withMessage("missing VF Module model");
328 setter.accept(original);
332 public void testConstructRequestInfo() throws CoderException {
333 SoRequestInfo info = oper.constructRequestInfo();
334 verifyRequest("reqinfo.json", info);
338 public void testBuildRequestParameters() throws CoderException {
340 verifyRequest("reqparams.json", oper.buildRequestParameters().get());
343 params.getPayload().put(SoOperation.REQ_PARAM_NM, "{invalid json");
344 assertThatIllegalArgumentException().isThrownBy(() -> oper.buildRequestParameters())
345 .withMessage("invalid payload value: " + SoOperation.REQ_PARAM_NM);
348 params.getPayload().remove(SoOperation.REQ_PARAM_NM);
349 assertTrue(oper.buildRequestParameters().isEmpty());
352 params = params.toBuilder().payload(null).build();
353 oper = new SoOperation(params, config) {};
354 assertTrue(oper.buildRequestParameters().isEmpty());
358 public void testBuildConfigurationParameters() {
360 assertEquals(List.of(Collections.emptyMap()), oper.buildConfigurationParameters().get());
363 params.getPayload().put(SoOperation.CONFIG_PARAM_NM, "{invalid json");
364 assertThatIllegalArgumentException().isThrownBy(() -> oper.buildConfigurationParameters())
365 .withMessage("invalid payload value: " + SoOperation.CONFIG_PARAM_NM);
368 params.getPayload().remove(SoOperation.CONFIG_PARAM_NM);
369 assertTrue(oper.buildConfigurationParameters().isEmpty());
372 params = params.toBuilder().payload(null).build();
373 oper = new SoOperation(params, config) {};
374 assertTrue(oper.buildConfigurationParameters().isEmpty());
378 public void testGetVnfItem() {
380 AaiCqResponse cq = mock(AaiCqResponse.class);
381 assertThatIllegalArgumentException().isThrownBy(() -> oper.getVnfItem(cq, oper.prepareSoModelInfo()))
382 .withMessage("missing generic VNF");
385 GenericVnf vnf = new GenericVnf();
386 when(cq.getGenericVnfByVfModuleModelInvariantId(MODEL_INVAR_ID)).thenReturn(vnf);
387 assertSame(vnf, oper.getVnfItem(cq, oper.prepareSoModelInfo()));
391 public void testGetServiceInstance() {
393 AaiCqResponse cq = mock(AaiCqResponse.class);
394 assertThatIllegalArgumentException().isThrownBy(() -> oper.getServiceInstance(cq))
395 .withMessage("missing VNF Service Item");
398 ServiceInstance instance = new ServiceInstance();
399 when(cq.getServiceInstance()).thenReturn(instance);
400 assertSame(instance, oper.getServiceInstance(cq));
404 public void testGetDefaultTenant() {
406 AaiCqResponse cq = mock(AaiCqResponse.class);
407 assertThatIllegalArgumentException().isThrownBy(() -> oper.getDefaultTenant(cq))
408 .withMessage("missing Tenant Item");
411 Tenant tenant = new Tenant();
412 when(cq.getDefaultTenant()).thenReturn(tenant);
413 assertSame(tenant, oper.getDefaultTenant(cq));
417 public void testGetDefaultCloudRegion() {
419 AaiCqResponse cq = mock(AaiCqResponse.class);
420 assertThatIllegalArgumentException().isThrownBy(() -> oper.getDefaultCloudRegion(cq))
421 .withMessage("missing Cloud Region");
424 CloudRegion region = new CloudRegion();
425 when(cq.getDefaultCloudRegion()).thenReturn(region);
426 assertSame(region, oper.getDefaultCloudRegion(cq));
430 public void testMakeCoder() throws CoderException {
431 Coder opcoder = oper.makeCoder();
433 // ensure we can decode an SO timestamp
434 String json = "{'request':{'finishTime':'Fri, 15 May 2020 12:14:21 GMT'}}";
435 SoResponse resp = opcoder.decode(json.replace('\'', '"'), SoResponse.class);
437 LocalDateTime tfinish = resp.getRequest().getFinishTime();
438 assertNotNull(tfinish);
439 assertEquals(2020, tfinish.getYear());
440 assertEquals(Month.MAY, tfinish.getMonth());