diff options
author | leventecsanyi <levente.csanyi@est.tech> | 2024-04-18 16:30:29 +0200 |
---|---|---|
committer | leventecsanyi <levente.csanyi@est.tech> | 2024-04-25 11:26:06 +0200 |
commit | 71bcac8b10e3298a8ed78e137540472c2f7f8e40 (patch) | |
tree | 13b247fd955224bff900e97591c081b170bf6798 /cps-ncmp-service/src/test/groovy | |
parent | e90f0aa082e92b332f7bac7878be7f6576e722a3 (diff) |
Replace RestTemplate with WebClient in synchronous DMI calls
- added DmiWebClientConfiguration
- use WebClient in DmiRestClient
- fixed unit tests
- ingnored failing NCMP related integration tests (related to WebClient, will be fixed in a future patch)
- encode query params for DMI requests
Issue-ID:CPS-989
Change-Id: I26434568f68266e95669a51c134978ddd197d802
Signed-off-by: leventecsanyi <levente.csanyi@est.tech>
Diffstat (limited to 'cps-ncmp-service/src/test/groovy')
7 files changed, 110 insertions, 39 deletions
diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/client/DmiRestClientSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/client/DmiRestClientSpec.groovy index c8e34b1a5e..003dbf5b04 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/client/DmiRestClientSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/client/DmiRestClientSpec.groovy @@ -24,20 +24,21 @@ package org.onap.cps.ncmp.api.impl.client 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.impl.config.NcmpConfiguration -import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration.DmiProperties; +import org.onap.cps.ncmp.api.impl.config.DmiWebClientConfiguration; import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException import org.onap.cps.ncmp.utils.TestUtils import org.spockframework.spring.SpringBean import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest -import org.springframework.http.HttpEntity + import org.springframework.http.HttpHeaders import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity import org.springframework.test.context.ContextConfiguration +import org.springframework.test.context.TestPropertySource import org.springframework.web.client.HttpServerErrorException -import org.springframework.web.client.RestTemplate +import org.springframework.web.reactive.function.client.WebClient +import reactor.core.publisher.Mono import spock.lang.Specification import static org.onap.cps.ncmp.api.impl.operations.OperationType.READ @@ -45,43 +46,49 @@ import static org.onap.cps.ncmp.api.impl.operations.OperationType.PATCH import static org.onap.cps.ncmp.api.impl.operations.OperationType.CREATE @SpringBootTest -@ContextConfiguration(classes = [DmiProperties, DmiRestClient, ObjectMapper]) +@ContextConfiguration(classes = [DmiWebClientConfiguration, DmiRestClient, ObjectMapper]) class DmiRestClientSpec extends Specification { static final NO_AUTH_HEADER = null static final BASIC_AUTH_HEADER = 'Basic c29tZS11c2VyOnNvbWUtcGFzc3dvcmQ=' static final BEARER_AUTH_HEADER = 'Bearer my-bearer-token' - @SpringBean - RestTemplate mockRestTemplate = Mock(RestTemplate) - @Autowired - NcmpConfiguration.DmiProperties dmiProperties + DmiWebClientConfiguration.DmiProperties dmiProperties @Autowired DmiRestClient objectUnderTest + @SpringBean + WebClient mockWebClient = Mock(WebClient); + @Autowired ObjectMapper objectMapper - def responseFromRestTemplate = Mock(ResponseEntity) - + def mockRequestBodyUriSpec = Mock(WebClient.RequestBodyUriSpec) + def mockResponseSpec = Mock(WebClient.ResponseSpec) + def mockResponseEntity = Mock(ResponseEntity) + def monoSpec = Mono.just(mockResponseEntity) def 'DMI POST operation with JSON.'() { - given: 'the rest template returns a valid response entity for the expected parameters' - mockRestTemplate.postForEntity('my url', _ as HttpEntity, Object.class) >> responseFromRestTemplate + given: 'the web client returns a valid response entity for the expected parameters' + mockWebClient.post() >> mockRequestBodyUriSpec + mockWebclientResponse() + mockRequestBodyUriSpec.body(_) >> mockRequestBodyUriSpec + mockResponseSpec.toEntity(Object.class) >> monoSpec + monoSpec.block() >> mockResponseEntity when: 'POST operation is invoked' - def result = objectUnderTest.postOperationWithJsonData('my url', 'some json', READ, null) + def result = objectUnderTest.postOperationWithJsonData('/my/url', 'some json', READ, null) then: 'the output of the method is equal to the output from the test template' - result == responseFromRestTemplate + result == mockResponseEntity } def 'Failing DMI POST operation.'() { given: 'the rest template returns a valid response entity' def serverResponse = 'server response'.getBytes() def httpServerErrorException = new HttpServerErrorException(HttpStatus.FORBIDDEN, 'status text', serverResponse, null) - mockRestTemplate.postForEntity(*_) >> { throw httpServerErrorException } + mockWebClient.post() >> { throw httpServerErrorException } when: 'POST operation is invoked' - def result = objectUnderTest.postOperationWithJsonData('some url', 'some json', operation, null) + def result = objectUnderTest.postOperationWithJsonData('/some', 'some json', operation, null) then: 'a Http Client Exception is thrown' def thrown = thrown(HttpClientRequestException) and: 'the exception has the relevant details from the error response' @@ -97,16 +104,20 @@ class DmiRestClientSpec extends Specification { def dmiPluginHealthCheckResponseJsonData = TestUtils.getResourceFileContent('dmiPluginHealthCheckResponse.json') def jsonNode = objectMapper.readValue(dmiPluginHealthCheckResponseJsonData, JsonNode.class) ((ObjectNode) jsonNode).put('status', 'my status') - mockRestTemplate.getForObject(*_) >> {jsonNode} + def monoResponse = Mono.just(jsonNode) + mockWebClient.get() >> mockRequestBodyUriSpec + mockWebclientResponse() + mockResponseSpec.bodyToMono(_) >> monoResponse + monoResponse.block() >> jsonNode when: 'get trust level of the dmi plugin' - def result = objectUnderTest.getDmiHealthStatus('some url') + def result = objectUnderTest.getDmiHealthStatus('some/url') then: 'the status value from the json is return' assert result == 'my status' } def 'Failing to get dmi plugin health status #scenario'() { given: 'rest template with #scenario' - mockRestTemplate.getForObject(*_) >> healthStatusResponse + mockWebClient.get() >> healthStatusResponse when: 'attempt to get health status of the dmi plugin' def result = objectUnderTest.getDmiHealthStatus('some url') then: 'result will be empty' @@ -133,4 +144,9 @@ class DmiRestClientSpec extends Specification { 'DMI basic auth disabled, with NCMP basic auth' | false | BASIC_AUTH_HEADER || NO_AUTH_HEADER } + def mockWebclientResponse() { + mockRequestBodyUriSpec.uri(_) >> mockRequestBodyUriSpec + mockRequestBodyUriSpec.headers(_) >> mockRequestBodyUriSpec + mockRequestBodyUriSpec.retrieve() >> mockResponseSpec + } } diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/config/DmiWebClientConfigurationSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/config/DmiWebClientConfigurationSpec.groovy new file mode 100644 index 0000000000..c9491cd02e --- /dev/null +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/config/DmiWebClientConfigurationSpec.groovy @@ -0,0 +1,62 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2023 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.api.impl.config + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.test.context.ContextConfiguration +import org.springframework.test.context.TestPropertySource +import org.springframework.web.reactive.function.client.WebClient +import spock.lang.Specification + +@SpringBootTest +@ContextConfiguration(classes = [DmiWebClientConfiguration.DmiProperties]) +@TestPropertySource(properties = ["ncmp.dmi.httpclient.connectionTimeoutInSeconds=1"]) +class DmiWebClientConfigurationSpec extends Specification { + + @Autowired + DmiWebClientConfiguration.DmiProperties dmiProperties + + def objectUnderTest = new DmiWebClientConfiguration() + + def setup() { + objectUnderTest.connectionTimeoutInSeconds = 10 + } + + def 'DMI Properties.'() { + expect: 'properties are set to values in test configuration yaml file' + dmiProperties.authUsername == 'some-user' + dmiProperties.authPassword == 'some-password' + } + + def 'Web Client Configuration construction.'() { + expect: 'the system can create an instance' + new DmiWebClientConfiguration() != null + } + + def 'Creating a WebClient instance.'() { + given: 'WebClient configuration invoked' + def webClientInstance = objectUnderTest.webClient() + expect: 'the system can create an instance' + assert webClientInstance != null + assert webClientInstance instanceof WebClient + } +} diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/config/NcmpConfigurationSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/config/NcmpConfigurationSpec.groovy index a4df9b37cf..f588e2ed25 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/config/NcmpConfigurationSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/config/NcmpConfigurationSpec.groovy @@ -31,15 +31,12 @@ import org.springframework.web.client.RestTemplate import spock.lang.Specification @SpringBootTest -@ContextConfiguration(classes = [NcmpConfiguration.DmiProperties, HttpClientConfiguration]) +@ContextConfiguration(classes = [ HttpClientConfiguration]) class NcmpConfigurationSpec extends Specification{ @Autowired - NcmpConfiguration.DmiProperties dmiProperties - - @Autowired HttpClientConfiguration httpClientConfiguration - + def mockRestTemplateBuilder = new RestTemplateBuilder() def 'NcmpConfiguration Construction.'() { @@ -47,12 +44,6 @@ class NcmpConfigurationSpec extends Specification{ new NcmpConfiguration() != null } - def 'DMI Properties.'() { - expect: 'properties are set to values in test configuration yaml file' - dmiProperties.authUsername == 'some-user' - dmiProperties.authPassword == 'some-password' - } - def 'Rest Template creation with CloseableHttpClient and MappingJackson2HttpMessageConverter.'() { when: 'a rest template is created' def result = NcmpConfiguration.restTemplate(mockRestTemplateBuilder, httpClientConfiguration) diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiDataOperationsSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiDataOperationsSpec.groovy index eb6c7a0f48..e2062bc801 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiDataOperationsSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiDataOperationsSpec.groovy @@ -23,7 +23,7 @@ package org.onap.cps.ncmp.api.impl.operations import com.fasterxml.jackson.databind.ObjectMapper import org.onap.cps.events.EventsPublisher -import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration +import org.onap.cps.ncmp.api.impl.config.DmiWebClientConfiguration import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException import org.onap.cps.ncmp.api.impl.utils.DmiServiceUrlBuilder import org.onap.cps.ncmp.api.impl.utils.context.CpsApplicationContext @@ -52,7 +52,7 @@ import static org.onap.cps.ncmp.api.impl.operations.OperationType.READ import static org.onap.cps.ncmp.api.impl.operations.OperationType.UPDATE @SpringBootTest -@ContextConfiguration(classes = [EventsPublisher, CpsApplicationContext, NcmpConfiguration.DmiProperties, DmiDataOperations]) +@ContextConfiguration(classes = [EventsPublisher, CpsApplicationContext, DmiWebClientConfiguration.DmiProperties, DmiDataOperations]) class DmiDataOperationsSpec extends DmiOperationsBaseSpec { @SpringBean diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiModelOperationsSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiModelOperationsSpec.groovy index e99e8a3d02..a2ec9d1f0f 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiModelOperationsSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiModelOperationsSpec.groovy @@ -23,7 +23,7 @@ package org.onap.cps.ncmp.api.impl.operations import com.fasterxml.jackson.core.JsonProcessingException import com.fasterxml.jackson.databind.ObjectMapper -import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration +import org.onap.cps.ncmp.api.impl.config.DmiWebClientConfiguration import org.onap.cps.spi.model.ModuleReference import org.onap.cps.utils.JsonObjectMapper import org.spockframework.spring.SpringBean @@ -37,7 +37,7 @@ import spock.lang.Shared import static org.onap.cps.ncmp.api.impl.operations.OperationType.READ @SpringBootTest -@ContextConfiguration(classes = [NcmpConfiguration.DmiProperties, DmiModelOperations]) +@ContextConfiguration(classes = [DmiWebClientConfiguration.DmiProperties, DmiModelOperations]) class DmiModelOperationsSpec extends DmiOperationsBaseSpec { @Shared diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiOperationsBaseSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiOperationsBaseSpec.groovy index b7af502de8..061878e95a 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiOperationsBaseSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiOperationsBaseSpec.groovy @@ -22,6 +22,7 @@ package org.onap.cps.ncmp.api.impl.operations import com.fasterxml.jackson.databind.ObjectMapper import org.onap.cps.ncmp.api.impl.client.DmiRestClient +import org.onap.cps.ncmp.api.impl.config.DmiWebClientConfiguration import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle import org.onap.cps.ncmp.api.impl.utils.DmiServiceUrlBuilder @@ -50,7 +51,7 @@ abstract class DmiOperationsBaseSpec extends Specification { ObjectMapper spyObjectMapper = Spy() @SpringBean - DmiServiceUrlBuilder dmiServiceUrlBuilder = new DmiServiceUrlBuilder(new NcmpConfiguration.DmiProperties(), mockCpsValidator) + DmiServiceUrlBuilder dmiServiceUrlBuilder = new DmiServiceUrlBuilder(new DmiWebClientConfiguration.DmiProperties(), mockCpsValidator) def yangModelCmHandle = new YangModelCmHandle() def static dmiServiceName = 'some service name' diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/DmiServiceUrlBuilderSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/DmiServiceUrlBuilderSpec.groovy index fbf2c3d78d..54e9f210d1 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/DmiServiceUrlBuilderSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/DmiServiceUrlBuilderSpec.groovy @@ -20,12 +20,13 @@ package org.onap.cps.ncmp.api.impl.utils +import org.onap.cps.ncmp.api.impl.config.DmiWebClientConfiguration + import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_RUNNING import org.onap.cps.ncmp.api.impl.operations.RequiredDmiService import org.onap.cps.spi.utils.CpsValidator import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle -import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle import spock.lang.Specification @@ -34,7 +35,7 @@ class DmiServiceUrlBuilderSpec extends Specification { static YangModelCmHandle yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle('dmiServiceName', 'dmiDataServiceName', 'dmiModuleServiceName', new NcmpServiceCmHandle(cmHandleId: 'some-cm-handle-id'),'my-module-set-tag', 'my-alternate-id', 'my-data-producer-identifier') - NcmpConfiguration.DmiProperties dmiProperties = new NcmpConfiguration.DmiProperties() + DmiWebClientConfiguration.DmiProperties dmiProperties = new DmiWebClientConfiguration.DmiProperties() def mockCpsValidator = Mock(CpsValidator) @@ -85,7 +86,7 @@ class DmiServiceUrlBuilderSpec extends Specification { when: 'a URL is created' def result = objectUnderTest.getDataOperationRequestUrl(batchRequestQueryParams, batchRequestUriVariables) then: 'it is formed correctly' - assert result.toString() == 'some-service/testBase/v1/data?topic=some topic&requestId=some id' + assert result.toString() == 'some-service/testBase/v1/data?topic=some+topic&requestId=some+id' } def 'Populate batch uri variables.'() { |