From 0ae8299c8a4c811cc5bc001a8531f367687f0678 Mon Sep 17 00:00:00 2001 From: ToineSiebelink Date: Mon, 26 Aug 2024 17:52:34 +0100 Subject: Policy Executor, refactor(move) common http classes Issue-ID: CPS-2343 Change-Id: Ia9655364462826a794a2af78214311cb38c40de9 Signed-off-by: ToineSiebelink --- .../onap/cps/ncmp/impl/data/DmiDataOperations.java | 8 +- .../impl/datajobs/DataJobResultServiceImpl.java | 6 +- .../impl/datajobs/DataJobStatusServiceImpl.java | 6 +- .../impl/datajobs/DmiSubJobRequestHandler.java | 6 +- .../org/onap/cps/ncmp/impl/dmi/DmiRestClient.java | 1 + .../impl/dmi/DmiServiceUrlTemplateBuilder.java | 137 --------------------- .../ncmp/impl/dmi/DmiWebClientsConfiguration.java | 2 +- .../cps/ncmp/impl/dmi/UrlTemplateParameters.java | 30 ----- .../impl/inventory/sync/DmiModelOperations.java | 6 +- .../trustlevel/DmiPluginTrustLevelWatchDog.java | 6 +- .../PolicyExecutorWebClientConfiguration.java | 2 +- .../ncmp/impl/utils/WebClientConfiguration.java | 83 ------------- .../utils/http/RestServiceUrlTemplateBuilder.java | 136 ++++++++++++++++++++ .../impl/utils/http/UrlTemplateParameters.java | 30 +++++ .../impl/utils/http/WebClientConfiguration.java | 83 +++++++++++++ .../ncmp/impl/data/DmiDataOperationsSpec.groovy | 2 +- .../datajobs/DataJobResultServiceImplSpec.groovy | 2 +- .../datajobs/DataJobStatusServiceImplSpec.groovy | 2 +- .../cps/ncmp/impl/dmi/DmiRestClientSpec.groovy | 1 + .../dmi/DmiServiceUrlTemplateBuilderSpec.groovy | 64 ---------- .../inventory/sync/DmiModelOperationsSpec.groovy | 2 +- .../DmiPluginTrustLevelWatchDogSpec.groovy | 2 +- .../http/RestServiceUrlTemplateBuilderSpec.groovy | 63 ++++++++++ 23 files changed, 340 insertions(+), 340 deletions(-) delete mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/dmi/DmiServiceUrlTemplateBuilder.java delete mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/dmi/UrlTemplateParameters.java delete mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/utils/WebClientConfiguration.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/utils/http/RestServiceUrlTemplateBuilder.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/utils/http/UrlTemplateParameters.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/utils/http/WebClientConfiguration.java delete mode 100644 cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/dmi/DmiServiceUrlTemplateBuilderSpec.groovy create mode 100644 cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/utils/http/RestServiceUrlTemplateBuilderSpec.groovy diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/data/DmiDataOperations.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/data/DmiDataOperations.java index 8be57fed3d..0cedeae0a4 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/data/DmiDataOperations.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/data/DmiDataOperations.java @@ -45,12 +45,12 @@ import org.onap.cps.ncmp.impl.data.policyexecutor.PolicyExecutor; import org.onap.cps.ncmp.impl.data.utils.DmiDataOperationsHelper; import org.onap.cps.ncmp.impl.dmi.DmiProperties; import org.onap.cps.ncmp.impl.dmi.DmiRestClient; -import org.onap.cps.ncmp.impl.dmi.DmiServiceUrlTemplateBuilder; -import org.onap.cps.ncmp.impl.dmi.UrlTemplateParameters; import org.onap.cps.ncmp.impl.inventory.InventoryPersistence; import org.onap.cps.ncmp.impl.inventory.models.CmHandleState; import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle; import org.onap.cps.ncmp.impl.models.DmiRequestBody; +import org.onap.cps.ncmp.impl.utils.http.RestServiceUrlTemplateBuilder; +import org.onap.cps.ncmp.impl.utils.http.UrlTemplateParameters; import org.onap.cps.spi.exceptions.CpsException; import org.onap.cps.utils.JsonObjectMapper; import org.springframework.http.ResponseEntity; @@ -213,7 +213,7 @@ public class DmiDataOperations { final String optionsParamInQuery, final String topicParamInQuery) { final String dmiServiceName = yangModelCmHandle.resolveDmiServiceName(DATA); - return DmiServiceUrlTemplateBuilder.newInstance() + return RestServiceUrlTemplateBuilder.newInstance() .fixedPathSegment("ch") .variablePathSegment("cmHandleId", yangModelCmHandle.getId()) .fixedPathSegment("data") @@ -228,7 +228,7 @@ public class DmiDataOperations { private UrlTemplateParameters getUrlTemplateParameters(final String dmiServiceName, final String requestId, final String topicParamInQuery) { - return DmiServiceUrlTemplateBuilder.newInstance() + return RestServiceUrlTemplateBuilder.newInstance() .fixedPathSegment("data") .queryParameter("requestId", requestId) .queryParameter("topic", topicParamInQuery) diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/DataJobResultServiceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/DataJobResultServiceImpl.java index 031cedc78b..8934c088a1 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/DataJobResultServiceImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/DataJobResultServiceImpl.java @@ -24,8 +24,8 @@ import lombok.RequiredArgsConstructor; import org.onap.cps.ncmp.api.datajobs.DataJobResultService; import org.onap.cps.ncmp.impl.dmi.DmiProperties; import org.onap.cps.ncmp.impl.dmi.DmiRestClient; -import org.onap.cps.ncmp.impl.dmi.DmiServiceUrlTemplateBuilder; -import org.onap.cps.ncmp.impl.dmi.UrlTemplateParameters; +import org.onap.cps.ncmp.impl.utils.http.RestServiceUrlTemplateBuilder; +import org.onap.cps.ncmp.impl.utils.http.UrlTemplateParameters; import org.springframework.stereotype.Service; @Service @@ -41,7 +41,7 @@ public class DataJobResultServiceImpl implements DataJobResultService { final String dataProducerId, final String dataProducerJobId, final String destination) { - final UrlTemplateParameters urlTemplateParameters = DmiServiceUrlTemplateBuilder.newInstance() + final UrlTemplateParameters urlTemplateParameters = RestServiceUrlTemplateBuilder.newInstance() .fixedPathSegment("cmwriteJob") .fixedPathSegment("dataProducer") .variablePathSegment("dataProducerId", dataProducerId) diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/DataJobStatusServiceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/DataJobStatusServiceImpl.java index fb17f066ce..1cfb8a9dff 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/DataJobStatusServiceImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/DataJobStatusServiceImpl.java @@ -24,8 +24,8 @@ import lombok.RequiredArgsConstructor; import org.onap.cps.ncmp.api.datajobs.DataJobStatusService; import org.onap.cps.ncmp.impl.dmi.DmiProperties; import org.onap.cps.ncmp.impl.dmi.DmiRestClient; -import org.onap.cps.ncmp.impl.dmi.DmiServiceUrlTemplateBuilder; -import org.onap.cps.ncmp.impl.dmi.UrlTemplateParameters; +import org.onap.cps.ncmp.impl.utils.http.RestServiceUrlTemplateBuilder; +import org.onap.cps.ncmp.impl.utils.http.UrlTemplateParameters; import org.springframework.stereotype.Service; /** @@ -54,7 +54,7 @@ public class DataJobStatusServiceImpl implements DataJobStatusService { private UrlTemplateParameters buildUrlParameters(final String dmiServiceName, final String dataProducerId, final String dataProducerJobId) { - return DmiServiceUrlTemplateBuilder.newInstance() + return RestServiceUrlTemplateBuilder.newInstance() .fixedPathSegment("cmwriteJob") .fixedPathSegment("dataProducer") .variablePathSegment("dataProducerId", dataProducerId) diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/DmiSubJobRequestHandler.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/DmiSubJobRequestHandler.java index 0d14dace5e..a118d53e7e 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/DmiSubJobRequestHandler.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/DmiSubJobRequestHandler.java @@ -33,9 +33,9 @@ import org.onap.cps.ncmp.api.datajobs.models.SubJobWriteRequest; import org.onap.cps.ncmp.api.datajobs.models.SubJobWriteResponse; import org.onap.cps.ncmp.impl.dmi.DmiProperties; import org.onap.cps.ncmp.impl.dmi.DmiRestClient; -import org.onap.cps.ncmp.impl.dmi.DmiServiceUrlTemplateBuilder; -import org.onap.cps.ncmp.impl.dmi.UrlTemplateParameters; import org.onap.cps.ncmp.impl.models.RequiredDmiService; +import org.onap.cps.ncmp.impl.utils.http.RestServiceUrlTemplateBuilder; +import org.onap.cps.ncmp.impl.utils.http.UrlTemplateParameters; import org.onap.cps.utils.JsonObjectMapper; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; @@ -88,7 +88,7 @@ public class DmiSubJobRequestHandler { } private UrlTemplateParameters getUrlTemplateParameters(final String destination, final ProducerKey producerKey) { - return DmiServiceUrlTemplateBuilder.newInstance() + return RestServiceUrlTemplateBuilder.newInstance() .fixedPathSegment("cmwriteJob") .queryParameter("destination", destination) .createUrlTemplateParameters(producerKey.dmiServiceName(), dmiProperties.getDmiBasePath()); diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/dmi/DmiRestClient.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/dmi/DmiRestClient.java index c10132060d..a177272dff 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/dmi/DmiRestClient.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/dmi/DmiRestClient.java @@ -34,6 +34,7 @@ import lombok.extern.slf4j.Slf4j; import org.onap.cps.ncmp.api.data.models.OperationType; import org.onap.cps.ncmp.api.exceptions.DmiClientRequestException; import org.onap.cps.ncmp.impl.models.RequiredDmiService; +import org.onap.cps.ncmp.impl.utils.http.UrlTemplateParameters; import org.onap.cps.utils.JsonObjectMapper; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.HttpHeaders; diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/dmi/DmiServiceUrlTemplateBuilder.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/dmi/DmiServiceUrlTemplateBuilder.java deleted file mode 100644 index e7dbea83f2..0000000000 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/dmi/DmiServiceUrlTemplateBuilder.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2022-2024 Nordix Foundation - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.cps.ncmp.impl.dmi; - -import java.util.Collections; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.Map; -import lombok.NoArgsConstructor; -import org.apache.commons.lang3.StringUtils; -import org.apache.logging.log4j.util.Strings; -import org.springframework.web.util.UriComponentsBuilder; - -@NoArgsConstructor -public class DmiServiceUrlTemplateBuilder { - - private final UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.newInstance(); - private static final String FIXED_PATH_SEGMENT = null; - private static final String VERSION_SEGMENT = "v1"; - private final Map pathSegments = new LinkedHashMap<>(); - private final Map queryParameters = new LinkedHashMap<>(); - - /** - * Static factory method to create a new instance of DmiServiceUrlTemplateBuilder. - * - * @return a new instance of DmiServiceUrlTemplateBuilder - */ - public static DmiServiceUrlTemplateBuilder newInstance() { - return new DmiServiceUrlTemplateBuilder(); - } - - /** - * Add a fixed pathSegment to the URL. - * - * @param pathSegment the path segment - * @return this builder instance - */ - public DmiServiceUrlTemplateBuilder fixedPathSegment(final String pathSegment) { - pathSegments.put(pathSegment, FIXED_PATH_SEGMENT); - return this; - } - - /** - * Add a variable pathSegment to the URL. - * Do NOT add { } braces. the builder will take care of that - * - * @param pathSegment the name of the variable path segment (with { and } - * @param value the value to be insert in teh URL for the given variable path segment - * @return this builder instance - */ - public DmiServiceUrlTemplateBuilder variablePathSegment(final String pathSegment, final String value) { - pathSegments.put(pathSegment, value); - return this; - } - - /** - * Add a query parameter to the URL. - * Do NOT encode as the builder wil take care of encoding - * - * @param queryParameterName the name of the variable - * @param queryParameterValue the value of the variable (only Strings are supported). - * - * @return this builder instance - */ - public DmiServiceUrlTemplateBuilder queryParameter(final String queryParameterName, - final String queryParameterValue) { - if (Strings.isNotBlank(queryParameterValue)) { - queryParameters.put(queryParameterName, queryParameterValue); - } - return this; - } - - /** - * Constructs a URL template with variables based on the accumulated path segments and query parameters. - * - * @param dmiServiceBaseUrl the base URL of the DMI service, e.g., "http://dmi-service.com". - * @param dmiBasePath the base path of the DMI service - * @return a UrlTemplateParameters instance containing the complete URL template and URL variables - */ - public UrlTemplateParameters createUrlTemplateParameters(final String dmiServiceBaseUrl, final String dmiBasePath) { - this.uriComponentsBuilder.pathSegment(dmiBasePath) - .pathSegment(VERSION_SEGMENT); - - final Map urlTemplateVariables = new HashMap<>(); - - pathSegments.forEach((pathSegmentName, variablePathValue) -> { - if (StringUtils.equals(variablePathValue, FIXED_PATH_SEGMENT)) { - this.uriComponentsBuilder.pathSegment(pathSegmentName); - } else { - this.uriComponentsBuilder.pathSegment("{" + pathSegmentName + "}"); - urlTemplateVariables.put(pathSegmentName, variablePathValue); - } - }); - - queryParameters.forEach((paramName, paramValue) -> { - this.uriComponentsBuilder.queryParam(paramName, "{" + paramName + "}"); - urlTemplateVariables.put(paramName, paramValue); - }); - - final String urlTemplate = dmiServiceBaseUrl + this.uriComponentsBuilder.build().toUriString(); - return new UrlTemplateParameters(urlTemplate, urlTemplateVariables); - } - - /** - * Constructs a URL for DMI health check based on the given base URL. - * - * @param dmiServiceBaseUrl the base URL of the DMI service, e.g., "http://dmi-service.com". - * @return a {@link UrlTemplateParameters} instance containing the complete URL template and empty URL variables, - * suitable for DMI health check. - */ - public UrlTemplateParameters createUrlTemplateParametersForHealthCheck(final String dmiServiceBaseUrl) { - this.uriComponentsBuilder.pathSegment("actuator") - .pathSegment("health"); - - final String urlTemplate = dmiServiceBaseUrl + this.uriComponentsBuilder.build().toUriString(); - return new UrlTemplateParameters(urlTemplate, Collections.emptyMap()); - } - -} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/dmi/DmiWebClientsConfiguration.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/dmi/DmiWebClientsConfiguration.java index 265d284239..4134a56ead 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/dmi/DmiWebClientsConfiguration.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/dmi/DmiWebClientsConfiguration.java @@ -22,7 +22,7 @@ package org.onap.cps.ncmp.impl.dmi; import lombok.RequiredArgsConstructor; import org.onap.cps.ncmp.config.DmiHttpClientConfig; -import org.onap.cps.ncmp.impl.utils.WebClientConfiguration; +import org.onap.cps.ncmp.impl.utils.http.WebClientConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.reactive.function.client.WebClient; diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/dmi/UrlTemplateParameters.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/dmi/UrlTemplateParameters.java deleted file mode 100644 index f51511116a..0000000000 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/dmi/UrlTemplateParameters.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2024 Nordix Foundation - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.cps.ncmp.impl.dmi; - -import java.util.Map; - -/** - * Represents a URL template with associated variables for dynamic substitution. - * This record encapsulates a URL template string and a map of variables used for substitution within the template. - */ -public record UrlTemplateParameters(String urlTemplate, Map urlVariables) { -} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/DmiModelOperations.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/DmiModelOperations.java index 433c67f100..8ba70b3a31 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/DmiModelOperations.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/DmiModelOperations.java @@ -36,10 +36,10 @@ import lombok.RequiredArgsConstructor; import org.onap.cps.ncmp.api.inventory.models.YangResource; import org.onap.cps.ncmp.impl.dmi.DmiProperties; import org.onap.cps.ncmp.impl.dmi.DmiRestClient; -import org.onap.cps.ncmp.impl.dmi.DmiServiceUrlTemplateBuilder; -import org.onap.cps.ncmp.impl.dmi.UrlTemplateParameters; import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle; import org.onap.cps.ncmp.impl.models.DmiRequestBody; +import org.onap.cps.ncmp.impl.utils.http.RestServiceUrlTemplateBuilder; +import org.onap.cps.ncmp.impl.utils.http.UrlTemplateParameters; import org.onap.cps.spi.model.ModuleReference; import org.onap.cps.utils.JsonObjectMapper; import org.springframework.http.ResponseEntity; @@ -107,7 +107,7 @@ public class DmiModelOperations { final String jsonRequestBody, final String cmHandle, final String resourceName) { - final UrlTemplateParameters urlTemplateParameters = DmiServiceUrlTemplateBuilder.newInstance() + final UrlTemplateParameters urlTemplateParameters = RestServiceUrlTemplateBuilder.newInstance() .fixedPathSegment("ch") .variablePathSegment("cmHandleId", cmHandle) .fixedPathSegment(resourceName) diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/DmiPluginTrustLevelWatchDog.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/DmiPluginTrustLevelWatchDog.java index c81e9b7840..94c5ef75f1 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/DmiPluginTrustLevelWatchDog.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/DmiPluginTrustLevelWatchDog.java @@ -26,9 +26,9 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.onap.cps.ncmp.api.inventory.models.TrustLevel; import org.onap.cps.ncmp.impl.dmi.DmiRestClient; -import org.onap.cps.ncmp.impl.dmi.DmiServiceUrlTemplateBuilder; -import org.onap.cps.ncmp.impl.dmi.UrlTemplateParameters; import org.onap.cps.ncmp.impl.inventory.CmHandleQueryService; +import org.onap.cps.ncmp.impl.utils.http.RestServiceUrlTemplateBuilder; +import org.onap.cps.ncmp.impl.utils.http.UrlTemplateParameters; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @@ -73,7 +73,7 @@ public class DmiPluginTrustLevelWatchDog { } private String getDmiHealthStatus(final String dmiServiceBaseUrl) { - final UrlTemplateParameters urlTemplateParameters = DmiServiceUrlTemplateBuilder.newInstance() + final UrlTemplateParameters urlTemplateParameters = RestServiceUrlTemplateBuilder.newInstance() .createUrlTemplateParametersForHealthCheck(dmiServiceBaseUrl); return dmiRestClient.getDmiHealthStatus(urlTemplateParameters).block(); } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/policyexecutor/PolicyExecutorWebClientConfiguration.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/policyexecutor/PolicyExecutorWebClientConfiguration.java index a927764825..333030ccd2 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/policyexecutor/PolicyExecutorWebClientConfiguration.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/policyexecutor/PolicyExecutorWebClientConfiguration.java @@ -22,7 +22,7 @@ package org.onap.cps.ncmp.impl.policyexecutor; import lombok.RequiredArgsConstructor; import org.onap.cps.ncmp.config.PolicyExecutorHttpClientConfig; -import org.onap.cps.ncmp.impl.utils.WebClientConfiguration; +import org.onap.cps.ncmp.impl.utils.http.WebClientConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.reactive.function.client.WebClient; diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/utils/WebClientConfiguration.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/utils/WebClientConfiguration.java deleted file mode 100644 index 967f74e942..0000000000 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/utils/WebClientConfiguration.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2024 Nordix Foundation. - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.cps.ncmp.impl.utils; - -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import io.netty.channel.ChannelOption; -import io.netty.handler.timeout.ReadTimeoutHandler; -import io.netty.handler.timeout.WriteTimeoutHandler; -import io.netty.resolver.DefaultAddressResolverGroup; -import java.time.Duration; -import java.util.concurrent.TimeUnit; -import org.onap.cps.ncmp.config.ServiceConfig; -import org.springframework.http.HttpHeaders; -import org.springframework.http.MediaType; -import org.springframework.http.client.reactive.ReactorClientHttpConnector; -import org.springframework.web.reactive.function.client.WebClient; -import reactor.netty.http.client.HttpClient; -import reactor.netty.resources.ConnectionProvider; - -/** - * Configures and creates WebClient beans for various rest services such as DMI and Policy Executor. - * The configuration utilizes Netty-based HttpClient with custom connection settings, read and write timeouts, - * and initializes WebClient with these settings to ensure optimal performance and resource management. - */ -public class WebClientConfiguration { - - private static final Duration DEFAULT_RESPONSE_TIMEOUT = Duration.ofSeconds(30); - - protected WebClient configureWebClient(final WebClient.Builder webClientBuilder, - final ServiceConfig serviceConfig) { - final ConnectionProvider connectionProvider = getConnectionProvider(serviceConfig); - final HttpClient httpClient = createHttpClient(serviceConfig, connectionProvider); - return buildAndGetWebClient(webClientBuilder, httpClient, serviceConfig.getMaximumInMemorySizeInMegabytes()); - } - - private static HttpClient createHttpClient(final ServiceConfig serviceConfig, - final ConnectionProvider connectionProvider) { - return HttpClient.create(connectionProvider) - .responseTimeout(DEFAULT_RESPONSE_TIMEOUT) - .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, serviceConfig.getConnectionTimeoutInSeconds() * 1000) - .doOnConnected(connection -> connection.addHandlerLast(new ReadTimeoutHandler( - serviceConfig.getReadTimeoutInSeconds(), TimeUnit.SECONDS)).addHandlerLast( - new WriteTimeoutHandler(serviceConfig.getWriteTimeoutInSeconds(), TimeUnit.SECONDS))) - .resolver(DefaultAddressResolverGroup.INSTANCE) - .compress(true); - } - - @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE") - private static ConnectionProvider getConnectionProvider(final ServiceConfig serviceConfig) { - return ConnectionProvider.builder(serviceConfig.getConnectionProviderName()) - .maxConnections(serviceConfig.getMaximumConnectionsTotal()) - .pendingAcquireMaxCount(serviceConfig.getPendingAcquireMaxCount()) - .build(); - } - - private WebClient buildAndGetWebClient(final WebClient.Builder webClientBuilder, final HttpClient httpClient, - final int maximumInMemorySizeInMegabytes) { - return webClientBuilder - .defaultHeaders(header -> header.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)) - .defaultHeaders(header -> header.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)) - .clientConnector(new ReactorClientHttpConnector(httpClient)) - .codecs(configurer -> configurer.defaultCodecs() - .maxInMemorySize(maximumInMemorySizeInMegabytes * 1024 * 1024)).build(); - } -} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/utils/http/RestServiceUrlTemplateBuilder.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/utils/http/RestServiceUrlTemplateBuilder.java new file mode 100644 index 0000000000..fafb09007d --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/utils/http/RestServiceUrlTemplateBuilder.java @@ -0,0 +1,136 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022-2024 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.ncmp.impl.utils.http; + +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; +import lombok.NoArgsConstructor; +import org.apache.commons.lang3.StringUtils; +import org.apache.logging.log4j.util.Strings; +import org.springframework.web.util.UriComponentsBuilder; + +@NoArgsConstructor +public class RestServiceUrlTemplateBuilder { + + private final UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.newInstance(); + private static final String FIXED_PATH_SEGMENT = null; + private static final String VERSION_SEGMENT = "v1"; + private final Map pathSegments = new LinkedHashMap<>(); + private final Map queryParameters = new LinkedHashMap<>(); + + /** + * Static factory method to create a new instance of DmiServiceUrlTemplateBuilder. + * + * @return a new instance of DmiServiceUrlTemplateBuilder + */ + public static RestServiceUrlTemplateBuilder newInstance() { + return new RestServiceUrlTemplateBuilder(); + } + + /** + * Add a fixed pathSegment to the URL. + * + * @param pathSegment the path segment + * @return this builder instance + */ + public RestServiceUrlTemplateBuilder fixedPathSegment(final String pathSegment) { + pathSegments.put(pathSegment, FIXED_PATH_SEGMENT); + return this; + } + + /** + * Add a variable pathSegment to the URL. + * Do NOT add { } braces. the builder will take care of that + * + * @param pathSegment the name of the variable path segment (with { and } + * @param value the value to be insert in teh URL for the given variable path segment + * @return this builder instance + */ + public RestServiceUrlTemplateBuilder variablePathSegment(final String pathSegment, final String value) { + pathSegments.put(pathSegment, value); + return this; + } + + /** + * Add a query parameter to the URL. + * Do NOT encode as the builder wil take care of encoding + * + * @param queryParameterName the name of the variable + * @param queryParameterValue the value of the variable (only Strings are supported). + * + * @return this builder instance + */ + public RestServiceUrlTemplateBuilder queryParameter(final String queryParameterName, + final String queryParameterValue) { + if (Strings.isNotBlank(queryParameterValue)) { + queryParameters.put(queryParameterName, queryParameterValue); + } + return this; + } + + /** + * Constructs a URL template with variables based on the accumulated path segments and query parameters. + * + * @param serviceBaseUrl the base URL of the service, e.g., "http://dmi-service.com". + * @param basePath the base path of the service + * @return a UrlTemplateParameters instance containing the complete URL template and URL variables + */ + public UrlTemplateParameters createUrlTemplateParameters(final String serviceBaseUrl, final String basePath) { + this.uriComponentsBuilder.pathSegment(basePath) + .pathSegment(VERSION_SEGMENT); + + final Map urlTemplateVariables = new HashMap<>(); + + pathSegments.forEach((pathSegmentName, variablePathValue) -> { + if (StringUtils.equals(variablePathValue, FIXED_PATH_SEGMENT)) { + this.uriComponentsBuilder.pathSegment(pathSegmentName); + } else { + this.uriComponentsBuilder.pathSegment("{" + pathSegmentName + "}"); + urlTemplateVariables.put(pathSegmentName, variablePathValue); + } + }); + + queryParameters.forEach((paramName, paramValue) -> { + this.uriComponentsBuilder.queryParam(paramName, "{" + paramName + "}"); + urlTemplateVariables.put(paramName, paramValue); + }); + + final String urlTemplate = serviceBaseUrl + this.uriComponentsBuilder.build().toUriString(); + return new UrlTemplateParameters(urlTemplate, urlTemplateVariables); + } + + /** + * Constructs a URL for DMI health check based on the given base URL. + * + * @param serviceBaseUrl the base URL of the service, e.g., "http://dmi-service.com". + * @return a {@link UrlTemplateParameters} instance containing the complete URL template and empty URL variables, + * suitable for DMI health check. + */ + public UrlTemplateParameters createUrlTemplateParametersForHealthCheck(final String serviceBaseUrl) { + this.uriComponentsBuilder.pathSegment("actuator").pathSegment("health"); + + final String urlTemplate = serviceBaseUrl + this.uriComponentsBuilder.build().toUriString(); + return new UrlTemplateParameters(urlTemplate, Collections.emptyMap()); + } + +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/utils/http/UrlTemplateParameters.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/utils/http/UrlTemplateParameters.java new file mode 100644 index 0000000000..839af71823 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/utils/http/UrlTemplateParameters.java @@ -0,0 +1,30 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.ncmp.impl.utils.http; + +import java.util.Map; + +/** + * Represents a URL template with associated variables for dynamic substitution. + * This record encapsulates a URL template string and a map of variables used for substitution within the template. + */ +public record UrlTemplateParameters(String urlTemplate, Map urlVariables) { +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/utils/http/WebClientConfiguration.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/utils/http/WebClientConfiguration.java new file mode 100644 index 0000000000..d8e8350345 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/utils/http/WebClientConfiguration.java @@ -0,0 +1,83 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.ncmp.impl.utils.http; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import io.netty.channel.ChannelOption; +import io.netty.handler.timeout.ReadTimeoutHandler; +import io.netty.handler.timeout.WriteTimeoutHandler; +import io.netty.resolver.DefaultAddressResolverGroup; +import java.time.Duration; +import java.util.concurrent.TimeUnit; +import org.onap.cps.ncmp.config.ServiceConfig; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.client.reactive.ReactorClientHttpConnector; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.netty.http.client.HttpClient; +import reactor.netty.resources.ConnectionProvider; + +/** + * Configures and creates WebClient beans for various rest services such as DMI and Policy Executor. + * The configuration utilizes Netty-based HttpClient with custom connection settings, read and write timeouts, + * and initializes WebClient with these settings to ensure optimal performance and resource management. + */ +public class WebClientConfiguration { + + private static final Duration DEFAULT_RESPONSE_TIMEOUT = Duration.ofSeconds(30); + + protected WebClient configureWebClient(final WebClient.Builder webClientBuilder, + final ServiceConfig serviceConfig) { + final ConnectionProvider connectionProvider = getConnectionProvider(serviceConfig); + final HttpClient httpClient = createHttpClient(serviceConfig, connectionProvider); + return buildAndGetWebClient(webClientBuilder, httpClient, serviceConfig.getMaximumInMemorySizeInMegabytes()); + } + + private static HttpClient createHttpClient(final ServiceConfig serviceConfig, + final ConnectionProvider connectionProvider) { + return HttpClient.create(connectionProvider) + .responseTimeout(DEFAULT_RESPONSE_TIMEOUT) + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, serviceConfig.getConnectionTimeoutInSeconds() * 1000) + .doOnConnected(connection -> connection.addHandlerLast(new ReadTimeoutHandler( + serviceConfig.getReadTimeoutInSeconds(), TimeUnit.SECONDS)).addHandlerLast( + new WriteTimeoutHandler(serviceConfig.getWriteTimeoutInSeconds(), TimeUnit.SECONDS))) + .resolver(DefaultAddressResolverGroup.INSTANCE) + .compress(true); + } + + @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE") + private static ConnectionProvider getConnectionProvider(final ServiceConfig serviceConfig) { + return ConnectionProvider.builder(serviceConfig.getConnectionProviderName()) + .maxConnections(serviceConfig.getMaximumConnectionsTotal()) + .pendingAcquireMaxCount(serviceConfig.getPendingAcquireMaxCount()) + .build(); + } + + private WebClient buildAndGetWebClient(final WebClient.Builder webClientBuilder, final HttpClient httpClient, + final int maximumInMemorySizeInMegabytes) { + return webClientBuilder + .defaultHeaders(header -> header.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)) + .defaultHeaders(header -> header.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)) + .clientConnector(new ReactorClientHttpConnector(httpClient)) + .codecs(configurer -> configurer.defaultCodecs() + .maxInMemorySize(maximumInMemorySizeInMegabytes * 1024 * 1024)).build(); + } +} diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/data/DmiDataOperationsSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/data/DmiDataOperationsSpec.groovy index 2324d48a93..ec13aee4bb 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/data/DmiDataOperationsSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/data/DmiDataOperationsSpec.groovy @@ -31,9 +31,9 @@ import org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent import org.onap.cps.ncmp.impl.data.policyexecutor.PolicyExecutor import org.onap.cps.ncmp.impl.dmi.DmiOperationsBaseSpec import org.onap.cps.ncmp.impl.dmi.DmiProperties -import org.onap.cps.ncmp.impl.dmi.UrlTemplateParameters import org.onap.cps.ncmp.impl.inventory.models.CmHandleState import org.onap.cps.ncmp.impl.utils.AlternateIdMatcher +import org.onap.cps.ncmp.impl.utils.http.UrlTemplateParameters import org.onap.cps.ncmp.utils.TestUtils import org.onap.cps.utils.JsonObjectMapper import org.spockframework.spring.SpringBean diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/datajobs/DataJobResultServiceImplSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/datajobs/DataJobResultServiceImplSpec.groovy index 3af474040e..74bd0484df 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/datajobs/DataJobResultServiceImplSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/datajobs/DataJobResultServiceImplSpec.groovy @@ -22,7 +22,7 @@ package org.onap.cps.ncmp.impl.datajobs import org.onap.cps.ncmp.impl.dmi.DmiProperties import org.onap.cps.ncmp.impl.dmi.DmiRestClient -import org.onap.cps.ncmp.impl.dmi.UrlTemplateParameters +import org.onap.cps.ncmp.impl.utils.http.UrlTemplateParameters import reactor.core.publisher.Mono import spock.lang.Specification diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/datajobs/DataJobStatusServiceImplSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/datajobs/DataJobStatusServiceImplSpec.groovy index d231dfa755..be46d885e0 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/datajobs/DataJobStatusServiceImplSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/datajobs/DataJobStatusServiceImplSpec.groovy @@ -22,7 +22,7 @@ package org.onap.cps.ncmp.impl.datajobs import org.onap.cps.ncmp.impl.dmi.DmiProperties import org.onap.cps.ncmp.impl.dmi.DmiRestClient -import org.onap.cps.ncmp.impl.dmi.UrlTemplateParameters +import org.onap.cps.ncmp.impl.utils.http.UrlTemplateParameters import reactor.core.publisher.Mono import spock.lang.Specification diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/dmi/DmiRestClientSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/dmi/DmiRestClientSpec.groovy index d92e69a136..4d47ef14a0 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/dmi/DmiRestClientSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/dmi/DmiRestClientSpec.groovy @@ -25,6 +25,7 @@ import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.node.ObjectNode import org.onap.cps.ncmp.api.exceptions.DmiClientRequestException +import org.onap.cps.ncmp.impl.utils.http.UrlTemplateParameters import org.onap.cps.ncmp.utils.TestUtils import org.onap.cps.utils.JsonObjectMapper import org.springframework.http.HttpHeaders diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/dmi/DmiServiceUrlTemplateBuilderSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/dmi/DmiServiceUrlTemplateBuilderSpec.groovy deleted file mode 100644 index 9e1b37023b..0000000000 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/dmi/DmiServiceUrlTemplateBuilderSpec.groovy +++ /dev/null @@ -1,64 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2022-2024 Nordix Foundation - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.cps.ncmp.impl.dmi - - -import spock.lang.Specification - -class DmiServiceUrlTemplateBuilderSpec extends Specification { - - def objectUnderTest = new DmiServiceUrlTemplateBuilder() - - def 'Build URL template parameters with (variable) path segments and query parameters.'() { - given: 'the URL details are given to the builder' - objectUnderTest.fixedPathSegment('segment') - objectUnderTest.variablePathSegment('myVariableSegment','someValue') - objectUnderTest.fixedPathSegment('segment?with:special&characters') - objectUnderTest.queryParameter('param1', 'abc') - objectUnderTest.queryParameter('param2', 'value?with#special:characters') - when: 'the URL template parameters are created' - def result = objectUnderTest.createUrlTemplateParameters('myDmiServer', 'myBasePath') - then: 'the URL template contains variable names instead of value and un-encoded fixed segment' - assert result.urlTemplate == 'myDmiServer/myBasePath/v1/segment/{myVariableSegment}/segment?with:special&characters?param1={param1}¶m2={param2}' - and: 'URL variables contains name and un-encoded value pairs' - assert result.urlVariables == ['myVariableSegment': 'someValue', 'param1': 'abc', 'param2': 'value?with#special:characters'] - } - - def 'Build URL template parameters with special characters in query parameters.'() { - given: 'the query parameter is given to the builder' - objectUnderTest.queryParameter('my¶m', 'special&characters=are?not\\encoded') - when: 'the URL template parameters are created' - def result = objectUnderTest.createUrlTemplateParameters('myDmiServer', 'myBasePath') - then: 'Special characters are not encoded' - assert result.urlVariables == ['my¶m': 'special&characters=are?not\\encoded'] - } - - def 'Build URL template parameters with empty query parameters.'() { - when: 'the query parameter is given to the builder' - objectUnderTest.queryParameter('param', value) - and: 'the URL template parameters are create' - def result = objectUnderTest.createUrlTemplateParameters('myDmiServer', 'myBasePath') - then: 'no parameter gets added' - assert result.urlVariables.isEmpty() - where: 'the following parameter values are used' - value << [ null, '', ' ' ] - } -} diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/inventory/sync/DmiModelOperationsSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/inventory/sync/DmiModelOperationsSpec.groovy index 196a1cd360..c80aa7b242 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/inventory/sync/DmiModelOperationsSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/inventory/sync/DmiModelOperationsSpec.groovy @@ -25,7 +25,7 @@ import com.fasterxml.jackson.core.JsonProcessingException import com.fasterxml.jackson.databind.ObjectMapper import org.onap.cps.ncmp.impl.dmi.DmiOperationsBaseSpec import org.onap.cps.ncmp.impl.dmi.DmiProperties -import org.onap.cps.ncmp.impl.dmi.UrlTemplateParameters +import org.onap.cps.ncmp.impl.utils.http.UrlTemplateParameters import org.onap.cps.spi.model.ModuleReference import org.onap.cps.utils.JsonObjectMapper import org.spockframework.spring.SpringBean diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/inventory/trustlevel/DmiPluginTrustLevelWatchDogSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/inventory/trustlevel/DmiPluginTrustLevelWatchDogSpec.groovy index 0a34d267c5..3a1cd14c48 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/inventory/trustlevel/DmiPluginTrustLevelWatchDogSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/inventory/trustlevel/DmiPluginTrustLevelWatchDogSpec.groovy @@ -22,8 +22,8 @@ package org.onap.cps.ncmp.impl.inventory.trustlevel import org.onap.cps.ncmp.api.inventory.models.TrustLevel import org.onap.cps.ncmp.impl.dmi.DmiRestClient -import org.onap.cps.ncmp.impl.dmi.UrlTemplateParameters import org.onap.cps.ncmp.impl.inventory.CmHandleQueryService +import org.onap.cps.ncmp.impl.utils.http.UrlTemplateParameters import reactor.core.publisher.Mono import spock.lang.Specification diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/utils/http/RestServiceUrlTemplateBuilderSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/utils/http/RestServiceUrlTemplateBuilderSpec.groovy new file mode 100644 index 0000000000..9e051156f7 --- /dev/null +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/utils/http/RestServiceUrlTemplateBuilderSpec.groovy @@ -0,0 +1,63 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022-2024 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.ncmp.impl.utils.http + +import spock.lang.Specification + +class RestServiceUrlTemplateBuilderSpec extends Specification { + + def objectUnderTest = new RestServiceUrlTemplateBuilder() + + def 'Build URL template parameters with (variable) path segments and query parameters.'() { + given: 'the URL details are given to the builder' + objectUnderTest.fixedPathSegment('segment') + objectUnderTest.variablePathSegment('myVariableSegment','someValue') + objectUnderTest.fixedPathSegment('segment?with:special&characters') + objectUnderTest.queryParameter('param1', 'abc') + objectUnderTest.queryParameter('param2', 'value?with#special:characters') + when: 'the URL template parameters are created' + def result = objectUnderTest.createUrlTemplateParameters('myServer', 'myBasePath') + then: 'the URL template contains variable names instead of value and un-encoded fixed segment' + assert result.urlTemplate == 'myServer/myBasePath/v1/segment/{myVariableSegment}/segment?with:special&characters?param1={param1}¶m2={param2}' + and: 'URL variables contains name and un-encoded value pairs' + assert result.urlVariables == ['myVariableSegment': 'someValue', 'param1': 'abc', 'param2': 'value?with#special:characters'] + } + + def 'Build URL template parameters with special characters in query parameters.'() { + given: 'the query parameter is given to the builder' + objectUnderTest.queryParameter('my¶m', 'special&characters=are?not\\encoded') + when: 'the URL template parameters are created' + def result = objectUnderTest.createUrlTemplateParameters('myServer', 'myBasePath') + then: 'Special characters are not encoded' + assert result.urlVariables == ['my¶m': 'special&characters=are?not\\encoded'] + } + + def 'Build URL template parameters with empty query parameters.'() { + when: 'the query parameter is given to the builder' + objectUnderTest.queryParameter('param', value) + and: 'the URL template parameters are create' + def result = objectUnderTest.createUrlTemplateParameters('myServer', 'myBasePath') + then: 'no parameter gets added' + assert result.urlVariables.isEmpty() + where: 'the following parameter values are used' + value << [ null, '', ' ' ] + } +} -- cgit 1.2.3-korg