15e4983cf80c9d5e9c1eda70c2631a937b5f2e69
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2020 Nordix Foundation. All rights reserved.
6  * ======================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.onap.ccsdk.oran.a1policymanagementservice.clients;
22
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.mockito.ArgumentMatchers.anyString;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28
29 import com.google.gson.Gson;
30 import com.google.gson.JsonElement;
31
32 import java.io.File;
33 import java.io.IOException;
34 import java.net.URL;
35 import java.nio.file.Files;
36 import java.util.Arrays;
37 import java.util.List;
38
39 import org.junit.jupiter.api.Test;
40 import org.junit.jupiter.api.extension.ExtendWith;
41 import org.mockito.Mock;
42 import org.mockito.junit.jupiter.MockitoExtension;
43 import org.mockito.stubbing.OngoingStubbing;
44 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1Client.A1ProtocolType;
45 import org.onap.ccsdk.oran.a1policymanagementservice.clients.CcsdkA1AdapterClient.AdapterOutput;
46 import org.onap.ccsdk.oran.a1policymanagementservice.clients.CcsdkA1AdapterClient.AdapterRequest;
47 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ControllerConfig;
48 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
49 import org.springframework.http.HttpStatus;
50 import org.springframework.web.reactive.function.client.WebClientResponseException;
51
52 import reactor.core.publisher.Mono;
53 import reactor.test.StepVerifier;
54
55 @ExtendWith(MockitoExtension.class)
56 class CcsdkA1AdapterClientTest {
57     private static final String CONTROLLER_USERNAME = "username";
58     private static final String CONTROLLER_PASSWORD = "password";
59     private static final String RIC_1_URL = "RicUrl";
60     private static final String GET_A1_POLICY_URL = "/A1-ADAPTER-API:getA1Policy";
61     private static final String PUT_A1_URL = "/A1-ADAPTER-API:putA1Policy";
62     private static final String DELETE_A1_URL = "/A1-ADAPTER-API:deleteA1Policy";
63     private static final String GET_A1_POLICY_STATUS_URL = "/A1-ADAPTER-API:getA1PolicyStatus";
64     private static final String POLICY_TYPE_1_ID = "type1";
65     private static final String POLICY_1_ID = "policy1";
66     private static final String POLICY_JSON_VALID = "{\"scope\":{\"ueId\":\"ue1\"}}";
67
68     CcsdkA1AdapterClient clientUnderTest;
69
70     @Mock
71     AsyncRestClient asyncRestClientMock;
72
73     private ControllerConfig controllerConfig() {
74         return ControllerConfig.builder() //
75                 .name("name") //
76                 .baseUrl("baseUrl") //
77                 .password(CONTROLLER_PASSWORD) //
78                 .userName(CONTROLLER_USERNAME) //
79                 .build();
80     }
81
82     @Test
83     void createClientWithWrongProtocol_thenErrorIsThrown() {
84         AsyncRestClient asyncRestClient = new AsyncRestClient("", null, null, new SecurityContext(""));
85         assertThrows(IllegalArgumentException.class, () -> {
86             new CcsdkA1AdapterClient(A1ProtocolType.STD_V1_1, null, null, asyncRestClient);
87         });
88     }
89
90     @Test
91     void getPolicyTypeIdentities_STD_V1() {
92         clientUnderTest = new CcsdkA1AdapterClient(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1, //
93                 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
94                 controllerConfig(), asyncRestClientMock);
95         List<String> policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block();
96         assertEquals(1, policyTypeIds.size(), "should hardcoded to one");
97         assertEquals("", policyTypeIds.get(0), "should hardcoded to empty");
98     }
99
100     private void testGetPolicyTypeIdentities(A1ProtocolType protocolType, String expUrl) {
101         clientUnderTest = new CcsdkA1AdapterClient(protocolType, //
102                 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
103                 controllerConfig(), asyncRestClientMock);
104
105         String response = createOkResponseWithBody(Arrays.asList(POLICY_TYPE_1_ID));
106         whenAsyncPostThenReturn(Mono.just(response));
107
108         List<String> policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block();
109
110         assertEquals(1, policyTypeIds.size());
111         assertEquals(POLICY_TYPE_1_ID, policyTypeIds.get(0));
112
113         AdapterRequest expectedParams = new AdapterRequest(expUrl, null);
114
115         String expInput = A1AdapterJsonHelper.createInputJsonString(expectedParams);
116         verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME,
117                 CONTROLLER_PASSWORD);
118     }
119
120     @Test
121     void getPolicyTypeIdentities_OSC() {
122         testGetPolicyTypeIdentities(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1, RIC_1_URL + "/a1-p/policytypes");
123     }
124
125     @Test
126     void getPolicyTypeIdentities_STD_V2() {
127         testGetPolicyTypeIdentities(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0, RIC_1_URL + "/A1-P/v2/policytypes");
128     }
129
130     @Test
131     void getTypeSchema_STD_V1() {
132
133         clientUnderTest = new CcsdkA1AdapterClient(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1, //
134                 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
135                 controllerConfig(), asyncRestClientMock);
136
137         String policyType = clientUnderTest.getPolicyTypeSchema("").block();
138
139         assertEquals("{}", policyType);
140     }
141
142     private void testGetTypeSchema(A1ProtocolType protocolType, String expUrl, String policyTypeId,
143             String getSchemaResponseFile) throws IOException {
144         clientUnderTest = new CcsdkA1AdapterClient(protocolType, //
145                 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
146                 controllerConfig(), asyncRestClientMock);
147
148         String ricResponse = loadFile(getSchemaResponseFile);
149         JsonElement elem = gson().fromJson(ricResponse, JsonElement.class);
150         String responseFromController = createOkResponseWithBody(elem);
151         whenAsyncPostThenReturn(Mono.just(responseFromController));
152
153         String response = clientUnderTest.getPolicyTypeSchema(policyTypeId).block();
154
155         JsonElement respJson = gson().fromJson(response, JsonElement.class);
156         assertEquals(policyTypeId, respJson.getAsJsonObject().get("title").getAsString(),
157                 "title should be updated to contain policyType ID");
158
159         AdapterRequest expectedParams = new AdapterRequest(expUrl, null);
160
161         String expInput = A1AdapterJsonHelper.createInputJsonString(expectedParams);
162
163         verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME,
164                 CONTROLLER_PASSWORD);
165     }
166
167     @Test
168     void getTypeSchema_OSC() throws IOException {
169         String expUrl = RIC_1_URL + "/a1-p/policytypes/policyTypeId";
170         testGetTypeSchema(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1, expUrl, "policyTypeId",
171                 "test_osc_get_schema_response.json");
172     }
173
174     @Test
175     void getTypeSchema_STD_V2() throws IOException {
176         String expUrl = RIC_1_URL + "/A1-P/v2/policytypes/policyTypeId";
177         testGetTypeSchema(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0, expUrl, "policyTypeId",
178                 "test_oran_get_schema_response.json");
179     }
180
181     @Test
182     void parseJsonArrayOfString() {
183         // One integer and one string
184         String inputString = "[1, \"1\" ]";
185
186         List<String> result = A1AdapterJsonHelper.parseJsonArrayOfString(inputString).collectList().block();
187         assertEquals(2, result.size());
188         assertEquals("1", result.get(0));
189         assertEquals("1", result.get(1));
190     }
191
192     private void getPolicyIdentities(A1ProtocolType protocolType, String... expUrls) {
193         clientUnderTest = new CcsdkA1AdapterClient(protocolType, //
194                 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
195                 controllerConfig(), asyncRestClientMock);
196         String resp = createOkResponseWithBody(Arrays.asList("xxx"));
197         whenAsyncPostThenReturn(Mono.just(resp));
198
199         List<String> returned = clientUnderTest.getPolicyIdentities().block();
200
201         assertEquals(1, returned.size());
202         for (String expUrl : expUrls) {
203             AdapterRequest expectedParams = new AdapterRequest(expUrl, null);
204
205             String expInput = A1AdapterJsonHelper.createInputJsonString(expectedParams);
206             verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME,
207                     CONTROLLER_PASSWORD);
208         }
209     }
210
211     @Test
212     void getPolicyIdentities_STD_V1() {
213         String expUrl = RIC_1_URL + "/A1-P/v1/policies";
214         getPolicyIdentities(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1, expUrl);
215     }
216
217     @Test
218     void getPolicyIdentities_STD_V2() {
219         String expUrlPolicies = RIC_1_URL + "/A1-P/v2/policytypes";
220         String expUrlInstances = RIC_1_URL + "/A1-P/v2/policytypes/xxx/policies";
221         getPolicyIdentities(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0, expUrlPolicies, expUrlInstances);
222     }
223
224     @Test
225     void getPolicyIdentities_OSC() {
226         String expUrlTypes = RIC_1_URL + "/a1-p/policytypes";
227         String expUrlInstances = RIC_1_URL + "/a1-p/policytypes/xxx/policies";
228         getPolicyIdentities(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1, expUrlTypes, expUrlInstances);
229     }
230
231     private void putPolicy(A1ProtocolType protocolType, String expUrl) {
232         clientUnderTest = new CcsdkA1AdapterClient(protocolType, //
233                 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
234                 controllerConfig(), asyncRestClientMock);
235
236         whenPostReturnOkResponse();
237
238         String returned = clientUnderTest
239                 .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID))
240                 .block();
241
242         assertEquals("OK", returned);
243         AdapterRequest expectedInputParams = new AdapterRequest(expUrl, POLICY_JSON_VALID);
244         String expInput = A1AdapterJsonHelper.createInputJsonString(expectedInputParams);
245
246         verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD);
247
248     }
249
250     @Test
251     void putPolicy_OSC() {
252         String expUrl = RIC_1_URL + "/a1-p/policytypes/type1/policies/policy1";
253         putPolicy(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1, expUrl);
254     }
255
256     @Test
257     void putPolicy_STD_V1() {
258         String expUrl = RIC_1_URL + "/A1-P/v1/policies/policy1";
259         putPolicy(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1, expUrl);
260     }
261
262     @Test
263     void putPolicy_STD_V2() {
264         String expUrl =
265                 RIC_1_URL + "/A1-P/v2/policytypes/type1/policies/policy1?notificationDestination=https://test.com";
266         putPolicy(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0, expUrl);
267     }
268
269     @Test
270     void postRejected() {
271         clientUnderTest = new CcsdkA1AdapterClient(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1, //
272                 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
273                 controllerConfig(), asyncRestClientMock);
274
275         final String policyJson = "{}";
276         AdapterOutput adapterOutput = new AdapterOutput(HttpStatus.BAD_REQUEST.value(), "NOK");
277
278         String resp = A1AdapterJsonHelper.createOutputJsonString(adapterOutput);
279         whenAsyncPostThenReturn(Mono.just(resp));
280
281         Mono<String> returnedMono = clientUnderTest
282                 .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, policyJson, POLICY_TYPE_1_ID));
283         StepVerifier.create(returnedMono) //
284                 .expectSubscription() //
285                 .expectErrorMatches(t -> t instanceof WebClientResponseException) //
286                 .verify();
287
288         StepVerifier.create(returnedMono).expectErrorMatches(throwable -> {
289             return throwable instanceof WebClientResponseException;
290         }).verify();
291     }
292
293     private void deleteAllPolicies(A1ProtocolType protocolType, String expUrl) {
294         clientUnderTest = new CcsdkA1AdapterClient(protocolType, //
295                 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
296                 controllerConfig(), asyncRestClientMock);
297         String resp = createOkResponseWithBody(Arrays.asList("xxx"));
298         whenAsyncPostThenReturn(Mono.just(resp));
299
300         clientUnderTest.deleteAllPolicies().blockLast();
301
302         AdapterRequest expectedParams = new AdapterRequest(expUrl, null);
303
304         String expInput = A1AdapterJsonHelper.createInputJsonString(expectedParams);
305         verify(asyncRestClientMock).postWithAuthHeader(DELETE_A1_URL, expInput, CONTROLLER_USERNAME,
306                 CONTROLLER_PASSWORD);
307     }
308
309     @Test
310     void deleteAllPolicies_STD_V2() {
311         String expUrl1 = RIC_1_URL + "/A1-P/v2/policytypes/xxx/policies/xxx";
312         deleteAllPolicies(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0, expUrl1);
313     }
314
315     @Test
316     void deleteAllPolicies_STD_V1() {
317         String expUrl1 = RIC_1_URL + "/A1-P/v1/policies/xxx";
318         deleteAllPolicies(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1, expUrl1);
319     }
320
321     @Test
322     void deleteAllPolicies_OSC() {
323         String expUrl1 = RIC_1_URL + "/a1-p/policytypes/xxx/policies/xxx";
324         deleteAllPolicies(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1, expUrl1);
325     }
326
327     @Test
328     void getVersion_OSC() {
329         clientUnderTest = new CcsdkA1AdapterClient(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1, // Version irrelevant here
330                 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
331                 controllerConfig(), asyncRestClientMock);
332
333         whenAsyncPostThenReturn(Mono.error(new Exception("Error"))).thenReturn(Mono.just(createOkResponseString(true)));
334
335         A1ProtocolType returnedVersion = clientUnderTest.getProtocolVersion().block();
336
337         assertEquals(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1, returnedVersion);
338     }
339
340     @Test
341     void testGetStatus() {
342         clientUnderTest = new CcsdkA1AdapterClient(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0, //
343                 A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
344                 controllerConfig(), asyncRestClientMock);
345         whenPostReturnOkResponse();
346
347         Policy policy = A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID);
348
349         String response = clientUnderTest.getPolicyStatus(policy).block();
350         assertEquals("OK", response);
351
352         String expUrl = RIC_1_URL + "/A1-P/v2/policytypes/type1/policies/policy1/status";
353         AdapterRequest expectedParams = new AdapterRequest(expUrl, null);
354
355         String expInput = A1AdapterJsonHelper.createInputJsonString(expectedParams);
356         verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_STATUS_URL, expInput, CONTROLLER_USERNAME,
357                 CONTROLLER_PASSWORD);
358
359     }
360
361     private Gson gson() {
362         return CcsdkA1AdapterClient.gson;
363     }
364
365     private String loadFile(String fileName) throws IOException {
366         ClassLoader loader = Thread.currentThread().getContextClassLoader();
367         URL url = loader.getResource(fileName);
368         File file = new File(url.getFile());
369         return new String(Files.readAllBytes(file.toPath()));
370     }
371
372     private void whenPostReturnOkResponse() {
373         whenAsyncPostThenReturn(Mono.just(createOkResponseString(true)));
374     }
375
376     void whenPostReturnOkResponseNoBody() {
377         whenAsyncPostThenReturn(Mono.just(createOkResponseString(false)));
378     }
379
380     private String createOkResponseWithBody(Object body) {
381         AdapterOutput output = new AdapterOutput(HttpStatus.OK.value(), gson().toJson(body));
382         return A1AdapterJsonHelper.createOutputJsonString(output);
383     }
384
385     private String createOkResponseString(boolean withBody) {
386         String body = withBody ? HttpStatus.OK.name() : null;
387         AdapterOutput output = new AdapterOutput(HttpStatus.OK.value(), body);
388         return A1AdapterJsonHelper.createOutputJsonString(output);
389     }
390
391     private OngoingStubbing<Mono<String>> whenAsyncPostThenReturn(Mono<String> response) {
392         return when(asyncRestClientMock.postWithAuthHeader(anyString(), anyString(), anyString(), anyString()))
393                 .thenReturn(response);
394     }
395 }