<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>
<artifactId>gson</artifactId>
<version>${immutables.version}</version>
</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>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>
<groupId>org.immutables</groupId>
<artifactId>gson</artifactId>
</dependency>
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpclient</artifactId>
- </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<groupId>org.springframework</groupId>
<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>
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;
* @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) {
return this;
}
- private Mono<ClientResponse> patchAaiRequest(ConsumerDmaapModel dmaapModel) throws URISyntaxException {
+ private Mono<ClientResponse> patchAaiRequest(ConsumerDmaapModel dmaapModel) {
return
webClient.patch()
.uri(getUri(dmaapModel.getSourceName()))
.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();
}
}
}).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"));
}
<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>
<groupId>org.immutables</groupId>
<artifactId>gson</artifactId>
</dependency>
- <dependency>
- <groupId>org.bouncycastle</groupId>
- <artifactId>bcprov-jdk15on</artifactId>
- </dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<artifactId>junit-platform-launcher</artifactId>
<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>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.assertj</groupId>
+ <artifactId>assertj-core</artifactId>
+ </dependency>
<dependency>
<groupId>io.springfox</groupId>
}
@Bean
- Map<String, String> mdcContextMap(){
+ Map<String, String> mdcContextMap() {
MDC.put(REQUEST_ID, "SampleRequestID");
MDC.put(INVOCATION_ID, UUID.randomUUID().toString());
return MDC.getCopyOfContextMap();
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;
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());
}
}
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) {
}
}
- 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();
}
}
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;
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";
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
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);
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 {
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
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
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;
@Bean
@Primary
public ScheduledTasks registerSimpleScheduledTask() {
- return spy(new ScheduledTasks(dmaapConsumerTaskImplSpy, dmaapPublisherTaskImplSpy, aaiPublisherTaskImplSpy, mdcContextMap));
+ return spy(new ScheduledTasks(dmaapConsumerTaskImplSpy, dmaapPublisherTaskImplSpy, aaiPublisherTaskImplSpy,
+ mdcContextMap));
}
}
<groupId>org.immutables</groupId>
<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>
<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>
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();
}
}
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()));
}
}
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
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;
/**
* @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() {
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();
}
}
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;
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
*/
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));
+
});
}
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();
}
}
}).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(),
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;
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
*/
}
@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"));
}