diff options
author | raviteja.karumuri <raviteja.karumuri@est.tech> | 2024-04-23 15:56:47 +0100 |
---|---|---|
committer | raviteja.karumuri <raviteja.karumuri@est.tech> | 2024-05-29 17:42:43 +0100 |
commit | d3aad19502465a7ff60d54cedbb06af301c99915 (patch) | |
tree | 54e8a6d4e9bd2ea5bee876a53f1fbd7578d81b35 | |
parent | 87ac816229c2bf3049402d727a2c3edc0592965a (diff) |
Implementing remaining end points for V3 controller
Issue-ID: CCSDK-4006
Change-Id: Ie186c5fc6b3d5eb90297d3a36ff6160ee4667e44
Signed-off-by: Raviteja Karumuri <raviteja.karumuri@est.tech>
7 files changed, 227 insertions, 53 deletions
diff --git a/a1-policy-management/api/pms-api-v3.yaml b/a1-policy-management/api/pms-api-v3.yaml index bdd08006..2caf7086 100644 --- a/a1-policy-management/api/pms-api-v3.yaml +++ b/a1-policy-management/api/pms-api-v3.yaml @@ -882,6 +882,7 @@ components: required: - nearRtRicId - policyObject + - policyTypeId ErrorInformation: description: Problem as defined in https://tools.ietf.org/html/rfc7807 properties: diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerV3.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerV3.java index 36da71da..83d3ecc8 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerV3.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerV3.java @@ -65,31 +65,31 @@ public class PolicyControllerV3 implements A1PolicyManagementApi { @Override public Mono<ResponseEntity<Void>> deletePolicy(String policyId, String accept, ServerWebExchange exchange) throws Exception { - return null; + return policyService.deletePolicyService(policyId, exchange); } @Override public Mono<ResponseEntity<Object>> getPolicy(String policyId, String accept, ServerWebExchange exchange) throws Exception { - return null; + return policyService.getPolicyService(policyId, exchange); } @Override public Mono<ResponseEntity<Flux<PolicyInformation>>> getPolicyIds(String policyTypeId, String nearRtRicId, String serviceId, String typeName, String accept, ServerWebExchange exchange) throws Exception { - return null; + return policyService.getPolicyIdsService(policyTypeId, nearRtRicId, serviceId, typeName, exchange); } @Override public Mono<ResponseEntity<Object>> getPolicyTypeDefinition(String policyTypeId, String accept, ServerWebExchange exchange) throws Exception { - return null; + return policyService.getPolicyTypeDefinitionService(policyTypeId); } @Override public Mono<ResponseEntity<Flux<PolicyTypeInformation>>> getPolicyTypes(String nearRtRicId, String typeName, String compatibleWithVersion, String accept, ServerWebExchange exchange) throws Exception { - return null; + return policyService.getPolicyTypesService(nearRtRicId, typeName, compatibleWithVersion, exchange); } @Override public Mono<ResponseEntity<Object>> putPolicy(String policyId, Mono<Object> body, ServerWebExchange exchange) throws Exception { - return null; + return body.flatMap(payload -> policyService.putPolicyService(policyId, payload, exchange)); } } 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 6c994b42..66b5ae0c 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 @@ -20,22 +20,36 @@ package org.onap.ccsdk.oran.a1policymanagementservice.service.v3; +import com.google.gson.Gson; import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory; import org.onap.ccsdk.oran.a1policymanagementservice.controllers.authorization.PolicyAuthorizationRequest.Input.AccessType; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.Consts; +import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.EntityNotFoundException; 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; +import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyTypeInformation; import org.onap.ccsdk.oran.a1policymanagementservice.repository.*; import org.onap.ccsdk.oran.a1policymanagementservice.util.v3.Helper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import java.lang.invoke.MethodHandles; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Map; + @Service public class PolicyService { + private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); @Autowired private Helper helper; @@ -57,24 +71,27 @@ public class PolicyService { @Autowired private ErrorHandlingService errorHandlingService; + @Autowired + private Gson gson; + public Mono<ResponseEntity<PolicyObjectInformation>> createPolicyService (PolicyObjectInformation policyObjectInfo, ServerWebExchange serverWebExchange) { try { - if (!helper.jsonSchemaValidation(policyObjectInfo.getPolicyObject())) + if (!helper.jsonSchemaValidation(gson.toJson(policyObjectInfo.getPolicyObject(), Map.class))) return Mono.error(new ServiceException("Schema validation failed", HttpStatus.BAD_REQUEST)); Ric ric = rics.getRic(policyObjectInfo.getNearRtRicId()); PolicyType policyType = policyTypes.getType(policyObjectInfo.getPolicyTypeId()); Policy policy = helper.buildPolicy(policyObjectInfo, policyType, ric, helper.policyIdGeneration()); return helper.isPolicyAlreadyCreated(policy,policies) .doOnError(error -> errorHandlingService.handleError(error)) - .flatMap(policyBuilt -> authorizationService.authCheck(serverWebExchange, policy, AccessType.WRITE)) + .flatMap(policyBuilt -> authorizationService.authCheck(serverWebExchange, policy, AccessType.WRITE) .doOnError(error -> errorHandlingService.handleError(error)) .flatMap(policyNotUsed -> ric.getLock().lock(Lock.LockType.SHARED, "createPolicy")) .flatMap(grant -> postPolicy(policy, grant)) .map(locationHeaderValue -> new ResponseEntity<PolicyObjectInformation>(policyObjectInfo,helper.createHttpHeaders( "location",helper.buildURI(policy.getId(), serverWebExchange)), HttpStatus.CREATED)) - .doOnError(error -> errorHandlingService.handleError(error)); + .doOnError(error -> errorHandlingService.handleError(error))); } catch (Exception ex) { return Mono.error(ex); } @@ -93,4 +110,105 @@ public class PolicyService { .doFinally(releaseLock -> grant.unlockBlocking()) .doOnError(error -> errorHandlingService.handleError(error)); } + + public Mono<ResponseEntity<Object>> putPolicyService(String policyId, Object body, ServerWebExchange exchange) { + + try { + Policy existingPolicy = policies.getPolicy(policyId); + PolicyObjectInformation pos = + new PolicyObjectInformation(existingPolicy.getRic().getConfig().getRicId(), body, existingPolicy.getType().getId()); + Policy updatedPolicy = helper.buildPolicy(pos, existingPolicy.getType(), existingPolicy.getRic(), policyId); + Ric ric = existingPolicy.getRic(); + ric.getLock().lock(Lock.LockType.SHARED, "updatePolicy") + .flatMap(grant -> postPolicy(updatedPolicy, grant)) + .doOnError(error -> errorHandlingService.handleError(error)); + return Mono.just(new ResponseEntity<>(policies.getPolicy(policyId), HttpStatus.OK)); + } catch(Exception ex) { + return Mono.error(ex); + } + } + + public Mono<ResponseEntity<Flux<PolicyTypeInformation>>> getPolicyTypesService(String nearRtRicId, String typeName, + String compatibleWithVersion, + ServerWebExchange webExchange) throws Exception { + if (compatibleWithVersion != null && typeName == null) { + throw new ServiceException("Parameter " + Consts.COMPATIBLE_WITH_VERSION_PARAM + " can only be used when " + + Consts.TYPE_NAME_PARAM + " is given", HttpStatus.BAD_REQUEST); + } + Collection<PolicyTypeInformation> listOfPolicyTypes = new ArrayList<>(); + if (nearRtRicId == null || nearRtRicId.isEmpty() || nearRtRicId.isBlank()) { + for(Ric ric : rics.getRics()) { + Collection<PolicyType> policyTypes = PolicyTypes.filterTypes(ric.getSupportedPolicyTypes(), typeName, + compatibleWithVersion); + listOfPolicyTypes.addAll(helper.toPolicyTypeInfoCollection(policyTypes, ric)); + } + } else { + Ric ric = rics.get(nearRtRicId); + if (ric == null) + throw new EntityNotFoundException("Near-RT RIC not Found using ID: " +nearRtRicId); + Collection<PolicyType> policyTypes = PolicyTypes.filterTypes(ric.getSupportedPolicyTypes(), typeName, + compatibleWithVersion); + listOfPolicyTypes.addAll(helper.toPolicyTypeInfoCollection(policyTypes, ric)); + } + return Mono.just(new ResponseEntity<>(Flux.fromIterable(listOfPolicyTypes), HttpStatus.OK)); + } + + public Mono<ResponseEntity<Flux<PolicyInformation>>> getPolicyIdsService(String policyTypeId, String nearRtRicId, + String serviceId, String typeName, + ServerWebExchange exchange) throws EntityNotFoundException { + if ((policyTypeId != null && this.policyTypes.get(policyTypeId) == null)) + throw new EntityNotFoundException("Policy type not found using ID: " +policyTypeId); + if ((nearRtRicId != null && this.rics.get(nearRtRicId) == null)) + throw new EntityNotFoundException("Near-RT RIC not found using ID: " +nearRtRicId); + + Collection<Policy> filtered = policies.filterPolicies(policyTypeId, nearRtRicId, serviceId, typeName); + return Flux.fromIterable(filtered) + .flatMap(policy -> authorizationService.authCheck(exchange, policy, AccessType.READ)) + .onErrorContinue((error,item) -> logger.warn("Error occurred during authorization check for " + + "policy {}: {}", item, error.getMessage())) + .collectList() + .map(authPolicies -> new ResponseEntity<>(helper.toFluxPolicyInformation(authPolicies), HttpStatus.OK)) + .doOnError(error -> logger.error(error.getMessage())); + } + + public Mono<ResponseEntity<Object>> getPolicyService(String policyId, ServerWebExchange serverWebExchange) + throws EntityNotFoundException{ + Policy policy = policies.getPolicy(policyId); + return authorizationService.authCheck(serverWebExchange, policy, AccessType.READ) + .map(x -> new ResponseEntity<Object>(policy.getJson(), HttpStatus.OK)) + .doOnError(error -> errorHandlingService.handleError(error)); + } + + public Mono<ResponseEntity<Object>> getPolicyTypeDefinitionService(String policyTypeId) + throws EntityNotFoundException{ + PolicyType singlePolicyType = policyTypes.get(policyTypeId); + if (singlePolicyType == null) + throw new EntityNotFoundException("PolicyType not found with ID: " + policyTypeId); + return Mono.just(new ResponseEntity<Object>(singlePolicyType.getSchema(), HttpStatus.OK)); + } + + public Mono<ResponseEntity<Void>> deletePolicyService(String policyId, ServerWebExchange serverWebExchange) + throws EntityNotFoundException { + Policy singlePolicy = policies.getPolicy(policyId); + return authorizationService.authCheck(serverWebExchange, singlePolicy, AccessType.WRITE) + .doOnError(error -> errorHandlingService.handleError(error)) + .flatMap(policy -> policy.getRic().getLock().lock(Lock.LockType.SHARED, "deletePolicy")) + .flatMap(grant -> deletePolicy(singlePolicy, grant)) + .doOnError(error -> errorHandlingService.handleError(error)); + } + + private Mono<ResponseEntity<Void>> deletePolicy(Policy policy, Lock.Grant grant) { + System.out.println(); + return helper.checkRicStateIdle(policy.getRic()) + .doOnError(error -> errorHandlingService.handleError(error)) + .flatMap(ric -> helper.checkSupportedType(ric, policy.getType())) + .doOnError(error -> errorHandlingService.handleError(error)) + .flatMap(ric -> a1ClientFactory.createA1Client(ric)) + .flatMap(a1Client -> a1Client.deletePolicy(policy)) + .doOnError(error -> errorHandlingService.handleError(error)) + .doOnNext(policyString -> policies.remove(policy)) + .doFinally(releaseLock -> grant.unlockBlocking()) + .map(successResponse -> new ResponseEntity<Void>(HttpStatus.NO_CONTENT)) + .doOnError(error -> errorHandlingService.handleError(error)); + } } 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 11edbfd9..88205919 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 @@ -23,7 +23,9 @@ package org.onap.ccsdk.oran.a1policymanagementservice.util.v3; import com.google.gson.Gson; import com.google.gson.GsonBuilder; 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; +import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyTypeInformation; import org.onap.ccsdk.oran.a1policymanagementservice.repository.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,13 +33,16 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; +import reactor.core.publisher.Flux; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.util.UriComponentsBuilder; import reactor.core.publisher.Mono; import java.lang.invoke.MethodHandles; import java.time.Instant; +import java.util.Collection; import java.util.UUID; +import java.util.stream.Collectors; @Component public class Helper { @@ -125,4 +130,16 @@ public class Helper { } return Mono.just(policy); } + + public Flux<PolicyInformation> toFluxPolicyInformation(Collection<Policy> policyCollection) { + return Flux.fromIterable(policyCollection.stream() + .map(singlePolicy -> new PolicyInformation(singlePolicy.getId(), singlePolicy.getRic().getConfig().getRicId())) + .collect(Collectors.toList())); + } + + public Collection<PolicyTypeInformation> toPolicyTypeInfoCollection(Collection<PolicyType> policyTypeCollection, Ric ric) { + return policyTypeCollection.stream() + .map(type -> new PolicyTypeInformation(type.getId(), ric.getConfig().getRicId())) + .collect(Collectors.toList()); + } } diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerTest.java index b8404a61..1ec85109 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerTest.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerTest.java @@ -21,7 +21,6 @@ package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v3; import org.junit.jupiter.api.*; -import org.mockito.Mockito; import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext; import org.onap.ccsdk.oran.a1policymanagementservice.config.TestConfig; import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig; @@ -32,8 +31,8 @@ import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services; import org.onap.ccsdk.oran.a1policymanagementservice.util.v3.Helper; -import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory; import org.onap.ccsdk.oran.a1policymanagementservice.utils.v3.TestHelper; +import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -50,6 +49,7 @@ import reactor.core.publisher.Mono; import java.lang.invoke.MethodHandles; import java.nio.file.Path; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; @TestMethodOrder(MethodOrderer.MethodName.class) @@ -68,6 +68,7 @@ public class PolicyControllerTest { @Autowired private ApplicationConfig applicationConfig; + @Autowired private TestHelper testHelper; @@ -138,7 +139,7 @@ public class PolicyControllerTest { testHelper.addPolicyType(policyTypeName, nonRtRicId); String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName); Mono<ResponseEntity<String>> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); - testHelper.testSuccessResponse(responseMono, HttpStatus.CREATED, "{\"servingCellNrcgi\":\"1\"}"); + testHelper.testSuccessResponse(responseMono, HttpStatus.CREATED, "{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}"); } @Test @@ -148,8 +149,7 @@ public class PolicyControllerTest { String policyTypeName = "type1_1.2.3"; String url = "/policies"; testHelper.addPolicyType(policyTypeName, nonRtRicId); - - when(helper.jsonSchemaValidation(Mockito.any())).thenReturn(Boolean.FALSE); + when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.FALSE); String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName); Mono<ResponseEntity<String>> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); testHelper.testErrorCode(responseMono, HttpStatus.BAD_REQUEST, " Schema validation failed"); @@ -161,6 +161,7 @@ public class PolicyControllerTest { String policyTypeName = "type1_1.2.3"; String url = "/policies"; testHelper.addPolicyType(policyTypeName, " "); + when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.TRUE); String policyBody = testHelper.postPolicyBody("noRic", policyTypeName); Mono<ResponseEntity<String>> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); testHelper.testErrorCode(responseMono, HttpStatus.NOT_FOUND, " Could not find ric: noRic"); @@ -173,6 +174,7 @@ public class PolicyControllerTest { String nonRtRicId = "ricOne"; String url = "/policies"; testHelper.addPolicyType(policyTypeName, nonRtRicId); + when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.TRUE); String policyBody = testHelper.postPolicyBody(nonRtRicId, "noPolicyType"); Mono<ResponseEntity<String>> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); testHelper.testErrorCode(responseMono, HttpStatus.NOT_FOUND, "Could not find type: noPolicyType"); 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 a67a0bb3..ee0073a4 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 @@ -20,41 +20,45 @@ package org.onap.ccsdk.oran.a1policymanagementservice.service.v3; -import org.junit.jupiter.api.MethodOrderer; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.*; import org.mockito.Mockito; import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory; import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException; import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyObjectInformation; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy; -import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes; -import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics; +import org.onap.ccsdk.oran.a1policymanagementservice.service.v3.AuthorizationService; +import org.onap.ccsdk.oran.a1policymanagementservice.service.v3.PolicyService; import org.onap.ccsdk.oran.a1policymanagementservice.util.v3.Helper; import org.onap.ccsdk.oran.a1policymanagementservice.utils.v3.TestHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.test.context.TestPropertySource; +import org.springframework.util.FileSystemUtils; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; import reactor.core.publisher.Mono; +import java.io.IOException; +import java.lang.invoke.MethodHandles; +import java.nio.file.Path; + import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; @TestMethodOrder(MethodOrderer.MethodName.class) @SpringBootTest() +@TestPropertySource(properties = { // + "app.vardata-directory=/tmp/pmstestv3", // +}) public class PolicyServiceTest { - @Autowired - private Rics rics; - - @Autowired - private PolicyTypes policyTypes; - + private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); @Autowired private Policies policies; @@ -76,22 +80,38 @@ public class PolicyServiceTest { @MockBean private AuthorizationService authorizationService; + @AfterEach + public void clear() { + policies.clear(); + } + + @AfterAll + static void clearTestDir() { + try { + FileSystemUtils.deleteRecursively(Path.of("/tmp/pmstestv3")); + } catch (Exception e) { + logger.warn("Could test directory : {}", e.getMessage()); + } + } + @Test - public void testPolicyAlreadyCreatedFail() { + public void testPolicyAlreadyCreatedTrue() throws Exception{ String policyTypeName = "uri_type_123"; String nonRtRicId = "Ric_347"; testHelper.addPolicyType(policyTypeName, nonRtRicId); ServerWebExchange serverWebExchange = Mockito.mock(DefaultServerWebExchange.class); + Policy policy = testHelper.buidTestPolicy(testHelper.policyObjectInfo(nonRtRicId, policyTypeName), "122344-5674"); when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.TRUE); + when(helper.buildPolicy(any(),any(), any(), any())).thenReturn(policy); when(helper.isPolicyAlreadyCreated(any(), any())).thenReturn(Mono.error(new ServiceException - ("Policy already created", HttpStatus.CONFLICT))); + ("Same policy content already created with policy ID: 122344-5674", HttpStatus.BAD_REQUEST))); Mono<ResponseEntity<PolicyObjectInformation>> responseMono = policyService.createPolicyService(testHelper.policyObjectInfo(nonRtRicId, policyTypeName), serverWebExchange); - testHelper.verifyMockError(responseMono, "Policy already created"); + testHelper.verifyMockError(responseMono, "Same policy content already created with policy ID: 122344-5674"); } @Test - public void testPolicyNotAuthorizedFail() { + public void testPolicyNotAuthorizedFail() throws IOException { String policyTypeName = "uri_type_123"; String nonRtRicId = "Ric_347"; diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/v3/TestHelper.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/v3/TestHelper.java index bfad88a1..f6b1ce82 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/v3/TestHelper.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/v3/TestHelper.java @@ -22,6 +22,10 @@ package org.onap.ccsdk.oran.a1policymanagementservice.utils.v3; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.io.CharStreams; +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient; import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClientFactory; import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext; @@ -44,10 +48,13 @@ import reactor.core.publisher.Mono; import reactor.test.StepVerifier; import reactor.util.annotation.Nullable; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.time.Instant; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.*; @@ -73,6 +80,9 @@ public class TestHelper { @Autowired private ObjectMapper objectMapper; + @Autowired + private Gson gson; + public int port; public AsyncRestClient restClientV3() { @@ -127,10 +137,13 @@ public class TestHelper { } } - public PolicyType createPolicyType(String policyTypeName) { + public PolicyType createPolicyType(String policyTypeName, String filePath) throws IOException { + InputStream in = getClass().getResourceAsStream(filePath); + assert in != null; + String schema = CharStreams.toString(new InputStreamReader(in, StandardCharsets.UTF_8)); return PolicyType.builder() .id(policyTypeName) - .schema("{\"title\":\"" + policyTypeName + "\"}") + .schema(schema) .build(); } @@ -160,37 +173,40 @@ public class TestHelper { this.rics.put(ric); return ric; } - public PolicyType addPolicyType(String policyTypeName, String ricId) { - PolicyType type = createPolicyType(policyTypeName); + public PolicyType addPolicyType(String policyTypeName, String ricId) throws IOException { + PolicyType type = createPolicyType(policyTypeName, "/policy_types/demo-policy-schema-3.json"); policyTypes.put(type); addRic(ricId).addSupportedPolicyType(type); return type; } - public Map<String,String> jsonString() { - Map<String,String> policyDataInMap = new HashMap<>(); - policyDataInMap.put("servingCellNrcgi","1"); - return policyDataInMap; + public String postPolicyBody(String nearRtRicId, String policyTypeName) { + PolicyObjectInformation policyObjectInfo = new PolicyObjectInformation(nearRtRicId, dummyPolicyObject(), policyTypeName); + return gson.toJson(policyObjectInfo); } - public String postPolicyBody(String nearRtRicId, String policyTypeName) throws JsonProcessingException { - PolicyObjectInformation policyObjectInfo = new PolicyObjectInformation(nearRtRicId, jsonString()); - policyObjectInfo.setPolicyTypeId(policyTypeName); - policyObjectInfo.setPolicyObject(dummyPolicyObject()); - return objectMapper.writeValueAsString(policyObjectInfo); + public PolicyObjectInformation policyObjectInfo(String nearRtRicId, String policyTypeName) { + return gson.fromJson(postPolicyBody(nearRtRicId, policyTypeName), PolicyObjectInformation.class); } - public PolicyObjectInformation policyObjectInfo(String nearRtRicId, String policyTypeName) { - PolicyObjectInformation policyObjectInfo = new PolicyObjectInformation(nearRtRicId, jsonString()); - policyObjectInfo.setPolicyTypeId(policyTypeName); - policyObjectInfo.setPolicyObject(dummyPolicyObject()); - return policyObjectInfo; + public JsonObject dummyPolicyObject() { + return JsonParser.parseString("{\n" + + " \"scope\": {\n" + + " \"ueId\": \"ue5100\",\n" + + " \"qosId\": \"qos5100\"\n" + + " },\n" + + " \"qosObjectives\": {\n" + + " \"priorityLevel\": 5100.0\n" + + " }\n" + + " }").getAsJsonObject(); } - private Map<String,String> dummyPolicyObject() { - Map<String,String> policyDataInMap = new HashMap<>(); - policyDataInMap.put("servingCellNrcgi","1"); - return policyDataInMap; + public Policy buidTestPolicy(PolicyObjectInformation policyInfo, String id) throws Exception{ + return Policy.builder().ric(rics.getRic(policyInfo.getNearRtRicId())) + .type(policyTypes.getType(policyInfo.getPolicyTypeId())) + .json(objectMapper.writeValueAsString(policyInfo.getPolicyObject())) + .lastModified(Instant.now()) + .id(id).build(); } public String createServiceJson(String name, long keepAliveIntervalSeconds) throws JsonProcessingException { |