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