diff options
author | pwielebs <piotr.wielebski@nokia.com> | 2018-04-12 10:34:26 +0200 |
---|---|---|
committer | pwielebs <piotr.wielebski@nokia.com> | 2018-04-12 14:16:07 +0200 |
commit | ed8fc543198ec2a878f7e4e2966fa66a6c4986a9 (patch) | |
tree | acf6b07744660fb290065a7be884fc755cbe5e02 /prh-aai-client | |
parent | da7aa97bebc796e898b9225d45a256f946d39f82 (diff) |
http patch request added
Change-Id: I9b3e339008d790b253e2b94e4b307b9a86907988
Issue-ID: DCAEGEN2-407
Signed-off-by: pwielebs <piotr.wielebski@nokia.com>
Diffstat (limited to 'prh-aai-client')
6 files changed, 66 insertions, 18 deletions
diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java index 133a537e..048d9fef 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java @@ -23,9 +23,11 @@ import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpEntity; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPatch; import org.apache.http.client.methods.HttpPut; 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.AAIHttpClientConfiguration; @@ -38,6 +40,7 @@ import org.springframework.beans.factory.annotation.Autowired; import javax.annotation.Nonnull; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; import java.util.Iterator; @@ -151,6 +154,9 @@ public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { return new HttpGet(extendedURI); } else if (isExtendedURINotNull(extendedURI) && (httpRequestDetails.requestVerb().equals(RequestVerbs.PUT))) { return new HttpPut(extendedURI); + } else if (isExtendedURINotNull(extendedURI) && + isPatchRequestValid(httpRequestDetails.requestVerb(),httpRequestDetails.jsonBody())) { + return createHttpPatch(extendedURI, httpRequestDetails.jsonBody()); } else { return null; } @@ -159,4 +165,31 @@ public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { private Boolean isExtendedURINotNull(URI extendedURI) { return extendedURI != null ? true : false; } + + private Optional<StringEntity> createStringEntity(Optional<String> jsonBody) { + return Optional.of(parseJson(jsonBody).get()); + } + + private HttpPatch createHttpPatch(URI extendedURI, Optional<String> jsonBody) { + HttpPatch httpPatch = new HttpPatch(extendedURI); + Optional<StringEntity> stringEntity = createStringEntity(jsonBody); + httpPatch.setEntity(stringEntity.get()); + 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.error("Exception while parsing JSON: {}", e); + } + + return stringEntity; + } + + private Boolean isPatchRequestValid(RequestVerbs requestVerb, Optional<String> jsonBody) { + return requestVerb == RequestVerbs.PATCH && jsonBody.isPresent(); + } } diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java index 896e3068..574cb22f 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java @@ -24,6 +24,7 @@ import org.immutables.value.Value; import java.io.Serializable; import java.util.Map; +import java.util.Optional; @Value.Immutable(prehash = true) @Value.Style(stagedBuilder = true) @@ -35,6 +36,9 @@ public abstract class HttpRequestDetails implements Serializable { public abstract String aaiAPIPath(); @Value.Parameter + public abstract Optional<String> jsonBody(); + + @Value.Parameter public abstract Map<String,String> queryParameters(); @Value.Parameter diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/RequestVerbs.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/RequestVerbs.java index f350aa48..9b331de0 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/RequestVerbs.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/RequestVerbs.java @@ -22,7 +22,8 @@ package org.onap.dcaegen2.services.utils; public enum RequestVerbs { GET, - PUT; + PUT, + PATCH; private RequestVerbs() {}; } diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImplTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImplTest.java index e114bbfd..9ff34b8e 100644 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImplTest.java +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImplTest.java @@ -31,8 +31,11 @@ import java.util.Map; import java.util.Optional; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPatch; import org.apache.http.client.methods.HttpPut; import org.apache.http.impl.client.CloseableHttpClient; +import org.immutables.value.internal.$processor$.meta.$GsonMirrors; +import org.junit.Ignore; import org.junit.jupiter.api.*; import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; import org.onap.dcaegen2.services.utils.HttpRequestDetails; @@ -45,19 +48,21 @@ public class AAIExtendedHttpClientImplTest { private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class); private static HttpRequestDetails httpRequestDetailsMock = mock(HttpRequestDetails.class); private static Optional<String> expectedResult = Optional.empty(); + private static final String JSON_MESSAGE = "{ \"ipaddress-v4-oam\": \"11.22.33.44\" }"; + private static final String PNF_ID = "NOKQTFCOC540002E"; @BeforeAll public static void init() throws NoSuchFieldException, IllegalAccessException { Map<String, String> queryParams = new HashMap<>(); - queryParams.put("ipaddress-v4-oam", "11.22.33.44"); + queryParams.put("pnf-id", PNF_ID); - Map<String, String> AAI_HEADERS = new HashMap<>(); - AAI_HEADERS.put("X-FromAppId", "prh"); - AAI_HEADERS.put("X-TransactionId", "vv-temp"); - AAI_HEADERS.put("Accept", "application/json"); - AAI_HEADERS.put("Real-Time", "true"); - AAI_HEADERS.put("Content-Type", "application/json"); + 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/json"); when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("54.45.33.2"); when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https"); @@ -65,9 +70,10 @@ public class AAIExtendedHttpClientImplTest { when(aaiHttpClientConfigurationMock.aaiUserName()).thenReturn("PRH"); when(aaiHttpClientConfigurationMock.aaiUserPassword()).thenReturn("PRH"); - when(httpRequestDetailsMock.aaiAPIPath()).thenReturn("/aai/v11/network/pnfs/pnf/NOKQTFCOC540002E"); - when(httpRequestDetailsMock.headers()).thenReturn(AAI_HEADERS); + when(httpRequestDetailsMock.aaiAPIPath()).thenReturn("/aai/v11/network/pnfs/pnf"); + when(httpRequestDetailsMock.headers()).thenReturn(aaiHeaders); when(httpRequestDetailsMock.queryParameters()).thenReturn(queryParams); + when(httpRequestDetailsMock.jsonBody()).thenReturn(Optional.of(JSON_MESSAGE)); testedObject = new AAIExtendedHttpClientImpl(aaiHttpClientConfigurationMock); setField(); @@ -83,13 +89,13 @@ public class AAIExtendedHttpClientImplTest { } @Test - public void getHttpResponseGet_success() throws IOException { - when(httpRequestDetailsMock.requestVerb()).thenReturn(RequestVerbs.GET); + public void getHttpResponsePatch_success() throws IOException { + when(httpRequestDetailsMock.requestVerb()).thenReturn(RequestVerbs.PATCH); - expectedResult = Optional.of("getExtendedDetailsOK"); + expectedResult = Optional.of(JSON_MESSAGE); - when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))). - thenReturn(expectedResult); + when(closeableHttpClientMock.execute(any(HttpPatch.class), any(ResponseHandler.class))) + .thenReturn(expectedResult); Optional<String> actualResult = testedObject.getHttpResponse(httpRequestDetailsMock); Assertions.assertEquals(expectedResult.get(), actualResult.get()); diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIHttpClientImplTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIHttpClientImplTest.java index d26e6afa..fd13f1e7 100644 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIHttpClientImplTest.java +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIHttpClientImplTest.java @@ -44,7 +44,6 @@ public class AAIHttpClientImplTest { when(aaiHttpClientConfigurationMock.aaiUserPassword()).thenReturn("PNF"); when(aaiHttpClientConfigurationMock.aaiIgnoreSSLCertificateErrors()).thenReturn(true); - testedObject = new AAIHttpClientImpl(aaiHttpClientConfigurationMock); } diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpRequestDetailsTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpRequestDetailsTest.java index 36105d05..563e6921 100644 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpRequestDetailsTest.java +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpRequestDetailsTest.java @@ -28,17 +28,20 @@ import org.onap.dcaegen2.services.utils.HttpRequestDetails; import org.onap.dcaegen2.services.utils.ImmutableHttpRequestDetails; import org.onap.dcaegen2.services.utils.RequestVerbs; +import java.util.Optional; + public class HttpRequestDetailsTest { private static HttpRequestDetails testObject; private static final String AAI_PATH = "aaiPathTest"; - private static final RequestVerbs HTTP_VERB = RequestVerbs.GET; + private static final RequestVerbs HTTP_VERB = RequestVerbs.PATCH; private static final String QUERY_KEY1 = "queryKey1"; private static final String QUERY_VALUE1 = "queryValue1"; private static final String HEADERS_KEY1 = "headersKey1"; private static final String HEADERS_VALUE1 = "headersValue1"; + private static final String JSON_MESSAGE = "{\"dare_to\": \"dream_big\"}"; @BeforeAll public static void init() { @@ -47,6 +50,7 @@ public class HttpRequestDetailsTest { .requestVerb(HTTP_VERB) .putQueryParameters(QUERY_KEY1,QUERY_VALUE1) .putHeaders(HEADERS_KEY1,HEADERS_VALUE1) + .jsonBody(JSON_MESSAGE) .build(); } @@ -55,6 +59,7 @@ public class HttpRequestDetailsTest { Assertions.assertEquals(AAI_PATH, testObject.aaiAPIPath()); Assertions.assertEquals(HEADERS_VALUE1, testObject.headers().get(HEADERS_KEY1)); Assertions.assertEquals(QUERY_VALUE1, testObject.queryParameters().get(QUERY_KEY1)); - Assertions.assertEquals(RequestVerbs.GET, testObject.requestVerb()); + Assertions.assertEquals(RequestVerbs.PATCH, testObject.requestVerb()); + Assertions.assertEquals(Optional.of(JSON_MESSAGE), testObject.jsonBody()); } } |