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