2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2020 Wipro Limited.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.actor.so;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
26 import static org.junit.Assert.assertEquals;
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.when;
33 import java.time.LocalDateTime;
34 import java.time.Month;
35 import java.util.Collections;
36 import java.util.List;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.aai.domain.yang.CloudRegion;
41 import org.onap.aai.domain.yang.Tenant;
42 import org.onap.policy.common.utils.coder.Coder;
43 import org.onap.policy.common.utils.coder.CoderException;
44 import org.onap.policy.controlloop.ControlLoopOperation;
45 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
46 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
47 import org.onap.policy.so.SoModelInfo;
48 import org.onap.policy.so.SoRequest;
49 import org.onap.policy.so.SoRequestInfo;
50 import org.onap.policy.so.SoRequestStatus;
51 import org.onap.policy.so.SoResponse;
53 public class SoOperationTest extends BasicSoOperation {
55 private static final List<String> PROP_NAMES = Collections.emptyList();
57 private SoOperation oper;
64 public void setUp() throws Exception {
69 oper = new SoOperation(params, config, PROP_NAMES, params.getTargetEntityIds()) {};
73 public void testConstructor() {
74 assertEquals(DEFAULT_ACTOR, oper.getActorName());
75 assertEquals(DEFAULT_OPERATION, oper.getName());
76 assertSame(config, oper.getConfig());
78 // check when Target is null
79 params = params.toBuilder().targetType(null).build();
80 assertThatIllegalArgumentException().isThrownBy(() -> new SoOperation(params, config, PROP_NAMES) {})
81 .withMessageContaining("Target information");
85 public void testValidateTarget() {
86 // check when various fields are null
87 verifyNotNull(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_CUSTOMIZATION_ID, targetEntities);
88 verifyNotNull(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_INVARIANT_ID, targetEntities);
89 verifyNotNull(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_VERSION_ID, targetEntities);
91 // verify it's still valid
92 assertThatCode(() -> new VfModuleCreate(params, config)).doesNotThrowAnyException();
95 private void verifyNotNull(String expectedText, Map<String, String> targetEntities) {
96 String originalValue = targetEntities.get(expectedText);
99 targetEntities.put(expectedText, null);
100 assertThatIllegalArgumentException().isThrownBy(() -> new VfModuleCreate(params, config))
101 .withMessageContaining(expectedText);
103 targetEntities.put(expectedText, originalValue);
107 public void testGetRequestState() {
108 SoResponse resp = new SoResponse();
109 assertNull(oper.getRequestState(resp));
111 SoRequest req = new SoRequest();
112 resp.setRequest(req);
113 assertNull(oper.getRequestState(resp));
115 SoRequestStatus status = new SoRequestStatus();
116 req.setRequestStatus(status);
117 assertNull(oper.getRequestState(resp));
119 status.setRequestState("my-state");
120 assertEquals("my-state", oper.getRequestState(resp));
124 public void testIsSuccess() {
127 assertTrue(oper.isSuccess(rawResponse, response));
129 when(rawResponse.getStatus()).thenReturn(500);
130 assertTrue(oper.isSuccess(rawResponse, response));
134 public void testSetOutcome() {
136 when(rawResponse.getStatus()).thenReturn(200);
137 assertSame(outcome, oper.setOutcome(outcome, OperationResult.SUCCESS, rawResponse, response));
139 assertEquals(OperationResult.SUCCESS, outcome.getResult());
140 assertEquals("200 " + ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
141 assertSame(response, outcome.getResponse());
144 when(rawResponse.getStatus()).thenReturn(500);
145 assertSame(outcome, oper.setOutcome(outcome, OperationResult.FAILURE, rawResponse, response));
147 assertEquals(OperationResult.FAILURE, outcome.getResult());
148 assertEquals("500 " + ControlLoopOperation.FAILED_MSG, outcome.getMessage());
149 assertSame(response, outcome.getResponse());
153 public void testPrepareSoModelInfo() throws CoderException {
155 SoModelInfo info = oper.prepareSoModelInfo();
156 verifyRequest("model.json", info);
158 // try with null target
159 params = params.toBuilder().targetType(null).build();
160 assertThatIllegalArgumentException().isThrownBy(() -> new SoOperation(params, config, PROP_NAMES) {})
161 .withMessageContaining("missing Target");
165 public void testConstructRequestInfo() throws CoderException {
166 SoRequestInfo info = oper.constructRequestInfo();
167 verifyRequest("reqinfo.json", info);
171 public void testBuildRequestParameters() throws CoderException {
173 verifyRequest("reqparams.json", oper.buildRequestParameters().get());
176 params.getPayload().put(SoOperation.REQ_PARAM_NM, "{invalid json");
177 assertThatIllegalArgumentException().isThrownBy(() -> oper.buildRequestParameters())
178 .withMessage("invalid payload value: " + SoOperation.REQ_PARAM_NM);
181 params.getPayload().remove(SoOperation.REQ_PARAM_NM);
182 assertTrue(oper.buildRequestParameters().isEmpty());
185 params = params.toBuilder().payload(null).build();
186 oper = new SoOperation(params, config, PROP_NAMES) {};
187 assertTrue(oper.buildRequestParameters().isEmpty());
191 public void testBuildConfigurationParameters() {
193 assertEquals(List.of(Collections.emptyMap()), oper.buildConfigurationParameters().get());
196 params.getPayload().put(SoOperation.CONFIG_PARAM_NM, "{invalid json");
197 assertThatIllegalArgumentException().isThrownBy(() -> oper.buildConfigurationParameters())
198 .withMessage("invalid payload value: " + SoOperation.CONFIG_PARAM_NM);
201 params.getPayload().remove(SoOperation.CONFIG_PARAM_NM);
202 assertTrue(oper.buildConfigurationParameters().isEmpty());
205 params = params.toBuilder().payload(null).build();
206 oper = new SoOperation(params, config, PROP_NAMES) {};
207 assertTrue(oper.buildConfigurationParameters().isEmpty());
211 public void testConstructCloudConfiguration() throws Exception {
212 Tenant tenantItem = new Tenant();
213 tenantItem.setTenantId("my-tenant-id");
215 CloudRegion cloudRegionItem = new CloudRegion();
216 cloudRegionItem.setCloudRegionId("my-cloud-id");
218 assertThatCode(() -> oper.constructCloudConfiguration(tenantItem, cloudRegionItem)).doesNotThrowAnyException();
220 tenantItem.setTenantId(null);
221 assertThatIllegalArgumentException()
222 .isThrownBy(() -> oper.constructCloudConfiguration(tenantItem, cloudRegionItem))
223 .withMessageContaining("missing tenant ID");
224 tenantItem.setTenantId("my-tenant-id");
226 cloudRegionItem.setCloudRegionId(null);
227 assertThatIllegalArgumentException()
228 .isThrownBy(() -> oper.constructCloudConfiguration(tenantItem, cloudRegionItem))
229 .withMessageContaining("missing cloud region ID");
230 cloudRegionItem.setCloudRegionId("my-cloud-id");
234 public void testGetRequiredText() throws Exception {
236 assertThatCode(() -> oper.getRequiredText("some value", "my value")).doesNotThrowAnyException();
238 assertThatIllegalArgumentException().isThrownBy(() -> oper.getRequiredText("some value", null))
239 .withMessageContaining("missing some value");
243 public void testGetCoder() throws CoderException {
244 Coder opcoder = oper.getCoder();
246 // ensure we can decode an SO timestamp
247 String json = "{'request':{'finishTime':'Fri, 15 May 2020 12:14:21 GMT'}}";
248 SoResponse resp = opcoder.decode(json.replace('\'', '"'), SoResponse.class);
250 LocalDateTime tfinish = resp.getRequest().getFinishTime();
251 assertNotNull(tfinish);
252 assertEquals(2020, tfinish.getYear());
253 assertEquals(Month.MAY, tfinish.getMonth());