2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2020 Wipro Limited.
7 * Modifications Copyright (C) 2023 Nordix Foundation.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.controlloop.actor.so;
25 import static org.assertj.core.api.Assertions.assertThatCode;
26 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertNull;
30 import static org.junit.Assert.assertSame;
31 import static org.junit.Assert.assertTrue;
32 import static org.mockito.Mockito.lenient;
34 import java.time.LocalDateTime;
35 import java.time.Month;
36 import java.util.Collections;
37 import java.util.List;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.junit.MockitoJUnitRunner;
43 import org.onap.aai.domain.yang.CloudRegion;
44 import org.onap.aai.domain.yang.Tenant;
45 import org.onap.policy.common.utils.coder.Coder;
46 import org.onap.policy.common.utils.coder.CoderException;
47 import org.onap.policy.controlloop.ControlLoopOperation;
48 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
49 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
50 import org.onap.policy.so.SoModelInfo;
51 import org.onap.policy.so.SoRequest;
52 import org.onap.policy.so.SoRequestInfo;
53 import org.onap.policy.so.SoRequestStatus;
54 import org.onap.policy.so.SoResponse;
56 @RunWith(MockitoJUnitRunner.class)
57 public class SoOperationTest extends BasicSoOperation {
59 private static final List<String> PROP_NAMES = Collections.emptyList();
61 private SoOperation oper;
68 public void setUp() throws Exception {
73 oper = new SoOperation(params, config, PROP_NAMES, params.getTargetEntityIds()) {};
77 public void testConstructor() {
78 assertEquals(DEFAULT_ACTOR, oper.getActorName());
79 assertEquals(DEFAULT_OPERATION, oper.getName());
80 assertSame(config, oper.getConfig());
82 // check when Target is null
83 params = params.toBuilder().targetType(null).build();
84 assertThatIllegalArgumentException().isThrownBy(() -> new SoOperation(params, config, PROP_NAMES) {})
85 .withMessageContaining("Target information");
89 public void testValidateTarget() {
90 // check when various fields are null
91 verifyNotNull(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_CUSTOMIZATION_ID, targetEntities);
92 verifyNotNull(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_INVARIANT_ID, targetEntities);
93 verifyNotNull(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_VERSION_ID, targetEntities);
95 // verify it's still valid
96 assertThatCode(() -> new VfModuleCreate(params, config)).doesNotThrowAnyException();
99 private void verifyNotNull(String expectedText, Map<String, String> targetEntities) {
100 String originalValue = targetEntities.get(expectedText);
103 targetEntities.put(expectedText, null);
104 assertThatIllegalArgumentException().isThrownBy(() -> new VfModuleCreate(params, config))
105 .withMessageContaining(expectedText);
107 targetEntities.put(expectedText, originalValue);
111 public void testGetRequestState() {
112 SoResponse resp = new SoResponse();
113 assertNull(oper.getRequestState(resp));
115 SoRequest req = new SoRequest();
116 resp.setRequest(req);
117 assertNull(oper.getRequestState(resp));
119 SoRequestStatus status = new SoRequestStatus();
120 req.setRequestStatus(status);
121 assertNull(oper.getRequestState(resp));
123 status.setRequestState("my-state");
124 assertEquals("my-state", oper.getRequestState(resp));
128 public void testIsSuccess() {
131 assertTrue(oper.isSuccess(rawResponse, response));
133 lenient().when(rawResponse.getStatus()).thenReturn(500);
134 assertTrue(oper.isSuccess(rawResponse, response));
138 public void testSetOutcome() {
140 lenient().when(rawResponse.getStatus()).thenReturn(200);
141 assertSame(outcome, oper.setOutcome(outcome, OperationResult.SUCCESS, rawResponse, response));
143 assertEquals(OperationResult.SUCCESS, outcome.getResult());
144 assertEquals("200 " + ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
145 assertSame(response, outcome.getResponse());
148 lenient().when(rawResponse.getStatus()).thenReturn(500);
149 assertSame(outcome, oper.setOutcome(outcome, OperationResult.FAILURE, rawResponse, response));
151 assertEquals(OperationResult.FAILURE, outcome.getResult());
152 assertEquals("500 " + ControlLoopOperation.FAILED_MSG, outcome.getMessage());
153 assertSame(response, outcome.getResponse());
157 public void testPrepareSoModelInfo() throws CoderException {
159 SoModelInfo info = oper.prepareSoModelInfo();
160 verifyRequest("model.json", info);
162 // try with null target
163 params = params.toBuilder().targetType(null).build();
164 assertThatIllegalArgumentException().isThrownBy(() -> new SoOperation(params, config, PROP_NAMES) {})
165 .withMessageContaining("missing Target");
169 public void testConstructRequestInfo() throws CoderException {
170 SoRequestInfo info = oper.constructRequestInfo();
171 verifyRequest("reqinfo.json", info);
175 public void testBuildRequestParameters() throws CoderException {
177 verifyRequest("reqparams.json", oper.buildRequestParameters().get());
180 params.getPayload().put(SoOperation.REQ_PARAM_NM, "{invalid json");
181 assertThatIllegalArgumentException().isThrownBy(() -> oper.buildRequestParameters())
182 .withMessage("invalid payload value: " + SoOperation.REQ_PARAM_NM);
185 params.getPayload().remove(SoOperation.REQ_PARAM_NM);
186 assertTrue(oper.buildRequestParameters().isEmpty());
189 params = params.toBuilder().payload(null).build();
190 oper = new SoOperation(params, config, PROP_NAMES) {};
191 assertTrue(oper.buildRequestParameters().isEmpty());
195 public void testBuildConfigurationParameters() {
197 assertEquals(List.of(Collections.emptyMap()), oper.buildConfigurationParameters().get());
200 params.getPayload().put(SoOperation.CONFIG_PARAM_NM, "{invalid json");
201 assertThatIllegalArgumentException().isThrownBy(() -> oper.buildConfigurationParameters())
202 .withMessage("invalid payload value: " + SoOperation.CONFIG_PARAM_NM);
205 params.getPayload().remove(SoOperation.CONFIG_PARAM_NM);
206 assertTrue(oper.buildConfigurationParameters().isEmpty());
209 params = params.toBuilder().payload(null).build();
210 oper = new SoOperation(params, config, PROP_NAMES) {};
211 assertTrue(oper.buildConfigurationParameters().isEmpty());
215 public void testConstructCloudConfiguration() throws Exception {
216 Tenant tenantItem = new Tenant();
217 tenantItem.setTenantId("my-tenant-id");
219 CloudRegion cloudRegionItem = new CloudRegion();
220 cloudRegionItem.setCloudRegionId("my-cloud-id");
222 assertThatCode(() -> oper.constructCloudConfiguration(tenantItem, cloudRegionItem)).doesNotThrowAnyException();
224 tenantItem.setTenantId(null);
225 assertThatIllegalArgumentException()
226 .isThrownBy(() -> oper.constructCloudConfiguration(tenantItem, cloudRegionItem))
227 .withMessageContaining("missing tenant ID");
228 tenantItem.setTenantId("my-tenant-id");
230 cloudRegionItem.setCloudRegionId(null);
231 assertThatIllegalArgumentException()
232 .isThrownBy(() -> oper.constructCloudConfiguration(tenantItem, cloudRegionItem))
233 .withMessageContaining("missing cloud region ID");
234 cloudRegionItem.setCloudRegionId("my-cloud-id");
238 public void testGetRequiredText() throws Exception {
240 assertThatCode(() -> oper.getRequiredText("some value", "my value")).doesNotThrowAnyException();
242 assertThatIllegalArgumentException().isThrownBy(() -> oper.getRequiredText("some value", null))
243 .withMessageContaining("missing some value");
247 public void testGetCoder() throws CoderException {
248 Coder opcoder = oper.getCoder();
250 // ensure we can decode an SO timestamp
251 String json = "{'request':{'finishTime':'Fri, 15 May 2020 12:14:21 GMT'}}";
252 SoResponse resp = opcoder.decode(json.replace('\'', '"'), SoResponse.class);
254 LocalDateTime tfinish = resp.getRequest().getFinishTime();
255 assertNotNull(tfinish);
256 assertEquals(2020, tfinish.getYear());
257 assertEquals(Month.MAY, tfinish.getMonth());