Rename XxxActorServiceProvider to XxxActor
[policy/models.git] / models-interactions / model-actors / actor.so / src / test / java / org / onap / policy / controlloop / actor / so / SoActorTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018 Ericsson. All rights reserved.
6  * Modifications Copyright (C) 2018-2020 AT&T. All rights reserved.
7  * Modifications Copyright (C) 2019 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.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29
30 import java.io.IOException;
31 import java.nio.charset.StandardCharsets;
32 import java.util.Arrays;
33 import java.util.LinkedList;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.TreeMap;
37 import java.util.UUID;
38 import java.util.stream.Collectors;
39 import org.apache.commons.io.IOUtils;
40 import org.junit.Test;
41 import org.onap.policy.aai.AaiCqResponse;
42 import org.onap.policy.controlloop.ControlLoopOperation;
43 import org.onap.policy.controlloop.VirtualControlLoopEvent;
44 import org.onap.policy.controlloop.actor.test.BasicActor;
45 import org.onap.policy.controlloop.policy.Policy;
46 import org.onap.policy.controlloop.policy.Target;
47 import org.onap.policy.so.SoOperationType;
48 import org.onap.policy.so.SoRequest;
49 import org.onap.policy.so.SoRequestParameters;
50 import org.onap.policy.so.util.Serialization;
51
52 public class SoActorTest extends BasicActor {
53
54     private static final String C_VALUE = "cvalue";
55     private static final String A_VALUE = "avalue";
56     private static final String VF_MODULE_CREATE = "VF Module Create";
57     private static final String VF_MODULE_DELETE = "VF Module Delete";
58
59     private void instantiateTargetCq(Policy policy) {
60
61         Target target = new Target();
62         target.setModelCustomizationId("47958575-138f-452a-8c8d-d89b595f8164");
63         target.setModelInvariantId("e6130d03-56f1-4b0a-9a1d-e1b2ebc30e0e");
64         target.setModelName("VfwclVfwsnkBbefb8ce2bde..base_vfw..module-0");
65         target.setModelVersion("1");
66         target.setModelVersionId("94b18b1d-cc91-4f43-911a-e6348665f292");
67
68         policy.setTarget(target);
69     }
70
71     @Test
72     public void testConstructor() {
73         SoActor prov = new SoActor();
74
75         // verify that it has the operators we expect
76         var expected = Arrays.asList(VfModuleCreate.NAME, VfModuleDelete.NAME).stream().sorted()
77                         .collect(Collectors.toList());
78         var actual = prov.getOperationNames().stream().sorted().collect(Collectors.toList());
79
80         assertEquals(expected.toString(), actual.toString());
81     }
82
83     @Test
84     public void testActorService() {
85         // verify that it all plugs into the ActorService
86         verifyActorService(SoActor.NAME, "service.yaml");
87     }
88
89     @Test
90     public void testSendRequest() {
91         assertThatCode(() -> SoActor.sendRequest(UUID.randomUUID().toString(), null, null, null, null,
92                         null)).doesNotThrowAnyException();
93     }
94
95     @Test
96     public void testMethods() {
97         SoActor sp = new SoActor();
98
99         assertEquals("SO", sp.actor());
100         assertEquals(2, sp.recipes().size());
101         assertEquals(VF_MODULE_CREATE, sp.recipes().get(0));
102         assertEquals(VF_MODULE_DELETE, sp.recipes().get(1));
103         assertEquals(0, sp.recipePayloads(VF_MODULE_CREATE).size());
104         assertEquals(0, sp.recipeTargets("unknown recipe").size());
105         assertEquals(1, sp.recipeTargets(VF_MODULE_CREATE).size());
106     }
107
108     @Test
109     public void testConstructRequestCq() throws Exception {
110         VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
111         final ControlLoopOperation operation = new ControlLoopOperation();
112         final AaiCqResponse aaiCqResp = loadAaiResponseCq("aai/AaiCqResponseFull.json");
113         final AaiCqResponse aaiCqRespMissing = loadAaiResponseCq("aai/AaiCqResponseMissing.json");
114         final UUID requestId = UUID.randomUUID();
115         onset.setRequestId(requestId);
116
117         Policy policy = new Policy();
118         policy.setActor("Dorothy");
119         policy.setRecipe("GoToOz");
120
121         instantiateTargetCq(policy);
122
123         assertNull(new SoActor().constructRequestCq(onset, operation, policy, aaiCqResp));
124
125         policy.setActor("SO");
126
127         assertNull(new SoActor().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
128
129         policy.setRecipe(VF_MODULE_CREATE);
130
131         // empty policy payload
132         SoRequest request = new SoActor().constructRequestCq(onset, operation, policy, aaiCqResp);
133         assertNotNull(request);
134
135         assertEquals("vfModuleName", request.getRequestDetails().getRequestInfo().getInstanceName());
136         assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId());
137         assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId());
138
139         // non-empty policy payload
140         policy.setPayload(makePayload());
141         request = new SoActor().constructRequestCq(onset, operation, policy, aaiCqResp);
142         assertNotNull(request);
143         assertEquals(true, request.getRequestDetails().getRequestParameters().isUsePreload());
144         assertEquals(A_VALUE, request.getRequestDetails().getRequestParameters().getUserParams().get(0).get("akey"));
145         assertEquals(1, request.getRequestDetails().getConfigurationParameters().size());
146         assertEquals(C_VALUE, request.getRequestDetails().getConfigurationParameters().get(0).get("ckey"));
147
148         // payload with config, but no request params
149         policy.setPayload(makePayload());
150         policy.getPayload().remove(SoActor.REQ_PARAM_NM);
151         request = new SoActor().constructRequestCq(onset, operation, policy, aaiCqResp);
152         assertNotNull(request);
153         assertNull(request.getRequestDetails().getRequestParameters());
154         assertNotNull(request.getRequestDetails().getConfigurationParameters());
155
156         // payload with request, but no config params
157         policy.setPayload(makePayload());
158         policy.getPayload().remove(SoActor.CONFIG_PARAM_NM);
159         request = new SoActor().constructRequestCq(onset, operation, policy, aaiCqResp);
160         assertNotNull(request);
161         assertNotNull(request.getRequestDetails().getRequestParameters());
162         assertNull(request.getRequestDetails().getConfigurationParameters());
163
164         // null response
165         assertNull(new SoActor().constructRequestCq(onset, operation, policy, null));
166
167         instantiateTargetCq(policy);
168         policy.setRecipe(VF_MODULE_DELETE);
169         SoRequest deleteRequest = new SoActor().constructRequestCq(onset, operation, policy, aaiCqResp);
170         assertNotNull(deleteRequest);
171         assertEquals(SoOperationType.DELETE_VF_MODULE, deleteRequest.getOperationType());
172
173         /*
174          * NOTE: The remaining tests must be done in order
175          */
176
177         policy.setRecipe(VF_MODULE_CREATE);
178
179         // null tenant
180         assertNull(new SoActor().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
181
182         // null service item
183         assertNull(new SoActor().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
184
185         assertNull(new SoActor().constructRequestCq(onset, operation, policy, null));
186     }
187
188     /**
189      * Reads an AAI vserver named-query response from a file.
190      *
191      * @param fileName name of the file containing the JSON response
192      * @return output from the AAI vserver named-query
193      * @throws IOException if the file cannot be read
194      */
195     private AaiCqResponse loadAaiResponseCq(String fileName) throws IOException {
196         String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
197         return new AaiCqResponse(resp);
198     }
199
200
201
202     /**
203      * Creates a policy payload containing request & configuration parameters.
204      *
205      * @return the payload
206      */
207     private Map<String, String> makePayload() {
208         Map<String, String> payload = new TreeMap<>();
209
210         payload.put(SoActor.REQ_PARAM_NM, makeReqParams());
211         payload.put(SoActor.CONFIG_PARAM_NM, makeConfigParams());
212
213         return payload;
214     }
215
216     /**
217      * Creates request parameters.
218      *
219      * @return request parameters, encoded as JSON
220      */
221     private String makeReqParams() {
222         SoRequestParameters params = new SoRequestParameters();
223
224         params.setUsePreload(true);
225
226         Map<String, String> map = new TreeMap<>();
227         map.put("akey", A_VALUE);
228
229         List<Map<String, String>> lst = new LinkedList<>();
230         lst.add(map);
231
232         params.setUserParams(lst);
233
234         return Serialization.gsonPretty.toJson(params);
235     }
236
237     /**
238      * Creates configuration parameters.
239      *
240      * @return configuration parameters, encoded as JSON
241      */
242     private String makeConfigParams() {
243         Map<String, String> map = new TreeMap<>();
244         map.put("ckey", C_VALUE);
245
246         List<Map<String, String>> lst = new LinkedList<>();
247         lst.add(map);
248
249         return Serialization.gsonPretty.toJson(lst);
250     }
251
252 }