50bbfee2bdf3cdc18e432589bdfe9c9269036b64
[policy/models.git] / models-interactions / model-actors / actor.so / src / test / java / org / onap / policy / controlloop / actor / so / SoOperationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.controlloop.actor.so;
23
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;
32
33 import java.time.LocalDateTime;
34 import java.time.Month;
35 import java.util.Collections;
36 import java.util.List;
37 import java.util.Map;
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;
52
53 public class SoOperationTest extends BasicSoOperation {
54
55     private static final List<String> PROP_NAMES = Collections.emptyList();
56
57     private SoOperation oper;
58
59     /**
60      * Sets up.
61      */
62     @Override
63     @Before
64     public void setUp() throws Exception {
65         super.setUp();
66
67         initConfig();
68
69         oper = new SoOperation(params, config, PROP_NAMES, params.getTargetEntityIds()) {};
70     }
71
72     @Test
73     public void testConstructor() {
74         assertEquals(DEFAULT_ACTOR, oper.getActorName());
75         assertEquals(DEFAULT_OPERATION, oper.getName());
76         assertSame(config, oper.getConfig());
77
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");
82     }
83
84     @Test
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);
90
91         // verify it's still valid
92         assertThatCode(() -> new VfModuleCreate(params, config)).doesNotThrowAnyException();
93     }
94
95     private void verifyNotNull(String expectedText, Map<String, String> targetEntities) {
96         String originalValue = targetEntities.get(expectedText);
97
98         // try with null
99         targetEntities.put(expectedText, null);
100         assertThatIllegalArgumentException().isThrownBy(() -> new VfModuleCreate(params, config))
101                         .withMessageContaining(expectedText);
102
103         targetEntities.put(expectedText, originalValue);
104     }
105
106     @Test
107     public void testGetRequestState() {
108         SoResponse resp = new SoResponse();
109         assertNull(oper.getRequestState(resp));
110
111         SoRequest req = new SoRequest();
112         resp.setRequest(req);
113         assertNull(oper.getRequestState(resp));
114
115         SoRequestStatus status = new SoRequestStatus();
116         req.setRequestStatus(status);
117         assertNull(oper.getRequestState(resp));
118
119         status.setRequestState("my-state");
120         assertEquals("my-state", oper.getRequestState(resp));
121     }
122
123     @Test
124     public void testIsSuccess() {
125         // always true
126
127         assertTrue(oper.isSuccess(rawResponse, response));
128
129         when(rawResponse.getStatus()).thenReturn(500);
130         assertTrue(oper.isSuccess(rawResponse, response));
131     }
132
133     @Test
134     public void testSetOutcome() {
135         // success case
136         when(rawResponse.getStatus()).thenReturn(200);
137         assertSame(outcome, oper.setOutcome(outcome, OperationResult.SUCCESS, rawResponse, response));
138
139         assertEquals(OperationResult.SUCCESS, outcome.getResult());
140         assertEquals("200 " + ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
141         assertSame(response, outcome.getResponse());
142
143         // failure case
144         when(rawResponse.getStatus()).thenReturn(500);
145         assertSame(outcome, oper.setOutcome(outcome, OperationResult.FAILURE, rawResponse, response));
146
147         assertEquals(OperationResult.FAILURE, outcome.getResult());
148         assertEquals("500 " + ControlLoopOperation.FAILED_MSG, outcome.getMessage());
149         assertSame(response, outcome.getResponse());
150     }
151
152     @Test
153     public void testPrepareSoModelInfo() throws CoderException {
154         // valid data
155         SoModelInfo info = oper.prepareSoModelInfo();
156         verifyRequest("model.json", info);
157
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");
162     }
163
164     @Test
165     public void testConstructRequestInfo() throws CoderException {
166         SoRequestInfo info = oper.constructRequestInfo();
167         verifyRequest("reqinfo.json", info);
168     }
169
170     @Test
171     public void testBuildRequestParameters() throws CoderException {
172         // valid data
173         verifyRequest("reqparams.json", oper.buildRequestParameters().get());
174
175         // invalid json
176         params.getPayload().put(SoOperation.REQ_PARAM_NM, "{invalid json");
177         assertThatIllegalArgumentException().isThrownBy(() -> oper.buildRequestParameters())
178                         .withMessage("invalid payload value: " + SoOperation.REQ_PARAM_NM);
179
180         // missing data
181         params.getPayload().remove(SoOperation.REQ_PARAM_NM);
182         assertTrue(oper.buildRequestParameters().isEmpty());
183
184         // null payload
185         params = params.toBuilder().payload(null).build();
186         oper = new SoOperation(params, config, PROP_NAMES) {};
187         assertTrue(oper.buildRequestParameters().isEmpty());
188     }
189
190     @Test
191     public void testBuildConfigurationParameters() {
192         // valid data
193         assertEquals(List.of(Collections.emptyMap()), oper.buildConfigurationParameters().get());
194
195         // invalid json
196         params.getPayload().put(SoOperation.CONFIG_PARAM_NM, "{invalid json");
197         assertThatIllegalArgumentException().isThrownBy(() -> oper.buildConfigurationParameters())
198                         .withMessage("invalid payload value: " + SoOperation.CONFIG_PARAM_NM);
199
200         // missing data
201         params.getPayload().remove(SoOperation.CONFIG_PARAM_NM);
202         assertTrue(oper.buildConfigurationParameters().isEmpty());
203
204         // null payload
205         params = params.toBuilder().payload(null).build();
206         oper = new SoOperation(params, config, PROP_NAMES) {};
207         assertTrue(oper.buildConfigurationParameters().isEmpty());
208     }
209
210     @Test
211     public void testConstructCloudConfiguration() throws Exception {
212         Tenant tenantItem = new Tenant();
213         tenantItem.setTenantId("my-tenant-id");
214
215         CloudRegion cloudRegionItem = new CloudRegion();
216         cloudRegionItem.setCloudRegionId("my-cloud-id");
217
218         assertThatCode(() -> oper.constructCloudConfiguration(tenantItem, cloudRegionItem)).doesNotThrowAnyException();
219
220         tenantItem.setTenantId(null);
221         assertThatIllegalArgumentException()
222                         .isThrownBy(() -> oper.constructCloudConfiguration(tenantItem, cloudRegionItem))
223                         .withMessageContaining("missing tenant ID");
224         tenantItem.setTenantId("my-tenant-id");
225
226         cloudRegionItem.setCloudRegionId(null);
227         assertThatIllegalArgumentException()
228                         .isThrownBy(() -> oper.constructCloudConfiguration(tenantItem, cloudRegionItem))
229                         .withMessageContaining("missing cloud region ID");
230         cloudRegionItem.setCloudRegionId("my-cloud-id");
231     }
232
233     @Test
234     public void testGetRequiredText() throws Exception {
235
236         assertThatCode(() -> oper.getRequiredText("some value", "my value")).doesNotThrowAnyException();
237
238         assertThatIllegalArgumentException().isThrownBy(() -> oper.getRequiredText("some value", null))
239                         .withMessageContaining("missing some value");
240     }
241
242     @Test
243     public void testGetCoder() throws CoderException {
244         Coder opcoder = oper.getCoder();
245
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);
249
250         LocalDateTime tfinish = resp.getRequest().getFinishTime();
251         assertNotNull(tfinish);
252         assertEquals(2020, tfinish.getYear());
253         assertEquals(Month.MAY, tfinish.getMonth());
254     }
255 }