Added Controller configuration
[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
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.extension.ExtendWith;
37 import org.mockito.junit.jupiter.MockitoExtension;
38 import org.mockito.stubbing.OngoingStubbing;
39 import org.oransc.policyagent.clients.A1Client.A1ProtocolType;
40 import org.oransc.policyagent.clients.SdncOscA1Client.AdapterRequest;
41 import org.oransc.policyagent.clients.SdncOscA1Client.AdapterResponse;
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         AdapterRequest expectedParams = ImmutableAdapterRequest.builder() //
109             .nearRtRicUrl(expUrl) //
110             .build();
111         String expInput = A1ClientHelper.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 obj) {
126         AdapterResponse output = ImmutableAdapterResponse.builder() //
127             .body(gson().toJson(obj)) //
128             .httpStatus(200) //
129             .build();
130
131         return gson().toJson(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         AdapterRequest expectedParams = ImmutableAdapterRequest.builder() //
144             .nearRtRicUrl(policiesUrl()) //
145             .build();
146         String expInput = A1ClientHelper.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 = A1ClientHelper.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         AdapterResponse adapterResponse = ImmutableAdapterResponse.builder() //
180             .body("NOK") //
181             .httpStatus(400) // ERROR
182             .build();
183
184         String resp = gson().toJson(adapterResponse);
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
190         final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
191         AdapterRequest expRequestParams = ImmutableAdapterRequest.builder() //
192             .nearRtRicUrl(expUrl) //
193             .body(policyJson) //
194             .build();
195         String expRequest = A1ClientHelper.createInputJsonString(expRequestParams);
196         verify(asyncRestClientMock).postWithAuthHeader(PUT_A1_URL, expRequest, CONTROLLER_USERNAME,
197             CONTROLLER_PASSWORD);
198         StepVerifier.create(returnedMono)
199             .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
200     }
201
202     @Test
203     public void testDeletePolicy() {
204         whenPostReturnOkResponse();
205
206         String returned = clientUnderTest
207             .deletePolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID))
208             .block();
209         assertEquals("OK", returned, "");
210         final String expUrl = policiesUrl() + "/" + POLICY_1_ID;
211         AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
212             .nearRtRicUrl(expUrl) //
213             .build();
214         String expInput = A1ClientHelper.createInputJsonString(expectedInputParams);
215
216         verify(asyncRestClientMock).postWithAuthHeader(DELETE_A1_URL, expInput, CONTROLLER_USERNAME,
217             CONTROLLER_PASSWORD);
218     }
219
220     @Test
221     public void testGetStatus() {
222         whenPostReturnOkResponse();
223
224         Policy policy = A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID);
225
226         String returnedStatus = clientUnderTest.getPolicyStatus(policy).block();
227
228         assertEquals("OK", returnedStatus, "unexpeted status");
229
230         final String expUrl = policiesUrl() + "/" + POLICY_1_ID + "/status";
231         AdapterRequest expectedInputParams = ImmutableAdapterRequest.builder() //
232             .nearRtRicUrl(expUrl) //
233             .build();
234         String expInput = A1ClientHelper.createInputJsonString(expectedInputParams);
235
236         verify(asyncRestClientMock).postWithAuthHeader(GET_A1_POLICY_STATUS_URL, expInput, CONTROLLER_USERNAME,
237             CONTROLLER_PASSWORD);
238     }
239
240     private void whenPostReturnOkResponse() {
241         AdapterResponse adapterResponse = ImmutableAdapterResponse.builder() //
242             .body("OK") //
243             .httpStatus(200) //
244             .build();
245
246         String resp = gson().toJson(adapterResponse);
247         whenAsyncPostThenReturn(Mono.just(resp));
248     }
249
250     private OngoingStubbing<Mono<String>> whenAsyncPostThenReturn(Mono<String> response) {
251         return when(asyncRestClientMock.postWithAuthHeader(anyString(), anyString(), anyString(), anyString()))
252             .thenReturn(response);
253     }
254 }