2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2024-2025 OpenInfra Foundation Europe. All rights reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v3;
23 import org.junit.jupiter.api.*;
24 import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext;
25 import org.onap.ccsdk.oran.a1policymanagementservice.config.TestConfig;
26 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
27 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.OpenPolicyAgentSimulatorController;
28 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.RappSimulatorController;
29 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
30 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
31 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
32 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
33 import org.onap.ccsdk.oran.a1policymanagementservice.util.v3.Helper;
34 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
35 import org.onap.ccsdk.oran.a1policymanagementservice.utils.v3.TestHelperTest;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.boot.test.context.SpringBootTest;
40 import org.springframework.boot.test.web.server.LocalServerPort;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.test.context.ContextConfiguration;
44 import org.springframework.test.context.TestPropertySource;
45 import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
46 import org.springframework.util.FileSystemUtils;
47 import reactor.core.publisher.Mono;
48 import reactor.test.StepVerifier;
50 import java.io.IOException;
51 import java.lang.invoke.MethodHandles;
52 import java.nio.file.Path;
53 import java.util.Objects;
55 import static org.mockito.ArgumentMatchers.any;
56 import static org.mockito.Mockito.when;
58 @TestMethodOrder(MethodOrderer.MethodName.class)
59 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
60 @ContextConfiguration(classes = TestConfig.class)
61 @TestPropertySource(properties = { //
62 "server.ssl.key-store=./config/keystore.jks", //
63 "app.webclient.trust-store=./config/truststore.jks", //
64 "app.webclient.trust-store-used=true", //
65 "app.vardata-directory=/tmp/pmstestv3", //
67 "app.s3.bucket=" // If this is set, S3 will be used to store data.
69 class PolicyControllerV3Test {
70 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
73 private ApplicationConfig applicationConfig;
76 private TestHelperTest testHelperTest;
82 private Policies policies;
85 private PolicyTypes policyTypes;
88 private Services services;
91 private MockA1ClientFactory a1ClientFactory;
94 private RappSimulatorController rAppSimulator;
97 private SecurityContext securityContext;
100 private OpenPolicyAgentSimulatorController openPolicyAgentSimulatorController;
106 private Helper helper;
108 private final String bearerToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
109 + "eyJpc3MiOiJleGFtcGxlX2lzc3VlciIsInN1YiI6IjEyMzQ1Njc4OTAiLCJhdWQiOiJteWNsaWVudCIs"
110 + "ImV4cCI6MzAwMDAwMDAwMCwiY2xpZW50X2lkIjoibXljbGllbnQiLCJyb2xlIjoidXNlciJ9."
111 + "O5QN_SWN4J1mWKyXk_-PCvOA6GF3ypv1rSdg2uTb_Ls";
113 private final String emptyBearerToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IiJ9."
114 + "eyJpYXQiOjE1MTYyMzkwMjJ9.uE72OfhNzhIFuyHhZyI0eYVPG6QJ7s7A-SVeKsLubCQ";
118 testHelperTest.port = port;
119 this.applicationConfig.setAuthProviderUrl(testHelperTest.baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL);
128 a1ClientFactory.reset();
129 this.rAppSimulator.getTestResults().clear();
130 this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app
131 this.securityContext.setAuthTokenFilePath(null);
132 this.openPolicyAgentSimulatorController.getTestResults().reset();
136 static void clearTestDir() {
138 FileSystemUtils.deleteRecursively(Path.of("/tmp/pmstestv3"));
139 } catch (Exception e) {
140 logger.warn("Could test directory : {}", e.getMessage());
145 @DisplayName("test Create Policy")
146 void testPostPolicy() throws Exception {
147 String nonRtRicId = "ric.1";
148 String policyTypeName = "type1_1.2.3";
149 String url = "/policies";
150 testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
151 String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "");
152 Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
153 testHelperTest.testSuccessResponse(responseMono, HttpStatus.CREATED, responseBody ->
154 responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}"));
155 testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/"));
159 @DisplayName("test Create Policy with PolicyID sending")
160 void testPostPolicyWithPolicyID() throws Exception {
161 String nonRtRicId = "ric.1";
162 String policyTypeName = "type1_1.2.3";
163 String url = "/policies";
164 testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
165 String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "1");
166 Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
167 testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/1"));
171 @DisplayName("test Create Policy with existing policy id")
172 void testPostPolicyWithExistingPolicyID() throws Exception {
173 String nonRtRicId = "ric.1";
174 String policyTypeName = "type1_1.2.3";
175 String url = "/policies";
176 String policyId = "policy_5g";
177 testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
178 String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, policyId);
179 testHelperTest.restClientV3().postForEntity(url, policyBody).block();
180 Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
181 testHelperTest.testErrorCode(responseMono, HttpStatus.CONFLICT, "Policy already created with ID: " +policyId);
185 @DisplayName("test delete Policy")
186 void testDeletePolicy() throws Exception {
187 String nonRtRicId = "ric.1";
188 String policyTypeName = "type1_1.2.3";
189 String url = "/policies";
190 testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
191 String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "");
192 Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
193 String []locationHeader = Objects.requireNonNull(Objects.requireNonNull(responseMono.block()).getHeaders()
194 .get("location")).get(0).split("/");
195 String policyID = locationHeader[(locationHeader.length) - 1];
196 Mono<ResponseEntity<String>> responseMonoDelete = testHelperTest.restClientV3().deleteForEntity(url+"/" +policyID);
197 testHelperTest.testSuccessResponse(responseMonoDelete, HttpStatus.NO_CONTENT, responseBody -> true);
201 @DisplayName("test Create Policy schema validation fail case")
202 void testPolicySchemaValidationFail() throws Exception {
203 String nonRtRicId = "ric.1";
204 String policyTypeName = "type1_1.2.3";
205 String url = "/policies";
206 testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
207 when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.FALSE);
208 String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "");
209 Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
210 testHelperTest.testErrorCode(responseMono, HttpStatus.BAD_REQUEST, " Schema validation failed");
214 @DisplayName("test Create Policy No Ric fail case")
215 void testCreatePolicyNoRic() throws Exception {
216 String policyTypeName = "type1_1.2.3";
217 String url = "/policies";
218 testHelperTest.addPolicyType(policyTypeName, " ");
219 when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.TRUE);
220 String policyBody = testHelperTest.postPolicyBody("noRic", policyTypeName, "");
221 Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
222 testHelperTest.testErrorCode(responseMono, HttpStatus.NOT_FOUND, " Could not find ric: noRic");
226 @DisplayName("test Create Policy with No Policy Type fail case")
227 void testCreatePolicyNoPolicyType() throws Exception {
228 String policyTypeName = "type1_1.2.3";
229 String nonRtRicId = "ricOne";
230 String url = "/policies";
231 testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
232 when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.TRUE);
233 String policyBody = testHelperTest.postPolicyBody(nonRtRicId, "noPolicyType", "");
234 Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
235 testHelperTest.testErrorCode(responseMono, HttpStatus.NOT_FOUND, "Could not find type: noPolicyType");
239 void testGetPolicyTypesNoRicFound() throws Exception{
240 String policyTypeName = "type1_1.2.3";
241 String nonRtRicId = "ricOne";
242 testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
243 Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().getForEntity("/policy-types" + "?nearRtRicId=\"noRic\"");
244 testHelperTest.testErrorCode(responseMono, HttpStatus.NOT_FOUND, "Near-RT RIC not Found using ID:");
248 @DisplayName("test get Policy")
249 void testGetPolicy() throws Exception {
250 String nonRtRicId = "ric.1";
251 String policyTypeName = "type1_1.2.3";
252 String url = "/policies";
253 testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
254 String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "");
255 Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
256 String []locationHeader = Objects.requireNonNull(Objects.requireNonNull(responseMono.block()).getHeaders()
257 .get("location")).get(0).split("/");
258 String policyID = locationHeader[(locationHeader.length) - 1];
259 Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url+"/" +policyID);
260 testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody ->
261 responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}"));
265 @DisplayName("test get all Policies")
266 void testGetAllPolicies() throws Exception {
267 String nonRtRicIdOne = "ric.11";
268 String nonRtRicIdTwo = "ric.22";
269 String policyTypeName = "type1_1.2.3";
270 String url = "/policies";
271 testHelperTest.addPolicyType(policyTypeName, nonRtRicIdOne);
272 String policyBodyOne = testHelperTest.postPolicyBody(nonRtRicIdOne, policyTypeName, "policyOne");
273 testHelperTest.addPolicyType(policyTypeName, nonRtRicIdTwo);
274 String policyBodyTwo = testHelperTest.postPolicyBody(nonRtRicIdTwo, policyTypeName, "policyTwo");
275 testHelperTest.restClientV3().postForEntity(url, policyBodyOne).block();
276 testHelperTest.restClientV3().postForEntity(url, policyBodyTwo).block();
277 Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url);
278 testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody ->
279 responseBody.contains("[{\"policyId\":\"policyTwo\",\"nearRtRicId\":\"ric.22\"},{\"policyId\":\"policyOne\",\"nearRtRicId\":\"ric.11\"}]"));
283 @DisplayName("test get PolicyType")
284 void testGetPolicyType() throws Exception {
285 String nonRtRicId = "ric.1";
286 String policyTypeName = "type1_1.2.3";
287 String url = "/policy-types";
288 testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
289 Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url+"/" +policyTypeName);
290 testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody -> !(responseBody.isEmpty()));
294 @DisplayName("test get All PolicyTypes")
295 void testGetAllPolicyTypes() throws Exception {
296 String nonRtRicId = "ric.1";
297 String policyTypeName = "type1_1.2.3";
298 String url = "/policy-types";
299 testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
300 Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url);
301 testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody -> responseBody.contains(
302 "{\"policyTypeId\":\"type1_1.2.3\",\"nearRtRicId\":\"ric.1\"}]"
307 @DisplayName("test update Policy")
308 void testUpdatePolicy() throws Exception {
309 String nonRtRicId = "ric.1";
310 String policyTypeName = "type1_1.2.3";
311 String url = "/policies";
312 testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
313 String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "policyOne");
314 testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block();
315 String policyBodyForPut = testHelperTest.putPolicyBody(nonRtRicId, policyTypeName, "policyOne", "ue5200",
316 "qos5200", "5200.0");
317 testHelperTest.restClientV3().putForEntity(url+"/policyOne", policyBodyForPut).block();
318 Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url+"/policyOne");
319 testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody ->
320 responseBody.contains("{\"scope\":{\"ueId\":\"ue5200\",\"qosId\":\"qos5200\"},\"qosObjectives\":{\"priorityLevel\":5200.0}"));
323 private void postPolicyWithTokenAndVerify(String clientId, String serviceId, String result) throws IOException {
324 testHelperTest.addPolicyType("type1_1.2.3", "ric.1");
325 String policyBody = testHelperTest.postPolicyBody("ric.1", "type1_1.2.3", "1");
327 if (serviceId != null) {
328 policyBody = policyBody.replace("\"serviceId\":\"\"", "\"serviceId\":\"" + serviceId + "\"");
331 StepVerifier.create(testHelperTest.restClientV3().postWithToken("/policies", policyBody, clientId)
332 .then(testHelperTest.restClientV3().getForEntity("/policies" + ((serviceId != null || clientId != null) ? "?serviceId=" + result : ""))))
333 .expectNextMatches(response -> response.getBody().contains("\"policyId\":\"1\""))
339 @DisplayName("client_id VALID + service_id NULL/EMPTY = client_id")
340 void testPostPolicyWithToken() throws IOException {
341 postPolicyWithTokenAndVerify(bearerToken, null, "myclient");
345 @DisplayName("client_id VALID + service_id VALID = service_id")
346 void testPostPolicyWithTokenAndServiceID() throws IOException {
347 postPolicyWithTokenAndVerify(bearerToken, "notmyclient", "notmyclient");
351 @DisplayName("client_id NULL + service_id EMPTY = empty")
352 void testClientIdNullServiceIdEmpty() throws Exception {
353 postPolicyWithTokenAndVerify(null, null, "");
357 @DisplayName("client_id NULL + service_id VALID = service_id")
358 void testClientIdNullServiceIdValid() throws Exception {
359 postPolicyWithTokenAndVerify(null, "validServiceId", "validServiceId");
363 @DisplayName("client_id EMPTY + service_id NULL/EMPTY = empty")
364 void testClientIdEmptyServiceIdEmpty() throws Exception {
365 postPolicyWithTokenAndVerify(emptyBearerToken, null, "");
369 @DisplayName("client_id EMPTY + service_id VALID = service_id")
370 void testEmptyClientIdServiceIdValid() throws Exception {
371 postPolicyWithTokenAndVerify(emptyBearerToken, "validServiceId", "validServiceId");
375 @DisplayName("test get Policy Status")
376 void testGetPolicyStatus() throws Exception {
377 String nonRtRicId = "ric.1";
378 String policyTypeName = "type1_1.2.3";
379 String url = "/policies";
380 String policyId = "policyOne";
381 testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
382 String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, policyId);
383 testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block();
384 Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url+"/"+ policyId +"/status");
385 testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody ->
386 responseBody.contains("OK"));