diff options
author | raviteja.karumuri <raviteja.karumuri@est.tech> | 2024-08-20 17:22:15 +0100 |
---|---|---|
committer | RAVITEJA KARUMURI <raviteja.karumuri@est.tech> | 2024-08-30 09:50:08 +0000 |
commit | c6e2c85fe7c024c56112869ed9231d7726a47d24 (patch) | |
tree | 73d64992099f099e3d5e42e51c06da6f8b7234c1 /a1-policy-management | |
parent | e1870f96ec0a0e731f82e0c158ae970409112046 (diff) |
Improve tests/issues found in Sonar report - A1 Oslo/NewDelhi/Montreal/London- Part1- Montreal
Issue-ID: CCSDK-4037
Change-Id: Ib7e6e62e2e922d3d483d95905e429aa9eac56c88
Signed-off-by: Raviteja Karumuri <raviteja.karumuri@est.tech>
Diffstat (limited to 'a1-policy-management')
27 files changed, 284 insertions, 169 deletions
diff --git a/a1-policy-management/pom.xml b/a1-policy-management/pom.xml index 40d610c3..b9a8cff8 100644 --- a/a1-policy-management/pom.xml +++ b/a1-policy-management/pom.xml @@ -24,8 +24,8 @@ <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.onap.ccsdk.parent</groupId> - <artifactId>spring-boot-31-starter-parent</artifactId> - <version>2.6.1</version> + <artifactId>spring-boot-33-starter-parent</artifactId> + <version>2.8.0-SNAPSHOT</version> <relativePath/> </parent> <groupId>org.onap.ccsdk.oran</groupId> @@ -36,11 +36,19 @@ <java.version.source>17</java.version.source> <java.version.target>17</java.version.target> <gson.version>2.10.1</gson.version> - <json.version>20231013</json.version> + <json.version>20240303</json.version> + <localstack.version>1.20.1</localstack.version> + <junit.jupiter.version>1.20.1</junit.jupiter.version> + <javax.annotation-api.version>1.3.2</javax.annotation-api.version> + <everit-json-schema.version>1.14.0</everit-json-schema.version> + <jackson-databind-nullable.version>0.2.6</jackson-databind-nullable.version> + <validation-api.version>2.0.1.Final</validation-api.version> + <swagger-annotations.version>2.2.15</swagger-annotations.version> + <spring-boot-actuator-autoconfigure.version>3.3.2</spring-boot-actuator-autoconfigure.version> <formatter-maven-plugin.version>2.22.0</formatter-maven-plugin.version> <spotless-maven-plugin.version>2.35.0</spotless-maven-plugin.version> <commons-io.version>2.14.0</commons-io.version> - <guava.version>32.0.1-jre</guava.version> + <guava.version>33.3.0-jre</guava.version> <springdoc.version>2.0.2</springdoc.version> <docker-maven-plugin>0.30.0</docker-maven-plugin> <surefire-maven-plugin.version>3.0.0-M8</surefire-maven-plugin.version> @@ -54,28 +62,22 @@ <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> - <version>2.0.1.Final</version> + <version>${validation-api.version}</version> </dependency> <dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> - <version>1.3.2</version> + <version>${javax.annotation-api.version}</version> </dependency> <dependency> <groupId>io.swagger.core.v3</groupId> <artifactId>swagger-annotations</artifactId> - <version>2.2.15</version> + <version>${swagger-annotations.version}</version> </dependency> <dependency> <groupId>org.openapitools</groupId> <artifactId>jackson-databind-nullable</artifactId> - <version>0.2.4</version> - </dependency> - <dependency> - <!-- Temporary until version 2 is included by the parent spring-boot-starter-parent --> - <groupId>org.yaml</groupId> - <artifactId>snakeyaml</artifactId> - <version>2.0</version> + <version>${jackson-databind-nullable.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> @@ -160,6 +162,18 @@ <scope>runtime</scope> </dependency> <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>junit-jupiter</artifactId> + <version>${junit.jupiter.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>localstack</artifactId> + <version>${localstack.version}</version> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> @@ -203,7 +217,7 @@ <dependency> <groupId>com.github.erosb</groupId> <artifactId>everit-json-schema</artifactId> - <version>1.14.0</version> + <version>${everit-json-schema.version}</version> </dependency> </dependencies> <build> diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/Application.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/Application.java index 6f458647..6fb4abbf 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/Application.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/Application.java @@ -22,10 +22,10 @@ package org.onap.ccsdk.oran.a1policymanagementservice; import java.lang.invoke.MethodHandles; +import lombok.RequiredArgsConstructor; import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RefreshConfigTask; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -34,10 +34,10 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; @SpringBootApplication +@RequiredArgsConstructor public class Application { - @Autowired - private RefreshConfigTask configRefresh; + private final RefreshConfigTask configRefresh; private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1AdapterJsonHelper.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1AdapterJsonHelper.java index ab93ea3e..66d32110 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1AdapterJsonHelper.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1AdapterJsonHelper.java @@ -92,26 +92,4 @@ class A1AdapterJsonHelper { return Mono.error(ex); } } - - public static Mono<String> getValueFromResponse(String response, String key) { - return getOutput(response) // - .map(responseParams -> { - if (!responseParams.has(key)) { - return ""; - } - return responseParams.get(key).toString(); - }); - } - - public static Mono<String> extractPolicySchema(String inputString) { - try { - JSONObject jsonObject = new JSONObject(inputString); - JSONObject schemaObject = jsonObject.getJSONObject("policySchema"); - String schemaString = schemaObject.toString(); - return Mono.just(schemaString); - } catch (Exception ex) { // invalid json - logger.debug("Invalid json {}", ex.getMessage()); - return Mono.error(ex); - } - } } diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactory.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactory.java index 87776c0f..28479712 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactory.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactory.java @@ -3,6 +3,7 @@ * ONAP : ccsdk oran * ====================================================================== * Copyright (C) 2020-2023 Nordix Foundation. All rights reserved. + * 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. @@ -20,20 +21,18 @@ package org.onap.ccsdk.oran.a1policymanagementservice.clients; -import java.lang.invoke.MethodHandles; -import java.lang.reflect.Constructor; - import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1Client.A1ProtocolType; import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig; -import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ControllerConfig; import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig; import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import reactor.core.publisher.Mono; +import java.lang.invoke.MethodHandles; +import java.lang.reflect.Constructor; + /** * Factory for A1 clients that supports four different protocol versions of the * A1 api. @@ -42,12 +41,9 @@ public class A1ClientFactory { private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - private final ApplicationConfig appConfig; - private final AsyncRestClientFactory restClientFactory; public A1ClientFactory(ApplicationConfig appConfig, SecurityContext securityContext) { - this.appConfig = appConfig; this.restClientFactory = new AsyncRestClientFactory(appConfig.getWebClientConfig(), securityContext); } @@ -94,15 +90,6 @@ public class A1ClientFactory { } } - private ControllerConfig getControllerConfig(Ric ric) throws ServiceException { - ControllerConfig controllerConfig = ric.getConfig().getControllerConfig(); - if (controllerConfig == null) { - ric.setProtocolVersion(A1ProtocolType.UNKNOWN); - throw new ServiceException("No controller configured for Near-RT RIC: " + ric.id()); - } - return controllerConfig; - } - private A1Client createCustomAdapter(Ric ric) throws ServiceException { try { Class<?> clazz = Class.forName(ric.getConfig().getCustomAdapterClass()); diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/CcsdkA1AdapterClient.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/CcsdkA1AdapterClient.java index 04ecf045..f62d0807 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/CcsdkA1AdapterClient.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/CcsdkA1AdapterClient.java @@ -100,7 +100,6 @@ public class CcsdkA1AdapterClient implements A1Client { * CCSDK_A1_ADAPTER_STD_V2_0_0 with * @param ricConfig the configuration of the Near-RT RIC to communicate * with - * @param controllerConfig the configuration of the CCSDK A1 Adapter to use * * @throws IllegalArgumentException when the protocolType is wrong. */ @@ -119,7 +118,6 @@ public class CcsdkA1AdapterClient implements A1Client { * CCSDK_A1_ADAPTER_STD_V2_0_0 with * @param ricConfig the configuration of the Near-RT RIC to communicate * with - * @param controllerConfig the configuration of the CCSDK A1 Adapter to use * @param restClient the REST client to use * * @throws IllegalArgumentException when the protocolType is illegal. @@ -131,8 +129,8 @@ public class CcsdkA1AdapterClient implements A1Client { this.restClient = restClient; this.ricConfig = ricConfig; this.protocolType = protocolType; - logger.debug("A1Client (" + getClass().getTypeName() + ") created for ric: {}, a1Controller: {}", - ricConfig.getRicId(), ricConfig.getControllerConfig()); + logger.debug("A1Client \"{}\" created for ric: {}, a1Controller: {}", + getClass().getTypeName(), ricConfig.getRicId(), ricConfig.getControllerConfig()); } else { logger.error("Not supported protocoltype: {}", protocolType); diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/OscA1Client.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/OscA1Client.java index 17681cb6..f536076b 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/OscA1Client.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/OscA1Client.java @@ -143,7 +143,7 @@ public class OscA1Client implements A1Client { public OscA1Client(RicConfig ricConfig, AsyncRestClient restClient) { this.restClient = restClient; uri = new UriBuilder(ricConfig); - logger.debug("A1Client (" + getClass().getTypeName() + ") created for ric: {}", ricConfig.getRicId()); + logger.debug("A1Client \"{}\" created for ric: {}", getClass().getTypeName(), ricConfig.getRicId()); } public static Mono<String> extractCreateSchema(String policyTypeResponse, String policyTypeId) { diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/StdA1ClientVersion1.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/StdA1ClientVersion1.java index 2f06b6a6..b033aeaa 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/StdA1ClientVersion1.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/StdA1ClientVersion1.java @@ -109,7 +109,7 @@ public class StdA1ClientVersion1 implements A1Client { public StdA1ClientVersion1(AsyncRestClient restClient, RicConfig ricConfig) { this.restClient = restClient; this.uri = new UriBuilder(ricConfig); - logger.debug("A1Client (" + getClass().getTypeName() + ") created for ric: {}", ricConfig.getRicId()); + logger.debug("A1Client \"{}\" created for ric: {}", getClass().getTypeName(), ricConfig.getRicId()); } @Override diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/StdA1ClientVersion2.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/StdA1ClientVersion2.java index 6f907539..3362b790 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/StdA1ClientVersion2.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/StdA1ClientVersion2.java @@ -138,7 +138,7 @@ public class StdA1ClientVersion2 implements A1Client { public StdA1ClientVersion2(RicConfig ricConfig, AsyncRestClient restClient) { this.restClient = restClient; uriBuiler = new OranV2UriBuilder(ricConfig); - logger.debug("A1Client (" + getClass().getTypeName() + ") created for ric: {}", ricConfig.getRicId()); + logger.debug("A1Client \"{}\" created for ric: {}", getClass().getTypeName(), ricConfig.getRicId()); } public static Mono<String> extractPolicySchema(String policyTypeResponse, String policyTypeId) { diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/ApplicationConfigParser.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/ApplicationConfigParser.java index 45305f84..f5f0f7e7 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/ApplicationConfigParser.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/ApplicationConfigParser.java @@ -84,7 +84,7 @@ public class ApplicationConfigParser { Map<String, ControllerConfig> controllerConfigs = parseControllerConfigs(pmsConfigJson); List<RicConfig> ricConfigs = parseRics(pmsConfigJson, controllerConfigs); - checkConfigurationConsistency(ricConfigs, controllerConfigs); + checkConfigurationConsistency(ricConfigs); return ConfigParserResult.builder() // .ricConfigs(ricConfigs) // @@ -123,8 +123,7 @@ public class ApplicationConfigParser { return CharStreams.toString(new InputStreamReader(in, StandardCharsets.UTF_8)); } - private void checkConfigurationConsistency(List<RicConfig> ricConfigs, - Map<String, ControllerConfig> controllerConfigs) throws ServiceException { + private void checkConfigurationConsistency(List<RicConfig> ricConfigs) throws ServiceException { Set<String> ricUrls = new HashSet<>(); Set<String> ricNames = new HashSet<>(); for (RicConfig ric : ricConfigs) { @@ -218,8 +217,4 @@ public class ApplicationConfigParser { private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException { return get(obj, memberName).getAsJsonArray(); } - - private static String getAsString(JsonObject obj, String memberName) throws ServiceException { - return get(obj, memberName).getAsString(); - } } diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/authorization/AuthorizationResult.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/authorization/AuthorizationResult.java index 796c44e9..dc717d38 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/authorization/AuthorizationResult.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/authorization/AuthorizationResult.java @@ -32,7 +32,7 @@ import lombok.Getter; @Builder public class AuthorizationResult { - @Schema(name = "result", description = "If true, the access is granted", required = true) + @Schema(name = "result", description = "If true, the access is granted", requiredMode = Schema.RequiredMode.REQUIRED) @JsonProperty(value = "result", required = true) @SerializedName("result") @Getter diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/authorization/PolicyAuthorizationRequest.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/authorization/PolicyAuthorizationRequest.java index 8dd4e7bb..cb93006a 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/authorization/PolicyAuthorizationRequest.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/authorization/PolicyAuthorizationRequest.java @@ -51,25 +51,25 @@ public class PolicyAuthorizationRequest { READ, WRITE, DELETE } - @Schema(name = "access_type", description = "Access type", required = true) + @Schema(name = "access_type", description = "Access type", requiredMode = Schema.RequiredMode.REQUIRED) @JsonProperty(value = "access_type", required = true) @SerializedName("access_type") @Getter private AccessType accessType; - @Schema(name = "policy_type_id", description = "Policy type identifier", required = true) + @Schema(name = "policy_type_id", description = "Policy type identifier", requiredMode = Schema.RequiredMode.REQUIRED) @SerializedName("policy_type_id") @JsonProperty(value = "policy_type_id", required = true) private String policyTypeId; - @Schema(name = "auth_token", description = "Authorization token", required = true) + @Schema(name = "auth_token", description = "Authorization token", requiredMode = Schema.RequiredMode.REQUIRED) @SerializedName("auth_token") @JsonProperty(value = "auth_token", required = true) private String authToken; } - @Schema(name = "input", description = "Input", required = true) + @Schema(name = "input", description = "Input", requiredMode = Schema.RequiredMode.REQUIRED) @JsonProperty(value = "input", required = true) @SerializedName("input") private Input input; diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationController.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationController.java index 4b0d6b58..51ac59bc 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationController.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationController.java @@ -39,7 +39,7 @@ import reactor.core.publisher.Mono; import java.io.IOException; import java.util.Optional; -@RestController("ConfigurationControllerV2") +@RestController("configurationControllerV2") @Tag( // name = ConfigurationController.API_NAME, // description = ConfigurationController.API_DESCRIPTION // diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/PolicyController.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/PolicyController.java index 53cf62a2..0ad3bf1e 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/PolicyController.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/PolicyController.java @@ -36,6 +36,7 @@ import java.util.List; import lombok.Getter; +import lombok.RequiredArgsConstructor; import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory; import org.onap.ccsdk.oran.a1policymanagementservice.controllers.api.v2.A1PolicyManagementApi; import org.onap.ccsdk.oran.a1policymanagementservice.controllers.authorization.AuthorizationCheck; @@ -51,7 +52,6 @@ import org.onap.ccsdk.oran.a1policymanagementservice.models.v2.PolicyStatusInfo; import org.onap.ccsdk.oran.a1policymanagementservice.repository.*; 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.web.bind.annotation.RestController; @@ -62,7 +62,8 @@ import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -@RestController("PolicyControllerV2") +@RestController("policyControllerV2") +@RequiredArgsConstructor @Tag(// name = PolicyController.API_NAME, // description = PolicyController.API_DESCRIPTION // @@ -71,7 +72,6 @@ public class PolicyController implements A1PolicyManagementApi { public static final String API_NAME = "A1 Policy Management"; public static final String API_DESCRIPTION = ""; - public static class RejectionException extends Exception { private static final long serialVersionUID = 1L; @@ -84,24 +84,16 @@ public class PolicyController implements A1PolicyManagementApi { } } - @Autowired - private Rics rics; - @Autowired - private PolicyTypes policyTypes; - @Autowired - private Policies policies; - @Autowired - private A1ClientFactory a1ClientFactory; - @Autowired - private Services services; - @Autowired - private ObjectMapper objectMapper; - @Autowired - private AuthorizationCheck authorization; + private final Rics rics; + private final PolicyTypes policyTypes; + private final Policies policies; + private final A1ClientFactory a1ClientFactory; + private final Services services; + private final ObjectMapper objectMapper; + private final AuthorizationCheck authorization; private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - private static Gson gson = new GsonBuilder() // - .create(); // + private static final Gson gson = new GsonBuilder().create(); @Override public Mono<ResponseEntity<PolicyTypeDefinition>> getPolicyTypeDefinition(String policyTypeId, ServerWebExchange exchange) diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/RicRepositoryController.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/RicRepositoryController.java index 8907774b..f7872a38 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/RicRepositoryController.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/RicRepositoryController.java @@ -24,6 +24,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; import org.onap.ccsdk.oran.a1policymanagementservice.controllers.api.v2.NearRtRicRepositoryApi; import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.EntityNotFoundException; import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.InvalidRequestException; @@ -32,7 +33,6 @@ import org.onap.ccsdk.oran.a1policymanagementservice.models.v2.RicInfoList; import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RestController; @@ -42,7 +42,8 @@ import reactor.core.publisher.Mono; import java.util.ArrayList; import java.util.List; -@RestController("RicRepositoryControllerV2") +@RestController("ricRepositoryControllerV2") +@RequiredArgsConstructor @Tag( // name = RicRepositoryController.API_NAME, // description = RicRepositoryController.API_DESCRIPTION // @@ -52,17 +53,11 @@ public class RicRepositoryController implements NearRtRicRepositoryApi { public static final String API_NAME = "NearRT-RIC Repository"; public static final String API_DESCRIPTION = ""; - @Autowired - private Rics rics; + private final Rics rics; + final PolicyTypes types; + final ObjectMapper objectMapper; - @Autowired - PolicyTypes types; - - @Autowired - ObjectMapper objectMapper; - - private static Gson gson = new GsonBuilder() // - .create(); // + private static final Gson gson = new GsonBuilder().create(); private static final String GET_RIC_BRIEF = "Returns info for one Near-RT RIC"; private static final String GET_RIC_DETAILS = diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ServiceController.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ServiceController.java index 4d5a9ba3..2b6c1711 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ServiceController.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ServiceController.java @@ -21,6 +21,7 @@ package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2; import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; import org.onap.ccsdk.oran.a1policymanagementservice.controllers.api.v2.ServiceRegistryAndSupervisionApi; import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException; import org.onap.ccsdk.oran.a1policymanagementservice.models.v2.ServiceRegistrationInfo; @@ -32,7 +33,6 @@ import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services; 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.web.bind.annotation.RestController; @@ -47,7 +47,8 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; -@RestController("ServiceControllerV2") +@RestController("serviceControllerV2") +@RequiredArgsConstructor @Tag( // name = ServiceController.API_NAME, // description = ServiceController.API_DESCRIPTION // @@ -63,13 +64,7 @@ public class ServiceController implements ServiceRegistryAndSupervisionApi { private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - @Autowired - private PolicyController policyController; - - ServiceController(Services services, Policies policies) { - this.services = services; - this.policies = policies; - } + private final PolicyController policyController; private static final String GET_SERVICE_DETAILS = "Either information about a registered service with given identity or all registered services are returned."; diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/StatusController.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/StatusController.java index 4ec3652d..b2cb69ce 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/StatusController.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/StatusController.java @@ -29,7 +29,7 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; -@RestController("StatusControllerV2") +@RestController("statusControllerV2") @Tag( name = StatusController.API_NAME, description = StatusController.API_DESCRIPTION ) diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/datastore/S3ObjectStore.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/datastore/S3ObjectStore.java index 1c67ff7d..d014ad53 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/datastore/S3ObjectStore.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/datastore/S3ObjectStore.java @@ -141,7 +141,7 @@ class S3ObjectStore implements DataStore { return Mono.fromFuture(future) // .map(f -> s3Bucket) // .doOnError(t -> logger.debug("Could not create S3 bucket: {}", t.getMessage())) - .onErrorResume(t -> Mono.just(s3Bucket)); + .onErrorResume(t -> Mono.just("Not Created")); } @Override diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactoryTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactoryTest.java index f50d5537..3a510102 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactoryTest.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1ClientFactoryTest.java @@ -22,6 +22,7 @@ package org.onap.ccsdk.oran.a1policymanagementservice.clients; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.spy; @@ -128,20 +129,20 @@ class A1ClientFactoryTest { @DisplayName("test Custom Adapter Creation") void testCustomAdapterCreation() { - Ric ric = new Ric(ricConfig("", CustomA1AdapterFactory.class.getName())); - A1Client client = factoryUnderTest.createA1Client(ric).block(); - + Ric ricLocal = new Ric(ricConfig("", CustomA1AdapterFactory.class.getName())); + A1Client client = factoryUnderTest.createA1Client(ricLocal).block(); + assertNotNull(client); assertEquals(client.getClass(), StdA1ClientVersion2.class); - ric = new Ric(ricConfig("", "org.onap.ccsdk.oran.a1policymanagementservice.clients.StdA1ClientVersion2")); - client = factoryUnderTest.createA1Client(ric).block(); - + ricLocal = new Ric(ricConfig("", "org.onap.ccsdk.oran.a1policymanagementservice.clients.StdA1ClientVersion2")); + client = factoryUnderTest.createA1Client(ricLocal).block(); + assertNotNull(client); assertEquals(client.getClass(), StdA1ClientVersion2.class); - ric = new Ric( + ricLocal = new Ric( ricConfig("", "org.onap.ccsdk.oran.a1policymanagementservice.clients.StdA1ClientVersion2$Factory")); - client = factoryUnderTest.createA1Client(ric).block(); - + client = factoryUnderTest.createA1Client(ricLocal).block(); + assertNotNull(client); assertEquals(client.getClass(), StdA1ClientVersion2.class); Exception e = Assertions.assertThrows(Exception.class, () -> { diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/MetersTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/MetersTest.java index 922d139d..c714268d 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/MetersTest.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/MetersTest.java @@ -22,8 +22,8 @@ package org.onap.ccsdk.oran.a1policymanagementservice.configuration; import static org.assertj.core.api.Assertions.assertThat; -import io.micrometer.prometheus.PrometheusConfig; -import io.micrometer.prometheus.PrometheusMeterRegistry; +import io.micrometer.prometheusmetrics.PrometheusConfig; +import io.micrometer.prometheusmetrics.PrometheusMeterRegistry; import java.time.Instant; import java.util.Arrays; diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ApplicationTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ApplicationTest.java index daa3a1d8..960ab563 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ApplicationTest.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ApplicationTest.java @@ -230,7 +230,6 @@ class ApplicationTest { } @Test - @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests. @DisplayName("test ZZ Actuator") void testZZActuator() throws Exception { // The test must be run last, hence the "ZZ" in the name. All succeeding tests @@ -246,14 +245,9 @@ class ApplicationTest { client.post("/actuator/loggers/org.springframework.boot.actuate", "{\"configuredLevel\":\"trace\"}").block(); // This will stop the web server and all coming tests will fail. - client.post("/actuator/shutdown", "").block(); - - Thread.sleep(1000); - - StepVerifier.create(restClient().get("/rics")) // Any call - .expectSubscription() // - .expectErrorMatches(t -> t instanceof WebClientRequestException) // - .verify(); + ResponseEntity<String> entity = client.postForEntity("/actuator/shutdown", "").block(); + assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(((String) entity.getBody())).contains("Shutting down");; } @@ -270,16 +264,16 @@ class ApplicationTest { waitforS3(); { - Policies policies = new Policies(this.applicationConfig); - policies.restoreFromDatabase(ric, this.policyTypes).blockLast(); - assertThat(policies.size()).isEqualTo(noOfPolicies); + Policies policiesLocal = new Policies(this.applicationConfig); + policiesLocal.restoreFromDatabase(ric, this.policyTypes).blockLast(); + assertThat(policiesLocal.size()).isEqualTo(noOfPolicies); } { restClient().delete("/policies/id2").block(); - Policies policies = new Policies(this.applicationConfig); - policies.restoreFromDatabase(ric, this.policyTypes).blockLast(); - assertThat(policies.size()).isEqualTo(noOfPolicies - 1); + Policies policiesAfterDelete = new Policies(this.applicationConfig); + policiesAfterDelete.restoreFromDatabase(ric, this.policyTypes).blockLast(); + assertThat(policiesAfterDelete.size()).isEqualTo(noOfPolicies - 1); } } @@ -394,7 +388,7 @@ class ApplicationTest { @Test @DisplayName("test Get Rics") - void testGetRics() throws Exception { + void testGetRics() throws JsonProcessingException { addRic("ric1"); this.addPolicyType("type1", "ric1"); String url = "/rics?policytype_id=type1"; @@ -799,7 +793,7 @@ class ApplicationTest { @Test @DisplayName("test Get Policy Types") - void testGetPolicyTypes() throws Exception { + void testGetPolicyTypes() throws JsonProcessingException { String TYPE_ID_1 = "A_type1_1.9.0"; String TYPE_ID_2 = "A_type1_2.0.0"; String TYPE_ID_3 = "A_type1_1.5.0"; @@ -1013,7 +1007,7 @@ class ApplicationTest { @Test @DisplayName("test Service Supervision") - void testServiceSupervision() throws Exception { + void testServiceSupervision() throws JsonProcessingException { putService("service1", 2, HttpStatus.CREATED); addPolicyType("type1", "ric1"); @@ -1186,7 +1180,7 @@ class ApplicationTest { assertThat(test.isFailed()).isFalse(); } assertThat(policies.size()).isZero(); - logger.info("Concurrency test took " + Duration.between(startTime, Instant.now())); + logger.info("Concurrency test took: {}", Duration.between(startTime, Instant.now())); assertThat(nonRespondingRic.getState()).isEqualTo(RicState.UNAVAILABLE); nonRespondingRic.setState(RicState.AVAILABLE); diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConcurrencyTestRunnable.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConcurrencyTestRunnable.java index 888325b2..a42d034a 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConcurrencyTestRunnable.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConcurrencyTestRunnable.java @@ -86,7 +86,7 @@ class ConcurrencyTestRunnable implements Runnable { System.out.println(Thread.currentThread() + result.getBody()); } catch (Exception e) { - logger.error(Thread.currentThread() + "Concurrency test printStatusInfo exception " + e.toString()); + logger.error("{} Concurrency test printStatusInfo exception {}", Thread.currentThread(), e.toString()); } } @@ -107,7 +107,7 @@ class ConcurrencyTestRunnable implements Runnable { deletePolicy(name + "-"); } } catch (Exception e) { - logger.error("Concurrency test exception " + e.toString()); + logger.error("Concurrency test exception {}", e.toString()); printStatusInfo(); failed = true; } diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationControllerTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationControllerTest.java index 42e3b0c9..e46b8367 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationControllerTest.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationControllerTest.java @@ -134,14 +134,14 @@ class ConfigurationControllerTest { } private String configAsString() throws Exception { - File configFile = + File configFileLocal = new File(getClass().getClassLoader().getResource("test_application_configuration.json").getFile()); - return FileUtils.readFileToString(configFile, "UTF-8"); + return FileUtils.readFileToString(configFileLocal, "UTF-8"); } @Test @DisplayName("put Invalid Configuration should Return Error 400") - void putInvalidConfiguration_shouldReturnError400() throws Exception { + void putInvalidConfiguration_shouldReturnError400() { String url = "a1-policy/v2/configuration"; // Valid JSON but invalid configuration. diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/datastore/S3ObjectStoreTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/datastore/S3ObjectStoreTest.java new file mode 100644 index 00000000..941b7770 --- /dev/null +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/datastore/S3ObjectStoreTest.java @@ -0,0 +1,134 @@ +/*- + * ========================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.datastore; + +import org.junit.jupiter.api.*; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig; +import org.reactivestreams.Publisher; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; +import org.testcontainers.containers.localstack.LocalStackContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.utility.DockerImageName; +import reactor.test.StepVerifier; + +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.function.Predicate; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@Testcontainers +@ExtendWith({MockitoExtension.class, OutputCaptureExtension.class}) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class S3ObjectStoreTest { + + static ApplicationConfig appConfig; + private static S3ObjectStore s3ObjectStore; + private static final String bucketName = "s3bucket"; + + @Container + private static final LocalStackContainer localstack = + new LocalStackContainer(DockerImageName.parse("localstack/localstack:0.11.3")) + .withServices(LocalStackContainer.Service.S3); + + @BeforeAll + static void init() { + appConfig = mock(ApplicationConfig.class); + when(appConfig.isS3Enabled()).thenReturn(Boolean.TRUE); + when(appConfig.getS3EndpointOverride()).thenReturn(localstack.getEndpoint().toString()); + when(appConfig.getS3AccessKeyId()).thenReturn(localstack.getAccessKey()); + when(appConfig.getS3SecretAccessKey()).thenReturn(localstack.getSecretKey()); + when(appConfig.getS3Bucket()).thenReturn(bucketName); + s3ObjectStore = new S3ObjectStore(appConfig, "location"); + } + @Test + @Order(1) + void testGetS3AsynchClient() { + assertNotNull(s3ObjectStore); + } + + @Test + @Order(2) + void testCreateAndDeleteS3BucketSuccess(CapturedOutput capturedOutput) { + + testSuccess(s3ObjectStore.createDataStore(), actual -> actual.equals(bucketName)); + assertFalse(capturedOutput.getOut().contains("Could not create S3 bucket:")); + + testSuccess(s3ObjectStore.deleteBucket(), actual -> actual.equals(bucketName)); + } + + @Test + @Order(3) + void testWriteAndReadAndDeleteObjectSuccess(CapturedOutput capturedOutput) { + + testSuccess(s3ObjectStore.createDataStore(), actual -> actual.equals(bucketName)); + byte[] fileData = "testData".getBytes(StandardCharsets.UTF_8); + new String(fileData); + testSuccess(s3ObjectStore.writeObject("test", fileData), + actual -> Arrays.equals(actual, fileData)); + assertFalse(capturedOutput.getOut().contains("Failed to store object")); + testSuccess(s3ObjectStore.readObject("test"), + actual -> Arrays.equals(actual, fileData)); + testSuccess(s3ObjectStore.deleteAllObjects(), actual -> actual.equals("OK")); + } + + @Test + @Order(4) + void testListObjectsSuccess() { + + s3ObjectStore.createDataStore().block(); + String objectName = "test"; + byte[] fileData = "testData".getBytes(StandardCharsets.UTF_8); + testSuccess(s3ObjectStore.writeObject(objectName, fileData), + actual -> Arrays.equals(actual, fileData)); + testSuccess(s3ObjectStore.listObjects(""), actual -> actual.equals(objectName)); + } + + @Test + @Order(5) + void testCreateAndDeleteS3BucketError(CapturedOutput capturedOutput) { + + when(appConfig.getS3Bucket()).thenReturn("S3Bucket"); + + testFailure(s3ObjectStore.createDataStore(), actual -> actual.equals("Not Created")); + + testFailure(s3ObjectStore.deleteBucket(), actual -> actual.equals("NOK")); + assertTrue(capturedOutput.getOut().contains("Could not delete bucket:")); + } + + <T> void testSuccess(Publisher<T> publisher, Predicate<T> equalityCheck) { + StepVerifier.create(publisher) + .expectNextMatches(equalityCheck) + .verifyComplete(); + } + + <T> void testFailure(Publisher<T> publisher, Predicate<T> equalityCheck) { + StepVerifier.create(publisher) + .expectNextMatches(equalityCheck) + .verifyComplete(); + } +} diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/LockTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/LockTest.java index e4d78599..ff818caf 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/LockTest.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/LockTest.java @@ -20,25 +20,22 @@ package org.onap.ccsdk.oran.a1policymanagementservice.repository; -import static org.assertj.core.api.Assertions.assertThat; -import static org.awaitility.Awaitility.await; - -import java.io.IOException; -import java.lang.invoke.MethodHandles; - import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; -import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock.LockType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; +import java.lang.invoke.MethodHandles; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.awaitility.Awaitility.await; + @ExtendWith(MockitoExtension.class) class LockTest { @@ -66,7 +63,7 @@ class LockTest { @Test @DisplayName("test Lock") - void testLock() throws IOException, ServiceException { + void testLock() { Lock lock = new Lock("l1"); Lock.Grant grant = lock.lockBlocking(LockType.SHARED, "test"); grant.unlockBlocking(); diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/tasks/RefreshConfigTaskTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/tasks/RefreshConfigTaskTest.java index 3b1ee8f7..dea228ec 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/tasks/RefreshConfigTaskTest.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/tasks/RefreshConfigTaskTest.java @@ -22,6 +22,7 @@ package org.onap.ccsdk.oran.a1policymanagementservice.tasks; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.spy; @@ -74,11 +75,11 @@ class RefreshConfigTaskTest { @Mock ConfigurationFile configurationFileMock; - private RefreshConfigTask createTestObject(boolean configFileExists) { - return createTestObject(configFileExists, spy(new Rics()), new Policies(appConfig), true); + private RefreshConfigTask createTestObject(boolean stubConfigFileExists) { + return createTestObject(spy(new Rics()), new Policies(appConfig), stubConfigFileExists); } - private RefreshConfigTask createTestObject(boolean configFileExists, Rics rics, Policies policies, + private RefreshConfigTask createTestObject(Rics rics, Policies policies, boolean stubConfigFileExists) { SecurityContext secContext = new SecurityContext(""); @@ -118,8 +119,30 @@ class RefreshConfigTaskTest { } @Test + @DisplayName("test handle updated ric config method with type removed") + void testHandleUpdatedRicConfigRemoved() { + + String ricId = "ric1"; + refreshTaskUnderTest = this.createTestObject(false); + refreshTaskUnderTest.handleUpdatedRicConfig(buildRicConfigUpdate(ricId, "ric_baseurl_removed", + Type.REMOVED)); + verify(refreshTaskUnderTest.rics, times(1)).remove(anyString()); + } + + @Test + @DisplayName("test handle updated ric config method with type changed and no ric available before") + void testHandleUpdatedRicConfigNullRicChanged() { + + String ricId = "ric1"; + refreshTaskUnderTest = this.createTestObject(false); + refreshTaskUnderTest.handleUpdatedRicConfig(buildRicConfigUpdate(ricId, "ric_baseurl_changed", + Type.CHANGED)); + verify(refreshTaskUnderTest.rics, times(1)).put(any(Ric.class)); + } + + @Test @DisplayName("test when File Exists But Json Is Incorrect then No Rics Are Put In Repository") - void whenFileExistsButJsonIsIncorrect_thenNoRicsArePutInRepository() throws Exception { + void whenFileExistsButJsonIsIncorrect_thenNoRicsArePutInRepository() { refreshTaskUnderTest = this.createTestObject(true); // When @@ -148,4 +171,16 @@ class RefreshConfigTaskTest { String string = Resources.toString(url, Charsets.UTF_8); return Optional.of(JsonParser.parseString(string).getAsJsonObject()); } + + private ApplicationConfig.RicConfigUpdate buildRicConfigUpdate(String ricID, String baseUrl, + Type event) { + return new ApplicationConfig.RicConfigUpdate(buildRicConfig(ricID, baseUrl), event); + } + + private RicConfig buildRicConfig(String ricID, String baseUrl) { + return RicConfig.builder() + .ricId(ricID) + .baseUrl(baseUrl) + .build(); + } } diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/MockA1Client.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/MockA1Client.java index 91b762c5..0cd9db20 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/MockA1Client.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/MockA1Client.java @@ -54,7 +54,7 @@ public class MockA1Client implements A1Client { @Setter private String errorInject; - public MockA1Client(String ricId, ApplicationConfig appConfig, PolicyTypes policyTypes, Duration asynchDelay) { + public MockA1Client(ApplicationConfig appConfig, PolicyTypes policyTypes, Duration asynchDelay) { this.policyTypes = policyTypes; this.asynchDelay = asynchDelay; ApplicationConfig cfg = spy(appConfig); diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/MockA1ClientFactory.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/MockA1ClientFactory.java index be7dfcb3..974c3b08 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/MockA1ClientFactory.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/MockA1ClientFactory.java @@ -59,7 +59,7 @@ public class MockA1ClientFactory extends A1ClientFactory { public MockA1Client getOrCreateA1Client(String ricId) { if (!clients.containsKey(ricId)) { logger.debug("Creating client for RIC: {}", ricId); - MockA1Client client = spy(new MockA1Client(ricId, appConfig, policyTypes, asynchDelay)); + MockA1Client client = spy(new MockA1Client(appConfig, policyTypes, asynchDelay)); clients.put(ricId, client); } return clients.get(ricId); |