From 4a1457c84c5f3a68ccdfb3e348996e14ccea89e8 Mon Sep 17 00:00:00 2001 From: wasala Date: Tue, 11 Sep 2018 09:50:21 +0200 Subject: PRH:security vulnerabilities fix *Removed unused libraries *Fixed vulnerablities in connection with clm scan *Replaced AssertJ in tests verification by using StepVerifier Change-Id: I81c3ac54e5514735f0fca8150fcc218d96dc5ce3 Issue-ID: DCAEGEN2-770 Signed-off-by: wasala --- pom.xml | 23 +------------- prh-aai-client/pom.xml | 8 ----- .../producer/AaiProducerReactiveHttpClient.java | 24 ++++++--------- .../AaiProducerReactiveHttpClientTest.java | 16 +--------- prh-app-server/pom.xml | 19 ++++++------ .../org/onap/dcaegen2/services/prh/MainApp.java | 2 +- .../prh/service/PrhConfigurationProvider.java | 36 ++++++++-------------- .../services/prh/service/HttpGetClientTest.java | 23 +++++--------- .../prh/service/PrhConfigurationProviderTest.java | 21 +++++-------- .../services/prh/tasks/ScheduleControllerSpy.java | 4 ++- prh-commons/pom.xml | 14 +++------ .../services/prh/model/utils/HttpUtils.java | 6 ++-- .../services/prh/model/CommonFunctionsTest.java | 20 +----------- .../services/prh/model/utils/HttpUtilsTest.java | 5 ++- .../consumer/DMaaPConsumerReactiveHttpClient.java | 35 +++++++++------------ .../producer/DMaaPProducerReactiveHttpClient.java | 20 ++++++------ .../DMaaPConsumerReactiveHttpClientTest.java | 14 --------- .../DMaaPProducerReactiveHttpClientTest.java | 19 ++---------- 18 files changed, 91 insertions(+), 218 deletions(-) diff --git a/pom.xml b/pom.xml index 82cdab74..1e773c6b 100644 --- a/pom.xml +++ b/pom.xml @@ -51,7 +51,7 @@ 1.59 5.0.5.RELEASE 2.0.4.RELEASE - 8.5.28 + 8.5.32 1.7.25 5.1.0 5.1.0 @@ -156,26 +156,11 @@ gson ${immutables.version} - - org.bouncycastle - bcprov-jdk15on - ${bouncycastle.version} - org.bouncycastle bcpkix-jdk15on ${bouncycastle.version} - - org.apache.httpcomponents - httpclient - 4.5.4 - - - org.apache.commons - commons-lang3 - 3.6 - org.springframework spring-beans @@ -278,12 +263,6 @@ 2.25.1 test - - org.springframework.boot - spring-boot-starter-test - 2.0.1.RELEASE - test - io.springfox diff --git a/prh-aai-client/pom.xml b/prh-aai-client/pom.xml index 7edf1b6c..d055d522 100644 --- a/prh-aai-client/pom.xml +++ b/prh-aai-client/pom.xml @@ -42,10 +42,6 @@ org.immutables gson - - org.apache.httpcomponents - httpclient - org.springframework spring-beans @@ -58,10 +54,6 @@ org.springframework spring-webflux - - org.apache.commons - commons-lang3 - org.springframework.boot spring-boot-starter-reactor-netty diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClient.java index 4e0758c1..9e282cde 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClient.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClient.java @@ -26,17 +26,19 @@ import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.X_INVOCA import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.X_ONAP_REQUEST_ID; import java.net.URI; -import java.net.URISyntaxException; import java.util.UUID; -import org.apache.http.client.utils.URIBuilder; + import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.slf4j.MDC; import org.springframework.web.reactive.function.client.ClientResponse; import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.util.DefaultUriBuilderFactory; import reactor.core.publisher.Mono; + + public class AaiProducerReactiveHttpClient { private WebClient webClient; @@ -66,11 +68,7 @@ public class AaiProducerReactiveHttpClient { * @return status code of operation */ public Mono getAaiProducerResponse(ConsumerDmaapModel consumerDmaapModelMono) { - try { - return patchAaiRequest(consumerDmaapModelMono); - } catch (URISyntaxException e) { - return Mono.error(e); - } + return patchAaiRequest(consumerDmaapModelMono); } public AaiProducerReactiveHttpClient createAaiWebClient(WebClient webClient) { @@ -78,7 +76,7 @@ public class AaiProducerReactiveHttpClient { return this; } - private Mono patchAaiRequest(ConsumerDmaapModel dmaapModel) throws URISyntaxException { + private Mono patchAaiRequest(ConsumerDmaapModel dmaapModel) { return webClient.patch() .uri(getUri(dmaapModel.getSourceName())) @@ -88,12 +86,8 @@ public class AaiProducerReactiveHttpClient { .exchange(); } - URI getUri(String pnfName) throws URISyntaxException { - return new URIBuilder() - .setScheme(aaiProtocol) - .setHost(aaiHost) - .setPort(aaiHostPortNumber) - .setPath(aaiBasePath + aaiPnfPath + "/" + pnfName) - .build(); + URI getUri(String pnfName) { + return new DefaultUriBuilderFactory().builder().scheme(aaiProtocol).host(aaiHost).port(aaiHostPortNumber) + .path(aaiBasePath + aaiPnfPath + "/" + pnfName).build(); } } diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClientTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClientTest.java index 4160f356..03f9ec64 100644 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClientTest.java +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClientTest.java @@ -105,23 +105,9 @@ class AaiProducerReactiveHttpClientTest { }).verifyComplete(); } - @Test - void getHttpResponse_whenUriSyntaxExceptionHasBeenThrown() throws URISyntaxException { - ///given - aaiProducerReactiveHttpClient = spy(aaiProducerReactiveHttpClient); - //when - when(webClient.patch()).thenReturn(requestBodyUriSpec); - aaiProducerReactiveHttpClient.createAaiWebClient(webClient); - doThrow(URISyntaxException.class).when(aaiProducerReactiveHttpClient).getUri(any()); - //then - StepVerifier.create( - aaiProducerReactiveHttpClient.getAaiProducerResponse( - dmaapModel - )).expectSubscription().expectError(Exception.class).verify(); - } @Test - void getAppropriateUri_whenPassingCorrectedPathForPnf() throws URISyntaxException { + void getAppropriateUri_whenPassingCorrectedPathForPnf() { Assertions.assertEquals(aaiProducerReactiveHttpClient.getUri("NOKnhfsadhff"), URI.create("https://54.45.33.2:1234/aai/v11/network/pnfs/pnf/NOKnhfsadhff")); } diff --git a/prh-app-server/pom.xml b/prh-app-server/pom.xml index b04c06b9..f663227c 100644 --- a/prh-app-server/pom.xml +++ b/prh-app-server/pom.xml @@ -130,6 +130,12 @@ org.springframework.boot spring-boot-starter-web + + + com.fasterxml.jackson.core + jackson-databind + + org.springframework.boot @@ -147,10 +153,6 @@ org.immutables gson - - org.bouncycastle - bcprov-jdk15on - org.bouncycastle bcpkix-jdk15on @@ -221,11 +223,6 @@ junit-platform-launcher test - - org.springframework.boot - spring-boot-starter-test - test - org.mockito mockito-core @@ -241,6 +238,10 @@ testng test + + org.assertj + assertj-core + io.springfox diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/MainApp.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/MainApp.java index 8b3cdcd9..3967dc0a 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/MainApp.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/MainApp.java @@ -48,7 +48,7 @@ public class MainApp { } @Bean - Map mdcContextMap(){ + Map mdcContextMap() { MDC.put(REQUEST_ID, "SampleRequestID"); MDC.put(INVOCATION_ID, UUID.randomUUID().toString()); return MDC.getCopyOfContextMap(); diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProvider.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProvider.java index c80ecfaf..38b060e9 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProvider.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProvider.java @@ -22,12 +22,11 @@ package org.onap.dcaegen2.services.prh.service; import com.google.gson.JsonArray; import com.google.gson.JsonObject; -import java.net.URISyntaxException; -import org.apache.http.client.utils.URIBuilder; import org.onap.dcaegen2.services.prh.model.EnvProperties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; +import org.springframework.web.util.DefaultUriBuilderFactory; import reactor.core.publisher.Mono; @@ -57,16 +56,12 @@ public class PrhConfigurationProvider { private Mono callConsulForConfigBindingServiceEndpoint(EnvProperties envProperties) { LOGGER.info("Retrieving Config Binding Service endpoint from Consul"); - try { - return httpGetClient.callHttpGet(getConsulUrl(envProperties), JsonArray.class) - .flatMap(jsonArray -> this.createConfigBindingServiceUrl(jsonArray, envProperties.appName())); - } catch (URISyntaxException e) { - LOGGER.warn("Malformed Consul uri", e); - return Mono.error(e); - } + return httpGetClient.callHttpGet(getConsulUrl(envProperties), JsonArray.class) + .flatMap(jsonArray -> this.createConfigBindingServiceUrl(jsonArray, envProperties.appName())); + } - private String getConsulUrl(EnvProperties envProperties) throws URISyntaxException { + private String getConsulUrl(EnvProperties envProperties) { return getUri(envProperties.consulHost(), envProperties.consulPort(), "/v1/catalog/service", envProperties.cbsName()); } @@ -83,13 +78,8 @@ public class PrhConfigurationProvider { } private Mono buildConfigBindingServiceUrl(JsonObject jsonObject, String appName) { - try { - return Mono.just(getUri(jsonObject.get("ServiceAddress").getAsString(), - jsonObject.get("ServicePort").getAsInt(), "/service_component", appName)); - } catch (URISyntaxException e) { - LOGGER.warn("Malformed Config Binding Service uri", e); - return Mono.error(e); - } + return Mono.just(getUri(jsonObject.get("ServiceAddress").getAsString(), + jsonObject.get("ServicePort").getAsInt(), "/service_component", appName)); } private Mono getConfigBindingObject(JsonArray jsonArray) { @@ -105,12 +95,12 @@ public class PrhConfigurationProvider { } } - private String getUri(String host, Integer port, String... paths) throws URISyntaxException { - return new URIBuilder() - .setScheme("http") - .setHost(host) - .setPort(port) - .setPath(String.join("/", paths)) + private String getUri(String host, Integer port, String... paths) { + return new DefaultUriBuilderFactory().builder() + .scheme("http") + .host(host) + .port(port) + .path(String.join("/", paths)) .build().toString(); } } diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/service/HttpGetClientTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/service/HttpGetClientTest.java index 20fbc6bf..ab789a00 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/service/HttpGetClientTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/service/HttpGetClientTest.java @@ -21,8 +21,6 @@ package org.onap.dcaegen2.services.prh.service; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; @@ -34,6 +32,8 @@ import com.google.gson.JsonSyntaxException; import org.junit.jupiter.api.Test; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + class HttpGetClientTest { private static final String SOMEURL = "http://someurl"; @@ -50,12 +50,9 @@ class HttpGetClientTest { HttpGetClient httpGetClient = new HttpGetClient(webClient); when(responseSpec.bodyToMono(String.class)).thenReturn(Mono.just(DATA)); - //when - Mono jsonObjectMono = httpGetClient.callHttpGet(SOMEURL, JsonObject.class); - - //then - assertThat(jsonObjectMono).isNotNull(); - assertThat(jsonObjectMono.block()).isEqualTo(gson.fromJson(DATA, JsonObject.class)); + //when/then + StepVerifier.create(httpGetClient.callHttpGet(SOMEURL, JsonObject.class)).expectSubscription() + .expectNext(gson.fromJson(DATA, JsonObject.class)).verifyComplete(); } @Test @@ -65,16 +62,12 @@ class HttpGetClientTest { HttpGetClient httpGetClient = new HttpGetClient(webClient); when(responseSpec.bodyToMono(String.class)).thenReturn(Mono.just("some wrong data")); - //when - Mono jsonObjectMono = httpGetClient.callHttpGet(SOMEURL, JsonObject.class); - - //then - assertThat(jsonObjectMono).isNotNull(); - assertThrows(JsonSyntaxException.class, jsonObjectMono::block); + //when/then + StepVerifier.create(httpGetClient.callHttpGet(SOMEURL, JsonObject.class)).expectSubscription() + .expectError(JsonSyntaxException.class).verify(); } - private void mockWebClientDependantObject() { doReturn(requestBodyUriSpec).when(webClient).get(); when(requestBodyUriSpec.uri(SOMEURL)).thenReturn(requestBodyUriSpec); diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProviderTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProviderTest.java index 7b305222..e99389f5 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProviderTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProviderTest.java @@ -20,18 +20,17 @@ package org.onap.dcaegen2.services.prh.service; -import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.onap.dcaegen2.services.prh.model.EnvProperties; import org.onap.dcaegen2.services.prh.model.ImmutableEnvProperties; import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; class PrhConfigurationProviderTest { @@ -72,12 +71,9 @@ class PrhConfigurationProviderTest { PrhConfigurationProvider provider = new PrhConfigurationProvider(webClient); - // when - Mono jsonObjectMono = provider.callForPrhConfiguration(envProperties); - - // then - assertThat(jsonObjectMono).isNotNull(); - assertThat(jsonObjectMono.block()).isEqualTo(prhMockConfigurationJson); + //when/then + StepVerifier.create(provider.callForPrhConfiguration(envProperties)).expectSubscription() + .expectNext(prhMockConfigurationJson).verifyComplete(); } @Test @@ -90,11 +86,8 @@ class PrhConfigurationProviderTest { PrhConfigurationProvider provider = new PrhConfigurationProvider(webClient); - // when - Mono jsonObjectMono = provider.callForPrhConfiguration(envProperties); - - // then - assertThat(jsonObjectMono).isNotNull(); - Assertions.assertThrows(IllegalStateException.class, jsonObjectMono::block); + //when/then + StepVerifier.create(provider.callForPrhConfiguration(envProperties)).expectSubscription() + .expectError(IllegalStateException.class).verify(); } } \ No newline at end of file diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduleControllerSpy.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduleControllerSpy.java index 5aa63e00..2f7ff61c 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduleControllerSpy.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduleControllerSpy.java @@ -23,6 +23,7 @@ package org.onap.dcaegen2.services.prh.tasks; import static org.mockito.Mockito.spy; import java.util.Map; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -49,6 +50,7 @@ public class ScheduleControllerSpy { @Bean @Primary public ScheduledTasks registerSimpleScheduledTask() { - return spy(new ScheduledTasks(dmaapConsumerTaskImplSpy, dmaapPublisherTaskImplSpy, aaiPublisherTaskImplSpy, mdcContextMap)); + return spy(new ScheduledTasks(dmaapConsumerTaskImplSpy, dmaapPublisherTaskImplSpy, aaiPublisherTaskImplSpy, + mdcContextMap)); } } diff --git a/prh-commons/pom.xml b/prh-commons/pom.xml index 94cd2459..231e4024 100644 --- a/prh-commons/pom.xml +++ b/prh-commons/pom.xml @@ -42,15 +42,6 @@ org.immutables gson - - org.apache.httpcomponents - httpclient - - - org.apache.commons - commons-lang3 - - org.junit.jupiter junit-jupiter-api @@ -79,5 +70,10 @@ org.slf4j log4j-over-slf4j + + org.springframework + spring-web + 5.0.5.RELEASE + diff --git a/prh-commons/src/main/java/org/onap/dcaegen2/services/prh/model/utils/HttpUtils.java b/prh-commons/src/main/java/org/onap/dcaegen2/services/prh/model/utils/HttpUtils.java index e0264eb3..89d9c4fd 100644 --- a/prh-commons/src/main/java/org/onap/dcaegen2/services/prh/model/utils/HttpUtils.java +++ b/prh-commons/src/main/java/org/onap/dcaegen2/services/prh/model/utils/HttpUtils.java @@ -20,14 +20,14 @@ package org.onap.dcaegen2.services.prh.model.utils; -import org.apache.http.HttpStatus; +import org.springframework.http.HttpStatus; -public final class HttpUtils implements HttpStatus { +public final class HttpUtils { private HttpUtils() { } public static boolean isSuccessfulResponseCode(Integer statusCode) { - return statusCode >= 200 && statusCode < 300; + return statusCode >= HttpStatus.OK.value() && statusCode < HttpStatus.MULTIPLE_CHOICES.value(); } } diff --git a/prh-commons/src/test/java/org/onap/dcaegen2/services/prh/model/CommonFunctionsTest.java b/prh-commons/src/test/java/org/onap/dcaegen2/services/prh/model/CommonFunctionsTest.java index b3fc87a1..90b32a09 100644 --- a/prh-commons/src/test/java/org/onap/dcaegen2/services/prh/model/CommonFunctionsTest.java +++ b/prh-commons/src/test/java/org/onap/dcaegen2/services/prh/model/CommonFunctionsTest.java @@ -21,33 +21,15 @@ package org.onap.dcaegen2.services.prh.model; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.StatusLine; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; class CommonFunctionsTest { - private static final HttpResponse httpResponseMock = mock(HttpResponse.class); - private static final HttpEntity httpEntityMock = mock(HttpEntity.class); - private static final StatusLine statusLineMock = mock(StatusLine.class); - // Given - private ConsumerDmaapModel model = new ConsumerDmaapModelForUnitTest(); - - @BeforeAll - static void setup() { - when(httpResponseMock.getEntity()).thenReturn(httpEntityMock); - when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock); - } - @Test void createJsonBody_shouldReturnJsonInString() { String expectedResult = "{\"sourceName\":\"NOKnhfsadhff\",\"ipaddress-v4-oam\":\"256.22.33.155\"" + ",\"ipaddress-v6-oam\":\"2001:0db8:85a3:0000:0000:8a2e:0370:7334\"}"; - assertEquals(expectedResult, CommonFunctions.createJsonBody(model)); + assertEquals(expectedResult, CommonFunctions.createJsonBody(new ConsumerDmaapModelForUnitTest())); } } diff --git a/prh-commons/src/test/java/org/onap/dcaegen2/services/prh/model/utils/HttpUtilsTest.java b/prh-commons/src/test/java/org/onap/dcaegen2/services/prh/model/utils/HttpUtilsTest.java index 334bd1bb..89c72026 100644 --- a/prh-commons/src/test/java/org/onap/dcaegen2/services/prh/model/utils/HttpUtilsTest.java +++ b/prh-commons/src/test/java/org/onap/dcaegen2/services/prh/model/utils/HttpUtilsTest.java @@ -23,18 +23,17 @@ package org.onap.dcaegen2.services.prh.model.utils; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import org.apache.http.HttpStatus; import org.junit.jupiter.api.Test; class HttpUtilsTest { @Test void isSuccessfulResponseCode_shouldReturnTrue() { - assertTrue(HttpUtils.isSuccessfulResponseCode(HttpUtils.SC_ACCEPTED)); + assertTrue(HttpUtils.isSuccessfulResponseCode(202)); } @Test void isSuccessfulResponseCode_shouldReturnFalse() { - assertFalse(HttpUtils.isSuccessfulResponseCode(HttpStatus.SC_BAD_GATEWAY)); + assertFalse(HttpUtils.isSuccessfulResponseCode(502)); } } \ No newline at end of file diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClient.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClient.java index 242214d2..02c6ba96 100644 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClient.java +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClient.java @@ -25,15 +25,14 @@ import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.X_INVOCA import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.X_ONAP_REQUEST_ID; import java.net.URI; -import java.net.URISyntaxException; import java.util.UUID; import java.util.function.Consumer; -import org.apache.http.client.utils.URIBuilder; import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration; import org.slf4j.MDC; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.util.DefaultUriBuilderFactory; import reactor.core.publisher.Mono; /** @@ -71,21 +70,17 @@ public class DMaaPConsumerReactiveHttpClient { * @return reactive response from DMaaP in string format */ public Mono getDMaaPConsumerResponse() { - try { - return webClient - .get() - .uri(getUri()) - .headers(getHeaders()) - .retrieve() - .onStatus(HttpStatus::is4xxClientError, clientResponse -> - Mono.error(new RuntimeException("DmaaPConsumer HTTP " + clientResponse.statusCode())) - ) - .onStatus(HttpStatus::is5xxServerError, clientResponse -> - Mono.error(new RuntimeException("DmaaPConsumer HTTP " + clientResponse.statusCode()))) - .bodyToMono(String.class); - } catch (URISyntaxException e) { - return Mono.error(e); - } + return webClient + .get() + .uri(getUri()) + .headers(getHeaders()) + .retrieve() + .onStatus(HttpStatus::is4xxClientError, clientResponse -> + Mono.error(new RuntimeException("DmaaPConsumer HTTP " + clientResponse.statusCode())) + ) + .onStatus(HttpStatus::is5xxServerError, clientResponse -> + Mono.error(new RuntimeException("DmaaPConsumer HTTP " + clientResponse.statusCode()))) + .bodyToMono(String.class); } private Consumer getHeaders() { @@ -105,8 +100,8 @@ public class DMaaPConsumerReactiveHttpClient { return this; } - URI getUri() throws URISyntaxException { - return new URIBuilder().setScheme(dmaapProtocol).setHost(dmaapHostName).setPort(dmaapPortNumber) - .setPath(createRequestPath()).build(); + URI getUri() { + return new DefaultUriBuilderFactory().builder().scheme(dmaapProtocol).host(dmaapHostName).port(dmaapPortNumber) + .path(createRequestPath()).build(); } } diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPProducerReactiveHttpClient.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPProducerReactiveHttpClient.java index 862ad841..6cd54846 100644 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPProducerReactiveHttpClient.java +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPProducerReactiveHttpClient.java @@ -26,9 +26,7 @@ import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.X_INVOCA import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.X_ONAP_REQUEST_ID; import java.net.URI; -import java.net.URISyntaxException; import java.util.UUID; -import org.apache.http.client.utils.URIBuilder; import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.slf4j.MDC; @@ -37,8 +35,11 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.DefaultUriBuilderFactory; import reactor.core.publisher.Mono; + + /** * @author Przemysław Wąsala on 7/4/18 */ @@ -74,12 +75,9 @@ public class DMaaPProducerReactiveHttpClient { public Mono> getDMaaPProducerResponse(ConsumerDmaapModel consumerDmaapModelMono) { return Mono.defer(() -> { - try { - HttpEntity request = new HttpEntity<>(createJsonBody(consumerDmaapModelMono), getAllHeaders()); - return Mono.just(restTemplate.exchange(getUri(), HttpMethod.POST, request, String.class)); - } catch (URISyntaxException e) { - return Mono.error(e); - } + HttpEntity request = new HttpEntity<>(createJsonBody(consumerDmaapModelMono), getAllHeaders()); + return Mono.just(restTemplate.exchange(getUri(), HttpMethod.POST, request, String.class)); + }); } @@ -97,9 +95,9 @@ public class DMaaPProducerReactiveHttpClient { return this; } - URI getUri() throws URISyntaxException { - return new URIBuilder().setScheme(dmaapProtocol).setHost(dmaapHostName).setPort(dmaapPortNumber) - .setPath(dmaapTopicName).build(); + URI getUri() { + return new DefaultUriBuilderFactory().builder().scheme(dmaapProtocol).host(dmaapHostName).port(dmaapPortNumber) + .path(dmaapTopicName).build(); } } diff --git a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClientTest.java b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClientTest.java index 26fa65f5..c8ffd12f 100644 --- a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClientTest.java +++ b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClientTest.java @@ -96,20 +96,6 @@ class DMaaPConsumerReactiveHttpClientTest { }).verifyComplete(); } - @Test - void getHttpResponse_whenUriSyntaxExceptionHasBeenThrown() throws URISyntaxException { - //given - dmaapConsumerReactiveHttpClient = spy(dmaapConsumerReactiveHttpClient); - //when - when(webClient.get()).thenReturn(requestHeadersSpec); - dmaapConsumerReactiveHttpClient.createDMaaPWebClient(webClient); - when(dmaapConsumerReactiveHttpClient.getUri()).thenThrow(URISyntaxException.class); - - //then - StepVerifier.create(dmaapConsumerReactiveHttpClient.getDMaaPConsumerResponse()).expectSubscription() - .expectError(Exception.class).verify(); - } - @Test void getAppropriateUri_whenPassingCorrectedPathForPnf() throws URISyntaxException { Assertions.assertEquals(dmaapConsumerReactiveHttpClient.getUri(), diff --git a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPProducerReactiveHttpClientTest.java b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPProducerReactiveHttpClientTest.java index 05b74895..29d1039f 100644 --- a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPProducerReactiveHttpClientTest.java +++ b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPProducerReactiveHttpClientTest.java @@ -23,12 +23,9 @@ package org.onap.dcaegen2.services.prh.service.producer; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import java.net.URI; -import java.net.URISyntaxException; - import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -42,6 +39,8 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; import reactor.test.StepVerifier; + + /** * @author Przemysław Wąsala on 7/4/18 */ @@ -86,19 +85,7 @@ class DMaaPProducerReactiveHttpClientTest { } @Test - void getHttpResponse_whenUriSyntaxExceptionHasBeenThrown() throws URISyntaxException { - //given - dmaapProducerReactiveHttpClient = spy(dmaapProducerReactiveHttpClient); - //when - when(dmaapProducerReactiveHttpClient.getUri()).thenThrow(URISyntaxException.class); - - //then - StepVerifier.create(dmaapProducerReactiveHttpClient.getDMaaPProducerResponse(any())).expectSubscription() - .expectError(Exception.class).verify(); - } - - @Test - void getAppropriateUri_whenPassingCorrectedPathForPnf() throws URISyntaxException { + void getAppropriateUri_whenPassingCorrectedPathForPnf() { Assertions.assertEquals(dmaapProducerReactiveHttpClient.getUri(), URI.create("https://54.45.33.2:1234/unauthenticated.PNF_READY")); } -- cgit 1.2.3-korg