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.Mockito.mock;
32 import static org.mockito.Mockito.when;
34 import java.time.LocalDateTime;
35 import java.time.Month;
36 import java.util.Collections;
37 import java.util.List;
38 import java.util.concurrent.CompletableFuture;
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.Coder;
49 import org.onap.policy.common.utils.coder.CoderException;
50 import org.onap.policy.controlloop.ControlLoopOperation;
51 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
52 import org.onap.policy.controlloop.policy.PolicyResult;
53 import org.onap.policy.so.SoModelInfo;
54 import org.onap.policy.so.SoRequest;
55 import org.onap.policy.so.SoRequestInfo;
56 import org.onap.policy.so.SoRequestStatus;
57 import org.onap.policy.so.SoResponse;
59 public class SoOperationTest extends BasicSoOperation {
61 private static final String VF_COUNT_KEY = SoConstants.VF_COUNT_PREFIX
62 + "[my-model-customization-id][my-model-invariant-id][my-model-version-id]";
64 private static final List<String> PROP_NAMES = Collections.emptyList();
66 private SoOperation oper;
72 public void setUp() throws Exception {
77 oper = new SoOperation(params, config, PROP_NAMES) {};
81 public void testConstructor() {
82 assertEquals(DEFAULT_ACTOR, oper.getActorName());
83 assertEquals(DEFAULT_OPERATION, oper.getName());
84 assertSame(config, oper.getConfig());
85 assertTrue(oper.isUsePolling());
87 // check when Target is null
88 params = params.toBuilder().target(null).build();
89 assertThatIllegalArgumentException().isThrownBy(() -> new SoOperation(params, config, PROP_NAMES) {})
90 .withMessageContaining("Target information");
94 public void testValidateTarget() {
95 // check when various fields are null
96 verifyNotNull("modelCustomizationId", target::getModelCustomizationId, target::setModelCustomizationId);
97 verifyNotNull("modelInvariantId", target::getModelInvariantId, target::setModelInvariantId);
98 verifyNotNull("modelVersionId", target::getModelVersionId, target::setModelVersionId);
100 // verify it's still valid
101 assertThatCode(() -> new VfModuleCreate(params, config)).doesNotThrowAnyException();
104 private void verifyNotNull(String expectedText, Supplier<String> getter, Consumer<String> setter) {
105 String originalValue = getter.get();
109 assertThatIllegalArgumentException().isThrownBy(() -> new VfModuleCreate(params, config))
110 .withMessageContaining(expectedText);
112 setter.accept(originalValue);
116 public void testStartPreprocessorAsync() {
117 assertNotNull(oper.startPreprocessorAsync());
121 public void testObtainVfCount_testGetVfCount_testSetVfCount() throws Exception {
122 // insert CQ data so it's there for the check
123 context.setProperty(AaiCqResponse.CONTEXT_KEY, makeCqResponse());
125 // shouldn't actually need to do anything
126 assertNull(oper.obtainVfCount());
128 // verify that the count was stored
129 Integer vfcount = context.getProperty(VF_COUNT_KEY);
130 assertEquals(VF_COUNT, vfcount);
131 assertEquals(VF_COUNT.intValue(), oper.getVfCount());
133 // change the count and then verify that it isn't overwritten by another call
134 oper.setVfCount(VF_COUNT + 1);
136 assertNull(oper.obtainVfCount());
137 vfcount = context.getProperty(VF_COUNT_KEY);
138 assertEquals(VF_COUNT + 1, vfcount.intValue());
139 assertEquals(VF_COUNT + 1, oper.getVfCount());
143 * Tests obtainVfCount() when it actually has to query.
146 public void testObtainVfCountQuery() throws Exception {
147 CompletableFuture<OperationOutcome> future2 = oper.obtainVfCount();
148 assertNotNull(future2);
149 assertTrue(executor.runAll(100));
152 assertFalse(future2.isDone());
154 provideCqResponse(makeCqResponse());
156 assertTrue(executor.runAll(100));
157 assertTrue(future2.isDone());
158 assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
160 // verify that the count was stored
161 Integer vfcount = context.getProperty(VF_COUNT_KEY);
162 assertEquals(VF_COUNT, vfcount);
164 // repeat - shouldn't need to do anything now
165 assertNull(oper.obtainVfCount());
169 public void testGetRequestState() {
170 SoResponse resp = new SoResponse();
171 assertNull(oper.getRequestState(resp));
173 SoRequest req = new SoRequest();
174 resp.setRequest(req);
175 assertNull(oper.getRequestState(resp));
177 SoRequestStatus status = new SoRequestStatus();
178 req.setRequestStatus(status);
179 assertNull(oper.getRequestState(resp));
181 status.setRequestState("my-state");
182 assertEquals("my-state", oper.getRequestState(resp));
186 public void testIsSuccess() {
189 assertTrue(oper.isSuccess(rawResponse, response));
191 when(rawResponse.getStatus()).thenReturn(500);
192 assertTrue(oper.isSuccess(rawResponse, response));
196 public void testSetOutcome() {
198 when(rawResponse.getStatus()).thenReturn(200);
199 assertSame(outcome, oper.setOutcome(outcome, PolicyResult.SUCCESS, rawResponse, response));
201 assertEquals(PolicyResult.SUCCESS, outcome.getResult());
202 assertEquals("200 " + ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
203 assertSame(response, outcome.getResponse());
206 when(rawResponse.getStatus()).thenReturn(500);
207 assertSame(outcome, oper.setOutcome(outcome, PolicyResult.FAILURE, rawResponse, response));
209 assertEquals(PolicyResult.FAILURE, outcome.getResult());
210 assertEquals("500 " + ControlLoopOperation.FAILED_MSG, outcome.getMessage());
211 assertSame(response, outcome.getResponse());
215 public void testPrepareSoModelInfo() throws CoderException {
216 verifyMissingModelInfo(target::getModelCustomizationId, target::setModelCustomizationId);
217 verifyMissingModelInfo(target::getModelInvariantId, target::setModelInvariantId);
218 verifyMissingModelInfo(target::getModelName, target::setModelName);
219 verifyMissingModelInfo(target::getModelVersion, target::setModelVersion);
220 verifyMissingModelInfo(target::getModelVersionId, target::setModelVersionId);
223 SoModelInfo info = oper.prepareSoModelInfo();
224 verifyRequest("model.json", info);
226 // try with null target
227 params = params.toBuilder().target(null).build();
228 assertThatIllegalArgumentException().isThrownBy(() -> new SoOperation(params, config, PROP_NAMES) {})
229 .withMessageContaining("missing Target");
232 private void verifyMissingModelInfo(Supplier<String> getter, Consumer<String> setter) {
233 String original = getter.get();
236 assertThatIllegalArgumentException().isThrownBy(() -> oper.prepareSoModelInfo())
237 .withMessage("missing VF Module model");
239 setter.accept(original);
243 public void testConstructRequestInfo() throws CoderException {
244 SoRequestInfo info = oper.constructRequestInfo();
245 verifyRequest("reqinfo.json", info);
249 public void testBuildRequestParameters() throws CoderException {
251 verifyRequest("reqparams.json", oper.buildRequestParameters().get());
254 params.getPayload().put(SoOperation.REQ_PARAM_NM, "{invalid json");
255 assertThatIllegalArgumentException().isThrownBy(() -> oper.buildRequestParameters())
256 .withMessage("invalid payload value: " + SoOperation.REQ_PARAM_NM);
259 params.getPayload().remove(SoOperation.REQ_PARAM_NM);
260 assertTrue(oper.buildRequestParameters().isEmpty());
263 params = params.toBuilder().payload(null).build();
264 oper = new SoOperation(params, config, PROP_NAMES) {};
265 assertTrue(oper.buildRequestParameters().isEmpty());
269 public void testBuildConfigurationParameters() {
271 assertEquals(List.of(Collections.emptyMap()), oper.buildConfigurationParameters().get());
274 params.getPayload().put(SoOperation.CONFIG_PARAM_NM, "{invalid json");
275 assertThatIllegalArgumentException().isThrownBy(() -> oper.buildConfigurationParameters())
276 .withMessage("invalid payload value: " + SoOperation.CONFIG_PARAM_NM);
279 params.getPayload().remove(SoOperation.CONFIG_PARAM_NM);
280 assertTrue(oper.buildConfigurationParameters().isEmpty());
283 params = params.toBuilder().payload(null).build();
284 oper = new SoOperation(params, config, PROP_NAMES) {};
285 assertTrue(oper.buildConfigurationParameters().isEmpty());
289 public void testGetVnfItem() {
291 AaiCqResponse cq = mock(AaiCqResponse.class);
292 assertThatIllegalArgumentException().isThrownBy(() -> oper.getVnfItem(cq, oper.prepareSoModelInfo()))
293 .withMessage("missing generic VNF");
296 GenericVnf vnf = new GenericVnf();
297 when(cq.getGenericVnfByVfModuleModelInvariantId(MODEL_INVAR_ID)).thenReturn(vnf);
298 assertSame(vnf, oper.getVnfItem(cq, oper.prepareSoModelInfo()));
302 public void testGetServiceInstance() {
304 AaiCqResponse cq = mock(AaiCqResponse.class);
305 assertThatIllegalArgumentException().isThrownBy(() -> oper.getServiceInstance(cq))
306 .withMessage("missing VNF Service Item");
309 ServiceInstance instance = new ServiceInstance();
310 when(cq.getServiceInstance()).thenReturn(instance);
311 assertSame(instance, oper.getServiceInstance(cq));
315 public void testGetDefaultTenant() {
317 AaiCqResponse cq = mock(AaiCqResponse.class);
318 assertThatIllegalArgumentException().isThrownBy(() -> oper.getDefaultTenant(cq))
319 .withMessage("missing Tenant Item");
322 Tenant tenant = new Tenant();
323 when(cq.getDefaultTenant()).thenReturn(tenant);
324 assertSame(tenant, oper.getDefaultTenant(cq));
328 public void testGetDefaultCloudRegion() {
330 AaiCqResponse cq = mock(AaiCqResponse.class);
331 assertThatIllegalArgumentException().isThrownBy(() -> oper.getDefaultCloudRegion(cq))
332 .withMessage("missing Cloud Region");
335 CloudRegion region = new CloudRegion();
336 when(cq.getDefaultCloudRegion()).thenReturn(region);
337 assertSame(region, oper.getDefaultCloudRegion(cq));
341 public void testGetCoder() throws CoderException {
342 Coder opcoder = oper.getCoder();
344 // ensure we can decode an SO timestamp
345 String json = "{'request':{'finishTime':'Fri, 15 May 2020 12:14:21 GMT'}}";
346 SoResponse resp = opcoder.decode(json.replace('\'', '"'), SoResponse.class);
348 LocalDateTime tfinish = resp.getRequest().getFinishTime();
349 assertNotNull(tfinish);
350 assertEquals(2020, tfinish.getYear());
351 assertEquals(Month.MAY, tfinish.getMonth());