From 10948b6d86645c7e30d86f787fcf17a0eefd22be Mon Sep 17 00:00:00 2001 From: "raviteja.karumuri" Date: Tue, 30 Apr 2024 22:15:58 +0100 Subject: Implementing other V3 Controllers Issue-ID: CCSDK-4008 Change-Id: I00408d0189f26142f50c8ee3ae90fc98647e7a0d Signed-off-by: Raviteja Karumuri --- a1-policy-management/api/pms-api-v3.yaml | 13 +- a1-policy-management/pom.xml | 10 + .../controllers/v3/ConfigurationControllerV3.java | 54 +++++ .../controllers/v3/PolicyControllerV3.java | 20 +- .../controllers/v3/RicRepositoryControllerV3.java | 69 ++++++ .../controllers/v3/ServiceControllerV3.java | 81 +++++++ .../controllers/v3/StatusControllerV3.java | 64 +++++ .../mappers/v3/RicRepositoryMapper.java | 35 +++ .../mappers/v3/ServiceControllerMapper.java | 35 +++ .../mappers/v3/StatusControllerMapper.java | 30 +++ .../service/v3/PolicyService.java | 2 +- .../a1policymanagementservice/util/v3/Helper.java | 8 +- .../v3/ConfigurationControllerTestV3.java | 128 ++++++++++ .../controllers/v3/PolicyControllerTest.java | 232 ------------------- .../controllers/v3/PolicyControllerTestV3.java | 257 +++++++++++++++++++++ .../v3/RicRepositoryControllerTestV3.java | 123 ++++++++++ .../controllers/v3/ServiceControllerTestV3.java | 141 +++++++++++ .../utils/v3/TestHelper.java | 14 +- csit/scripts/healthcheck/docker-compose.yml | 2 +- 19 files changed, 1068 insertions(+), 250 deletions(-) create mode 100644 a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ConfigurationControllerV3.java create mode 100644 a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/RicRepositoryControllerV3.java create mode 100644 a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ServiceControllerV3.java create mode 100644 a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/StatusControllerV3.java create mode 100644 a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/mappers/v3/RicRepositoryMapper.java create mode 100644 a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/mappers/v3/ServiceControllerMapper.java create mode 100644 a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/mappers/v3/StatusControllerMapper.java create mode 100644 a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ConfigurationControllerTestV3.java delete mode 100644 a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerTest.java create mode 100644 a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerTestV3.java create mode 100644 a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/RicRepositoryControllerTestV3.java create mode 100644 a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ServiceControllerTestV3.java diff --git a/a1-policy-management/api/pms-api-v3.yaml b/a1-policy-management/api/pms-api-v3.yaml index 2caf7086..3d390569 100644 --- a/a1-policy-management/api/pms-api-v3.yaml +++ b/a1-policy-management/api/pms-api-v3.yaml @@ -19,7 +19,7 @@ openapi: 3.0.3 info: title: 'A1 policy management API' - version: 3.0.0 + version: 1.0.0 x-api-id: a31c510b-20e6-4a08-af16-368c44d7fba8 x-audience: external-public description: "

General

The O-RAN Non-RT RIC Policy Management Service\ @@ -168,7 +168,7 @@ paths: summary: Query Near-RT RIC information tags: - NearRT-RIC Repository - /policyTypes: + /policytypes: get: operationId: getPolicyTypes parameters: @@ -238,7 +238,7 @@ paths: description: Query policy type identities tags: - A1 Policy Management - /policyTypes/{policyTypeId}: + /policytypes/{policyTypeId}: get: operationId: getPolicyTypeDefinition parameters: @@ -301,7 +301,7 @@ paths: content: application/json: schema: - type: object + $ref: '#/components/schemas/PolicyObject' examples: policyObject: $ref: '#/components/examples/PolicyObject' @@ -863,6 +863,11 @@ components: \ false." nullable: false type: boolean + policyId: + description: identity of the Policy + type: string + example: + 'POLICY-ID' serviceId: description: the identity of the service owning the policy. This can be used to group the policies (it is possible to get all policies associated diff --git a/a1-policy-management/pom.xml b/a1-policy-management/pom.xml index 9480201e..b9b34a27 100644 --- a/a1-policy-management/pom.xml +++ b/a1-policy-management/pom.xml @@ -127,6 +127,16 @@ lombok provided + + org.mapstruct + mapstruct + 1.5.5.Final + + + org.mapstruct + mapstruct-processor + 1.5.5.Final + org.springframework.boot diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ConfigurationControllerV3.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ConfigurationControllerV3.java new file mode 100644 index 00000000..d04254c0 --- /dev/null +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ConfigurationControllerV3.java @@ -0,0 +1,54 @@ +/*- + * ========================LICENSE_START================================= + * Copyright (C) 2020-2023 Nordix Foundation. 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ========================LICENSE_END=================================== + */ + +package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v3; + +import io.swagger.v3.oas.annotations.tags.Tag; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.api.v3.ConfigurationApi; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.ConfigurationController; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +@RestController("ConfigurationControllerV3") +@Tag( // + name = ConfigurationControllerV3.API_NAME, // + description = ConfigurationControllerV3.API_DESCRIPTION // +) +@RequestMapping("/a1policymanagement/v1") +public class ConfigurationControllerV3 implements ConfigurationApi { + + public static final String API_NAME = "Management of configuration"; + public static final String API_DESCRIPTION = "API used to create or fetch the application configuration"; + + @Autowired + private ConfigurationController configurationController; + + @Override + public Mono> getConfiguration(ServerWebExchange exchange) throws Exception { + return configurationController.getConfiguration(exchange); + } + + @Override + public Mono> putConfiguration(Mono body, ServerWebExchange exchange) throws Exception { + return configurationController.putConfiguration(body, exchange); + } +} 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 edc3be12..4d9f3277 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 @@ -57,34 +57,38 @@ public class PolicyControllerV3 implements A1PolicyManagementApi { @Override public Mono> createPolicy(Mono policyObjectInformation, ServerWebExchange exchange) { - return policyObjectInformation.flatMap(policyObjectInfo -> { - return policyService.createPolicyService(policyObjectInfo, exchange); - }); + return policyObjectInformation.flatMap(policyObjectInfo -> policyService.createPolicyService(policyObjectInfo, exchange) + .doOnError(error -> errorHandlingService.handleError(error))); } @Override public Mono> deletePolicy(String policyId, String accept, ServerWebExchange exchange) throws Exception { - return policyService.deletePolicyService(policyId, exchange); + return policyService.deletePolicyService(policyId, exchange) + .doOnError(error -> errorHandlingService.handleError(error)); } @Override public Mono> getPolicy(String policyId, String accept, ServerWebExchange exchange) throws Exception { - return policyService.getPolicyService(policyId, exchange); + return policyService.getPolicyService(policyId, exchange) + .doOnError(error -> errorHandlingService.handleError(error)); } @Override public Mono>> getPolicyIds(String policyTypeId, String nearRtRicId, String serviceId, String typeName, String accept, ServerWebExchange exchange) throws Exception { - return policyService.getPolicyIdsService(policyTypeId, nearRtRicId, serviceId, typeName, exchange); + return policyService.getPolicyIdsService(policyTypeId, nearRtRicId, serviceId, typeName, exchange) + .doOnError(error -> errorHandlingService.handleError(error)); } @Override public Mono> getPolicyTypeDefinition(String policyTypeId, String accept, ServerWebExchange exchange) throws Exception { - return policyService.getPolicyTypeDefinitionService(policyTypeId); + return policyService.getPolicyTypeDefinitionService(policyTypeId) + .doOnError(error -> errorHandlingService.handleError(error)); } @Override public Mono>> getPolicyTypes(String nearRtRicId, String typeName, String compatibleWithVersion, String accept, ServerWebExchange exchange) throws Exception { - return policyService.getPolicyTypesService(nearRtRicId, typeName, compatibleWithVersion, exchange); + return policyService.getPolicyTypesService(nearRtRicId, typeName, compatibleWithVersion, exchange) + .doOnError(error -> errorHandlingService.handleError(error)); } @Override diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/RicRepositoryControllerV3.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/RicRepositoryControllerV3.java new file mode 100644 index 00000000..9160bad2 --- /dev/null +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/RicRepositoryControllerV3.java @@ -0,0 +1,69 @@ +/*- + * ========================LICENSE_START================================= + * ONAP : ccsdk oran + * ====================================================================== + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ========================LICENSE_END=================================== + */ + +package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v3; + +import io.swagger.v3.oas.annotations.tags.Tag; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.api.v3.NearRtRicRepositoryApi; +import org.onap.ccsdk.oran.a1policymanagementservice.mappers.v3.RicRepositoryMapper; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.RicRepositoryController; +import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.RicInfo; +import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.RicInfoList; +import org.onap.ccsdk.oran.a1policymanagementservice.service.v3.ErrorHandlingService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +@RestController("RicRepositoryControllerV3") +@Tag( + name = RicRepositoryControllerV3.API_NAME, + description = RicRepositoryControllerV3.API_DESCRIPTION +) +@RequestMapping("/a1policymanagement/v1") +public class RicRepositoryControllerV3 implements NearRtRicRepositoryApi { + + public static final String API_NAME = "NearRT-RIC Repository V3"; + public static final String API_DESCRIPTION = "API used to get the NearRT-RIC for the managed element"; + @Autowired + private RicRepositoryController ricRepositoryController; + + @Autowired + private RicRepositoryMapper ricRepositoryMapper; + + @Autowired + ErrorHandlingService errorHandlingService; + + @Override + public Mono> getRic(String managedElementId, String ricId, String accept, ServerWebExchange exchange) throws Exception { + return ricRepositoryController.getRic(managedElementId, ricId, exchange) + .map(responseEntity -> new ResponseEntity<>(ricRepositoryMapper.toRicInfoV3(responseEntity.getBody()), responseEntity.getStatusCode())) + .doOnError(error -> errorHandlingService.handleError(error)); + } + + @Override + public Mono> getRics(String policyTypeId, String accept, ServerWebExchange exchange) throws Exception { + return ricRepositoryController.getRics(policyTypeId, exchange) + .map(responseEntity -> new ResponseEntity<>(ricRepositoryMapper.toRicInfoListV3(responseEntity.getBody()), responseEntity.getStatusCode())) + .doOnError(error -> errorHandlingService.handleError(error)); + } +} diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ServiceControllerV3.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ServiceControllerV3.java new file mode 100644 index 00000000..1d5461c3 --- /dev/null +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ServiceControllerV3.java @@ -0,0 +1,81 @@ +/*- + * ========================LICENSE_START================================= + * ONAP : ccsdk oran + * ====================================================================== + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ========================LICENSE_END=================================== + */ + +package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v3; + +import io.swagger.v3.oas.annotations.tags.Tag; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.api.v3.ServiceRegistryAndSupervisionApi; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.ServiceController; +import org.onap.ccsdk.oran.a1policymanagementservice.mappers.v3.ServiceControllerMapper; +import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.ServiceRegistrationInfo; +import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.ServiceStatusList; +import org.onap.ccsdk.oran.a1policymanagementservice.service.v3.ErrorHandlingService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +@RestController("ServiceControllerV3") +@Tag( // + name = ServiceControllerV3.API_NAME, // + description = ServiceControllerV3.API_DESCRIPTION // +) +@RequestMapping("/a1policymanagement/v1") +public class ServiceControllerV3 implements ServiceRegistryAndSupervisionApi { + + public static final String API_NAME = "Service Registry and Supervision"; + public static final String API_DESCRIPTION = "API used to keep the service Alive with in the timeout period"; + + @Autowired + private ServiceController serviceController; + + @Autowired + private ServiceControllerMapper serviceControllerMapper; + + @Autowired + ErrorHandlingService errorHandlingService; + + @Override + public Mono> deleteService(String serviceId, String accept, ServerWebExchange exchange) throws Exception { + return serviceController.deleteService(serviceId, exchange); + } + + @Override + public Mono> getServices(String serviceId, String accept, ServerWebExchange exchange) throws Exception { + return serviceController.getServices(serviceId, exchange) + .map(responseEntity -> new ResponseEntity<>(serviceControllerMapper.toServiceStatusListV3( + responseEntity.getBody()), responseEntity.getStatusCode())) + .doOnError(error -> errorHandlingService.handleError(error)); + } + + @Override + public Mono> keepAliveService(String serviceId, String accept, Mono body, ServerWebExchange exchange) throws Exception { + return serviceController.keepAliveService(serviceId, exchange); + } + + @Override + public Mono> putService(Mono serviceRegistrationInfo, ServerWebExchange exchange) throws Exception { + return serviceController.putService(serviceRegistrationInfo.map(serviceRegistrationInfoV2 -> + serviceControllerMapper.toServiceRegistrationInfoV2(serviceRegistrationInfoV2)), exchange) + .doOnError(error -> errorHandlingService.handleError(error)); + } +} diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/StatusControllerV3.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/StatusControllerV3.java new file mode 100644 index 00000000..4bed9fe8 --- /dev/null +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/StatusControllerV3.java @@ -0,0 +1,64 @@ +/*- + * ========================LICENSE_START================================= + * ONAP : ccsdk oran + * ====================================================================== + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ========================LICENSE_END=================================== + */ + +package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v3; + +import io.swagger.v3.oas.annotations.tags.Tag; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.api.v3.HealthCheckApi; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.RicRepositoryController; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.StatusController; +import org.onap.ccsdk.oran.a1policymanagementservice.mappers.v3.StatusControllerMapper; +import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.StatusInfo; +import org.onap.ccsdk.oran.a1policymanagementservice.service.v3.ErrorHandlingService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +@RestController("StatusControllerV3") +@Tag( // + name = StatusControllerV3.API_NAME, // + description = StatusControllerV3.API_DESCRIPTION // +) +@RequestMapping("/a1policymanagement/v1") +public class StatusControllerV3 implements HealthCheckApi { + + public static final String API_NAME = "Health Check"; + public static final String API_DESCRIPTION = "API used to get the health status and statistics of this service"; + + @Autowired + private StatusController statusController; + + @Autowired + private StatusControllerMapper statusControllerMapper; + + @Autowired + ErrorHandlingService errorHandlingService; + + @Override + public Mono> getStatus(ServerWebExchange exchange) throws Exception { + return statusController.getStatus(exchange) + .map(statusInfoResponseEntity -> new ResponseEntity<>(statusControllerMapper.toStatusInfoV3 + (statusInfoResponseEntity.getBody()), statusInfoResponseEntity.getStatusCode())) + .doOnError(error -> errorHandlingService.handleError(error)); + } +} diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/mappers/v3/RicRepositoryMapper.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/mappers/v3/RicRepositoryMapper.java new file mode 100644 index 00000000..c616d247 --- /dev/null +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/mappers/v3/RicRepositoryMapper.java @@ -0,0 +1,35 @@ +/*- + * ========================LICENSE_START================================= + * ONAP : ccsdk oran + * ====================================================================== + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ========================LICENSE_END=================================== + */ + +package org.onap.ccsdk.oran.a1policymanagementservice.mappers.v3; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.RicInfo; +import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.RicInfoList; + +@Mapper(componentModel = "spring") +public interface RicRepositoryMapper { + + @Mapping(source = "policytypeIds", target = "policyTypeIds") + RicInfo toRicInfoV3(org.onap.ccsdk.oran.a1policymanagementservice.models.v2.RicInfo ricInfoV2); + + RicInfoList toRicInfoListV3(org.onap.ccsdk.oran.a1policymanagementservice.models.v2.RicInfoList ricInfoListV2); +} diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/mappers/v3/ServiceControllerMapper.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/mappers/v3/ServiceControllerMapper.java new file mode 100644 index 00000000..37f52502 --- /dev/null +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/mappers/v3/ServiceControllerMapper.java @@ -0,0 +1,35 @@ +/*- + * ========================LICENSE_START================================= + * ONAP : ccsdk oran + * ====================================================================== + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ========================LICENSE_END=================================== + */ + +package org.onap.ccsdk.oran.a1policymanagementservice.mappers.v3; + +import org.mapstruct.Mapper; +import org.onap.ccsdk.oran.a1policymanagementservice.models.v2.ServiceRegistrationInfo; +import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.ServiceStatusList; + +@Mapper(componentModel = "spring") +public interface ServiceControllerMapper { + + ServiceRegistrationInfo toServiceRegistrationInfoV2( + org.onap.ccsdk.oran.a1policymanagementservice.models.v3.ServiceRegistrationInfo serviceRegistrationInfo); + + ServiceStatusList toServiceStatusListV3( + org.onap.ccsdk.oran.a1policymanagementservice.models.v2.ServiceStatusList serviceStatusList); +} diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/mappers/v3/StatusControllerMapper.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/mappers/v3/StatusControllerMapper.java new file mode 100644 index 00000000..17b858ba --- /dev/null +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/mappers/v3/StatusControllerMapper.java @@ -0,0 +1,30 @@ +/*- + * ========================LICENSE_START================================= + * ONAP : ccsdk oran + * ====================================================================== + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ========================LICENSE_END=================================== + */ + +package org.onap.ccsdk.oran.a1policymanagementservice.mappers.v3; + +import org.mapstruct.Mapper; +import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.StatusInfo; + +@Mapper(componentModel = "spring") +public interface StatusControllerMapper { + + StatusInfo toStatusInfoV3(org.onap.ccsdk.oran.a1policymanagementservice.models.v2.StatusInfo statusInfo); +} 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 6cd9b8d3..a892bfa3 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 @@ -81,7 +81,7 @@ public class PolicyService { 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()); + Policy policy = helper.buildPolicy(policyObjectInfo, policyType, ric, helper.policyIdGeneration(policyObjectInfo)); return helper.isPolicyAlreadyCreated(policy,policies) .doOnError(error -> errorHandlingService.handleError(error)) .flatMap(policyBuilt -> authorizationService.authCheck(serverWebExchange, policy, AccessType.WRITE) 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 88205919..638d9504 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 @@ -102,8 +102,12 @@ public class Helper { return true; } - public String policyIdGeneration() { - return UUID.randomUUID().toString(); + public String policyIdGeneration(PolicyObjectInformation policyObjectInfo) { + if (policyObjectInfo.getPolicyId() == null || policyObjectInfo.getPolicyId().isEmpty() || + policyObjectInfo.getPolicyId().isBlank()) + return UUID.randomUUID().toString(); + else + return policyObjectInfo.getPolicyId().trim(); } public String toJson(Object jsonObject) { diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ConfigurationControllerTestV3.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ConfigurationControllerTestV3.java new file mode 100644 index 00000000..c634ecc5 --- /dev/null +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ConfigurationControllerTestV3.java @@ -0,0 +1,128 @@ +/*- + * ========================LICENSE_START================================= + * Copyright (C) 2020-2023 Nordix Foundation. 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ========================LICENSE_END=================================== + */ + +package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v3; + +import org.junit.jupiter.api.*; +import org.junit.jupiter.api.io.TempDir; +import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics; +import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RefreshConfigTask; +import org.onap.ccsdk.oran.a1policymanagementservice.utils.v3.TestHelper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; +import org.springframework.boot.web.servlet.server.ServletWebServerFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.TestPropertySource; +import reactor.core.publisher.Mono; + +import java.io.File; +import java.lang.reflect.Field; +import java.time.Duration; +import java.util.Objects; + +import static org.awaitility.Awaitility.await; +import static org.hamcrest.CoreMatchers.equalTo; + +@TestMethodOrder(MethodOrderer.MethodName.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@TestPropertySource(properties = { // + "server.ssl.key-store=./config/keystore.jks", // + "app.webclient.trust-store=./config/truststore.jks", // + "app.vardata-directory=./target", // + "app.config-file-schema-path=/application_configuration_schema.json" // +}) +class ConfigurationControllerTestV3 { + @Autowired + ApplicationContext context; + + @Autowired + ApplicationConfig applicationConfig; + + @Autowired + private Rics rics; + + @Autowired + private TestHelper testHelper; + + @TempDir + public static File temporaryFolder; + private static File configFile; + + @LocalServerPort + private int port; + + @BeforeEach + void init() { + testHelper.port = port; + } + @BeforeAll + static void setup() throws Exception { + Field f1 = RefreshConfigTask.class.getDeclaredField("configRefreshInterval"); + f1.setAccessible(true); + f1.set(null, Duration.ofSeconds(1)); + } + + public static class MockApplicationConfig extends ApplicationConfig { + @Override + public String getLocalConfigurationFilePath() { + configFile = new File(temporaryFolder, "config.json"); + return configFile.getAbsolutePath(); + } + } + + /** + * Overrides the BeanFactory. + */ + @TestConfiguration + static class TestBeanFactory { + @Bean + public ApplicationConfig getApplicationConfig() { + return new MockApplicationConfig(); + } + + @Bean + public ServletWebServerFactory servletContainer() { + return new TomcatServletWebServerFactory(); + } + } + + @Test + void testPutConfiguration() throws Exception { + Mono> responseEntityMono = testHelper.restClientV3().putForEntity("/configuration", + testHelper.configAsString()); + testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, Objects::isNull); + //put Valid Configuration With New Ric should Update Repository. So, will wait until the ric size is 2 + await().until(rics::size, equalTo(2)); + //test Get Configuration + Mono> responseGetConfigMono = testHelper.restClientV3().getForEntity("/configuration"); + testHelper.testSuccessResponse(responseGetConfigMono, HttpStatus.OK, responseBody -> responseBody.contains("config")); + } + + @Test + public void testHealthCheck() { + Mono> responseHealthCheckMono = testHelper.restClientV3().getForEntity("/status"); + testHelper.testSuccessResponse(responseHealthCheckMono, HttpStatus.OK, responseBody -> responseBody.contains("status")); + } +} 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 deleted file mode 100644 index d92097bd..00000000 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerTest.java +++ /dev/null @@ -1,232 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * ONAP : ccsdk oran - * ====================================================================== - * Copyright (C) 2024 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ========================LICENSE_END=================================== - */ - -package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v3; - -import com.google.gson.Gson; -import org.junit.jupiter.api.*; -import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext; -import org.onap.ccsdk.oran.a1policymanagementservice.config.TestConfig; -import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig; -import org.onap.ccsdk.oran.a1policymanagementservice.controllers.OpenPolicyAgentSimulatorController; -import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.RappSimulatorController; -import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyObjectInformation; -import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies; -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.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; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.SpyBean; -import org.springframework.boot.test.web.server.LocalServerPort; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.TestPropertySource; -import org.springframework.util.FileSystemUtils; -import reactor.core.publisher.Mono; - -import java.lang.invoke.MethodHandles; -import java.nio.file.Path; -import java.util.Objects; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; - -@TestMethodOrder(MethodOrderer.MethodName.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@ContextConfiguration(classes = TestConfig.class) -@TestPropertySource(properties = { // - "server.ssl.key-store=./config/keystore.jks", // - "app.webclient.trust-store=./config/truststore.jks", // - "app.webclient.trust-store-used=true", // - "app.vardata-directory=/tmp/pmstestv3", // - "app.filepath=", // - "app.s3.bucket=" // If this is set, S3 will be used to store data. -}) -public class PolicyControllerTest { - private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - - @Autowired - private ApplicationConfig applicationConfig; - - @Autowired - private TestHelper testHelper; - - @Autowired - private Rics rics; - - @Autowired - private Policies policies; - - @Autowired - private PolicyTypes policyTypes; - - @Autowired - private Services services; - - @Autowired - private MockA1ClientFactory a1ClientFactory; - - @Autowired - private RappSimulatorController rAppSimulator; - - @Autowired - private SecurityContext securityContext; - - @Autowired - private OpenPolicyAgentSimulatorController openPolicyAgentSimulatorController; - - @Autowired - private Gson gson; - - @LocalServerPort - private int port; - - @SpyBean - private Helper helper; - - @BeforeEach - void init() { - testHelper.port = port; - this.applicationConfig.setAuthProviderUrl(testHelper.baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL); - } - - @AfterEach - void reset() { - rics.clear(); - policies.clear(); - policyTypes.clear(); - services.clear(); - a1ClientFactory.reset(); - this.rAppSimulator.getTestResults().clear(); - this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app - this.securityContext.setAuthTokenFilePath(null); - this.openPolicyAgentSimulatorController.getTestResults().reset(); - } - - @AfterAll - static void clearTestDir() { - try { - FileSystemUtils.deleteRecursively(Path.of("/tmp/pmstestv3")); - } catch (Exception e) { - logger.warn("Could test directory : {}", e.getMessage()); - } - } - - @Test - @DisplayName("test Create Policy") - void testPostPolicy() throws Exception { - String nonRtRicId = "ric.1"; - String policyTypeName = "type1_1.2.3"; - String url = "/policies"; - testHelper.addPolicyType(policyTypeName, nonRtRicId); - String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName); - Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); - testHelper.testSuccessResponse(responseMono, HttpStatus.CREATED, responseBody -> - responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}")); - testHelper.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1policymanagement/v1/policies/")); - } - - @Test - @DisplayName("test delete Policy") - void testDeletePolicy() throws Exception { - String nonRtRicId = "ric.1"; - String policyTypeName = "type1_1.2.3"; - String url = "/policies"; - testHelper.addPolicyType(policyTypeName, nonRtRicId); - String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName); - Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); - String []locationHeader = Objects.requireNonNull(Objects.requireNonNull(responseMono.block()).getHeaders() - .get("location")).get(0).split("/"); - String policyID = locationHeader[(locationHeader.length) - 1]; - Mono> responseMonoDelete = testHelper.restClientV3().deleteForEntity(url+"/" +policyID); - testHelper.testSuccessResponse(responseMonoDelete, HttpStatus.NO_CONTENT, responseBody -> true); - } - - @Test - @DisplayName("test Create Policy schema validation fail case") - void testPolicySchemaValidationFail() throws Exception { - String nonRtRicId = "ric.1"; - String policyTypeName = "type1_1.2.3"; - String url = "/policies"; - testHelper.addPolicyType(policyTypeName, nonRtRicId); - when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.FALSE); - String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName); - Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); - testHelper.testErrorCode(responseMono, HttpStatus.BAD_REQUEST, " Schema validation failed"); - } - - @Test - @DisplayName("test Create Policy No Ric fail case") - void testCreatePolicyNoRic() throws Exception { - 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> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); - testHelper.testErrorCode(responseMono, HttpStatus.NOT_FOUND, " Could not find ric: noRic"); - } - - @Test - @DisplayName("test Create Policy with No Policy Type fail case") - void testCreatePolicyNoPolicyType() throws Exception { - String policyTypeName = "type1_1.2.3"; - String nonRtRicId = "ricOne"; - String url = "/policies"; - testHelper.addPolicyType(policyTypeName, nonRtRicId); - when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.TRUE); - String policyBody = testHelper.postPolicyBody(nonRtRicId, "noPolicyType"); - Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); - testHelper.testErrorCode(responseMono, HttpStatus.NOT_FOUND, "Could not find type: noPolicyType"); - } - - @Test - public void testGetPolicyTypesNoRicFound() throws Exception{ - String policyTypeName = "type1_1.2.3"; - String nonRtRicId = "ricOne"; - testHelper.addPolicyType(policyTypeName, nonRtRicId); - Mono> responseMono = testHelper.restClientV3().getForEntity("/policyTypes" + "?nearRtRicId=\"noRic\""); - testHelper.testErrorCode(responseMono, HttpStatus.NOT_FOUND, "Near-RT RIC not Found using ID:"); - } - - @Test - @DisplayName("test get Policy") - void testGetPolicy() throws Exception { - String nonRtRicId = "ric.1"; - String policyTypeName = "type1_1.2.3"; - String url = "/policies"; - testHelper.addPolicyType(policyTypeName, nonRtRicId); - String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName); - Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); - String []locationHeader = Objects.requireNonNull(Objects.requireNonNull(responseMono.block()).getHeaders() - .get("location")).get(0).split("/"); - String policyID = locationHeader[(locationHeader.length) - 1]; - Mono> responseMonoGet = testHelper.restClientV3().getForEntity(url+"/" +policyID); - testHelper.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody -> - responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}")); - } -} diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerTestV3.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerTestV3.java new file mode 100644 index 00000000..a8c5fbae --- /dev/null +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerTestV3.java @@ -0,0 +1,257 @@ +/*- + * ========================LICENSE_START================================= + * ONAP : ccsdk oran + * ====================================================================== + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ========================LICENSE_END=================================== + */ + +package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v3; + +import com.google.gson.Gson; +import org.junit.jupiter.api.*; +import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext; +import org.onap.ccsdk.oran.a1policymanagementservice.config.TestConfig; +import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.OpenPolicyAgentSimulatorController; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.RappSimulatorController; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies; +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.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.SpyBean; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.util.FileSystemUtils; +import reactor.core.publisher.Mono; + +import java.lang.invoke.MethodHandles; +import java.nio.file.Path; +import java.util.Objects; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +@TestMethodOrder(MethodOrderer.MethodName.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ContextConfiguration(classes = TestConfig.class) +@TestPropertySource(properties = { // + "server.ssl.key-store=./config/keystore.jks", // + "app.webclient.trust-store=./config/truststore.jks", // + "app.webclient.trust-store-used=true", // + "app.vardata-directory=/tmp/pmstestv3", // + "app.filepath=", // + "app.s3.bucket=" // If this is set, S3 will be used to store data. +}) +public class PolicyControllerTestV3 { + private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + @Autowired + private ApplicationConfig applicationConfig; + + @Autowired + private TestHelper testHelper; + + @Autowired + private Rics rics; + + @Autowired + private Policies policies; + + @Autowired + private PolicyTypes policyTypes; + + @Autowired + private Services services; + + @Autowired + private MockA1ClientFactory a1ClientFactory; + + @Autowired + private RappSimulatorController rAppSimulator; + + @Autowired + private SecurityContext securityContext; + + @Autowired + private OpenPolicyAgentSimulatorController openPolicyAgentSimulatorController; + + @Autowired + private Gson gson; + + @LocalServerPort + private int port; + + @SpyBean + private Helper helper; + + @BeforeEach + void init() { + testHelper.port = port; + this.applicationConfig.setAuthProviderUrl(testHelper.baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL); + } + + @AfterEach + void reset() { + rics.clear(); + policies.clear(); + policyTypes.clear(); + services.clear(); + a1ClientFactory.reset(); + this.rAppSimulator.getTestResults().clear(); + this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app + this.securityContext.setAuthTokenFilePath(null); + this.openPolicyAgentSimulatorController.getTestResults().reset(); + } + + @AfterAll + static void clearTestDir() { + try { + FileSystemUtils.deleteRecursively(Path.of("/tmp/pmstestv3")); + } catch (Exception e) { + logger.warn("Could test directory : {}", e.getMessage()); + } + } + + @Test + @DisplayName("test Create Policy") + void testPostPolicy() throws Exception { + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName, ""); + Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); + testHelper.testSuccessResponse(responseMono, HttpStatus.CREATED, responseBody -> + responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}")); + testHelper.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1policymanagement/v1/policies/")); + } + + @Test + @DisplayName("test Create Policy with PolicyID sending") + void testPostPolicyWithPolicyID() throws Exception { + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName, "1"); + Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); + testHelper.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1policymanagement/v1/policies/1")); + } + + @Test + @DisplayName("test Create Policy with exisitng policy id") + void testPostPolicyWithExistingPolicyID() throws Exception { + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + String policyId = "policy_5g"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName, policyId); + testHelper.restClientV3().postForEntity(url, policyBody).block(); + Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); + testHelper.testErrorCode(responseMono, HttpStatus.CONFLICT, "Policy already created with ID: " +policyId); + } + + @Test + @DisplayName("test delete Policy") + void testDeletePolicy() throws Exception { + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName, ""); + Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); + String []locationHeader = Objects.requireNonNull(Objects.requireNonNull(responseMono.block()).getHeaders() + .get("location")).get(0).split("/"); + String policyID = locationHeader[(locationHeader.length) - 1]; + Mono> responseMonoDelete = testHelper.restClientV3().deleteForEntity(url+"/" +policyID); + testHelper.testSuccessResponse(responseMonoDelete, HttpStatus.NO_CONTENT, responseBody -> true); + } + + @Test + @DisplayName("test Create Policy schema validation fail case") + void testPolicySchemaValidationFail() throws Exception { + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.FALSE); + String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName, ""); + Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); + testHelper.testErrorCode(responseMono, HttpStatus.BAD_REQUEST, " Schema validation failed"); + } + + @Test + @DisplayName("test Create Policy No Ric fail case") + void testCreatePolicyNoRic() throws Exception { + 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> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); + testHelper.testErrorCode(responseMono, HttpStatus.NOT_FOUND, " Could not find ric: noRic"); + } + + @Test + @DisplayName("test Create Policy with No Policy Type fail case") + void testCreatePolicyNoPolicyType() throws Exception { + String policyTypeName = "type1_1.2.3"; + String nonRtRicId = "ricOne"; + String url = "/policies"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.TRUE); + String policyBody = testHelper.postPolicyBody(nonRtRicId, "noPolicyType", ""); + Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); + testHelper.testErrorCode(responseMono, HttpStatus.NOT_FOUND, "Could not find type: noPolicyType"); + } + + @Test + public void testGetPolicyTypesNoRicFound() throws Exception{ + String policyTypeName = "type1_1.2.3"; + String nonRtRicId = "ricOne"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + Mono> responseMono = testHelper.restClientV3().getForEntity("/policytypes" + "?nearRtRicId=\"noRic\""); + testHelper.testErrorCode(responseMono, HttpStatus.NOT_FOUND, "Near-RT RIC not Found using ID:"); + } + + @Test + @DisplayName("test get Policy") + void testGetPolicy() throws Exception { + String nonRtRicId = "ric.1"; + String policyTypeName = "type1_1.2.3"; + String url = "/policies"; + testHelper.addPolicyType(policyTypeName, nonRtRicId); + String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName, ""); + Mono> responseMono = testHelper.restClientV3().postForEntity(url, policyBody); + String []locationHeader = Objects.requireNonNull(Objects.requireNonNull(responseMono.block()).getHeaders() + .get("location")).get(0).split("/"); + String policyID = locationHeader[(locationHeader.length) - 1]; + Mono> responseMonoGet = testHelper.restClientV3().getForEntity(url+"/" +policyID); + testHelper.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody -> + responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}")); + } +} diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/RicRepositoryControllerTestV3.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/RicRepositoryControllerTestV3.java new file mode 100644 index 00000000..576ae7f6 --- /dev/null +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/RicRepositoryControllerTestV3.java @@ -0,0 +1,123 @@ +/*- + * ========================LICENSE_START================================= + * ONAP : ccsdk oran + * ====================================================================== + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ========================LICENSE_END=================================== + */ + +package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v3; + +import org.junit.jupiter.api.*; +import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext; +import org.onap.ccsdk.oran.a1policymanagementservice.config.TestConfig; +import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.OpenPolicyAgentSimulatorController; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.RappSimulatorController; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics; +import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory; +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.web.server.LocalServerPort; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import reactor.core.publisher.Mono; + +import java.io.IOException; +import java.lang.invoke.MethodHandles; + +@TestMethodOrder(MethodOrderer.MethodName.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ContextConfiguration(classes = TestConfig.class) +@TestPropertySource(properties = { // + "server.ssl.key-store=./config/keystore.jks", // + "app.webclient.trust-store=./config/truststore.jks", // + "app.webclient.trust-store-used=true", // + "app.vardata-directory=/tmp/pmstestv3", //a + "app.filepath=", // + "app.s3.bucket=" // If this is set, S3 will be used to store data. +}) +public class RicRepositoryControllerTestV3 { + + private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + @Autowired + private TestHelper testHelper; + + @Autowired + private ApplicationConfig applicationConfig; + + @Autowired + private MockA1ClientFactory a1ClientFactory; + + @Autowired + private RappSimulatorController rAppSimulator; + + @Autowired + private SecurityContext securityContext; + + @Autowired + private OpenPolicyAgentSimulatorController openPolicyAgentSimulatorController; + + @Autowired + private PolicyTypes policyTypes; + + @Autowired + private Rics rics; + + @LocalServerPort + private int port; + + @BeforeEach + void init() { + testHelper.port = port; + this.applicationConfig.setAuthProviderUrl(testHelper.baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL); + } + + @AfterEach + void reset() { + rics.clear(); + a1ClientFactory.reset(); + this.rAppSimulator.getTestResults().clear(); + this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app + this.securityContext.setAuthTokenFilePath(null); + this.openPolicyAgentSimulatorController.getTestResults().reset(); + } + + @Test + public void testGetRic() throws IOException { + testHelper.addPolicyType("1", "ricAdded"); + Mono> responseEntityMono = testHelper.restClientV3().getForEntity("/rics/ric?ricId=ricAdded"); + testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBody -> responseBody + .contains("{\"ricId\":\"ricAdded\",\"managedElementIds\":[],\"state\":\"AVAILABLE\",\"policyTypeIds\":[\"1\"]}")); + } + + @Test + public void testGetRics() throws IOException { + testHelper.addPolicyType("1", "ricAddedOne"); + testHelper.addPolicyType("2", "ricAddedTwo"); + Mono> responseEntityMono = testHelper.restClientV3().getForEntity("/rics"); + testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBody -> responseBody + .contains("{\"rics\":[{\"ricId\":\"ricAddedTwo\",\"managedElementIds\":[],\"state\":\"AVAILABLE\"," + + "\"policyTypeIds\":[\"2\"]},{\"ricId\":\"ricAddedOne\",\"managedElementIds\":[]," + + "\"state\":\"AVAILABLE\",\"policyTypeIds\":[\"1\"]}]}")); + } +} diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ServiceControllerTestV3.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ServiceControllerTestV3.java new file mode 100644 index 00000000..9eeaa3af --- /dev/null +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/ServiceControllerTestV3.java @@ -0,0 +1,141 @@ +/*- + * ========================LICENSE_START================================= + * ONAP : ccsdk oran + * ====================================================================== + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ========================LICENSE_END=================================== + */ + +package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v3; + +import com.google.gson.Gson; +import org.junit.jupiter.api.*; +import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext; +import org.onap.ccsdk.oran.a1policymanagementservice.config.TestConfig; +import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.OpenPolicyAgentSimulatorController; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.RappSimulatorController; +import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.ServiceRegistrationInfo; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service; +import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services; +import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory; +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.web.server.LocalServerPort; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import reactor.core.publisher.Mono; + +import java.lang.invoke.MethodHandles; +import java.time.Duration; + +@TestMethodOrder(MethodOrderer.MethodName.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ContextConfiguration(classes = TestConfig.class) +@TestPropertySource(properties = { // + "server.ssl.key-store=./config/keystore.jks", // + "app.webclient.trust-store=./config/truststore.jks", // + "app.webclient.trust-store-used=true", // + "app.vardata-directory=/tmp/pmstestv3", //a + "app.filepath=", // + "app.s3.bucket=" // If this is set, S3 will be used to store data. +}) +public class ServiceControllerTestV3 { + + private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + @Autowired + private TestHelper testHelper; + + @Autowired + private ApplicationConfig applicationConfig; + + @Autowired + private Services services; + + @Autowired + private MockA1ClientFactory a1ClientFactory; + + @Autowired + private RappSimulatorController rAppSimulator; + + @Autowired + private SecurityContext securityContext; + + @Autowired + private OpenPolicyAgentSimulatorController openPolicyAgentSimulatorController; + + @Autowired + private PolicyTypes policyTypes; + + @Autowired + private Gson gson; + + @LocalServerPort + private int port; + + @BeforeEach + void init() { + testHelper.port = port; + this.applicationConfig.setAuthProviderUrl(testHelper.baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL); + } + + @AfterEach + void reset() { + services.clear(); + a1ClientFactory.reset(); + this.rAppSimulator.getTestResults().clear(); + this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app + this.securityContext.setAuthTokenFilePath(null); + this.openPolicyAgentSimulatorController.getTestResults().reset(); + } + + @Test + public void testPutService() { + ServiceRegistrationInfo serviceRegistrationInfo = new ServiceRegistrationInfo("serviceId"); + serviceRegistrationInfo.callbackUrl("http://callback.com/").keepAliveIntervalSeconds(10L); + Mono> responseEntityMono = testHelper.restClientV3() + .putForEntity("/services", gson.toJson(serviceRegistrationInfo)); + testHelper.testSuccessResponse(responseEntityMono, HttpStatus.CREATED, responseBody -> services.size() == 1); + } + + @Test + public void testGetService() { + services.put(new Service("newServiceId", Duration.ofSeconds(10L), "http://callback.com/")); + Mono> responseEntityMono = testHelper.restClientV3().getForEntity("/services"); + testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBoy -> responseBoy + .contains("http://callback.com/")); + } + + @Test + public void testDeleteService() { + services.put(new Service("newServiceId", Duration.ofSeconds(10L), "http://callback.com/")); + Mono> responseEntityMono = testHelper.restClientV3().deleteForEntity("/services/newServiceId"); + testHelper.testSuccessResponse(responseEntityMono, HttpStatus.NO_CONTENT, responseBody -> services.size() == 0); + } + + @Test + public void testKeepAliveService() { + services.put(new Service("newServiceId", Duration.ofSeconds(10L), "http://callback.com/")); + Mono> responseEntityMono = testHelper.restClientV3().putForEntity("/services/newServiceId/keepalive", ""); + testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBody -> services.size() == 1); + } +} 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 bfec3f6b..5463b0a1 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 @@ -25,6 +25,7 @@ import com.google.common.io.CharStreams; import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import org.apache.commons.io.FileUtils; import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient; import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClientFactory; import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext; @@ -45,6 +46,7 @@ import org.springframework.web.reactive.function.client.WebClientResponseExcepti import reactor.core.publisher.Mono; import reactor.test.StepVerifier; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -161,13 +163,15 @@ public class TestHelper { return type; } - public String postPolicyBody(String nearRtRicId, String policyTypeName) { + public String postPolicyBody(String nearRtRicId, String policyTypeName, String policyId) { PolicyObjectInformation policyObjectInfo = new PolicyObjectInformation(nearRtRicId, dummyPolicyObject(), 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); + return gson.fromJson(postPolicyBody(nearRtRicId, policyTypeName, ""), PolicyObjectInformation.class); } public JsonObject dummyPolicyObject() { @@ -251,4 +255,10 @@ public class TestHelper { }) .verify(); } + + public String configAsString() throws Exception { + File configFile = + new File(Objects.requireNonNull(getClass().getClassLoader().getResource("test_application_configuration.json")).getFile()); + return FileUtils.readFileToString(configFile, "UTF-8"); + } } diff --git a/csit/scripts/healthcheck/docker-compose.yml b/csit/scripts/healthcheck/docker-compose.yml index d6dda495..0384d441 100644 --- a/csit/scripts/healthcheck/docker-compose.yml +++ b/csit/scripts/healthcheck/docker-compose.yml @@ -31,7 +31,7 @@ services: - 8081:8081 - 8433:8433 volumes: - - ./config/application_configuration.json:/opt/app/policy-agent/data/application_configuration.json:ro + - ./config/application_configuration.json.nosdnc:/opt/app/policy-agent/data/application_configuration.json:ro # For using own certs instead of the default ones (built into the container), # place them in config/ directory, update the application-policyagent.yaml file, and uncomment the following lines # - ./config/keystore-policyagent.jks:/opt/app/policy-agent/etc/cert/keystore.jks:ro -- cgit 1.2.3-korg