summaryrefslogtreecommitdiffstats
path: root/prh-dmaap-client
diff options
context:
space:
mode:
Diffstat (limited to 'prh-dmaap-client')
-rw-r--r--prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImpl.java36
-rw-r--r--prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImplTest.java31
2 files changed, 20 insertions, 47 deletions
diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImpl.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImpl.java
index 72e70b98..b93c9c6b 100644
--- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImpl.java
+++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImpl.java
@@ -20,27 +20,24 @@
package org.onap.dcaegen2.services.prh.service.producer;
-import java.io.IOException;
-import java.io.UnsupportedEncodingException;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.Optional;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
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.prh.config.DmaapPublisherConfiguration;
import org.onap.dcaegen2.services.prh.model.CommonFunctions;
import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
import org.onap.dcaegen2.services.prh.service.DmaapHttpClientImpl;
-import org.onap.dcaegen2.services.prh.service.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Optional;
+
public class ExtendedDmaapProducerHttpClientImpl {
private static Logger logger = LoggerFactory.getLogger(ExtendedDmaapProducerHttpClientImpl.class);
@@ -63,7 +60,7 @@ public class ExtendedDmaapProducerHttpClientImpl {
this.dmaapContentType = configuration.dmaapContentType();
}
- public Optional<String> getHttpProducerResponse(ConsumerDmaapModel consumerDmaapModel) {
+ public Optional<Integer> getHttpProducerResponse(ConsumerDmaapModel consumerDmaapModel) {
this.consumerDmaapModel = consumerDmaapModel;
try {
return createRequest()
@@ -74,9 +71,9 @@ public class ExtendedDmaapProducerHttpClientImpl {
return Optional.empty();
}
- private Optional<String> executeHttpClient(HttpRequestBase httpRequestBase) {
+ private Optional<Integer> executeHttpClient(HttpRequestBase httpRequestBase) {
try {
- return closeableHttpClient.execute(httpRequestBase, this::getDmaapProducerResponseHandler);
+ return closeableHttpClient.execute(httpRequestBase, CommonFunctions::handleResponse);
} catch (IOException e) {
logger.warn("Exception while executing HTTP request: ", e);
}
@@ -112,19 +109,4 @@ public class ExtendedDmaapProducerHttpClientImpl {
}
return Optional.empty();
}
-
- private Optional<String> getDmaapProducerResponseHandler(HttpResponse httpResponse) throws IOException {
- final int responseCode = httpResponse.getStatusLine().getStatusCode();
- logger.info("Status code of operation: {}", responseCode);
- final HttpEntity responseEntity = httpResponse.getEntity();
-
- if (HttpUtils.isSuccessfulResponseCode(responseCode)) {
- logger.trace("HTTP response successful.");
- return Optional.of("" + responseCode);
- } else {
- String response = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
- logger.trace("HTTP response not successful : {}", response);
- return Optional.of("" + responseCode);
- }
- }
} \ No newline at end of file
diff --git a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImplTest.java b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImplTest.java
index 3cb84206..aa6810e3 100644
--- a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImplTest.java
+++ b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImplTest.java
@@ -42,18 +42,15 @@ import static org.mockito.Mockito.when;
public class ExtendedDmaapProducerHttpClientImplTest {
private static ExtendedDmaapProducerHttpClientImpl objectUnderTest;
-
private static DmaapPublisherConfiguration configurationMock = mock(DmaapPublisherConfiguration.class);
private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class);
private static ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest();
-
- private static Optional<String> expectedResult = Optional.empty();
- private static final String RESPONSE_SUCCESS = "200";
- private static final String RESPONSE_FAILURE = "404";
+ private static Integer expectedResult;
+ private static final Integer RESPONSE_SUCCESS = 200;
+ private static final Integer RESPONSE_FAILURE = 404;
@BeforeAll
public static void init() throws NoSuchFieldException, IllegalAccessException {
-
when(configurationMock.dmaapHostName()).thenReturn("54.45.33.2");
when(configurationMock.dmaapProtocol()).thenReturn("https");
when(configurationMock.dmaapPortNumber()).thenReturn(1234);
@@ -61,35 +58,29 @@ public class ExtendedDmaapProducerHttpClientImplTest {
when(configurationMock.dmaapUserPassword()).thenReturn("PRH");
when(configurationMock.dmaapContentType()).thenReturn("application/json");
when(configurationMock.dmaapTopicName()).thenReturn("pnfReady");
-
objectUnderTest = new ExtendedDmaapProducerHttpClientImpl(configurationMock);
-
setField();
}
@Test
public void getHttpResponsePost_success() throws IOException {
- expectedResult = Optional.of(RESPONSE_SUCCESS);
-
+ expectedResult = RESPONSE_SUCCESS;
when(closeableHttpClientMock.execute(any(HttpPost.class), any(ResponseHandler.class)))
- .thenReturn(expectedResult);
-
- Optional<String> actualResult = objectUnderTest.getHttpProducerResponse(consumerDmaapModel);
-
- Assertions.assertEquals(expectedResult.get(), actualResult.get());
+ .thenReturn(Optional.of(expectedResult));
+ Optional<Integer> actualResult = objectUnderTest.getHttpProducerResponse(consumerDmaapModel);
+ Assertions.assertEquals(expectedResult, actualResult.get());
}
@Test
public void getExtendedDetails_returnsFailure() throws IOException {
- expectedResult = Optional.of(RESPONSE_FAILURE);
+ expectedResult = RESPONSE_FAILURE;
when(closeableHttpClientMock.execute(any(HttpPost.class), any(ResponseHandler.class)))
- .thenReturn(Optional.empty());
- Optional<String> actualResult = objectUnderTest.getHttpProducerResponse(consumerDmaapModel);
- Assertions.assertEquals(Optional.empty(), actualResult);
+ .thenReturn(Optional.of(expectedResult));
+ Optional<Integer> actualResult = objectUnderTest.getHttpProducerResponse(consumerDmaapModel);
+ Assertions.assertEquals(expectedResult, actualResult.get());
}
-
private static void setField() throws NoSuchFieldException, IllegalAccessException {
Field field = objectUnderTest.getClass().getDeclaredField("closeableHttpClient");
field.setAccessible(true);