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