2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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.policy.common.utils.coder.Coder;
41 import org.onap.policy.common.utils.coder.CoderException;
42 import org.onap.policy.controlloop.ControlLoopOperation;
43 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
44 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
45 import org.onap.policy.so.SoModelInfo;
46 import org.onap.policy.so.SoRequest;
47 import org.onap.policy.so.SoRequestInfo;
48 import org.onap.policy.so.SoRequestStatus;
49 import org.onap.policy.so.SoResponse;
51 public class SoOperationTest extends BasicSoOperation {
53 private static final List<String> PROP_NAMES = Collections.emptyList();
55 private SoOperation oper;
62 public void setUp() throws Exception {
67 oper = new SoOperation(params, config, PROP_NAMES, params.getTargetEntityIds()) {};
71 public void testConstructor() {
72 assertEquals(DEFAULT_ACTOR, oper.getActorName());
73 assertEquals(DEFAULT_OPERATION, oper.getName());
74 assertSame(config, oper.getConfig());
76 // check when Target is null
77 params = params.toBuilder().targetType(null).build();
78 assertThatIllegalArgumentException().isThrownBy(() -> new SoOperation(params, config, PROP_NAMES) {})
79 .withMessageContaining("Target information");
83 public void testValidateTarget() {
84 // check when various fields are null
85 verifyNotNull(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_CUSTOMIZATION_ID, targetEntities);
86 verifyNotNull(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_INVARIANT_ID, targetEntities);
87 verifyNotNull(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_VERSION_ID, targetEntities);
89 // verify it's still valid
90 assertThatCode(() -> new VfModuleCreate(params, config)).doesNotThrowAnyException();
93 private void verifyNotNull(String expectedText, Map<String, String> targetEntities) {
94 String originalValue = targetEntities.get(expectedText);
97 targetEntities.put(expectedText, null);
98 assertThatIllegalArgumentException().isThrownBy(() -> new VfModuleCreate(params, config))
99 .withMessageContaining(expectedText);
101 targetEntities.put(expectedText, originalValue);
105 public void testGetRequestState() {
106 SoResponse resp = new SoResponse();
107 assertNull(oper.getRequestState(resp));
109 SoRequest req = new SoRequest();
110 resp.setRequest(req);
111 assertNull(oper.getRequestState(resp));
113 SoRequestStatus status = new SoRequestStatus();
114 req.setRequestStatus(status);
115 assertNull(oper.getRequestState(resp));
117 status.setRequestState("my-state");
118 assertEquals("my-state", oper.getRequestState(resp));
122 public void testIsSuccess() {
125 assertTrue(oper.isSuccess(rawResponse, response));
127 when(rawResponse.getStatus()).thenReturn(500);
128 assertTrue(oper.isSuccess(rawResponse, response));
132 public void testSetOutcome() {
134 when(rawResponse.getStatus()).thenReturn(200);
135 assertSame(outcome, oper.setOutcome(outcome, OperationResult.SUCCESS, rawResponse, response));
137 assertEquals(OperationResult.SUCCESS, outcome.getResult());
138 assertEquals("200 " + ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
139 assertSame(response, outcome.getResponse());
142 when(rawResponse.getStatus()).thenReturn(500);
143 assertSame(outcome, oper.setOutcome(outcome, OperationResult.FAILURE, rawResponse, response));
145 assertEquals(OperationResult.FAILURE, outcome.getResult());
146 assertEquals("500 " + ControlLoopOperation.FAILED_MSG, outcome.getMessage());
147 assertSame(response, outcome.getResponse());
151 public void testPrepareSoModelInfo() throws CoderException {
153 SoModelInfo info = oper.prepareSoModelInfo();
154 verifyRequest("model.json", info);
156 // try with null target
157 params = params.toBuilder().targetType(null).build();
158 assertThatIllegalArgumentException().isThrownBy(() -> new SoOperation(params, config, PROP_NAMES) {})
159 .withMessageContaining("missing Target");
163 public void testConstructRequestInfo() throws CoderException {
164 SoRequestInfo info = oper.constructRequestInfo();
165 verifyRequest("reqinfo.json", info);
169 public void testBuildRequestParameters() throws CoderException {
171 verifyRequest("reqparams.json", oper.buildRequestParameters().get());
174 params.getPayload().put(SoOperation.REQ_PARAM_NM, "{invalid json");
175 assertThatIllegalArgumentException().isThrownBy(() -> oper.buildRequestParameters())
176 .withMessage("invalid payload value: " + SoOperation.REQ_PARAM_NM);
179 params.getPayload().remove(SoOperation.REQ_PARAM_NM);
180 assertTrue(oper.buildRequestParameters().isEmpty());
183 params = params.toBuilder().payload(null).build();
184 oper = new SoOperation(params, config, PROP_NAMES) {};
185 assertTrue(oper.buildRequestParameters().isEmpty());
189 public void testBuildConfigurationParameters() {
191 assertEquals(List.of(Collections.emptyMap()), oper.buildConfigurationParameters().get());
194 params.getPayload().put(SoOperation.CONFIG_PARAM_NM, "{invalid json");
195 assertThatIllegalArgumentException().isThrownBy(() -> oper.buildConfigurationParameters())
196 .withMessage("invalid payload value: " + SoOperation.CONFIG_PARAM_NM);
199 params.getPayload().remove(SoOperation.CONFIG_PARAM_NM);
200 assertTrue(oper.buildConfigurationParameters().isEmpty());
203 params = params.toBuilder().payload(null).build();
204 oper = new SoOperation(params, config, PROP_NAMES) {};
205 assertTrue(oper.buildConfigurationParameters().isEmpty());
209 public void testGetCoder() throws CoderException {
210 Coder opcoder = oper.getCoder();
212 // ensure we can decode an SO timestamp
213 String json = "{'request':{'finishTime':'Fri, 15 May 2020 12:14:21 GMT'}}";
214 SoResponse resp = opcoder.decode(json.replace('\'', '"'), SoResponse.class);
216 LocalDateTime tfinish = resp.getRequest().getFinishTime();
217 assertNotNull(tfinish);
218 assertEquals(2020, tfinish.getYear());
219 assertEquals(Month.MAY, tfinish.getMonth());