Further updates to the auto-test environment
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / clients / SdncOnapA1ClientTest.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.mockito.ArgumentMatchers.anyString;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
31
32 import org.json.JSONException;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.extension.ExtendWith;
36 import org.mockito.junit.jupiter.MockitoExtension;
37 import org.mockito.stubbing.OngoingStubbing;
38 import org.oransc.policyagent.configuration.ControllerConfig;
39 import org.oransc.policyagent.configuration.ImmutableControllerConfig;
40
41 import reactor.core.publisher.Flux;
42 import reactor.core.publisher.Mono;
43 import reactor.test.StepVerifier;
44
45 @ExtendWith(MockitoExtension.class)
46 public class SdncOnapA1ClientTest {
47     private static final String CONTROLLER_USERNAME = "username";
48     private static final String CONTROLLER_PASSWORD = "password";
49     private static final String RIC_1_URL = "RicUrl";
50     private static final String POLICYTYPES_IDENTITIES_URL = "/A1-ADAPTER-API:getPolicyTypes";
51     private static final String POLICIES_IDENTITIES_URL = "/A1-ADAPTER-API:getPolicyInstances";
52     private static final String POLICYTYPES_URL = "/A1-ADAPTER-API:getPolicyType";
53     private static final String PUT_POLICY_URL = "/A1-ADAPTER-API:createPolicyInstance";
54     private static final String DELETE_POLICY_URL = "/A1-ADAPTER-API:deletePolicyInstance";
55
56     private static final String POLICY_TYPE_1_ID = "type1";
57     private static final String POLICY_TYPE_2_ID = "type2";
58     private static final String POLICY_TYPE_SCHEMA_VALID = "{\"type\":\"type1\"}";
59     private static final String POLICY_TYPE_SCHEMA_INVALID = "\"type\":\"type1\"}";
60     private static final String POLICY_1_ID = "policy1";
61     private static final String POLICY_2_ID = "policy2";
62     private static final String POLICY_JSON_VALID = "{\"scope\":{\"ueId\":\"ue1\"}}";
63
64     SdncOnapA1Client clientUnderTest;
65
66     AsyncRestClient asyncRestClientMock;
67
68     @BeforeEach
69     public void init() {
70         asyncRestClientMock = mock(AsyncRestClient.class);
71         ControllerConfig controllerCfg = ImmutableControllerConfig.builder() //
72             .name("name") //
73             .baseUrl("baseUrl") //
74             .password(CONTROLLER_PASSWORD) //
75             .userName(CONTROLLER_USERNAME) //
76             .build();
77
78         clientUnderTest =
79             new SdncOnapA1Client(A1ClientHelper.createRic(RIC_1_URL).getConfig(), controllerCfg, asyncRestClientMock);
80     }
81
82     @Test
83     public void testGetPolicyTypeIdentities() {
84         SdncOnapA1Client.SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
85             .nearRtRicId(RIC_1_URL) //
86             .build();
87         String inputJsonString = SdncJsonHelper.createInputJsonString(inputParams);
88
89         List<String> policyTypeIds = Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID);
90         Mono<String> policyTypeIdsResp =
91             A1ClientHelper.createOutputJsonResponse("policy-type-id-list", policyTypeIds.toString());
92         whenAsyncPostThenReturn(policyTypeIdsResp);
93
94         Mono<List<String>> returnedMono = clientUnderTest.getPolicyTypeIdentities();
95         verify(asyncRestClientMock).postWithAuthHeader(POLICYTYPES_IDENTITIES_URL, inputJsonString, CONTROLLER_USERNAME,
96             CONTROLLER_PASSWORD);
97         StepVerifier.create(returnedMono).expectNext(policyTypeIds).expectComplete().verify();
98     }
99
100     @Test
101     public void testGetPolicyIdentities() {
102         SdncOnapA1Client.SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
103             .nearRtRicId(RIC_1_URL) //
104             .build();
105         String inputJsonStringGetTypeIds = SdncJsonHelper.createInputJsonString(inputParams);
106         inputParams = ImmutableSdncOnapAdapterInput.builder() //
107             .nearRtRicId(RIC_1_URL) //
108             .policyTypeId(POLICY_TYPE_1_ID) //
109             .build();
110         String inputJsonStringGetPolicyIdsType1 = SdncJsonHelper.createInputJsonString(inputParams);
111         inputParams = ImmutableSdncOnapAdapterInput.builder() //
112             .nearRtRicId(RIC_1_URL) //
113             .policyTypeId(POLICY_TYPE_2_ID) //
114             .build();
115         String inputJsonStringGetPolicyIdsType2 = SdncJsonHelper.createInputJsonString(inputParams);
116
117         List<String> policyTypeIds = Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID);
118         Mono<String> policyTypeIdsResp =
119             A1ClientHelper.createOutputJsonResponse("policy-type-id-list", policyTypeIds.toString());
120         List<String> policyIdsType1 = Arrays.asList(POLICY_1_ID);
121         Mono<String> policyIdsType1Resp =
122             A1ClientHelper.createOutputJsonResponse("policy-instance-id-list", policyIdsType1.toString());
123         List<String> policyIdsType2 = Arrays.asList(POLICY_2_ID);
124         Mono<String> policyIdsType2Resp =
125             A1ClientHelper.createOutputJsonResponse("policy-instance-id-list", policyIdsType2.toString());
126         whenAsyncPostThenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp).thenReturn(policyIdsType2Resp);
127
128         Mono<List<String>> returnedMono = clientUnderTest.getPolicyIdentities();
129         StepVerifier.create(returnedMono).expectNext(Arrays.asList(POLICY_1_ID, POLICY_2_ID)).expectComplete().verify();
130         verify(asyncRestClientMock).postWithAuthHeader(POLICYTYPES_IDENTITIES_URL, inputJsonStringGetTypeIds,
131             CONTROLLER_USERNAME, CONTROLLER_PASSWORD);
132         verify(asyncRestClientMock).postWithAuthHeader(POLICIES_IDENTITIES_URL, inputJsonStringGetPolicyIdsType1,
133             CONTROLLER_USERNAME, CONTROLLER_PASSWORD);
134         verify(asyncRestClientMock).postWithAuthHeader(POLICIES_IDENTITIES_URL, inputJsonStringGetPolicyIdsType2,
135             CONTROLLER_USERNAME, CONTROLLER_PASSWORD);
136     }
137
138     @Test
139     public void testGetValidPolicyType() {
140         SdncOnapA1Client.SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
141             .nearRtRicId(RIC_1_URL) //
142             .policyTypeId(POLICY_TYPE_1_ID) //
143             .build();
144         String inputJsonString = SdncJsonHelper.createInputJsonString(inputParams);
145
146         String policyType = "{\"policySchema\": " + POLICY_TYPE_SCHEMA_VALID + ", \"statusSchema\": {} }";
147         Mono<String> policyTypeResp = A1ClientHelper.createOutputJsonResponse("policy-type", policyType);
148         whenAsyncPostThenReturn(policyTypeResp);
149
150         Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
151         verify(asyncRestClientMock).postWithAuthHeader(POLICYTYPES_URL, inputJsonString, CONTROLLER_USERNAME,
152             CONTROLLER_PASSWORD);
153         StepVerifier.create(returnedMono).expectNext(POLICY_TYPE_SCHEMA_VALID).expectComplete().verify();
154     }
155
156     @Test
157     public void testGetInvalidPolicyType() {
158         SdncOnapA1Client.SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
159             .nearRtRicId(RIC_1_URL) //
160             .policyTypeId(POLICY_TYPE_1_ID) //
161             .build();
162         String inputJsonString = SdncJsonHelper.createInputJsonString(inputParams);
163
164         String policyType = "{\"policySchema\": " + POLICY_TYPE_SCHEMA_INVALID + ", \"statusSchema\": {} }";
165         Mono<String> policyTypeResp = A1ClientHelper.createOutputJsonResponse("policy-type", policyType);
166         whenAsyncPostThenReturn(policyTypeResp);
167
168         Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
169         verify(asyncRestClientMock).postWithAuthHeader(POLICYTYPES_URL, inputJsonString, CONTROLLER_USERNAME,
170             CONTROLLER_PASSWORD);
171         StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify();
172     }
173
174     @Test
175     public void testPutPolicy() {
176         SdncOnapA1Client.SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
177             .nearRtRicId(RIC_1_URL) //
178             .policyTypeId(POLICY_TYPE_1_ID) //
179             .policyInstanceId(POLICY_1_ID) //
180             .policyInstance(POLICY_JSON_VALID) //
181             .properties(new ArrayList<String>()) //
182             .build();
183         String inputJsonString = SdncJsonHelper.createInputJsonString(inputParams);
184
185         whenAsyncPostThenReturn(Mono.empty());
186
187         Mono<String> returnedMono = clientUnderTest
188             .putPolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID));
189         verify(asyncRestClientMock).postWithAuthHeader(PUT_POLICY_URL, inputJsonString, CONTROLLER_USERNAME,
190             CONTROLLER_PASSWORD);
191         StepVerifier.create(returnedMono).expectComplete().verify();
192     }
193
194     @Test
195     public void testDeletePolicy() {
196         SdncOnapA1Client.SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
197             .nearRtRicId(RIC_1_URL) //
198             .policyTypeId(POLICY_TYPE_1_ID) //
199             .policyInstanceId(POLICY_1_ID) //
200             .build();
201         String inputJsonString = SdncJsonHelper.createInputJsonString(inputParams);
202
203         whenAsyncPostThenReturn(Mono.empty());
204
205         Mono<String> returnedMono = clientUnderTest
206             .deletePolicy(A1ClientHelper.createPolicy(RIC_1_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID));
207         verify(asyncRestClientMock).postWithAuthHeader(DELETE_POLICY_URL, inputJsonString, CONTROLLER_USERNAME,
208             CONTROLLER_PASSWORD);
209         StepVerifier.create(returnedMono).expectComplete().verify();
210     }
211
212     @Test
213     public void testDeleteAllPolicies() {
214         SdncOnapA1Client.SdncOnapAdapterInput inputParams = ImmutableSdncOnapAdapterInput.builder() //
215             .nearRtRicId(RIC_1_URL) //
216             .build();
217         String inputJsonStringGetTypeIds = SdncJsonHelper.createInputJsonString(inputParams);
218         inputParams = ImmutableSdncOnapAdapterInput.builder() //
219             .nearRtRicId(RIC_1_URL) //
220             .policyTypeId(POLICY_TYPE_1_ID) //
221             .build();
222         String inputJsonStringGetPolicyIdsType1 = SdncJsonHelper.createInputJsonString(inputParams);
223         inputParams = ImmutableSdncOnapAdapterInput.builder() //
224             .nearRtRicId(RIC_1_URL) //
225             .policyTypeId(POLICY_TYPE_2_ID) //
226             .build();
227         String inputJsonStringGetPolicyIdsType2 = SdncJsonHelper.createInputJsonString(inputParams);
228         inputParams = ImmutableSdncOnapAdapterInput.builder() //
229             .nearRtRicId(RIC_1_URL) //
230             .policyTypeId(POLICY_TYPE_1_ID) //
231             .policyInstanceId(POLICY_1_ID) //
232             .build();
233         String inputJsonStringDeletePolicy1 = SdncJsonHelper.createInputJsonString(inputParams);
234         inputParams = ImmutableSdncOnapAdapterInput.builder() //
235             .nearRtRicId(RIC_1_URL) //
236             .policyTypeId(POLICY_TYPE_2_ID) //
237             .policyInstanceId(POLICY_2_ID) //
238             .build();
239         String inputJsonStringDeletePolicy2 = SdncJsonHelper.createInputJsonString(inputParams);
240
241         List<String> policyTypeIds = Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID);
242         Mono<String> policyTypeIdsResp =
243             A1ClientHelper.createOutputJsonResponse("policy-type-id-list", policyTypeIds.toString());
244         List<String> policyIdsType1 = Arrays.asList(POLICY_1_ID);
245         Mono<String> policyIdsType1Resp =
246             A1ClientHelper.createOutputJsonResponse("policy-instance-id-list", policyIdsType1.toString());
247         List<String> policyIdsType2 = Arrays.asList(POLICY_2_ID);
248         Mono<String> policyIdsType2Resp =
249             A1ClientHelper.createOutputJsonResponse("policy-instance-id-list", policyIdsType2.toString());
250         whenAsyncPostThenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp).thenReturn(Mono.empty())
251             .thenReturn(policyIdsType2Resp).thenReturn(Mono.empty());
252
253         Flux<String> returnedFlux = clientUnderTest.deleteAllPolicies();
254         StepVerifier.create(returnedFlux).expectComplete().verify();
255         verify(asyncRestClientMock).postWithAuthHeader(POLICYTYPES_IDENTITIES_URL, inputJsonStringGetTypeIds,
256             CONTROLLER_USERNAME, CONTROLLER_PASSWORD);
257         verify(asyncRestClientMock).postWithAuthHeader(POLICIES_IDENTITIES_URL, inputJsonStringGetPolicyIdsType1,
258             CONTROLLER_USERNAME, CONTROLLER_PASSWORD);
259         verify(asyncRestClientMock).postWithAuthHeader(DELETE_POLICY_URL, inputJsonStringDeletePolicy1,
260             CONTROLLER_USERNAME, CONTROLLER_PASSWORD);
261         verify(asyncRestClientMock).postWithAuthHeader(POLICIES_IDENTITIES_URL, inputJsonStringGetPolicyIdsType2,
262             CONTROLLER_USERNAME, CONTROLLER_PASSWORD);
263         verify(asyncRestClientMock).postWithAuthHeader(DELETE_POLICY_URL, inputJsonStringDeletePolicy2,
264             CONTROLLER_USERNAME, CONTROLLER_PASSWORD);
265     }
266
267     private OngoingStubbing<Mono<String>> whenAsyncPostThenReturn(Mono<String> response) {
268         return when(asyncRestClientMock.postWithAuthHeader(anyString(), anyString(), anyString(), anyString()))
269             .thenReturn(response);
270     }
271 }