Fix checkstyle declarations
[policy/drools-applications.git] / controlloop / common / 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  * Modifications Copyright (C) 2018 AT&T. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.controlloop.actor.so;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.fail;
28
29 import java.io.IOException;
30 import java.nio.charset.StandardCharsets;
31 import java.util.LinkedList;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.TreeMap;
35 import java.util.UUID;
36 import org.apache.commons.io.IOUtils;
37 import org.junit.Test;
38 import org.onap.policy.aai.AaiNqResponse;
39 import org.onap.policy.aai.AaiNqResponseWrapper;
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.so.SORequest;
44 import org.onap.policy.so.SORequestParameters;
45 import org.onap.policy.so.SoOperationType;
46 import org.onap.policy.so.util.Serialization;
47
48 public class SoActorServiceProviderTest {
49
50     private static final String VF_MODULE_CREATE = "VF Module Create";
51     private static final String VF_MODULE_DELETE = "VF Module Delete";
52
53     @Test
54     public void testConstructRequest() throws Exception {
55         VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
56         final ControlLoopOperation operation = new ControlLoopOperation();
57         final AaiNqResponseWrapper aaiNqResp = loadAaiResponse(onset, "aai/AaiNqResponse-Full.json");
58
59         final UUID requestId = UUID.randomUUID();
60         onset.setRequestId(requestId);
61
62         Policy policy = new Policy();
63         policy.setActor("Dorothy");
64         policy.setRecipe("GoToOz");
65         
66         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
67
68         policy.setActor("SO");
69         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
70
71         policy.setRecipe(VF_MODULE_CREATE);
72
73         // empty policy payload
74         SORequest request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
75         assertNotNull(request);
76
77         assertEquals("my_module_3", request.getRequestDetails().getRequestInfo().getInstanceName());
78         assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId());
79         assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId());
80
81         // non-empty policy payload
82         policy.setPayload(makePayload());
83         request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
84         assertNotNull(request);
85         assertEquals(true, request.getRequestDetails().getRequestParameters().isUsePreload());
86         assertEquals("avalue", request.getRequestDetails().getRequestParameters().getUserParams().get(0).get("akey"));
87         assertEquals(1, request.getRequestDetails().getConfigurationParameters().size());
88         assertEquals("cvalue", request.getRequestDetails().getConfigurationParameters().get(0).get("ckey"));
89         
90         // payload with config, but no request params
91         policy.setPayload(makePayload());
92         policy.getPayload().remove(SoActorServiceProvider.REQ_PARAM_NM);
93         request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
94         assertNotNull(request);
95         assertNull(request.getRequestDetails().getRequestParameters());
96         assertNotNull(request.getRequestDetails().getConfigurationParameters());
97         
98         // payload with request, but no config params
99         policy.setPayload(makePayload());
100         policy.getPayload().remove(SoActorServiceProvider.CONFIG_PARAM_NM);
101         request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
102         assertNotNull(request);
103         assertNotNull(request.getRequestDetails().getRequestParameters());
104         assertNull(request.getRequestDetails().getConfigurationParameters());
105
106         // null response
107         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, null));
108
109         // response has no base VF module
110         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy,
111                         loadAaiResponse(onset, "aai/AaiNqResponse-NoBase.json")));
112
113         // response has no non-base VF modules (other than the "dummy")
114         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy,
115                         loadAaiResponse(onset, "aai/AaiNqResponse-NoNonBase.json")));
116
117         policy.setRecipe(VF_MODULE_DELETE);
118         SORequest deleteRequest = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
119         assertNotNull(deleteRequest);
120         assertEquals(SoOperationType.DELETE_VF_MODULE, deleteRequest.getOperationType());
121
122         /*
123          * NOTE: The remaining tests must be done in order
124          */
125
126         policy.setRecipe(VF_MODULE_CREATE);
127
128         // null tenant
129         aaiNqResp.getAaiNqResponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems()
130                         .remove(1);
131         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
132
133         // null service item
134         aaiNqResp.getAaiNqResponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0)
135                         .setItems(null);
136         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
137
138         // null response
139         aaiNqResp.setAaiNqResponse(null);
140         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
141     }
142
143     @Test
144     public void testSendRequest() {
145         try {
146             SoActorServiceProvider.sendRequest(UUID.randomUUID().toString(), null, null);
147         } catch (Exception e) {
148             fail("Test should not throw an exception");
149         }
150     }
151
152     @Test
153     public void testMethods() {
154         SoActorServiceProvider sp = new SoActorServiceProvider();
155
156         assertEquals("SO", sp.actor());
157         assertEquals(2, sp.recipes().size());
158         assertEquals(VF_MODULE_CREATE, sp.recipes().get(0));
159         assertEquals(VF_MODULE_DELETE, sp.recipes().get(1));
160         assertEquals(0, sp.recipePayloads(VF_MODULE_CREATE).size());
161         assertEquals(0, sp.recipeTargets("unknown recipe").size());
162         assertEquals(1, sp.recipeTargets(VF_MODULE_CREATE).size());
163     }
164
165     /**
166      * Creates a policy payload containing request & configuration parameters.
167      *
168      * @return the payload
169      */
170     private Map<String, String> makePayload() {
171         Map<String, String> payload = new TreeMap<>();
172
173         payload.put(SoActorServiceProvider.REQ_PARAM_NM, makeReqParams());
174         payload.put(SoActorServiceProvider.CONFIG_PARAM_NM, makeConfigParams());
175
176         return payload;
177     }
178
179     /**
180      * Creates request parameters.
181      *
182      * @return request parameters, encoded as JSON
183      */
184     private String makeReqParams() {
185         SORequestParameters params = new SORequestParameters();
186
187         params.setUsePreload(true);
188
189         Map<String, String> map = new TreeMap<>();
190         map.put("akey", "avalue");
191
192         List<Map<String, String>> lst = new LinkedList<>();
193         lst.add(map);
194
195         params.setUserParams(lst);
196
197         return Serialization.gsonPretty.toJson(params);
198     }
199
200     /**
201      * Creates configuration parameters.
202      *
203      * @return configuration parameters, encoded as JSON
204      */
205     private String makeConfigParams() {
206         Map<String, String> map = new TreeMap<>();
207         map.put("ckey", "cvalue");
208
209         List<Map<String, String>> lst = new LinkedList<>();
210         lst.add(map);
211
212         return Serialization.gsonPretty.toJson(lst);
213     }
214
215     /**
216      * Reads an AAI vserver named-query response from a file.
217      *
218      * @param onset the ONSET event
219      * @param fileName name of the file containing the JSON response
220      * @return output from the AAI vserver named-query
221      * @throws IOException if the file cannot be read
222      */
223     private AaiNqResponseWrapper loadAaiResponse(VirtualControlLoopEvent onset, String fileName) throws IOException {
224         String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
225         AaiNqResponse aaiNqResponse = Serialization.gsonPretty.fromJson(resp, AaiNqResponse.class);
226
227         return new AaiNqResponseWrapper(onset.getRequestId(), aaiNqResponse);
228     }
229 }