aboutsummaryrefslogtreecommitdiffstats
path: root/prh-aai-client
diff options
context:
space:
mode:
Diffstat (limited to 'prh-aai-client')
-rw-r--r--prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClient.java2
-rw-r--r--prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIProducerClient.java51
-rw-r--r--prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/utils/HttpUtils.java31
-rw-r--r--prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIProducerClientTest.java61
-rw-r--r--prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/utils/HttpUtilsTest.java42
5 files changed, 60 insertions, 127 deletions
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 aa4630b5..dfacc6b1 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
@@ -29,7 +29,7 @@ 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.ConsumerDmaapModel;
-import org.onap.dcaegen2.services.prh.utils.HttpUtils;
+import org.onap.dcaegen2.services.prh.model.utils.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 bb4d145e..dce326ec 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,19 +20,14 @@
package org.onap.dcaegen2.services.prh.service;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
-import org.apache.http.client.ResponseHandler;
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.utils.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -53,7 +48,7 @@ public class AAIProducerClient implements AAIExtendedHttpClient {
private final String aaiProtocol;
private final Integer aaiHostPortNumber;
private final String aaiPath;
- private final Map<String, String> aaiHeaders;
+ private final Map<String,String> aaiHeaders;
public AAIProducerClient(AAIClientConfiguration aaiClientConfiguration) {
@@ -67,16 +62,16 @@ public class AAIProducerClient implements AAIExtendedHttpClient {
@Override
- public Optional<Integer> getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws
- URISyntaxException {
- return createRequest(consumerDmaapModel).flatMap(x -> {
+ public Optional<Integer> getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws URISyntaxException {
+ return createRequest(consumerDmaapModel).flatMap(httpRequestBase -> {
try {
- return closeableHttpClient.execute(x, aaiResponseHandler());
+ return closeableHttpClient.execute(httpRequestBase, CommonFunctions::handleResponse);
} catch (IOException e) {
logger.warn(EXCEPTION_MESSAGE, e);
return Optional.empty();
}
});
+
}
private Optional<HttpRequestBase> createRequest(ConsumerDmaapModel consumerDmaapModel) throws URISyntaxException {
@@ -92,38 +87,20 @@ public class AAIProducerClient implements AAIExtendedHttpClient {
.setPath(aaiPath + "/" + pnfName).build();
}
- private ResponseHandler<Optional<Integer>> aaiResponseHandler() {
- return (HttpResponse httpResponse) -> {
- final Integer 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 aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
- logger.warn("HTTP response not successful : {}", aaiResponse);
- return Optional.of(responseCode);
+ 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_MESSAGE, e);
}
- };
- }
-
- 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_MESSAGE, e);
- }
- return Optional.empty();
- });
+ return Optional.empty();
+ });
}
private HttpPatch createHttpPatch(URI extendedURI, String jsonBody) throws UnsupportedEncodingException {
HttpPatch httpPatch = new HttpPatch(extendedURI);
- httpPatch.setEntity(new StringEntity(jsonBody));
+ httpPatch.setEntity( new StringEntity(jsonBody));
aaiHeaders.forEach(httpPatch::addHeader);
httpPatch.addHeader("Content-Type", "application/merge-patch+json");
return httpPatch;
diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/utils/HttpUtils.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/utils/HttpUtils.java
deleted file mode 100644
index 3d5c3abf..00000000
--- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/utils/HttpUtils.java
+++ /dev/null
@@ -1,31 +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.prh.utils;
-
-import org.apache.http.HttpStatus;
-
-public final class HttpUtils implements HttpStatus {
-
- private HttpUtils() {}
-
- public static boolean isSuccessfulResponseCode(Integer statusCode) {
- return statusCode >= 200 && statusCode < 300;
- }
-}
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 ec926294..594df662 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
@@ -23,19 +23,22 @@ package org.onap.dcaegen2.services.prh.service;
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.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -49,18 +52,8 @@ public class AAIProducerClientTest {
private static ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest();
- @Test
- public void getHttpResponse_shouldReturnSuccessStatusCode()
- throws IOException, URISyntaxException, NoSuchFieldException, IllegalAccessException {
-
- //given
- Map<String, String> aaiHeaders = new HashMap<>();
- aaiHeaders.put("X-FromAppId", "prh");
- aaiHeaders.put("X-TransactionId", "vv-temp");
- aaiHeaders.put("Accept", "application/json");
- aaiHeaders.put("Real-Time", "true");
- aaiHeaders.put("Content-Type", "application/merge-patch+json");
-
+ @BeforeAll
+ static void setup() throws NoSuchFieldException, IllegalAccessException {
when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("eucalyptus.es-si-eu-dhn-20.eecloud.nsn-net.net");
when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https");
when(aaiHttpClientConfigurationMock.aaiHostPortNumber()).thenReturn(1234);
@@ -68,16 +61,41 @@ public class AAIProducerClientTest {
when(aaiHttpClientConfigurationMock.aaiUserPassword()).thenReturn("PRH");
when(aaiHttpClientConfigurationMock.aaiBasePath()).thenReturn("/aai/v11");
when(aaiHttpClientConfigurationMock.aaiPnfPath()).thenReturn("/network/pnfs/pnf");
- when(aaiHttpClientConfigurationMock.aaiHeaders()).thenReturn(aaiHeaders);
+ when(aaiHttpClientConfigurationMock.aaiHeaders()).thenReturn(setupHeaders());
testedObject = new AAIProducerClient(aaiHttpClientConfigurationMock);
setField();
+ }
+
+ @Test
+ void getHttpResponse_shouldReturnSuccessStatusCode() throws IOException, URISyntaxException {
+ // when
when(closeableHttpClientMock.execute(any(HttpPatch.class), any(ResponseHandler.class)))
.thenReturn(Optional.of(SUCCESS));
Optional<Integer> actualResult = testedObject.getHttpResponse(consumerDmaapModel);
+ // then
+ assertEquals(SUCCESS, actualResult.get());
+ }
- //then
- Assertions.assertEquals(SUCCESS, actualResult.get());
+ @Test
+ void getHttpResponse_shouldHandleIOException() throws IOException, URISyntaxException {
+ // when
+ when(closeableHttpClientMock.execute(any(HttpPatch.class), any(ResponseHandler.class)))
+ .thenThrow(new IOException("Error occur"));
+
+ testedObject.getHttpResponse(consumerDmaapModel);
+ // then
+ assertNotNull(testedObject.getHttpResponse(consumerDmaapModel));
+ }
+
+ @Test
+ void createHttpRequest_shouldCatchUnsupportedEncodingException() throws URISyntaxException, IOException {
+ // when
+ when(closeableHttpClientMock.execute(any(HttpPatch.class), any(ResponseHandler.class)))
+ .thenThrow(new UnsupportedEncodingException("A new Error"));
+ testedObject.getHttpResponse(consumerDmaapModel);
+ // then
+ assertNotNull(testedObject.getHttpResponse(consumerDmaapModel));
}
private static void setField() throws NoSuchFieldException, IllegalAccessException {
@@ -85,4 +103,15 @@ public class AAIProducerClientTest {
field.setAccessible(true);
field.set(testedObject, closeableHttpClientMock);
}
+
+ private static Map<String,String> setupHeaders() {
+ Map<String, String> aaiHeaders = new HashMap<>();
+ aaiHeaders.put("X-FromAppId", "prh");
+ aaiHeaders.put("X-TransactionId", "vv-temp");
+ aaiHeaders.put("Accept", "application/json");
+ aaiHeaders.put("Real-Time", "true");
+ aaiHeaders.put("Content-Type", "application/merge-patch+json");
+ return aaiHeaders;
+
+ }
}
diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/utils/HttpUtilsTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/utils/HttpUtilsTest.java
deleted file mode 100644
index 87a14799..00000000
--- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/utils/HttpUtilsTest.java
+++ /dev/null
@@ -1,42 +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.prh.service.utils;
-
-import org.apache.http.HttpStatus;
-import org.junit.Test;
-import org.onap.dcaegen2.services.prh.utils.HttpUtils;
-
-import static junit.framework.TestCase.assertFalse;
-import static junit.framework.TestCase.assertTrue;
-
-
-public class HttpUtilsTest {
-
- @Test
- public void isSuccessfulResponseCode_shouldReturnTrue() {
- assertTrue(HttpUtils.isSuccessfulResponseCode(HttpUtils.SC_ACCEPTED));
- }
-
- @Test
- public void isSuccessfulResponseCode_shouldReturnFalse() {
- assertFalse(HttpUtils.isSuccessfulResponseCode(HttpStatus.SC_BAD_GATEWAY));
- }
-} \ No newline at end of file