fcc55ead39e874c42089a1d17528065477bf0418
[policy/drools-applications.git] /
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.util.Serialization;
46
47 public class SoActorServiceProviderTest {
48
49     @Test
50     public void testConstructRequest() throws Exception {
51         VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
52         final ControlLoopOperation operation = new ControlLoopOperation();
53         final AaiNqResponseWrapper aaiNqResp = loadAaiResponse(onset, "aai/AaiNqResponse-Full.json");
54
55         final UUID requestId = UUID.randomUUID();
56         onset.setRequestId(requestId);
57
58         Policy policy = new Policy();
59         policy.setActor("Dorothy");
60         policy.setRecipe("GoToOz");
61         assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
62
63         policy.setActor("SO");
64         assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
65
66         policy.setRecipe("VF Module Create");
67
68         // empty policy payload
69         SORequest request = new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
70         assertNotNull(request);
71
72         assertEquals("my_module_3", request.getRequestDetails().getRequestInfo().getInstanceName());
73         assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId());
74         assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId());
75
76         // non-empty policy payload
77         policy.setPayload(makePayload());
78         request = new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
79         assertNotNull(request);
80         assertEquals(true, request.getRequestDetails().getRequestParameters().isUsePreload());
81         assertEquals("avalue", request.getRequestDetails().getRequestParameters().getUserParams().get(0).get("akey"));
82         assertEquals(1, request.getRequestDetails().getConfigurationParameters().size());
83         assertEquals("cvalue", request.getRequestDetails().getConfigurationParameters().get(0).get("ckey"));
84
85         // null response
86         assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy, null));
87
88         // response has no base VF module
89         assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy,
90                         loadAaiResponse(onset, "aai/AaiNqResponse-NoBase.json")));
91
92         // response has no non-base VF modules (other than the "dummy")
93         assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy,
94                         loadAaiResponse(onset, "aai/AaiNqResponse-NoNonBase.json")));
95     }
96
97     @Test
98     public void testSendRequest() {
99         try {
100             SOActorServiceProvider.sendRequest(UUID.randomUUID().toString(), null, null);
101         } catch (Exception e) {
102             fail("Test should not throw an exception");
103         }
104     }
105
106     @Test
107     public void testMethods() {
108         SOActorServiceProvider sp = new SOActorServiceProvider();
109
110         assertEquals("SO", sp.actor());
111         assertEquals(1, sp.recipes().size());
112         assertEquals("VF Module Create", sp.recipes().get(0));
113         assertEquals(0, sp.recipePayloads("VF Module Create").size());
114     }
115
116     /**
117      * Creates a policy payload containing request & configuration parameters.
118      *
119      * @return the payload
120      */
121     private Map<String, String> makePayload() {
122         Map<String, String> payload = new TreeMap<>();
123
124         payload.put(SOActorServiceProvider.REQ_PARAM_NM, makeReqParams());
125         payload.put(SOActorServiceProvider.CONFIG_PARAM_NM, makeConfigParams());
126
127         return payload;
128     }
129
130     /**
131      * Creates request parameters.
132      *
133      * @return request parameters, encoded as JSON
134      */
135     private String makeReqParams() {
136         SORequestParameters params = new SORequestParameters();
137
138         params.setUsePreload(true);
139
140         Map<String, String> map = new TreeMap<>();
141         map.put("akey", "avalue");
142
143         List<Map<String, String>> lst = new LinkedList<>();
144         lst.add(map);
145
146         params.setUserParams(lst);
147
148         return Serialization.gsonPretty.toJson(params);
149     }
150
151     /**
152      * Creates configuration parameters.
153      *
154      * @return configuration parameters, encoded as JSON
155      */
156     private String makeConfigParams() {
157         Map<String, String> map = new TreeMap<>();
158         map.put("ckey", "cvalue");
159
160         List<Map<String, String>> lst = new LinkedList<>();
161         lst.add(map);
162
163         return Serialization.gsonPretty.toJson(lst);
164     }
165
166     /**
167      * Reads an AAI vserver named-query response from a file.
168      *
169      * @param onset the ONSET event
170      * @param fileName name of the file containing the JSON response
171      * @return output from the AAI vserver named-query
172      * @throws IOException if the file cannot be read
173      */
174     private AaiNqResponseWrapper loadAaiResponse(VirtualControlLoopEvent onset, String fileName) throws IOException {
175         String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
176         AaiNqResponse aaiNqResponse = Serialization.gsonPretty.fromJson(resp, AaiNqResponse.class);
177
178         return new AaiNqResponseWrapper(onset.getRequestId(), aaiNqResponse);
179     }
180 }