diff options
12 files changed, 137 insertions, 138 deletions
@@ -351,12 +351,16 @@ <goal>check</goal> </goals> <configuration> + <excludes> + <exclude>**/Immutable*</exclude> + <exclude>**/GsonAdapters*</exclude> + <exclude>**/*ForUnitTest*</exclude> + </excludes> <rules> <rule> <element>CLASS</element> <limits> <limit> - <counter>LINE</counter> <value>COVEREDRATIO</value> <!--<minimum>0.70</minimum>--> </limit> diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClient.java index 0ada9072..99a4a8b2 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClient.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClient.java @@ -42,7 +42,7 @@ import java.util.Optional; public class AAIConsumerClient { - Logger logger = LoggerFactory.getLogger(AAIConsumerClient.class); + private Logger logger = LoggerFactory.getLogger(AAIConsumerClient.class); private final CloseableHttpClient closeableHttpClient; private final String aaiHost; diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIExtendedHttpClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIExtendedHttpClient.java index cb884aed..14c7e8f3 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIExtendedHttpClient.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIExtendedHttpClient.java @@ -21,11 +21,11 @@ package org.onap.dcaegen2.services.prh.service; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; -import java.io.IOException; +import java.net.URISyntaxException; import java.util.Optional; @FunctionalInterface public interface AAIExtendedHttpClient { - Optional<Integer> getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws IOException; + Optional<Integer> getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws URISyntaxException; } diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIProducerClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIProducerClient.java index 3a07e94d..4f48c1c3 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIProducerClient.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIProducerClient.java @@ -41,11 +41,10 @@ import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; import java.util.Map; -import java.util.Objects; import java.util.Optional; public class AAIProducerClient implements AAIExtendedHttpClient { - Logger logger = LoggerFactory.getLogger(AAIProducerClient.class); + private Logger logger = LoggerFactory.getLogger(AAIProducerClient.class); private final CloseableHttpClient closeableHttpClient; private final String aaiHost; @@ -66,30 +65,34 @@ public class AAIProducerClient implements AAIExtendedHttpClient { @Override - public Optional<Integer> getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws IOException { - Optional<HttpRequestBase> request = createRequest(consumerDmaapModel); + public Optional<Integer> getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws + URISyntaxException { try { - return closeableHttpClient.execute(request.get(), aaiResponseHandler()); - } catch (IOException e) { + return createRequest(consumerDmaapModel).flatMap(x->{ + try { + return closeableHttpClient.execute(x, aaiResponseHandler()); + } catch (IOException e) { + logger.warn("Exception while executing http client: ", e); + return Optional.empty(); + } + }); + } catch (URISyntaxException e ) { logger.warn("Exception while executing http client: ", e); - throw new IOException(); + throw e; } } - private URI createAAIExtendedURI(final String pnfName) { - URI extendedURI = null; - final URIBuilder uriBuilder = new URIBuilder() + private Optional<HttpRequestBase> createRequest(ConsumerDmaapModel consumerDmaapModel) throws URISyntaxException { + final URI extendedURI = createAAIExtendedURI(consumerDmaapModel.getPnfName()); + return createHttpRequest(extendedURI, consumerDmaapModel); + } + + private URI createAAIExtendedURI(final String pnfName) throws URISyntaxException { + return new URIBuilder() .setScheme(aaiProtocol) .setHost(aaiHost) .setPort(aaiHostPortNumber) - .setPath(aaiPath + "/" + pnfName); - try { - extendedURI = uriBuilder.build(); - logger.trace("Building extended URI: {}", extendedURI); - } catch (URISyntaxException e) { - logger.warn("Exception while building extended URI: ", e); - } - return extendedURI; + .setPath(aaiPath + "/" + pnfName).build(); } private ResponseHandler<Optional<Integer>> aaiResponseHandler() { @@ -109,47 +112,22 @@ public class AAIProducerClient implements AAIExtendedHttpClient { }; } - private HttpRequestBase createHttpRequest(URI extendedURI, ConsumerDmaapModel consumerDmaapModel) { - String jsonBody = CommonFunctions.createJsonBody(consumerDmaapModel); - - if (isExtendedURINotNull(extendedURI) && jsonBody != null && !"".equals(jsonBody)) { - return createHttpPatch(extendedURI, Optional.ofNullable(CommonFunctions.createJsonBody(consumerDmaapModel))); - } else { - return null; - } - } - - private Boolean isExtendedURINotNull(URI extendedURI) { - return extendedURI != null; - } - - - private Optional<StringEntity> createStringEntity(Optional<String> jsonBody) { - return Optional.of(parseJson(jsonBody).get()); + private Optional<HttpRequestBase> createHttpRequest(URI extendedURI, ConsumerDmaapModel consumerDmaapModel) { + return Optional.ofNullable(CommonFunctions.createJsonBody(consumerDmaapModel)).filter(x-> !x.isEmpty()).flatMap(myJson -> { + try { + return Optional.of(createHttpPatch(extendedURI, myJson)); + } catch (UnsupportedEncodingException e) { + logger.warn("Exception while executing http client: ", e); + } + return Optional.empty(); + }); } - private HttpPatch createHttpPatch(URI extendedURI, Optional<String> jsonBody) { + private HttpPatch createHttpPatch(URI extendedURI, String jsonBody) throws UnsupportedEncodingException { HttpPatch httpPatch = new HttpPatch(extendedURI); - Optional<StringEntity> stringEntity = createStringEntity(jsonBody); - httpPatch.setEntity(stringEntity.get()); + httpPatch.setEntity( new StringEntity(jsonBody)); + aaiHeaders.forEach(httpPatch::addHeader); + httpPatch.addHeader("Content-Type", "application/merge-patch+json"); return httpPatch; } - - private Optional<StringEntity> parseJson(Optional<String> jsonBody) { - Optional<StringEntity> stringEntity = Optional.empty(); - try { - stringEntity = Optional.of(new StringEntity(jsonBody.get())); - } catch (UnsupportedEncodingException e) { - logger.warn("Exception while parsing JSON: ", e); - } - return stringEntity; - } - - private Optional<HttpRequestBase> createRequest(ConsumerDmaapModel consumerDmaapModel) { - final URI extendedURI = createAAIExtendedURI(consumerDmaapModel.getPnfName()); - HttpRequestBase request = createHttpRequest(extendedURI, consumerDmaapModel); - aaiHeaders.forEach(Objects.requireNonNull(request)::addHeader); - Objects.requireNonNull(request).addHeader("Content-Type", "application/merge-patch+json"); - return Optional.of(request); - } } diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIProducerClientTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIProducerClientTest.java index dbe857e6..ec926294 100644 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIProducerClientTest.java +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIProducerClientTest.java @@ -24,15 +24,14 @@ import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpPatch; import org.apache.http.impl.client.CloseableHttpClient; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.onap.dcaegen2.services.prh.config.AAIClientConfiguration; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModelForUnitTest; - import java.io.IOException; import java.lang.reflect.Field; +import java.net.URISyntaxException; import java.util.HashMap; import java.util.Map; import java.util.Optional; @@ -50,8 +49,9 @@ public class AAIProducerClientTest { private static ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest(); - @BeforeAll - public static void init() throws NoSuchFieldException, IllegalAccessException { + @Test + public void getHttpResponse_shouldReturnSuccessStatusCode() + throws IOException, URISyntaxException, NoSuchFieldException, IllegalAccessException { //given Map<String, String> aaiHeaders = new HashMap<>(); @@ -61,7 +61,6 @@ public class AAIProducerClientTest { aaiHeaders.put("Real-Time", "true"); aaiHeaders.put("Content-Type", "application/merge-patch+json"); - //when when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("eucalyptus.es-si-eu-dhn-20.eecloud.nsn-net.net"); when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https"); when(aaiHttpClientConfigurationMock.aaiHostPortNumber()).thenReturn(1234); @@ -73,11 +72,6 @@ public class AAIProducerClientTest { testedObject = new AAIProducerClient(aaiHttpClientConfigurationMock); setField(); - } - - @Test - public void getHttpResponsePatch_shouldReturnSuccessStatusCode() throws IOException { - //when when(closeableHttpClientMock.execute(any(HttpPatch.class), any(ResponseHandler.class))) .thenReturn(Optional.of(SUCCESS)); Optional<Integer> actualResult = testedObject.getHttpResponse(consumerDmaapModel); diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java index ece1621d..6d24ade6 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java @@ -84,7 +84,7 @@ public abstract class PrhAppConfig implements Config { JsonParser parser = new JsonParser(); JsonObject jsonObject; try (InputStream inputStream = getInputStream(filepath)) { - JsonElement rootElement = parser.parse(new InputStreamReader(inputStream)); + JsonElement rootElement = getJsonElement(parser, inputStream); if (rootElement.isJsonObject()) { jsonObject = rootElement.getAsJsonObject(); aaiClientConfiguration = deserializeType(gsonBuilder, @@ -99,8 +99,6 @@ public abstract class PrhAppConfig implements Config { jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP).getAsJsonObject(DMAAP_PRODUCER), DmaapPublisherConfiguration.class); } - } catch (FileNotFoundException e) { - logger.warn("File doesn't exist in filepath: {}", filepath, e); } catch (IOException e) { logger.warn("Problem with file loading, file: {}", filepath, e); } catch (JsonSyntaxException e) { @@ -108,12 +106,16 @@ public abstract class PrhAppConfig implements Config { } } + JsonElement getJsonElement(JsonParser parser, InputStream inputStream) { + return parser.parse(new InputStreamReader(inputStream)); + } + private <T> T deserializeType(@NotNull GsonBuilder gsonBuilder, @NotNull JsonObject jsonObject, @NotNull Class<T> type) { return gsonBuilder.create().fromJson(jsonObject, type); } - InputStream getInputStream(@NotNull String filepath) throws FileNotFoundException { + InputStream getInputStream(@NotNull String filepath) throws IOException { return new BufferedInputStream(new FileInputStream(filepath)); } diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImpl.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImpl.java index 7487d084..ba3fade7 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImpl.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImpl.java @@ -19,14 +19,11 @@ */ package org.onap.dcaegen2.services.prh.tasks; -import java.io.IOException; -import java.util.Optional; import org.onap.dcaegen2.services.prh.config.AAIClientConfiguration; -import org.onap.dcaegen2.services.prh.exceptions.DmaapNotFoundException; -import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.prh.configuration.AppConfig; import org.onap.dcaegen2.services.prh.configuration.Config; import org.onap.dcaegen2.services.prh.exceptions.AAINotFoundException; +import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.prh.service.AAIProducerClient; import org.onap.dcaegen2.services.prh.service.HttpUtils; import org.slf4j.Logger; @@ -34,6 +31,9 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.net.URISyntaxException; +import java.util.Optional; + /** * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 4/13/18 */ @@ -58,7 +58,7 @@ public class AAIProducerTaskImpl extends return aaiProducerClient.getHttpResponse(consumerDmaapModel) .filter(HttpUtils::isSuccessfulResponseCode).map(response -> consumerDmaapModel).orElseThrow(() -> new AAINotFoundException("Incorrect response code for continuation of tasks workflow")); - } catch (IOException e) { + } catch ( URISyntaxException e) { logger.warn("Patch request not successful", e); throw new AAINotFoundException("Patch request not successful"); } diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfigTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfigTest.java index 2efffd80..8a1d0ecd 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfigTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfigTest.java @@ -22,12 +22,16 @@ package org.onap.dcaegen2.services.prh.configuration; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; import java.io.ByteArrayInputStream; -import java.io.FileNotFoundException; +import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.Objects; @@ -35,6 +39,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.function.Executable; import org.onap.dcaegen2.services.prh.IT.junit5.mockito.MockitoExtension; /** @@ -74,7 +79,7 @@ class PrhAppConfigTest { @Test public void whenTheConfigurationFits_GetAaiAndDmaapObjectRepresentationConfiguration() - throws FileNotFoundException { + throws IOException { // // Given // @@ -107,7 +112,7 @@ class PrhAppConfigTest { } @Test - public void whenFileIsNotExist_ThrowFileNotFoundException() { + public void whenFileIsNotExist_ThrowIOException() { // // Given // @@ -129,7 +134,7 @@ class PrhAppConfigTest { } @Test - public void whenFileIsExistsButJsonIsIncorrect() throws FileNotFoundException { + public void whenFileIsExistsButJsonIsIncorrect() throws IOException { // // Given // @@ -151,6 +156,31 @@ class PrhAppConfigTest { Assertions.assertNotNull(prhAppConfig.getDmaapConsumerConfiguration()); Assertions.assertNull(prhAppConfig.getDmaapPublisherConfiguration()); + } + + + @Test + public void whenTheConfigurationFits_ButRootElementIsNotAJsonObject() + throws IOException { + // Given + InputStream inputStream = new ByteArrayInputStream((jsonString.getBytes( + StandardCharsets.UTF_8))); + // When + prhAppConfig.setFilepath(filePath); + doReturn(inputStream).when(prhAppConfig).getInputStream(any()); + JsonElement jsonElement = mock(JsonElement.class); + when(jsonElement.isJsonObject()).thenReturn(false); + doReturn(jsonElement).when(prhAppConfig).getJsonElement(any(JsonParser.class), any(InputStream.class)); + prhAppConfig.initFileStreamReader(); + appConfig.dmaapConsumerConfiguration = prhAppConfig.getDmaapConsumerConfiguration(); + appConfig.dmaapPublisherConfiguration = prhAppConfig.getDmaapPublisherConfiguration(); + appConfig.aaiClientConfiguration = prhAppConfig.getAAIClientConfiguration(); + // Then + verify(prhAppConfig, times(1)).setFilepath(anyString()); + verify(prhAppConfig, times(1)).initFileStreamReader(); + Assertions.assertNull(prhAppConfig.getAAIClientConfiguration()); + Assertions.assertNull(prhAppConfig.getDmaapConsumerConfiguration()); + Assertions.assertNull(prhAppConfig.getDmaapPublisherConfiguration()); } }
\ No newline at end of file diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImplTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImplTest.java index c82a98e4..70da39b7 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImplTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImplTest.java @@ -30,6 +30,7 @@ import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; import java.io.IOException; +import java.net.URISyntaxException; import java.util.Optional; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; @@ -95,7 +96,7 @@ class AAIProducerTaskImplTest { } @Test - public void whenPassedObjectFits_ReturnsCorrectStatus() throws AAINotFoundException, IOException { + public void whenPassedObjectFits_ReturnsCorrectStatus() throws AAINotFoundException, URISyntaxException { //given/when getAAIProducerTask_whenMockingResponseObject(200, false); ConsumerDmaapModel response = aaiProducerTask.execute(consumerDmaapModel); @@ -109,7 +110,7 @@ class AAIProducerTaskImplTest { @Test - public void whenPassedObjectFits_butIncorrectResponseReturns() throws IOException { + public void whenPassedObjectFits_butIncorrectResponseReturns() throws IOException, URISyntaxException { //given/when getAAIProducerTask_whenMockingResponseObject(400, false); Executable executableCode = () -> aaiProducerTask.execute(consumerDmaapModel); @@ -121,7 +122,7 @@ class AAIProducerTaskImplTest { } @Test - public void whenPassedObjectFits_butHTTPClientThrowsIOExceptionHandleIt() throws IOException { + public void whenPassedObjectFits_butHTTPClientThrowsIOExceptionHandleIt() throws URISyntaxException { //given/when getAAIProducerTask_whenMockingResponseObject(0, true); @@ -135,10 +136,11 @@ class AAIProducerTaskImplTest { private static void getAAIProducerTask_whenMockingResponseObject(int statusCode, boolean throwsException) - throws IOException { + throws URISyntaxException { + //given aaiProducerClient = mock(AAIProducerClient.class); if (throwsException) { - when(aaiProducerClient.getHttpResponse(consumerDmaapModel)).thenThrow(IOException.class); + when(aaiProducerClient.getHttpResponse(consumerDmaapModel)).thenThrow(URISyntaxException.class); } else { when(aaiProducerClient.getHttpResponse(consumerDmaapModel)).thenReturn(Optional.of(statusCode)); } diff --git a/prh-commons/src/main/java/org/onap/dcaegen2/services/prh/model/ConsumerDmaapModelForUnitTest.java b/prh-commons/src/main/java/org/onap/dcaegen2/services/prh/model/ConsumerDmaapModelForUnitTest.java index c8753177..9e272aae 100644 --- a/prh-commons/src/main/java/org/onap/dcaegen2/services/prh/model/ConsumerDmaapModelForUnitTest.java +++ b/prh-commons/src/main/java/org/onap/dcaegen2/services/prh/model/ConsumerDmaapModelForUnitTest.java @@ -22,21 +22,21 @@ package org.onap.dcaegen2.services.prh.model; public class ConsumerDmaapModelForUnitTest implements ConsumerDmaapModel { - private String pnfName = "NOKnhfsadhff"; - private String ipv4 = "11.22.33.155"; - private String ipv6 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"; + private final String PNF_NAME = "NOKnhfsadhff"; + private final String IPv4 = "256.22.33.155"; + private final String IPv6 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"; public String getPnfName() { - return pnfName; + return PNF_NAME; } public String getIpv4() { - return ipv4; + return IPv4; } public String getIpv6() { - return ipv6; + return IPv6; } } 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 2b9f2a97..6f99f1c7 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 @@ -26,7 +26,7 @@ import org.junit.jupiter.api.Test; public class CommonFunctionsTest { private ConsumerDmaapModel model = new ConsumerDmaapModelForUnitTest(); - private String expectedResult = "{\"pnfName\":\"NOKnhfsadhff\",\"ipv4\":\"11.22.33.155\",\"ipv6\":\"2001:0db8:85a3:0000:0000:8a2e:0370:7334\"}"; + private String expectedResult = "{\"PNF_NAME\":\"NOKnhfsadhff\",\"IPv4\":\"256.22.33.155\",\"IPv6\":\"2001:0db8:85a3:0000:0000:8a2e:0370:7334\"}"; @Test public void createJsonBody_shouldReturnJsonInString() { diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java index 2b46f51b..3bae6989 100644 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java @@ -20,6 +20,10 @@ package org.onap.dcaegen2.services.prh.service.consumer; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Optional; import org.apache.http.HttpEntity; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; @@ -33,11 +37,6 @@ import org.onap.dcaegen2.services.prh.service.HttpUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.Optional; - public class ExtendedDmaapConsumerHttpClientImpl { @@ -66,60 +65,50 @@ public class ExtendedDmaapConsumerHttpClientImpl { public Optional<String> getHttpConsumerResponse() { - Optional<String> extendedDetails = Optional.empty(); - Optional<HttpRequestBase> request = createRequest(); - try { - extendedDetails = closeableHttpClient.execute(request.get(), dmaapConsumerResponseHandler()); - } catch (IOException | NullPointerException e) { - logger.error("Exception while executing HTTP request: {}", e); + return createRequest() + .flatMap(this::executeHttpClient); + } catch (NullPointerException | URISyntaxException e) { + logger.warn("Exception while executing HTTP request: ", e); } - - return extendedDetails; + return Optional.empty(); } - private static HttpRequestBase createHttpRequest(URI extendedURI) { - if (isExtendedURINotNull(extendedURI)) { - return new HttpGet(extendedURI); + private Optional<String> executeHttpClient(HttpRequestBase httpRequestBase) { + try { + return closeableHttpClient.execute(httpRequestBase, getDmaapConsumerResponseHandler()); + } catch (IOException e) { + logger.warn("Exception while executing HTTP request: ", e); } - - return null; + return Optional.empty(); } - private static Boolean isExtendedURINotNull(URI extendedURI) { - return extendedURI != null; + private Optional<HttpRequestBase> createRequest() throws URISyntaxException { + return "application/json".equals(dmaapContentType) + ? createDmaapConsumerExtendedURI().map(this::createHttpRequest) + : Optional.empty(); } - private Optional<HttpRequestBase> createRequest() { - - Optional<HttpRequestBase> request = Optional.empty(); - final URI extendedURI = createDmaapConsumerExtendedURI(); - - if ("application/json".equals(dmaapContentType)) { - request = Optional.ofNullable(createHttpRequest(extendedURI)); - request.get().addHeader("Content-type", dmaapContentType); - } - - return request; + private HttpRequestBase createHttpRequest(URI extendedURI) { + HttpRequestBase httpRequestBase = new HttpGet(extendedURI); + httpRequestBase.addHeader("Content-type", dmaapContentType); + return httpRequestBase; } + private String createRequestPath() { return dmaapTopicName + "/" + consumerGroup + "/" + consumerId; } - private URI createDmaapConsumerExtendedURI() { - try { - return new URIBuilder() - .setScheme(dmaapProtocol) - .setHost(dmaapHostName) - .setPort(dmaapPortNumber) - .setPath(createRequestPath()).build(); - } catch (URISyntaxException e) { - throw new RuntimeException("Exception while building extended URI: {}", e); - } + private Optional<URI> createDmaapConsumerExtendedURI() throws URISyntaxException { + return Optional.ofNullable(new URIBuilder() + .setScheme(dmaapProtocol) + .setHost(dmaapHostName) + .setPort(dmaapPortNumber) + .setPath(createRequestPath()).build()); } - private ResponseHandler<Optional<String>> dmaapConsumerResponseHandler() { + private ResponseHandler<Optional<String>> getDmaapConsumerResponseHandler() { return httpResponse -> { final int responseCode = httpResponse.getStatusLine().getStatusCode(); logger.info("Status code of operation: {}", responseCode); @@ -131,7 +120,7 @@ public class ExtendedDmaapConsumerHttpClientImpl { return Optional.of(dmaapResponse); } else { String dmaapResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; - logger.error("HTTP response not successful : {}", dmaapResponse); + logger.warn("HTTP response not successful : {}", dmaapResponse); return Optional.of(String.valueOf(responseCode)); } }; |