diff options
author | pwielebs <piotr.wielebski@nokia.com> | 2018-04-03 12:12:43 +0200 |
---|---|---|
committer | pwielebs <piotr.wielebski@nokia.com> | 2018-04-09 15:01:40 +0200 |
commit | 9725138e6882182c50cbac79ace5d66774a17619 (patch) | |
tree | 7213ff0ebf74533306a16f85e9d8a4ee99390d96 | |
parent | 6930f1f6540273cbdfe325e5f77dcbb4960fd9f3 (diff) |
Http put added
Change-Id: Id21b799a35b531c30f0c1b5df5c6daac6fd56e92
Issue-ID: DCAEGEN2-407
Signed-off-by: pwielebs <piotr.wielebski@nokia.com>
14 files changed, 407 insertions, 205 deletions
diff --git a/prh-aai-client/src/main/java/services/config/AAIHttpClientConfiguration.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIHttpClientConfiguration.java index d5a9281c..9f93f896 100644 --- a/prh-aai-client/src/main/java/services/config/AAIHttpClientConfiguration.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIHttpClientConfiguration.java @@ -17,14 +17,15 @@ * limitations under the License. * ============LICENSE_END========================================================= */ +package org.onap.dcaegen2.services.config; -package services.config; - -import java.io.Serializable; import org.immutables.value.Value; import org.springframework.stereotype.Component; +import java.io.Serializable; + + @Component @Value.Immutable(prehash = true) @Value.Style(stagedBuilder = true) diff --git a/prh-aai-client/src/main/java/services/service/AAIExtendedHttpClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClient.java index 41cf87e5..24149e6d 100644 --- a/prh-aai-client/src/main/java/services/service/AAIExtendedHttpClient.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClient.java @@ -17,11 +17,14 @@ * limitations under the License. * ============LICENSE_END========================================================= */ +package org.onap.dcaegen2.services.service; -package services.service; +import org.onap.dcaegen2.services.utils.HttpRequestDetails; -import java.util.Map; +import java.util.Optional; + +@FunctionalInterface public interface AAIExtendedHttpClient { - String getExtendedDetails(String aaiAPIPath, Map<String, String> queryParams, Map<String, String> headers); + Optional<String> getHttpResponse(HttpRequestDetails httpRequestDetails); } diff --git a/prh-aai-client/src/main/java/services/service/AAIExtendedHttpClientImpl.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java index 7a5a9f8b..133a537e 100644 --- a/prh-aai-client/src/main/java/services/service/AAIExtendedHttpClientImpl.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java @@ -17,30 +17,37 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package services.service; +package org.onap.dcaegen2.services.service; -import java.util.Optional; 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.HttpPut; +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.AAIHttpClientConfiguration; +import org.onap.dcaegen2.services.utils.HttpRequestDetails; +import org.onap.dcaegen2.services.utils.HttpUtils; +import org.onap.dcaegen2.services.utils.RequestVerbs; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import services.config.AAIHttpClientConfiguration; -import services.utils.HttpUtils; - import javax.annotation.Nonnull; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.Iterator; import java.util.Map; +import java.util.Optional; public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { + Logger logger = LoggerFactory.getLogger(AAIExtendedHttpClientImpl.class); + private final CloseableHttpClient closeableHttpClient; private final String aaiHost; private final String aaiProtocol; @@ -56,53 +63,52 @@ public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { } @Override - public String getExtendedDetails(final String aaiAPIPath, final Map<String, String> queryParams, - final Map<String, String> headers) { - final URI extendedURI = - createAAIExtendedURI(aaiProtocol, aaiHost, aaiHostPortNumber, aaiAPIPath, queryParams); + public Optional<String> getHttpResponse(HttpRequestDetails httpRequestDetails) { - if (extendedURI == null) { - return null; - } + Optional<String> extendedDetails = Optional.empty(); - final HttpGet getRequest = new HttpGet(extendedURI); + final URI extendedURI = createAAIExtendedURI(httpRequestDetails.aaiAPIPath(), + httpRequestDetails.queryParameters()); + final HttpRequestBase request = createHttpRequest(extendedURI, httpRequestDetails); - for (Map.Entry<String, String> headersEntry : headers.entrySet()) { - getRequest.addHeader(headersEntry.getKey(), headersEntry.getValue()); + if (request == null) { + return Optional.empty(); } - Optional<String> extendedDetails = Optional.empty(); + for (Map.Entry<String, String> headersEntry : httpRequestDetails.headers().entrySet()) { + request.addHeader(headersEntry.getKey(), headersEntry.getValue()); + } try { - extendedDetails = closeableHttpClient.execute(getRequest, aaiResponseHandler()); - } catch (IOException ex) { - //ToDo loging + extendedDetails = closeableHttpClient.execute(request, aaiResponseHandler()); + } catch (IOException e) { + logger.error("Exception while executing HTTP request: {}", e); } - // return response if (extendedDetails.isPresent()) { - return extendedDetails.get(); + return extendedDetails; } else { - return null; + return Optional.empty(); } } - private URI createAAIExtendedURI(final String protocol, final String hostName, final Integer portNumber, - final String path, Map<String, String> queryParams) { - final URIBuilder uriBuilder = new URIBuilder().setScheme(protocol).setHost(hostName).setPort(portNumber) - .setPath(path); + private URI createAAIExtendedURI(final String path, Map<String, String> queryParams) { + URI extendedURI = null; + final URIBuilder uriBuilder = new URIBuilder().setScheme(this.aaiProtocol).setHost(this.aaiHost) + .setPort(this.aaiHostPortNumber) + .setPath(path); final String customQuery = createCustomQuery(queryParams); + if (StringUtils.isNoneBlank(customQuery)) { uriBuilder.setCustomQuery(customQuery); } - URI extendedURI = null; - try { + logger.info("Building extended URI"); extendedURI = uriBuilder.build(); } catch (URISyntaxException e) { - // ToDo loging + logger.error("Exception while building extended URI: {}", e); } return extendedURI; @@ -111,15 +117,15 @@ public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { private String createCustomQuery(@Nonnull final Map<String, String> queryParams) { final StringBuilder queryStringBuilder = new StringBuilder(""); final Iterator<Map.Entry<String, String>> queryParamIterator = queryParams.entrySet().iterator(); + while (queryParamIterator.hasNext()) { final Map.Entry<String, String> queryParamsEntry = queryParamIterator.next(); - queryStringBuilder.append(queryParamsEntry.getKey()); - queryStringBuilder.append("="); - queryStringBuilder.append(queryParamsEntry.getValue()); + queryStringBuilder.append(queryParamsEntry.getKey()).append("=").append(queryParamsEntry.getValue()); if (queryParamIterator.hasNext()) { queryStringBuilder.append("&"); } } + return queryStringBuilder.toString(); } @@ -128,15 +134,29 @@ public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { final int responseCode = httpResponse.getStatusLine().getStatusCode(); final HttpEntity responseEntity = httpResponse.getEntity(); - if (HttpUtils.isSuccessfulResponseCode(responseCode) && null != responseEntity) { + if (HttpUtils.isSuccessfulResponseCode(responseCode) && responseEntity != null) { + logger.info("HTTP response successful."); final String aaiResponse = EntityUtils.toString(responseEntity); return Optional.of(aaiResponse); } else { String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; - //ToDo loging + logger.error("HTTP response not successful : {}", aaiResponse); return Optional.empty(); } }; } + private HttpRequestBase createHttpRequest(URI extendedURI, HttpRequestDetails httpRequestDetails) { + if (isExtendedURINotNull(extendedURI) && (httpRequestDetails.requestVerb().equals(RequestVerbs.GET))) { + return new HttpGet(extendedURI); + } else if (isExtendedURINotNull(extendedURI) && (httpRequestDetails.requestVerb().equals(RequestVerbs.PUT))) { + return new HttpPut(extendedURI); + } else { + return null; + } + } + + private Boolean isExtendedURINotNull(URI extendedURI) { + return extendedURI != null ? true : false; + } } diff --git a/prh-aai-client/src/main/java/services/service/AAIHttpClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClient.java index c1054d7b..c60027c2 100644 --- a/prh-aai-client/src/main/java/services/service/AAIHttpClient.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClient.java @@ -18,10 +18,11 @@ * ============LICENSE_END========================================================= */ -package services.service; +package org.onap.dcaegen2.services.service; import org.apache.http.impl.client.CloseableHttpClient; +@FunctionalInterface public interface AAIHttpClient { CloseableHttpClient getAAIHttpClient(); } diff --git a/prh-aai-client/src/main/java/services/service/AAIHttpClientImpl.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClientImpl.java index 39c91ddc..90b551db 100644 --- a/prh-aai-client/src/main/java/services/service/AAIHttpClientImpl.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClientImpl.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package services.service; +package org.onap.dcaegen2.services.service; import org.apache.http.auth.AuthScope; import org.apache.http.auth.Credentials; @@ -31,11 +31,10 @@ import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContextBuilder; import org.apache.http.ssl.TrustStrategy; +import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import services.config.AAIHttpClientConfiguration; - import java.security.KeyManagementException; import java.security.KeyStoreException; @@ -62,12 +61,13 @@ public class AAIHttpClientImpl implements AAIHttpClient { if (aaiIgnoreSSLCertificateErrors) { try { - SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); - sslContextBuilder.loadTrustMaterial(null, acceptingTrustStrategy); - httpClientBuilder.setSSLContext(sslContextBuilder.build()); + logger.info("Setting SSL Context for AAI HTTP Client"); + httpClientBuilder.setSSLContext(new SSLContextBuilder() + .loadTrustMaterial(null, acceptingTrustStrategy) + .build()); } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e ) { - logger.error("Exception while setting SSL Context for AAI HTTP Client."); + logger.error("Exception while setting SSL Context for AAI HTTP Client: {}", e); } httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); diff --git a/prh-aai-client/src/test/java/services/service/AAIHttpClientImplTest.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java index 242e56d4..896e3068 100644 --- a/prh-aai-client/src/test/java/services/service/AAIHttpClientImplTest.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java @@ -17,40 +17,29 @@ * limitations under the License. * ============LICENSE_END========================================================= */ +package org.onap.dcaegen2.services.utils; -package services.service; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import services.config.AAIHttpClientConfiguration; -import services.service.utils.HTTPConfiguration; +import org.immutables.value.Value; -import static org.junit.Assert.assertNotNull; +import java.io.Serializable; +import java.util.Map; +@Value.Immutable(prehash = true) +@Value.Style(stagedBuilder = true) +public abstract class HttpRequestDetails implements Serializable { -public class AAIHttpClientImplTest { + private static final long serialVersionUID = 1L; - private AAIHttpClientImpl aaiHttpClientImpl; - private AAIHttpClientConfiguration aaiHttpClientConfiguration; + @Value.Parameter + public abstract String aaiAPIPath(); + @Value.Parameter + public abstract Map<String,String> queryParameters(); - @Before - public void setup() { - aaiHttpClientConfiguration = new HTTPConfiguration(); - aaiHttpClientImpl = new AAIHttpClientImpl(aaiHttpClientConfiguration); - } - - @After - public void teaDown() { - aaiHttpClientImpl = null; - } - - @Test - public void getAAIHttpClientObject_shouldNotBeNull() { - aaiHttpClientImpl.getAAIHttpClient(); - assertNotNull(aaiHttpClientImpl.getAAIHttpClient()); - } + @Value.Parameter + public abstract Map<String,String> headers(); + @Value.Parameter + public abstract RequestVerbs requestVerb(); } - diff --git a/prh-aai-client/src/main/java/services/utils/HttpUtils.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpUtils.java index d4d5c880..eac7f83a 100644 --- a/prh-aai-client/src/main/java/services/utils/HttpUtils.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpUtils.java @@ -17,16 +17,16 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package services.utils; +package org.onap.dcaegen2.services.utils; import org.apache.http.HttpStatus; -public class HttpUtils implements HttpStatus { - - private HttpUtils() {} +public final class HttpUtils implements HttpStatus { public static final String JSON_APPLICATION_TYPE = "application/json"; + private HttpUtils() {} + public static boolean isSuccessfulResponseCode(Integer statusCode) { return statusCode >= 200 && statusCode < 300; } diff --git a/prh-aai-client/src/test/java/services/service/utils/HTTPConfiguration.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/RequestVerbs.java index 82f656d9..f350aa48 100644 --- a/prh-aai-client/src/test/java/services/service/utils/HTTPConfiguration.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/RequestVerbs.java @@ -18,45 +18,11 @@ * ============LICENSE_END========================================================= */ -package services.service.utils; +package org.onap.dcaegen2.services.utils; -import services.config.AAIHttpClientConfiguration; +public enum RequestVerbs { + GET, + PUT; -public class HTTPConfiguration extends AAIHttpClientConfiguration { - - private static final String AAI_HOST_NAME = "1.2.3.4"; - private static final Integer AAI_HOST_PORT_NUMBER = 1234; - private static final String AAI_HOST_PROTOCOL = "https"; - private static final String AAI_USER_NAME = "PRH"; - private static final String AAI_USER_PASS = "PRH"; - - @Override - public String aaiHost() { - return AAI_HOST_NAME; - } - - @Override - public Integer aaiHostPortNumber() { - return AAI_HOST_PORT_NUMBER; - } - - @Override - public String aaiProtocol() { - return AAI_HOST_PROTOCOL; - } - - @Override - public String aaiUserName() { - return AAI_USER_NAME; - } - - @Override - public String aaiUserPassword() { - return AAI_USER_PASS; - } - - @Override - public Boolean aaiIgnoreSSLCertificateErrors() { - return true; - } -}
\ No newline at end of file + 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 new file mode 100644 index 00000000..e114bbfd --- /dev/null +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImplTest.java @@ -0,0 +1,131 @@ +/*- + * ============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 static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.HashMap; +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.HttpPut; +import org.apache.http.impl.client.CloseableHttpClient; +import org.junit.jupiter.api.*; +import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; +import org.onap.dcaegen2.services.utils.HttpRequestDetails; +import org.onap.dcaegen2.services.utils.RequestVerbs; + +public class AAIExtendedHttpClientImplTest { + + private static AAIExtendedHttpClientImpl testedObject; + private static AAIHttpClientConfiguration aaiHttpClientConfigurationMock = mock(AAIHttpClientConfiguration.class); + private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class); + private static HttpRequestDetails httpRequestDetailsMock = mock(HttpRequestDetails.class); + private static Optional<String> expectedResult = Optional.empty(); + + @BeforeAll + public static void init() throws NoSuchFieldException, IllegalAccessException { + + Map<String, String> queryParams = new HashMap<>(); + queryParams.put("ipaddress-v4-oam", "11.22.33.44"); + + 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"); + + when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("54.45.33.2"); + when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https"); + when(aaiHttpClientConfigurationMock.aaiHostPortNumber()).thenReturn(1234); + 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.queryParameters()).thenReturn(queryParams); + + testedObject = new AAIExtendedHttpClientImpl(aaiHttpClientConfigurationMock); + setField(); + } + + @AfterAll + public static void teardown() { + testedObject = null; + aaiHttpClientConfigurationMock = null; + closeableHttpClientMock = null; + httpRequestDetailsMock = null; + expectedResult = null; + } + + @Test + public void getHttpResponseGet_success() throws IOException { + when(httpRequestDetailsMock.requestVerb()).thenReturn(RequestVerbs.GET); + + expectedResult = Optional.of("getExtendedDetailsOK"); + + when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))). + thenReturn(expectedResult); + Optional<String> actualResult = testedObject.getHttpResponse(httpRequestDetailsMock); + + Assertions.assertEquals(expectedResult.get(), actualResult.get()); + } + + @Test + public void getHttpResponsePut_success() throws IOException { + when(httpRequestDetailsMock.requestVerb()).thenReturn(RequestVerbs.PUT); + + expectedResult = Optional.of("getExtendedDetailsOK"); + + when(closeableHttpClientMock.execute(any(HttpPut.class), any(ResponseHandler.class))). + thenReturn(expectedResult); + Optional<String> actualResult = testedObject.getHttpResponse(httpRequestDetailsMock); + + Assertions.assertEquals(expectedResult.get(), actualResult.get()); + } + + @Test + public void getExtendedDetails_returnsNull() throws IOException { + when(httpRequestDetailsMock.requestVerb()).thenReturn(RequestVerbs.GET); + when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))). + thenReturn(Optional.empty()); + Optional<String> actualResult = testedObject.getHttpResponse(httpRequestDetailsMock); + Assertions.assertEquals(Optional.empty(),actualResult); + } + + @Test + public void getHttpResponsePut_failure() { + when(httpRequestDetailsMock.requestVerb()).thenReturn(RequestVerbs.PUT); + + } + + private static void setField() throws NoSuchFieldException, IllegalAccessException { + Field field = testedObject.getClass().getDeclaredField("closeableHttpClient"); + field.setAccessible(true); + field.set(testedObject, closeableHttpClientMock); + } +} 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 new file mode 100644 index 00000000..d26e6afa --- /dev/null +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIHttpClientImplTest.java @@ -0,0 +1,57 @@ +/*- + * ============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.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; + +import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + + +public class AAIHttpClientImplTest { + + private static AAIHttpClientImpl testedObject; + private static AAIHttpClientConfiguration aaiHttpClientConfigurationMock; + + + @BeforeAll + public static void setup() { + aaiHttpClientConfigurationMock = mock(AAIHttpClientConfiguration.class); + when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("54.45.33.2"); + when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https"); + when(aaiHttpClientConfigurationMock.aaiHostPortNumber()).thenReturn(1234); + when(aaiHttpClientConfigurationMock.aaiUserName()).thenReturn("PNF"); + when(aaiHttpClientConfigurationMock.aaiUserPassword()).thenReturn("PNF"); + when(aaiHttpClientConfigurationMock.aaiIgnoreSSLCertificateErrors()).thenReturn(true); + + + testedObject = new AAIHttpClientImpl(aaiHttpClientConfigurationMock); + } + + @Test + public void getAAIHttpClientObject_shouldNotBeNull() { + testedObject.getAAIHttpClient(); + assertNotNull(testedObject.getAAIHttpClient()); + } +} + diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/config/AAIHttpClientConfigurationTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/config/AAIHttpClientConfigurationTest.java new file mode 100644 index 00000000..a759aca5 --- /dev/null +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/config/AAIHttpClientConfigurationTest.java @@ -0,0 +1,58 @@ +/*- + * ============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.config; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; +import org.onap.dcaegen2.services.config.ImmutableAAIHttpClientConfiguration; + +public class AAIHttpClientConfigurationTest { + + private static AAIHttpClientConfiguration client; + private static final String AAI_HOST = "/aai/v11/network/pnfs/pnf/NOKQTFCOC540002E"; + private static final Integer PORT = 1234; + private static final String PROTOCOL = "https"; + private static final String USER_NAME_PASSWORD = "PRH"; + + @BeforeAll + public static void init() { + client = ImmutableAAIHttpClientConfiguration.builder() + .aaiHost(AAI_HOST) + .aaiHostPortNumber(PORT) + .aaiProtocol(PROTOCOL) + .aaiUserName(USER_NAME_PASSWORD) + .aaiUserPassword(USER_NAME_PASSWORD) + .aaiIgnoreSSLCertificateErrors(true) + .build(); + } + + @Test + public void testGetters_success() { + Assertions.assertEquals(AAI_HOST,client.aaiHost()); + Assertions.assertEquals(PORT, client.aaiHostPortNumber()); + Assertions.assertEquals(PROTOCOL,client.aaiProtocol()); + Assertions.assertEquals(USER_NAME_PASSWORD, client.aaiUserName()); + Assertions.assertEquals(USER_NAME_PASSWORD, client.aaiUserPassword()); + Assertions.assertEquals(true, client.aaiIgnoreSSLCertificateErrors()); + } +} 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 new file mode 100644 index 00000000..36105d05 --- /dev/null +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpRequestDetailsTest.java @@ -0,0 +1,60 @@ +/*- + * ============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.utils; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import org.onap.dcaegen2.services.utils.HttpRequestDetails; +import org.onap.dcaegen2.services.utils.ImmutableHttpRequestDetails; +import org.onap.dcaegen2.services.utils.RequestVerbs; + + +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 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"; + + @BeforeAll + public static void init() { + testObject = ImmutableHttpRequestDetails.builder() + .aaiAPIPath(AAI_PATH) + .requestVerb(HTTP_VERB) + .putQueryParameters(QUERY_KEY1,QUERY_VALUE1) + .putHeaders(HEADERS_KEY1,HEADERS_VALUE1) + .build(); + } + + @Test + public void testGetters_success() { + 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()); + } +} diff --git a/prh-aai-client/src/test/java/services/service/utils/HttpUtilsTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpUtilsTest.java index d54b335b..53b31da2 100644 --- a/prh-aai-client/src/test/java/services/service/utils/HttpUtilsTest.java +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpUtilsTest.java @@ -18,11 +18,11 @@ * ============LICENSE_END========================================================= */ -package services.service.utils; +package org.onap.dcaegen2.services.service.utils; import org.apache.http.HttpStatus; import org.junit.Test; -import services.utils.HttpUtils; +import org.onap.dcaegen2.services.utils.HttpUtils; import static junit.framework.TestCase.assertFalse; import static junit.framework.TestCase.assertTrue; diff --git a/prh-aai-client/src/test/java/services/service/AAIExtendedHttpClientImplTest.java b/prh-aai-client/src/test/java/services/service/AAIExtendedHttpClientImplTest.java deleted file mode 100644 index c46e034c..00000000 --- a/prh-aai-client/src/test/java/services/service/AAIExtendedHttpClientImplTest.java +++ /dev/null @@ -1,84 +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 services.service; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.io.IOException; -import java.lang.reflect.Field; -import java.util.HashMap; -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.impl.client.CloseableHttpClient; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import services.config.AAIHttpClientConfiguration; - -public class AAIExtendedHttpClientImplTest { - - private AAIExtendedHttpClientImpl testedObject; - private AAIHttpClientConfiguration aaiHttpClientConfigurationMock = mock(AAIHttpClientConfiguration.class); - private CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class); - - @Before - public void init() throws NoSuchFieldException, IllegalAccessException { - when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("hostTest"); - when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https"); - when(aaiHttpClientConfigurationMock.aaiHostPortNumber()).thenReturn(1234); - testedObject = new AAIExtendedHttpClientImpl(aaiHttpClientConfigurationMock); - setField(); - } - - @Test - public void getExtendedDetails_success() throws IOException { - String expectedResult = "getExtendedDetailsOK"; - Map<String, String> queryParams = new HashMap<>(); - queryParams.put("key1", "value1"); - Map<String, String> headers = new HashMap<>(); - headers.put("headerKey", "headerValue"); - - when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))). - thenReturn(Optional.of(expectedResult)); - String actualResult = testedObject.getExtendedDetails("testPath", queryParams, headers); - Assert.assertEquals(expectedResult, actualResult); - } - - @Test - public void getExtendedDetails_returnsNull() throws IOException { - when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))). - thenReturn(Optional.empty()); - String actualResult = testedObject.getExtendedDetails("testPath", new HashMap<>(), new HashMap<>()); - Assert.assertNull(actualResult); - } - - private void setField() throws NoSuchFieldException, IllegalAccessException { - Field field = testedObject.getClass().getDeclaredField("closeableHttpClient"); - field.setAccessible(true); - field.set(testedObject, closeableHttpClientMock); - } - - -} |