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