diff options
author | Tony Hansen <tony@att.com> | 2018-09-11 21:08:32 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-09-11 21:08:32 +0000 |
commit | 3cc41a9deb1aa19961670c5bd3a8cbad4d362d3b (patch) | |
tree | 418fc5ff93c6a21e1f154c525a125b637edd218b | |
parent | c055a50794e3933df9910514a58e4ff37ce19607 (diff) | |
parent | 4a1457c84c5f3a68ccdfb3e348996e14ccea89e8 (diff) |
Merge "PRH:security vulnerabilities fix"
18 files changed, 91 insertions, 218 deletions
@@ -51,7 +51,7 @@ <bouncycastle.version>1.59</bouncycastle.version> <spring.version>5.0.5.RELEASE</spring.version> <spring-boot.version>2.0.4.RELEASE</spring-boot.version> - <tomcat.version>8.5.28</tomcat.version> + <tomcat.version>8.5.32</tomcat.version> <slf4j.version>1.7.25</slf4j.version> <junit-jupiter.version>5.1.0</junit-jupiter.version> <junit-vintage.version>5.1.0</junit-vintage.version> @@ -158,25 +158,10 @@ </dependency> <dependency> <groupId>org.bouncycastle</groupId> - <artifactId>bcprov-jdk15on</artifactId> - <version>${bouncycastle.version}</version> - </dependency> - <dependency> - <groupId>org.bouncycastle</groupId> <artifactId>bcpkix-jdk15on</artifactId> <version>${bouncycastle.version}</version> </dependency> <dependency> - <groupId>org.apache.httpcomponents</groupId> - <artifactId>httpclient</artifactId> - <version>4.5.4</version> - </dependency> - <dependency> - <groupId>org.apache.commons</groupId> - <artifactId>commons-lang3</artifactId> - <version>3.6</version> - </dependency> - <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> @@ -278,12 +263,6 @@ <version>2.25.1</version> <scope>test</scope> </dependency> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-test</artifactId> - <version>2.0.1.RELEASE</version> - <scope>test</scope> - </dependency> <dependency> <groupId>io.springfox</groupId> 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 @@ -43,10 +43,6 @@ <artifactId>gson</artifactId> </dependency> <dependency> - <groupId>org.apache.httpcomponents</groupId> - <artifactId>httpclient</artifactId> - </dependency> - <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> @@ -59,10 +55,6 @@ <artifactId>spring-webflux</artifactId> </dependency> <dependency> - <groupId>org.apache.commons</groupId> - <artifactId>commons-lang3</artifactId> - </dependency> - <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-reactor-netty</artifactId> </dependency> 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<ClientResponse> 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<ClientResponse> patchAaiRequest(ConsumerDmaapModel dmaapModel) throws URISyntaxException { + private Mono<ClientResponse> 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 @@ <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> + <exclusions> + <exclusion> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + </exclusion> + </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> @@ -149,10 +155,6 @@ </dependency> <dependency> <groupId>org.bouncycastle</groupId> - <artifactId>bcprov-jdk15on</artifactId> - </dependency> - <dependency> - <groupId>org.bouncycastle</groupId> <artifactId>bcpkix-jdk15on</artifactId> </dependency> <dependency> @@ -222,11 +224,6 @@ <scope>test</scope> </dependency> <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-test</artifactId> - <scope>test</scope> - </dependency> - <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <scope>test</scope> @@ -241,6 +238,10 @@ <artifactId>testng</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + </dependency> <dependency> <groupId>io.springfox</groupId> 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<String, String> mdcContextMap(){ + Map<String, String> 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<String> 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<String> 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<JsonObject> 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<JsonObject> 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<JsonObject> 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<JsonObject> 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<JsonObject> 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 @@ -43,15 +43,6 @@ <artifactId>gson</artifactId> </dependency> <dependency> - <groupId>org.apache.httpcomponents</groupId> - <artifactId>httpclient</artifactId> - </dependency> - <dependency> - <groupId>org.apache.commons</groupId> - <artifactId>commons-lang3</artifactId> - </dependency> - - <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <scope>test</scope> @@ -79,5 +70,10 @@ <groupId>org.slf4j</groupId> <artifactId>log4j-over-slf4j</artifactId> </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-web</artifactId> + <version>5.0.5.RELEASE</version> + </dependency> </dependencies> </project> 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<String> 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<HttpHeaders> 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 <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18 */ @@ -74,12 +75,9 @@ public class DMaaPProducerReactiveHttpClient { public Mono<ResponseEntity<String>> getDMaaPProducerResponse(ConsumerDmaapModel consumerDmaapModelMono) { return Mono.defer(() -> { - try { - HttpEntity<String> 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<String> 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 @@ -97,20 +97,6 @@ class DMaaPConsumerReactiveHttpClientTest { } @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(), URI.create("https://54.45.33.2:1234/unauthenticated.SEC_OTHER_OUTPUT/OpenDCAE-c12/c12")); 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 <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> 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")); } |