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