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