Merge "Parse new model ids from operational policy"
[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 import static org.junit.Assert.fail;
30
31 import java.io.IOException;
32 import java.nio.charset.StandardCharsets;
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 org.apache.commons.io.IOUtils;
39 import org.junit.Test;
40 import org.onap.policy.aai.AaiNqResponse;
41 import org.onap.policy.aai.AaiNqResponseWrapper;
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 VF_MODULE_CREATE = "VF Module Create";
54     private static final String VF_MODULE_DELETE = "VF Module Delete";
55
56     @Test
57     public void testConstructRequest() throws Exception {
58         VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
59         final ControlLoopOperation operation = new ControlLoopOperation();
60         final AaiNqResponseWrapper aaiNqResp = loadAaiResponse(onset, "aai/AaiNqResponse-Full.json");
61
62         final UUID requestId = UUID.randomUUID();
63         onset.setRequestId(requestId);
64
65         Policy policy = new Policy();
66         policy.setActor("Dorothy");
67         policy.setRecipe("GoToOz");
68
69         instantiateTarget(policy);
70
71         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
72
73         policy.setActor("SO");
74         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
75
76         policy.setRecipe(VF_MODULE_CREATE);
77
78         // empty policy payload
79         SoRequest request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
80         assertNotNull(request);
81
82         assertEquals("my_module_3", request.getRequestDetails().getRequestInfo().getInstanceName());
83         assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId());
84         assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId());
85
86         // non-empty policy payload
87         policy.setPayload(makePayload());
88         request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
89         assertNotNull(request);
90         assertEquals(true, request.getRequestDetails().getRequestParameters().isUsePreload());
91         assertEquals("avalue", request.getRequestDetails().getRequestParameters().getUserParams().get(0).get("akey"));
92         assertEquals(1, request.getRequestDetails().getConfigurationParameters().size());
93         assertEquals("cvalue", request.getRequestDetails().getConfigurationParameters().get(0).get("ckey"));
94
95         // payload with config, but no request params
96         policy.setPayload(makePayload());
97         policy.getPayload().remove(SoActorServiceProvider.REQ_PARAM_NM);
98         request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
99         assertNotNull(request);
100         assertNull(request.getRequestDetails().getRequestParameters());
101         assertNotNull(request.getRequestDetails().getConfigurationParameters());
102
103         // payload with request, but no config params
104         policy.setPayload(makePayload());
105         policy.getPayload().remove(SoActorServiceProvider.CONFIG_PARAM_NM);
106         request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
107         assertNotNull(request);
108         assertNotNull(request.getRequestDetails().getRequestParameters());
109         assertNull(request.getRequestDetails().getConfigurationParameters());
110
111         // null response
112         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, null));
113
114         // response has no base VF module
115         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy,
116                         loadAaiResponse(onset, "aai/AaiNqResponse-NoBase.json")));
117
118         policy.setTarget(null);
119
120         // response has no non-base VF modules (other than the "dummy")
121         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy,
122                         loadAaiResponse(onset, "aai/AaiNqResponse-NoNonBase.json")));
123
124         instantiateTarget(policy);
125         policy.setRecipe(VF_MODULE_DELETE);
126         SoRequest deleteRequest = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
127         assertNotNull(deleteRequest);
128         assertEquals(SoOperationType.DELETE_VF_MODULE, deleteRequest.getOperationType());
129
130         /*
131          * NOTE: The remaining tests must be done in order
132          */
133
134         policy.setRecipe(VF_MODULE_CREATE);
135
136         // null tenant
137         aaiNqResp.getAaiNqResponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems()
138                         .remove(1);
139         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
140
141         // null service item
142         aaiNqResp.getAaiNqResponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0)
143                         .setItems(null);
144         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
145
146         // null response
147         aaiNqResp.setAaiNqResponse(null);
148         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
149     }
150
151     private void instantiateTarget(Policy policy) {
152
153         Target target = new Target();
154         target.setModelCustomizationId("3e2d67ad-3495-4732-82f6-b0b872791fff");
155         target.setModelInvariantId("90b793b5-b8ae-4c36-b10b-4b6372859d3a");
156         target.setModelName("SproutScalingVf..scaling_sprout..module-1");
157         target.setModelVersion("1");
158         target.setModelVersionId("2210154d-e61a-4d7f-8fb9-0face1aee3f8");
159
160         policy.setTarget(target);
161     }
162
163     @Test
164     public void testSendRequest() {
165         try {
166             SoActorServiceProvider.sendRequest(UUID.randomUUID().toString(), null, null, null, null, null);
167         } catch (Exception e) {
168             fail("Test should not throw an exception");
169         }
170     }
171
172     @Test
173     public void testMethods() {
174         SoActorServiceProvider sp = new SoActorServiceProvider();
175
176         assertEquals("SO", sp.actor());
177         assertEquals(2, sp.recipes().size());
178         assertEquals(VF_MODULE_CREATE, sp.recipes().get(0));
179         assertEquals(VF_MODULE_DELETE, sp.recipes().get(1));
180         assertEquals(0, sp.recipePayloads(VF_MODULE_CREATE).size());
181         assertEquals(0, sp.recipeTargets("unknown recipe").size());
182         assertEquals(1, sp.recipeTargets(VF_MODULE_CREATE).size());
183     }
184
185     /**
186      * Creates a policy payload containing request & configuration parameters.
187      *
188      * @return the payload
189      */
190     private Map<String, String> makePayload() {
191         Map<String, String> payload = new TreeMap<>();
192
193         payload.put(SoActorServiceProvider.REQ_PARAM_NM, makeReqParams());
194         payload.put(SoActorServiceProvider.CONFIG_PARAM_NM, makeConfigParams());
195
196         return payload;
197     }
198
199     /**
200      * Creates request parameters.
201      *
202      * @return request parameters, encoded as JSON
203      */
204     private String makeReqParams() {
205         SoRequestParameters params = new SoRequestParameters();
206
207         params.setUsePreload(true);
208
209         Map<String, String> map = new TreeMap<>();
210         map.put("akey", "avalue");
211
212         List<Map<String, String>> lst = new LinkedList<>();
213         lst.add(map);
214
215         params.setUserParams(lst);
216
217         return Serialization.gsonPretty.toJson(params);
218     }
219
220     /**
221      * Creates configuration parameters.
222      *
223      * @return configuration parameters, encoded as JSON
224      */
225     private String makeConfigParams() {
226         Map<String, String> map = new TreeMap<>();
227         map.put("ckey", "cvalue");
228
229         List<Map<String, String>> lst = new LinkedList<>();
230         lst.add(map);
231
232         return Serialization.gsonPretty.toJson(lst);
233     }
234
235     /**
236      * Reads an AAI vserver named-query response from a file.
237      *
238      * @param onset the ONSET event
239      * @param fileName name of the file containing the JSON response
240      * @return output from the AAI vserver named-query
241      * @throws IOException if the file cannot be read
242      */
243     private AaiNqResponseWrapper loadAaiResponse(VirtualControlLoopEvent onset, String fileName) throws IOException {
244         String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
245         AaiNqResponse aaiNqResponse = Serialization.gsonPretty.fromJson(resp, AaiNqResponse.class);
246
247         return new AaiNqResponseWrapper(onset.getRequestId(), aaiNqResponse);
248     }
249 }