diff options
author | 2025-01-22 15:12:56 +0000 | |
---|---|---|
committer | 2025-02-11 15:48:00 +0000 | |
commit | 5d40c7577f4df9c36778360af5602867eeb3618c (patch) | |
tree | f6ff9758d7a207aba37dbc55d6167271d48045fd /a1-policy-management/src | |
parent | 2315bc53794e0cd054bbfad193e2e3b3683ad096 (diff) |
Issue-ID: CCSDK-3996
Change-Id: I27cbdda829d55f963f80fe1316d838f531f67eb5
Signed-off-by: saul.gill <saul.gill@est.tech>
Diffstat (limited to 'a1-policy-management/src')
6 files changed, 312 insertions, 7 deletions
diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/ApplicationConfig.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/ApplicationConfig.java index d3b51461..6d8d52de 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/ApplicationConfig.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/ApplicationConfig.java @@ -3,7 +3,7 @@ * ONAP : ccsdk oran * ====================================================================== * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved. - * Copyright (C) 2023-2024 OpenInfra Foundation Europe. All rights reserved. + * Modifications Copyright (C) 2023-2025 OpenInfra Foundation Europe. All rights reserved. * ====================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,20 +22,16 @@ package org.onap.ccsdk.oran.a1policymanagementservice.configuration; import com.google.common.base.Strings; - import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Map; - import lombok.Getter; import lombok.Setter; - import org.onap.ccsdk.oran.a1policymanagementservice.configuration.WebClientConfig.HttpProxyConfig; import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.EnableConfigurationProperties; - import reactor.core.publisher.Flux; import reactor.netty.transport.ProxyProvider; @@ -110,6 +106,18 @@ public class ApplicationConfig { @Value("${app.database-enabled:}") private boolean databaseEnabled; + public enum ValidateSchema { + NONE, + INFO, + WARN, + FAIL + } + + @Getter + @Setter + @Value("${app.validate-policy-instance-schema:NONE}") + private ValidateSchema validatePolicyInstanceSchema; + private Map<String, RicConfig> ricConfigs = new HashMap<>(); private WebClientConfig webClientConfig = null; diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/PolicyService.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/PolicyService.java index f2ee6e18..bd3dbc3d 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/PolicyService.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/PolicyService.java @@ -70,6 +70,8 @@ public class PolicyService { Ric ric = rics.getRic(policyObjectInfo.getNearRtRicId()); PolicyType policyType = policyTypes.getType(policyObjectInfo.getPolicyTypeId()); Policy policy = helper.buildPolicy(policyObjectInfo, policyType, ric, helper.policyIdGeneration(policyObjectInfo), serverWebExchange); + if (Boolean.FALSE.equals(helper.performPolicySchemaValidation(policy, policyType))) + return Mono.error(new ServiceException("Policy Type Schema validation failed in create", HttpStatus.BAD_REQUEST)); return helper.isPolicyAlreadyCreated(policy,policies) .doOnError(errorHandlingService::handleError) .flatMap(policyBuilt -> authorizationService.authCheck(serverWebExchange, policy, AccessType.WRITE) @@ -105,6 +107,9 @@ public class PolicyService { PolicyObjectInformation pos = new PolicyObjectInformation(existingPolicy.getRic().getConfig().getRicId(), body, existingPolicy.getType().getId()); Policy updatedPolicy = helper.buildPolicy(pos, existingPolicy.getType(), existingPolicy.getRic(), policyId, exchange); + PolicyType policyType = policyTypes.getType(pos.getPolicyTypeId()); + if (Boolean.FALSE.equals(helper.performPolicySchemaValidation(updatedPolicy, policyType))) + return Mono.error(new ServiceException("Policy Type Schema validation failed in update", HttpStatus.BAD_REQUEST)); Ric ric = existingPolicy.getRic(); return authorizationService.authCheck(exchange, updatedPolicy, AccessType.WRITE) .doOnError(errorHandlingService::handleError) diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/util/v3/Helper.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/util/v3/Helper.java index 3f301b77..1202c0ff 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/util/v3/Helper.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/util/v3/Helper.java @@ -2,7 +2,7 @@ * ========================LICENSE_START================================= * ONAP : ccsdk oran * ====================================================================== - * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved. + * Copyright (C) 2024-2025 OpenInfra Foundation Europe. All rights reserved. * ====================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,9 @@ package org.onap.ccsdk.oran.a1policymanagementservice.util.v3; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import lombok.RequiredArgsConstructor; +import org.everit.json.schema.loader.SchemaLoader; +import org.json.JSONObject; +import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig; import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException; import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyInformation; import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyObjectInformation; @@ -50,6 +53,8 @@ import java.util.stream.Collectors; @RequiredArgsConstructor public class Helper { + private final ApplicationConfig applicationConfig; + @Autowired private TokenService tokenService; @@ -105,6 +110,53 @@ public class Helper { return true; } + private boolean policyTypeSchemaValidation(Policy policy, PolicyType policyType) { + try { + JSONObject schemaJson = new JSONObject(policyType.getSchema()); + var schema = SchemaLoader.load(schemaJson); + JSONObject policyJson = new JSONObject(policy.getJson()); + + // PUT request body is not automatically deserialized - so we manually extract the desired policy object + if (policyJson.has("policyObject")) { + policyJson = policyJson.getJSONObject("policyObject"); + } + + schema.validate(policyJson); + logger.info("Policy type schema validation successful"); + return true; // Validation passed + } catch (Exception e) { + logger.error("Policy type schema validation failed", e); + return false; // Validation failed + } + } + + public Boolean performPolicySchemaValidation(Policy policy, PolicyType policyType) { + + switch (applicationConfig.getValidatePolicyInstanceSchema()) { + case INFO: + if (policyTypeSchemaValidation(policy, policyType)) { + return true; + } + logger.info("Policy Schema validation failed but not enforced."); + return true; + case WARN: + if (policyTypeSchemaValidation(policy, policyType)) { + return true; + } + logger.warn("Policy Schema validation failed but not enforced."); + return true; + case FAIL: + if (policyTypeSchemaValidation(policy, policyType)) { + return true; + } + logger.error("Policy Schema validation failed."); + return false; + default: + logger.info("Policy schema validation disabled."); + return true; + } + } + public String policyIdGeneration(PolicyObjectInformation policyObjectInfo) { if (policyObjectInfo.getPolicyId() == null || policyObjectInfo.getPolicyId().isEmpty() || policyObjectInfo.getPolicyId().isBlank()) diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerV3Test.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerV3Test.java index 26c10d2a..834b2186 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerV3Test.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerV3Test.java @@ -117,6 +117,7 @@ class PolicyControllerV3Test { void init() { testHelperTest.port = port; this.applicationConfig.setAuthProviderUrl(testHelperTest.baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL); + this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.NONE); } @AfterEach @@ -155,6 +156,93 @@ class PolicyControllerV3Test { testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/")); } + + @Test + @DisplayName("test Create Policy Success when schema validation set to FAIL") + void testPolicyTypeSchemaValidationFail() throws Exception { + this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.FAIL); + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelperTest.addPolicyType(policyTypeName, nonRtRicId); + String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, ""); + Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody); + testHelperTest.testSuccessResponse(responseMono, HttpStatus.CREATED, responseBody -> + responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}")); + testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/")); + } + + + @Test + @DisplayName("test Create Policy Success when schema validation set to INFO") + void testPolicyTypeSchemaValidationInfo() throws Exception { + this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.INFO); + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelperTest.addPolicyType(policyTypeName, nonRtRicId); + String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, ""); + Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody); + testHelperTest.testSuccessResponse(responseMono, HttpStatus.CREATED, responseBody -> + responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}")); + testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/")); + } + + + @Test + @DisplayName("test Create Policy Success when schema validation set to WARN") + void testPolicyTypeSchemaValidationWarn() throws Exception { + this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.WARN); + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelperTest.addPolicyType(policyTypeName, nonRtRicId); + String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, ""); + Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody); + testHelperTest.testSuccessResponse(responseMono, HttpStatus.CREATED, responseBody -> + responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}")); + testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/")); + } + + @Test + @DisplayName("test bad Create Policy when schema validation set to FAIL") + void testBadPolicyTypeSchemaValidationFail() throws Exception { + this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.FAIL); + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelperTest.addPolicyType(policyTypeName, nonRtRicId); + String policyBody = testHelperTest.postBadPolicyBody(nonRtRicId, policyTypeName, ""); + Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody); + testHelperTest.testErrorCode(responseMono, HttpStatus.BAD_REQUEST, "Policy Type Schema validation failed"); + } + + @Test + @DisplayName("test bad Create Policy when schema validation set to INFO") + void testBadPolicyTypeSchemaValidationInfo() throws Exception { + this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.INFO); + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelperTest.addPolicyType(policyTypeName, nonRtRicId); + String policyBody = testHelperTest.postBadPolicyBody(nonRtRicId, policyTypeName, ""); + Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody); + testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/")); + } + + @Test + @DisplayName("test bad Create Policy when schema validation set to WARN") + void testBadPolicyTypeSchemaValidationWarn() throws Exception { + this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.WARN); + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelperTest.addPolicyType(policyTypeName, nonRtRicId); + String policyBody = testHelperTest.postBadPolicyBody(nonRtRicId, policyTypeName, ""); + Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody); + testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/")); + } + @Test @DisplayName("test Create Policy with PolicyID sending") void testPostPolicyWithPolicyID() throws Exception { @@ -320,6 +408,113 @@ class PolicyControllerV3Test { responseBody.contains("{\"scope\":{\"ueId\":\"ue5200\",\"qosId\":\"qos5200\"},\"qosObjectives\":{\"priorityLevel\":5200.0}")); } + + @Test + @DisplayName("test Update Policy Success when schema validation set to FAIL") + void testUpdatePolicyTypeSchemaValidationFail() throws Exception { + this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.FAIL); + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelperTest.addPolicyType(policyTypeName, nonRtRicId); + String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "policyOne"); + testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block(); + String policyBodyForPut = testHelperTest.putPolicyBody(nonRtRicId, policyTypeName, "policyOne", "ue5200", + "qos5200", "5200.0"); + testHelperTest.restClientV3().putForEntity(url+"/policyOne", policyBodyForPut).block(); + Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url+"/policyOne"); + testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody -> + responseBody.contains("{\"scope\":{\"ueId\":\"ue5200\",\"qosId\":\"qos5200\"},\"qosObjectives\":{\"priorityLevel\":5200.0}")); + } + + + @Test + @DisplayName("test Update Policy Success when schema validation set to INFO") + void testUpdatePolicyTypeSchemaValidationInfo() throws Exception { + this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.INFO); + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelperTest.addPolicyType(policyTypeName, nonRtRicId); + String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "policyOne"); + testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block(); + String policyBodyForPut = testHelperTest.putPolicyBody(nonRtRicId, policyTypeName, "policyOne", "ue5200", + "qos5200", "5200.0"); + testHelperTest.restClientV3().putForEntity(url+"/policyOne", policyBodyForPut).block(); + Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url+"/policyOne"); + testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody -> + responseBody.contains("{\"scope\":{\"ueId\":\"ue5200\",\"qosId\":\"qos5200\"},\"qosObjectives\":{\"priorityLevel\":5200.0}")); + } + + + @Test + @DisplayName("test Update Policy Success when schema validation set to WARN") + void testUpdatePolicyTypeSchemaValidationWarn() throws Exception { + this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.WARN); + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelperTest.addPolicyType(policyTypeName, nonRtRicId); + String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "policyOne"); + testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block(); + String policyBodyForPut = testHelperTest.putPolicyBody(nonRtRicId, policyTypeName, "policyOne", "ue5200", + "qos5200", "5200.0"); + testHelperTest.restClientV3().putForEntity(url+"/policyOne", policyBodyForPut).block(); + Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url+"/policyOne"); + testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody -> + responseBody.contains("{\"scope\":{\"ueId\":\"ue5200\",\"qosId\":\"qos5200\"},\"qosObjectives\":{\"priorityLevel\":5200.0}")); + } + + @Test + @DisplayName("test bad Update Policy when schema validation set to FAIL") + void testUpdateBadPolicyTypeSchemaValidationFail() throws Exception { + this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.FAIL); + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelperTest.addPolicyType(policyTypeName, nonRtRicId); + String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "policyOne"); + testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block(); + String policyBodyForPut = testHelperTest.putBadPolicyBody(nonRtRicId, policyTypeName, "policyOne", "ue5200", + "qos5200", "5200.0", "bar"); + Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().putForEntity(url+"/policyOne", policyBodyForPut); + testHelperTest.testErrorCode(responseMono, HttpStatus.BAD_REQUEST, "Policy Type Schema validation failed"); + } + + @Test + @DisplayName("test bad Update Policy when schema validation set to WARN") + void testUpdateBadPolicyTypeSchemaValidationWarn() throws Exception { + this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.WARN); + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelperTest.addPolicyType(policyTypeName, nonRtRicId); + String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "policyOne"); + testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block(); + String policyBodyForPut = testHelperTest.putBadPolicyBody(nonRtRicId, policyTypeName, "policyOne", "ue5200", + "qos5200", "5200.0", "bar"); + Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().putForEntity(url+"/policyOne", policyBodyForPut); + testHelperTest.testSuccessResponse(responseMono, HttpStatus.OK, responseBody -> + responseBody.contains("{\"scope\":{\"ueId\":\"ue5200\",\"qosId\":\"qos5200\",\"foo\":\"bar\"},\"qosObjectives\":{\"priorityLevel\":5200.0}}")); + } + + @Test + @DisplayName("test bad Update Policy when schema validation set to INFO") + void testUpdateBadPolicyTypeSchemaValidationInfo() throws Exception { + this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.INFO); + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelperTest.addPolicyType(policyTypeName, nonRtRicId); + String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "policyOne"); + testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block(); + String policyBodyForPut = testHelperTest.putBadPolicyBody(nonRtRicId, policyTypeName, "policyOne", "ue5200", + "qos5200", "5200.0", "bar"); + Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().putForEntity(url+"/policyOne", policyBodyForPut); + testHelperTest.testSuccessResponse(responseMono, HttpStatus.OK, responseBody -> + responseBody.contains("{\"scope\":{\"ueId\":\"ue5200\",\"qosId\":\"qos5200\",\"foo\":\"bar\"},\"qosObjectives\":{\"priorityLevel\":5200.0}}")); + } + private void postPolicyWithTokenAndVerify(String clientId, String serviceId, String result) throws IOException { testHelperTest.addPolicyType("type1_1.2.3", "ric.1"); String policyBody = testHelperTest.postPolicyBody("ric.1", "type1_1.2.3", "1"); diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/PolicyServiceTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/PolicyServiceTest.java index f2b74ef3..8e0d3c1c 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/PolicyServiceTest.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/PolicyServiceTest.java @@ -122,6 +122,7 @@ class PolicyServiceTest { Policy policy = testHelperTest.buidTestPolicy(testHelperTest.policyObjectInfo(nonRtRicId, policyTypeName), "122344-5674"); when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.TRUE); when(helper.buildPolicy(any(),any(), any(), any(), any())).thenReturn(policy); + when(helper.performPolicySchemaValidation(any(), any())).thenReturn(Boolean.TRUE); when(helper.isPolicyAlreadyCreated(any(), any())).thenReturn(Mono.error(new ServiceException ("Same policy content already created with policy ID: 122344-5674", HttpStatus.BAD_REQUEST))); Mono<ResponseEntity<PolicyObjectInformation>> responseMono = policyService.createPolicyService(testHelperTest.policyObjectInfo(nonRtRicId, policyTypeName), serverWebExchange); @@ -136,6 +137,7 @@ class PolicyServiceTest { testHelperTest.addPolicyType(policyTypeName, nonRtRicId); ServerWebExchange serverWebExchange = Mockito.mock(DefaultServerWebExchange.class); when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.TRUE); + when(helper.performPolicySchemaValidation(any(), any())).thenReturn(Boolean.TRUE); when(helper.isPolicyAlreadyCreated(any(), any())).thenReturn(Mono.just(Policy.builder().build())); when(authorizationService.authCheck(any(), any(), any())).thenReturn(Mono.error(new ServiceException("Not authorized", HttpStatus.UNAUTHORIZED))); Mono<ResponseEntity<PolicyObjectInformation>> responseMono = policyService.createPolicyService(testHelperTest.policyObjectInfo(nonRtRicId, policyTypeName), serverWebExchange); @@ -189,6 +191,7 @@ class PolicyServiceTest { when(helper.buildPolicy(any(),any(), any(), any(), any())).thenReturn(updatedPolicy); when(helper.checkRicStateIdle(any())).thenReturn(Mono.just(updatedPolicy.getRic())); when(helper.checkSupportedType(any(), any())).thenReturn(Mono.just(updatedPolicy.getRic())); + when(helper.performPolicySchemaValidation(any(), any())).thenReturn(Boolean.TRUE); when(authorizationService.authCheck(any(), any(), any())).thenReturn(Mono.just(updatedPolicy)); Mono<ResponseEntity<Object>> responseMono = policyService.putPolicyService(policy.getId(), updatedPolicyObjectInfo.getPolicyObject(), serverWebExchange); testHelperTest.testSuccessResponse(responseMono, HttpStatus.OK, responseBody -> { diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/v3/TestHelperTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/v3/TestHelperTest.java index 78ab385c..c36429c6 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/v3/TestHelperTest.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/v3/TestHelperTest.java @@ -2,7 +2,7 @@ * ========================LICENSE_START================================= * ONAP : ccsdk oran * ====================================================================== - * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved. + * Copyright (C) 2024-2025 OpenInfra Foundation Europe. All rights reserved. * ====================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -170,6 +170,13 @@ public class TestHelperTest { return gson.toJson(policyObjectInfo); } + public String postBadPolicyBody(String nearRtRicId, String policyTypeName, String policyId) { + PolicyObjectInformation policyObjectInfo = new PolicyObjectInformation(nearRtRicId, dummyBadPolicyObject(), policyTypeName); + if (policyId != null && !policyId.isEmpty() && !policyId.isBlank()) + policyObjectInfo.setPolicyId(policyId); + return gson.toJson(policyObjectInfo); + } + public String putPolicyBody(String nearRtRicId, String policyTypeName, String policyId, String ueId, String qosId, String priorityLevel) { PolicyObjectInformation policyObjectInfo = new PolicyObjectInformation(nearRtRicId, dummyPolicyObjectForPut( @@ -179,6 +186,15 @@ public class TestHelperTest { return gson.toJson(policyObjectInfo); } + public String putBadPolicyBody(String nearRtRicId, String policyTypeName, String policyId, String ueId, String qosId, + String priorityLevel, String foo) { + PolicyObjectInformation policyObjectInfo = new PolicyObjectInformation(nearRtRicId, dummyBadPolicyObjectForPut( + ueId, qosId, priorityLevel, foo), policyTypeName); + if (policyId != null && !policyId.isEmpty() && !policyId.isBlank()) + policyObjectInfo.setPolicyId(policyId); + return gson.toJson(policyObjectInfo); + } + public PolicyObjectInformation policyObjectInfo(String nearRtRicId, String policyTypeName) { return gson.fromJson(postPolicyBody(nearRtRicId, policyTypeName, ""), PolicyObjectInformation.class); } @@ -195,6 +211,19 @@ public class TestHelperTest { " }").getAsJsonObject(); } + public JsonObject dummyBadPolicyObjectForPut(String... values) { + return JsonParser.parseString("{\n" + + " \"scope\": {\n" + + " \"ueId\": \"" + values[0] + "\",\n" + + " \"qosId\": \"" + values[1] + "\",\n" + + " \"foo\": \"" + values[3] + "\"\n" + + " },\n" + + " \"qosObjectives\": {\n" + + " \"priorityLevel\": " + values[2] + "\n" + + " }\n" + + " }").getAsJsonObject(); + } + public JsonObject dummyPolicyObject() { return JsonParser.parseString("{\n" + " \"scope\": {\n" + @@ -207,6 +236,19 @@ public class TestHelperTest { " }").getAsJsonObject(); } + public JsonObject dummyBadPolicyObject() { + return JsonParser.parseString("{\n" + + " \"scope\": {\n" + + " \"ueId\": \"ue5100\",\n" + + " \"qosId\": \"qos5100\",\n" + + " \"foo\": \"bar\"\n" + + " },\n" + + " \"qosObjectives\": {\n" + + " \"priorityLevel\": 5100.0\n" + + " }\n" + + " }").getAsJsonObject(); + } + public Policy buidTestPolicy(PolicyObjectInformation policyInfo, String id) throws Exception{ return Policy.builder().ric(rics.getRic(policyInfo.getNearRtRicId())) .type(policyTypes.getType(policyInfo.getPolicyTypeId())) |