Make Actors event-agnostic
[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 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.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;
50
51 public class SoOperationTest extends BasicSoOperation {
52
53     private static final List<String> PROP_NAMES = Collections.emptyList();
54
55     private SoOperation oper;
56
57     /**
58      * Sets up.
59      */
60     @Override
61     @Before
62     public void setUp() throws Exception {
63         super.setUp();
64
65         initConfig();
66
67         oper = new SoOperation(params, config, PROP_NAMES, params.getTargetEntityIds()) {};
68     }
69
70     @Test
71     public void testConstructor() {
72         assertEquals(DEFAULT_ACTOR, oper.getActorName());
73         assertEquals(DEFAULT_OPERATION, oper.getName());
74         assertSame(config, oper.getConfig());
75
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");
80     }
81
82     @Test
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);
88
89         // verify it's still valid
90         assertThatCode(() -> new VfModuleCreate(params, config)).doesNotThrowAnyException();
91     }
92
93     private void verifyNotNull(String expectedText, Map<String, String> targetEntities) {
94         String originalValue = targetEntities.get(expectedText);
95
96         // try with null
97         targetEntities.put(expectedText, null);
98         assertThatIllegalArgumentException().isThrownBy(() -> new VfModuleCreate(params, config))
99                         .withMessageContaining(expectedText);
100
101         targetEntities.put(expectedText, originalValue);
102     }
103
104     @Test
105     public void testGetRequestState() {
106         SoResponse resp = new SoResponse();
107         assertNull(oper.getRequestState(resp));
108
109         SoRequest req = new SoRequest();
110         resp.setRequest(req);
111         assertNull(oper.getRequestState(resp));
112
113         SoRequestStatus status = new SoRequestStatus();
114         req.setRequestStatus(status);
115         assertNull(oper.getRequestState(resp));
116
117         status.setRequestState("my-state");
118         assertEquals("my-state", oper.getRequestState(resp));
119     }
120
121     @Test
122     public void testIsSuccess() {
123         // always true
124
125         assertTrue(oper.isSuccess(rawResponse, response));
126
127         when(rawResponse.getStatus()).thenReturn(500);
128         assertTrue(oper.isSuccess(rawResponse, response));
129     }
130
131     @Test
132     public void testSetOutcome() {
133         // success case
134         when(rawResponse.getStatus()).thenReturn(200);
135         assertSame(outcome, oper.setOutcome(outcome, OperationResult.SUCCESS, rawResponse, response));
136
137         assertEquals(OperationResult.SUCCESS, outcome.getResult());
138         assertEquals("200 " + ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
139         assertSame(response, outcome.getResponse());
140
141         // failure case
142         when(rawResponse.getStatus()).thenReturn(500);
143         assertSame(outcome, oper.setOutcome(outcome, OperationResult.FAILURE, rawResponse, response));
144
145         assertEquals(OperationResult.FAILURE, outcome.getResult());
146         assertEquals("500 " + ControlLoopOperation.FAILED_MSG, outcome.getMessage());
147         assertSame(response, outcome.getResponse());
148     }
149
150     @Test
151     public void testPrepareSoModelInfo() throws CoderException {
152         // valid data
153         SoModelInfo info = oper.prepareSoModelInfo();
154         verifyRequest("model.json", info);
155
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");
160     }
161
162     @Test
163     public void testConstructRequestInfo() throws CoderException {
164         SoRequestInfo info = oper.constructRequestInfo();
165         verifyRequest("reqinfo.json", info);
166     }
167
168     @Test
169     public void testBuildRequestParameters() throws CoderException {
170         // valid data
171         verifyRequest("reqparams.json", oper.buildRequestParameters().get());
172
173         // invalid json
174         params.getPayload().put(SoOperation.REQ_PARAM_NM, "{invalid json");
175         assertThatIllegalArgumentException().isThrownBy(() -> oper.buildRequestParameters())
176                         .withMessage("invalid payload value: " + SoOperation.REQ_PARAM_NM);
177
178         // missing data
179         params.getPayload().remove(SoOperation.REQ_PARAM_NM);
180         assertTrue(oper.buildRequestParameters().isEmpty());
181
182         // null payload
183         params = params.toBuilder().payload(null).build();
184         oper = new SoOperation(params, config, PROP_NAMES) {};
185         assertTrue(oper.buildRequestParameters().isEmpty());
186     }
187
188     @Test
189     public void testBuildConfigurationParameters() {
190         // valid data
191         assertEquals(List.of(Collections.emptyMap()), oper.buildConfigurationParameters().get());
192
193         // invalid json
194         params.getPayload().put(SoOperation.CONFIG_PARAM_NM, "{invalid json");
195         assertThatIllegalArgumentException().isThrownBy(() -> oper.buildConfigurationParameters())
196                         .withMessage("invalid payload value: " + SoOperation.CONFIG_PARAM_NM);
197
198         // missing data
199         params.getPayload().remove(SoOperation.CONFIG_PARAM_NM);
200         assertTrue(oper.buildConfigurationParameters().isEmpty());
201
202         // null payload
203         params = params.toBuilder().payload(null).build();
204         oper = new SoOperation(params, config, PROP_NAMES) {};
205         assertTrue(oper.buildConfigurationParameters().isEmpty());
206     }
207
208     @Test
209     public void testGetCoder() throws CoderException {
210         Coder opcoder = oper.getCoder();
211
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);
215
216         LocalDateTime tfinish = resp.getRequest().getFinishTime();
217         assertNotNull(tfinish);
218         assertEquals(2020, tfinish.getYear());
219         assertEquals(Month.MAY, tfinish.getMonth());
220     }
221 }