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