diff options
Diffstat (limited to 'prh-aai-client')
2 files changed, 52 insertions, 2 deletions
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 f9c2708e..f85ac8aa 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 @@ -20,14 +20,18 @@ package org.onap.dcaegen2.services.prh.service; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPatch; 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.AAIClientConfiguration; import org.onap.dcaegen2.services.prh.model.CommonFunctions; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; +import org.onap.dcaegen2.services.prh.model.utils.HttpUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -70,7 +74,7 @@ public class AAIProducerClient implements AAIExtendedHttpClient { public Optional<Integer> getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws URISyntaxException { return createRequest(consumerDmaapModel).flatMap(httpRequestBase -> { try { - return closeableHttpClient.execute(httpRequestBase, CommonFunctions::handleResponse); + return closeableHttpClient.execute(httpRequestBase, this::handleResponse); } catch (IOException e) { logger.warn(EXCEPTION_MESSAGE, e); return Optional.empty(); @@ -115,4 +119,20 @@ public class AAIProducerClient implements AAIExtendedHttpClient { return Base64.getEncoder().encodeToString((this.aaiUserName + ":" + this.aaiUserPassword) .getBytes("UTF-8")); } + + Optional<Integer> handleResponse(HttpResponse response) throws IOException { + + final Integer responseCode = response.getStatusLine().getStatusCode(); + logger.info("Status code of operation: {}", responseCode); + final HttpEntity responseEntity = response.getEntity(); + + if (HttpUtils.isSuccessfulResponseCode(responseCode)) { + logger.trace("HTTP response successful."); + return Optional.of(responseCode); + } else { + String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; + logger.warn("HTTP response not successful : {}", aaiResponse); + return Optional.of(responseCode); + } + } } 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 1ef90180..b7515ad5 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 @@ -20,12 +20,17 @@ package org.onap.dcaegen2.services.prh.service; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.StatusLine; 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.BeforeAll; import org.junit.jupiter.api.Test; import org.onap.dcaegen2.services.prh.config.AAIClientConfiguration; +import org.onap.dcaegen2.services.prh.model.CommonFunctions; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModelForUnitTest; @@ -44,7 +49,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -public class AAIProducerClientTest { +class AAIProducerClientTest { private static final Integer SUCCESS = 200; private static AAIProducerClient testedObject; @@ -52,6 +57,11 @@ public class AAIProducerClientTest { private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class); private static ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest(); + private final static HttpResponse httpResponseMock = mock(HttpResponse.class); + private final static HttpEntity httpEntityMock = mock(HttpEntity.class); + private final static StatusLine statusLineMock = mock(StatusLine.class); + + @BeforeAll static void setup() throws NoSuchFieldException, IllegalAccessException { @@ -121,6 +131,26 @@ public class AAIProducerClientTest { assertEquals(expected, patch.getLastHeader("Authorization").toString()); } + @Test + void handleResponse_shouldReturn200() throws IOException { + // When + when(httpResponseMock.getEntity()).thenReturn(httpEntityMock); + when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock); + when(httpResponseMock.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_OK); + // Then + assertEquals(Optional.of(HttpStatus.SC_OK), testedObject.handleResponse(httpResponseMock)); + } + + @Test + void handleResponse_shouldReturn300() throws IOException { + // When + when(httpResponseMock.getEntity()).thenReturn(httpEntityMock); + when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock); + when(httpResponseMock.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_BAD_REQUEST); + // Then + assertEquals(Optional.of(HttpStatus.SC_BAD_REQUEST), testedObject.handleResponse(httpResponseMock)); + } + private static void setField() throws NoSuchFieldException, IllegalAccessException { Field field = testedObject.getClass().getDeclaredField("closeableHttpClient"); |