Removed throwing of runtime exceptions
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / clients / SdncOscA1ClientTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
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.oransc.policyagent.clients;
22
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.mockito.ArgumentMatchers.anyString;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28
29 import com.google.gson.Gson;
30
31 import java.util.Arrays;
32 import java.util.List;
33 import java.util.Optional;
34
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.extension.ExtendWith;
38 import org.mockito.junit.jupiter.MockitoExtension;
39 import org.mockito.stubbing.OngoingStubbing;
40 import org.oransc.policyagent.clients.A1Client.A1ProtocolType;
41 import org.oransc.policyagent.clients.SdncOscA1Client.AdapterOutput;
42 import org.oransc.policyagent.clients.SdncOscA1Client.AdapterRequest;
43 import org.oransc.policyagent.configuration.ControllerConfig;
44 import org.oransc.policyagent.configuration.ImmutableControllerConfig;
45 import org.oransc.policyagent.repository.Policy;
46 import org.oransc.policyagent.repository.Ric;
47 import org.springframework.web.reactive.function.client.WebClientResponseException;
48
49 import reactor.core.publisher.Mono;
50 import reactor.test.StepVerifier;
51
52 @ExtendWith(MockitoExtension.class)
53 public class SdncOscA1ClientTest {
54     private static final String CONTROLLER_USERNAME = "username";
55     private static final String CONTROLLER_PASSWORD = "password";
56     private static final String RIC_1_URL = "RicUrl";
57     private static final String GET_A1_POLICY_URL = "/A1-ADAPTER-API:getA1Policy";
58     private static final String PUT_A1_URL = "/A1-ADAPTER-API:putA1Policy";
59     private static final String DELETE_A1_URL = "/A1-ADAPTER-API:deleteA1Policy";
60     private static final String GET_A1_POLICY_STATUS_URL = "/A1-ADAPTER-API:getA1PolicyStatus";
61     private static final String POLICY_TYPE_1_ID = "type1";
62     private static final String POLICY_1_ID = "policy1";
63     private static final String POLICY_2_ID = "policy2";
64     private static final String POLICY_JSON_VALID = "{\"scope\":{\"ueId\":\"ue1\"}}";
65
66     SdncOscA1Client clientUnderTest;
67
68     AsyncRestClient asyncRestClientMock;
69
70     private ControllerConfig controllerConfig() {
71         return ImmutableControllerConfig.builder() //
72             .name("name") //
73             .baseUrl("baseUrl") //
74             .password(CONTROLLER_PASSWORD) //
75             .userName(CONTROLLER_USERNAME) //
76             .build();
77     }
78
79     @BeforeEach
80     public void init() {
81         asyncRestClientMock = mock(AsyncRestClient.class);
82         Ric ric = A1ClientHelper.createRic(RIC_1_URL);
83
84         clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_STD_V1_1, ric.getConfig(), controllerConfig(),
85             asyncRestClientMock);
86     }
87
88     @Test
89     public void testGetPolicyTypeIdentities_STD() {
90         List<String> policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block();
91         assertEquals(1, policyTypeIds.size(), "should hardcoded to one");
92         assertEquals("", policyTypeIds.get(0), "should hardcoded to empty");
93     }
94
95     @Test
96     public void testGetPolicyTypeIdentities_OSC() {
97         clientUnderTest = new SdncOscA1Client(A1ProtocolType.SDNC_OSC_OSC_V1, //
98             A1ClientHelper.createRic(RIC_1_URL).getConfig(), //
99             controllerConfig(), asyncRestClientMock);
100
101         String response = createResponse(Arrays.asList(POLICY_TYPE_1_ID));
102         whenAsyncPostThenReturn(Mono.just(response));
103
104         List<String> policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block();
105         assertEquals(1, policyTypeIds.size(), "");
106         assertEquals(POLICY_TYPE_1_ID, policyTypeIds.get(0), "");
107
108         String expUrl = RIC_1_URL + "/a1-p/policytypes";
109         ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() //
110             .nearRtRicUrl(expUrl) //
111             .build();
112         String expInput = SdncJsonHelper.createInputJsonString(expectedParams);
113         verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME,
114             CONTROLLER_PASSWORD);
115
116     }
117
118     private String policiesUrl() {
119         return RIC_1_URL + "/A1-P/v1/policies";
120     }
121
122     private Gson gson() {
123         return SdncOscA1Client.gson;
124     }
125
126     private String createResponse(Object body) {
127         AdapterOutput output = ImmutableAdapterOutput.builder() //
128             .body(gson().toJson(body)) //
129             .httpStatus(200) //
130             .build();
131         return SdncJsonHelper.createOutputJsonString(output);
132     }
133
134     @Test
135     public void testGetPolicyIdentities() {
136
137         String policyIdsResp = createResponse(Arrays.asList(POLICY_1_ID, POLICY_2_ID));
138         whenAsyncPostThenReturn(Mono.just(policyIdsResp));
139
140         List<String> returned = clientUnderTest.getPolicyIdentities().block();
141         assertEquals(2, returned.size(), "");
142
143         ImmutableAdapterRequest expectedParams = ImmutableAdapterRequest.builder() //
144             .nearRtRicUrl(policiesUrl()) //
145             .build();
146         String expInput = SdncJsonHelper.createInputJsonString(expectedParams);
147         verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_URL, expInput, CONTROLLER_USERNAME,
148             CONTROLLER_PASSWORD);
149
150     }
151
152     @Test
153     public void testGetValidPolicyType() {
154         String policyType = clientUnderTest.getPolicyTypeSchema("").block();
155         assertEquals("{}", policyType, "");
156     }
157
158     @Test
159     public void testPutPolicyValidResponse() {
160         whenPostReturnOkResponse();
161
162         String returned = clientUnderTest
163             .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID))
164             .block();
165         assertEquals("OK", returned, "");
166         final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
167         AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
168             .nearRtRicUrl(expUrl) //
169             .body(POLICY_JSON_VALID) //
170             .build();
171         String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams);
172
173         verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expInput, CONTROLLER_USERNAME, CONTROLLER_PASSWORD);
174     }
175
176     @Test
177     public void testPutPolicyRejected() {
178         final String policyJson = "{}";
179         AdapterOutput adapterOutput = ImmutableAdapterOutput.builder() //
180             .body("NOK") //
181             .httpStatus(400) // ERROR
182             .build();
183
184         String resp = SdncJsonHelper.createOutputJsonString(adapterOutput);
185         whenAsyncPostThenReturn(Mono.just(resp));
186
187         Mono<String> returnedMono = clientUnderTest
188             .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, policyJson, POLICY_TYPE_1_ID));
189         StepVerifier.create(returnedMono) //
190             .expectSubscription() //
191             .expectErrorMatches(t -> t instanceof WebClientResponseException) //
192             .verify();
193
194         final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
195         AdapterRequest expRequestParams = ImmutableAdapterRequest.builder() //
196             .nearRtRicUrl(expUrl) //
197             .body(policyJson) //
198             .build();
199         String expRequest = SdncJsonHelper.createInputJsonString(expRequestParams);
200         verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expRequest, CONTROLLER_USERNAME,
201             CONTROLLER_PASSWORD);
202         StepVerifier.create(returnedMono)
203             .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
204     }
205
206     @Test
207     public void testDeletePolicy() {
208         whenPostReturnOkResponse();
209
210         String returned = clientUnderTest
211             .deletePolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID))
212             .block();
213         assertEquals("OK", returned, "");
214         final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
215         AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
216             .nearRtRicUrl(expUrl) //
217             .build();
218         String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams);
219
220         verify(asyncRestClientMock).postWithAuthHeader(DELETE_A1_URL, expInput, CONTROLLER_USERNAME,
221             CONTROLLER_PASSWORD);
222     }
223
224     @Test
225     public void testGetStatus() {
226         whenPostReturnOkResponse();
227
228         Policy policy = A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID);
229
230         String returnedStatus = clientUnderTest.getPolicyStatus(policy).block();
231
232         assertEquals("OK", returnedStatus, "unexpected status");
233
234         final String expUrl = policiesUrl() + "/" + POLICY_1_ID + "/status";
235         AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
236             .nearRtRicUrl(expUrl) //
237             .build();
238         String expInput = SdncJsonHelper.createInputJsonString(expectedInputParams);
239
240         verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_STATUS_URL, expInput, CONTROLLER_USERNAME,
241             CONTROLLER_PASSWORD);
242     }
243
244     @Test
245     public void testGetVersion() {
246         whenPostReturnOkResponse();
247         A1ProtocolType returnedVersion = clientUnderTest.getProtocolVersion().block();
248         assertEquals(A1ProtocolType.SDNC_OSC_STD_V1_1, returnedVersion, "");
249
250         whenPostReturnOkResponseNoBody();
251         returnedVersion = clientUnderTest.getProtocolVersion().block();
252         assertEquals(A1ProtocolType.SDNC_OSC_STD_V1_1, returnedVersion, "");
253     }
254
255     private void whenPostReturnOkResponse() {
256         AdapterOutput adapterOutput = ImmutableAdapterOutput.builder() //
257             .body("OK") //
258             .httpStatus(200) //
259             .build();
260
261         String resp = SdncJsonHelper.createOutputJsonString(adapterOutput);
262         whenAsyncPostThenReturn(Mono.just(resp));
263     }
264
265     private void whenPostReturnOkResponseNoBody() {
266         AdapterOutput adapterOutput = ImmutableAdapterOutput.builder() //
267             .httpStatus(200) //
268             .body(Optional.empty()) //
269             .build();
270
271         String resp = SdncJsonHelper.createOutputJsonString(adapterOutput);
272         whenAsyncPostThenReturn(Mono.just(resp));
273     }
274
275     private OngoingStubbing<Mono<String>> whenAsyncPostThenReturn(Mono<String> response) {
276         return when(asyncRestClientMock.postWithAuthHeader(anyString(), anyString(), anyString(), anyString()))
277             .thenReturn(response);
278     }
279 }