diff options
Diffstat (limited to 'prh-dmaap-client')
4 files changed, 49 insertions, 59 deletions
diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/CommonMethods.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/CommonMethods.java deleted file mode 100644 index fe792809..00000000 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/CommonMethods.java +++ /dev/null @@ -1,52 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ -package org.onap.dcaegen2.services.service; - -import org.apache.http.HttpEntity; -import org.apache.http.client.ResponseHandler; -import org.apache.http.util.EntityUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Optional; - -public class CommonMethods { - - private static Logger logger = LoggerFactory.getLogger(CommonMethods.class); - - private CommonMethods() {} - - public static ResponseHandler<Optional<String>> dmaapResponseHandler() { - return httpResponse -> { - final int responseCode = httpResponse.getStatusLine().getStatusCode(); - final HttpEntity responseEntity = httpResponse.getEntity(); - - if (HttpUtils.isSuccessfulResponseCode(responseCode) && responseEntity != null) { - logger.info("HTTP response successful."); - final String response = EntityUtils.toString(responseEntity); - return Optional.of(response); - } else { - String response = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; - logger.error("HTTP response not successful : {}", response); - return Optional.empty(); - } - }; - } -} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java index 5acf2042..0b76ccaa 100644 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java @@ -20,13 +20,16 @@ package org.onap.dcaegen2.services.service.consumer; +import org.apache.http.HttpEntity; +import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.util.EntityUtils; import org.onap.dcaegen2.services.config.DmaapConsumerConfiguration; -import org.onap.dcaegen2.services.service.CommonMethods; import org.onap.dcaegen2.services.service.DmaapHttpClientImpl; +import org.onap.dcaegen2.services.service.HttpUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -67,7 +70,7 @@ public class ExtendedDmaapConsumerHttpClientImpl { Optional<HttpRequestBase> request = createRequest(); try { - extendedDetails = closeableHttpClient.execute(request.get(), CommonMethods.dmaapResponseHandler()); + extendedDetails = closeableHttpClient.execute(request.get(), dmaapConsumerResponseHandler()); } catch (IOException | NullPointerException e) { logger.error("Exception while executing HTTP request: {}", e); } @@ -122,6 +125,24 @@ public class ExtendedDmaapConsumerHttpClientImpl { return extendedURI; } + + private ResponseHandler<Optional<String>> dmaapConsumerResponseHandler() { + return httpResponse -> { + final int responseCode = httpResponse.getStatusLine().getStatusCode(); + logger.info("Status code of operation: {}", responseCode); + final HttpEntity responseEntity = httpResponse.getEntity(); + + if (HttpUtils.isSuccessfulResponseCode(responseCode) ) { + logger.info("HTTP response successful."); + final String dmaapResponse = EntityUtils.toString(responseEntity); + return Optional.of(dmaapResponse); + } else { + String dmaapResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; + logger.error("HTTP response not successful : {}", dmaapResponse); + return Optional.of("" + responseCode); + } + }; + } } diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImpl.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImpl.java index 9b6c96ff..17e34ed0 100644 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImpl.java +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImpl.java @@ -20,14 +20,17 @@ package org.onap.dcaegen2.services.service.producer; +import org.apache.http.HttpEntity; +import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.util.EntityUtils; import org.onap.dcaegen2.services.config.DmaapPublisherConfiguration; -import org.onap.dcaegen2.services.service.CommonMethods; import org.onap.dcaegen2.services.service.DmaapHttpClientImpl; +import org.onap.dcaegen2.services.service.HttpUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -64,7 +67,7 @@ public class ExtendedDmaapProducerHttpClientImpl { Optional<HttpRequestBase> request = createRequest(requestDetails); try { - extendedDetails = closeableHttpClient.execute(request.get(), CommonMethods.dmaapResponseHandler()); + extendedDetails = closeableHttpClient.execute(request.get(), dmaapProducerResponseHandler()); } catch (IOException | NullPointerException e) { logger.error("Exception while executing HTTP request: {}", e); } @@ -138,4 +141,20 @@ public class ExtendedDmaapProducerHttpClientImpl { post.setEntity(stringEntity.get()); return post; } + + private ResponseHandler<Optional<String>> dmaapProducerResponseHandler() { + return httpResponse -> { + final int responseCode = httpResponse.getStatusLine().getStatusCode(); + final HttpEntity responseEntity = httpResponse.getEntity(); + + if (HttpUtils.isSuccessfulResponseCode(responseCode)) { + logger.info("HTTP response successful."); + return Optional.of("" + responseCode); + } else { + String response = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; + logger.error("HTTP response not successful : {}", response); + return Optional.of("" + responseCode); + } + }; + } } diff --git a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImplTest.java b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImplTest.java index 67c58f55..0560432b 100644 --- a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImplTest.java +++ b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImplTest.java @@ -47,6 +47,8 @@ public class ExtendedDmaapProducerHttpClientImplTest { private static Optional<String> expectedResult = Optional.empty(); private static final String JSON_MESSAGE = "{ \"ipaddress-v4-oam\": \"11.22.33.44\" }"; + private static final String RESPONSE_SUCCESS = "200"; + private static final String RESPONSE_FAILURE = "404"; @BeforeAll public static void init() throws NoSuchFieldException, IllegalAccessException { @@ -70,7 +72,7 @@ public class ExtendedDmaapProducerHttpClientImplTest { @Test public void getHttpResponsePost_success() throws IOException { - expectedResult = Optional.of(JSON_MESSAGE); + expectedResult = Optional.of(RESPONSE_SUCCESS); when(closeableHttpClientMock.execute(any(HttpPost.class), any(ResponseHandler.class))) .thenReturn(expectedResult); @@ -81,8 +83,8 @@ public class ExtendedDmaapProducerHttpClientImplTest { } @Test - public void getExtendedDetails_returnsNull() throws IOException { - expectedResult = Optional.of(JSON_MESSAGE); + public void getExtendedDetails_returnsFailure() throws IOException { + expectedResult = Optional.of(RESPONSE_FAILURE); when(closeableHttpClientMock.execute(any(HttpPost.class), any(ResponseHandler.class))). thenReturn(Optional.empty()); Optional<String> actualResult = objectUnderTest.getHttpProducerResponse(requestDetailsMock); |