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