Add SO actor
[policy/models.git] / models-interactions / model-actors / actor.so / src / test / java / org / onap / policy / controlloop / actor / so / SoActorServiceProviderTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * TestSOActorServiceProvider
4  * ================================================================================
5  * Copyright (C) 2018 Ericsson. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2018-2019 AT&T. All rights reserved.
8  * Modifications Copyright (C) 2019 Nordix Foundation.
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.controlloop.actor.so;
25
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.policy.Policy;
45 import org.onap.policy.controlloop.policy.Target;
46 import org.onap.policy.so.SoOperationType;
47 import org.onap.policy.so.SoRequest;
48 import org.onap.policy.so.SoRequestParameters;
49 import org.onap.policy.so.util.Serialization;
50
51 public class SoActorServiceProviderTest {
52
53     private static final String C_VALUE = "cvalue";
54     private static final String A_VALUE = "avalue";
55     private static final String VF_MODULE_CREATE = "VF Module Create";
56     private static final String VF_MODULE_DELETE = "VF Module Delete";
57
58     private void instantiateTargetCq(Policy policy) {
59
60         Target target = new Target();
61         target.setModelCustomizationId("47958575-138f-452a-8c8d-d89b595f8164");
62         target.setModelInvariantId("e6130d03-56f1-4b0a-9a1d-e1b2ebc30e0e");
63         target.setModelName("VfwclVfwsnkBbefb8ce2bde..base_vfw..module-0");
64         target.setModelVersion("1");
65         target.setModelVersionId("94b18b1d-cc91-4f43-911a-e6348665f292");
66
67         policy.setTarget(target);
68     }
69
70     @Test
71     public void testConstructor() {
72         SoActorServiceProvider prov = new SoActorServiceProvider();
73
74         // verify that it has the operators we expect
75         var expected = Arrays.asList(VfModuleCreate.NAME).stream().sorted().collect(Collectors.toList());
76         var actual = prov.getOperationNames().stream().sorted().collect(Collectors.toList());
77
78         assertEquals(expected.toString(), actual.toString());
79     }
80
81     @Test
82     public void testSendRequest() {
83         SoActorServiceProvider.sendRequest(UUID.randomUUID().toString(), null, null, null, null, null);
84     }
85
86     @Test
87     public void testMethods() {
88         SoActorServiceProvider sp = new SoActorServiceProvider();
89
90         assertEquals("SO", sp.actor());
91         assertEquals(2, sp.recipes().size());
92         assertEquals(VF_MODULE_CREATE, sp.recipes().get(0));
93         assertEquals(VF_MODULE_DELETE, sp.recipes().get(1));
94         assertEquals(0, sp.recipePayloads(VF_MODULE_CREATE).size());
95         assertEquals(0, sp.recipeTargets("unknown recipe").size());
96         assertEquals(1, sp.recipeTargets(VF_MODULE_CREATE).size());
97     }
98
99     @Test
100     public void testConstructRequestCq() throws Exception {
101         VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
102         final ControlLoopOperation operation = new ControlLoopOperation();
103         final AaiCqResponse aaiCqResp = loadAaiResponseCq("aai/AaiCqResponseFull.json");
104         final AaiCqResponse aaiCqRespMissing = loadAaiResponseCq("aai/AaiCqResponseMissing.json");
105         final UUID requestId = UUID.randomUUID();
106         onset.setRequestId(requestId);
107
108         Policy policy = new Policy();
109         policy.setActor("Dorothy");
110         policy.setRecipe("GoToOz");
111
112         instantiateTargetCq(policy);
113
114         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp));
115
116         policy.setActor("SO");
117
118         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
119
120         policy.setRecipe(VF_MODULE_CREATE);
121
122         // empty policy payload
123         SoRequest request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
124         assertNotNull(request);
125
126         assertEquals("vfModuleName", request.getRequestDetails().getRequestInfo().getInstanceName());
127         assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId());
128         assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId());
129
130         // non-empty policy payload
131         policy.setPayload(makePayload());
132         request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
133         assertNotNull(request);
134         assertEquals(true, request.getRequestDetails().getRequestParameters().isUsePreload());
135         assertEquals(A_VALUE, request.getRequestDetails().getRequestParameters().getUserParams().get(0).get("akey"));
136         assertEquals(1, request.getRequestDetails().getConfigurationParameters().size());
137         assertEquals(C_VALUE, request.getRequestDetails().getConfigurationParameters().get(0).get("ckey"));
138
139         // payload with config, but no request params
140         policy.setPayload(makePayload());
141         policy.getPayload().remove(SoActorServiceProvider.REQ_PARAM_NM);
142         request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
143         assertNotNull(request);
144         assertNull(request.getRequestDetails().getRequestParameters());
145         assertNotNull(request.getRequestDetails().getConfigurationParameters());
146
147         // payload with request, but no config params
148         policy.setPayload(makePayload());
149         policy.getPayload().remove(SoActorServiceProvider.CONFIG_PARAM_NM);
150         request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
151         assertNotNull(request);
152         assertNotNull(request.getRequestDetails().getRequestParameters());
153         assertNull(request.getRequestDetails().getConfigurationParameters());
154
155         // null response
156         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, null));
157
158         instantiateTargetCq(policy);
159         policy.setRecipe(VF_MODULE_DELETE);
160         SoRequest deleteRequest = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
161         assertNotNull(deleteRequest);
162         assertEquals(SoOperationType.DELETE_VF_MODULE, deleteRequest.getOperationType());
163
164         /*
165          * NOTE: The remaining tests must be done in order
166          */
167
168         policy.setRecipe(VF_MODULE_CREATE);
169
170         // null tenant
171         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
172
173         // null service item
174         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
175
176         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, null));
177     }
178
179     /**
180      * Reads an AAI vserver named-query response from a file.
181      *
182      * @param fileName name of the file containing the JSON response
183      * @return output from the AAI vserver named-query
184      * @throws IOException if the file cannot be read
185      */
186     private AaiCqResponse loadAaiResponseCq(String fileName) throws IOException {
187         String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
188         return new AaiCqResponse(resp);
189     }
190
191
192
193     /**
194      * Creates a policy payload containing request & configuration parameters.
195      *
196      * @return the payload
197      */
198     private Map<String, String> makePayload() {
199         Map<String, String> payload = new TreeMap<>();
200
201         payload.put(SoActorServiceProvider.REQ_PARAM_NM, makeReqParams());
202         payload.put(SoActorServiceProvider.CONFIG_PARAM_NM, makeConfigParams());
203
204         return payload;
205     }
206
207     /**
208      * Creates request parameters.
209      *
210      * @return request parameters, encoded as JSON
211      */
212     private String makeReqParams() {
213         SoRequestParameters params = new SoRequestParameters();
214
215         params.setUsePreload(true);
216
217         Map<String, String> map = new TreeMap<>();
218         map.put("akey", A_VALUE);
219
220         List<Map<String, String>> lst = new LinkedList<>();
221         lst.add(map);
222
223         params.setUserParams(lst);
224
225         return Serialization.gsonPretty.toJson(params);
226     }
227
228     /**
229      * Creates configuration parameters.
230      *
231      * @return configuration parameters, encoded as JSON
232      */
233     private String makeConfigParams() {
234         Map<String, String> map = new TreeMap<>();
235         map.put("ckey", C_VALUE);
236
237         List<Map<String, String>> lst = new LinkedList<>();
238         lst.add(map);
239
240         return Serialization.gsonPretty.toJson(lst);
241     }
242
243 }