From 9725138e6882182c50cbac79ace5d66774a17619 Mon Sep 17 00:00:00 2001 From: pwielebs Date: Tue, 3 Apr 2018 12:12:43 +0200 Subject: Http put added Change-Id: Id21b799a35b531c30f0c1b5df5c6daac6fd56e92 Issue-ID: DCAEGEN2-407 Signed-off-by: pwielebs --- .../config/AAIHttpClientConfiguration.java | 54 +++++++ .../services/service/AAIExtendedHttpClient.java | 30 ++++ .../service/AAIExtendedHttpClientImpl.java | 162 +++++++++++++++++++++ .../dcaegen2/services/service/AAIHttpClient.java | 29 ++++ .../services/service/AAIHttpClientImpl.java | 93 ++++++++++++ .../services/utils/HttpRequestDetails.java | 45 ++++++ .../onap/dcaegen2/services/utils/HttpUtils.java | 33 +++++ .../onap/dcaegen2/services/utils/RequestVerbs.java | 28 ++++ .../config/AAIHttpClientConfiguration.java | 53 ------- .../services/service/AAIExtendedHttpClient.java | 27 ---- .../service/AAIExtendedHttpClientImpl.java | 142 ------------------ .../main/java/services/service/AAIHttpClient.java | 28 ---- .../java/services/service/AAIHttpClientImpl.java | 93 ------------ .../src/main/java/services/utils/HttpUtils.java | 33 ----- .../service/AAIExtendedHttpClientImplTest.java | 131 +++++++++++++++++ .../services/service/AAIHttpClientImplTest.java | 57 ++++++++ .../config/AAIHttpClientConfigurationTest.java | 58 ++++++++ .../service/utils/HttpRequestDetailsTest.java | 60 ++++++++ .../services/service/utils/HttpUtilsTest.java | 42 ++++++ .../service/AAIExtendedHttpClientImplTest.java | 84 ----------- .../services/service/AAIHttpClientImplTest.java | 56 ------- .../services/service/utils/HTTPConfiguration.java | 62 -------- .../java/services/service/utils/HttpUtilsTest.java | 42 ------ 23 files changed, 822 insertions(+), 620 deletions(-) create mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIHttpClientConfiguration.java create mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClient.java create mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java create mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClient.java create mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClientImpl.java create mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java create mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpUtils.java create mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/RequestVerbs.java delete mode 100644 prh-aai-client/src/main/java/services/config/AAIHttpClientConfiguration.java delete mode 100644 prh-aai-client/src/main/java/services/service/AAIExtendedHttpClient.java delete mode 100644 prh-aai-client/src/main/java/services/service/AAIExtendedHttpClientImpl.java delete mode 100644 prh-aai-client/src/main/java/services/service/AAIHttpClient.java delete mode 100644 prh-aai-client/src/main/java/services/service/AAIHttpClientImpl.java delete mode 100644 prh-aai-client/src/main/java/services/utils/HttpUtils.java create mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImplTest.java create mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIHttpClientImplTest.java create mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/config/AAIHttpClientConfigurationTest.java create mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpRequestDetailsTest.java create mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpUtilsTest.java delete mode 100644 prh-aai-client/src/test/java/services/service/AAIExtendedHttpClientImplTest.java delete mode 100644 prh-aai-client/src/test/java/services/service/AAIHttpClientImplTest.java delete mode 100644 prh-aai-client/src/test/java/services/service/utils/HTTPConfiguration.java delete mode 100644 prh-aai-client/src/test/java/services/service/utils/HttpUtilsTest.java diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIHttpClientConfiguration.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIHttpClientConfiguration.java new file mode 100644 index 00000000..9f93f896 --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIHttpClientConfiguration.java @@ -0,0 +1,54 @@ +/*- + * ============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.config; + + +import org.immutables.value.Value; +import org.springframework.stereotype.Component; + +import java.io.Serializable; + + +@Component +@Value.Immutable(prehash = true) +@Value.Style(stagedBuilder = true) +public abstract class AAIHttpClientConfiguration implements Serializable { + + private static final long serialVersionUID = 1L; + + @Value.Parameter + public abstract String aaiHost(); + + @Value.Parameter + public abstract Integer aaiHostPortNumber(); + + @Value.Parameter + public abstract String aaiProtocol(); + + @Value.Parameter + public abstract String aaiUserName(); + + @Value.Parameter + public abstract String aaiUserPassword(); + + @Value.Parameter + public abstract Boolean aaiIgnoreSSLCertificateErrors(); + +} diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClient.java new file mode 100644 index 00000000..24149e6d --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClient.java @@ -0,0 +1,30 @@ +/*- + * ============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.onap.dcaegen2.services.utils.HttpRequestDetails; + +import java.util.Optional; + + +@FunctionalInterface +public interface AAIExtendedHttpClient { + Optional getHttpResponse(HttpRequestDetails httpRequestDetails); +} 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 new file mode 100644 index 00000000..133a537e --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java @@ -0,0 +1,162 @@ +/*- + * ============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.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 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; + private final Integer aaiHostPortNumber; + + @Autowired + public AAIExtendedHttpClientImpl (AAIHttpClientConfiguration aaiHttpClientConfiguration) { + final AAIHttpClient aaiHttpClient = new AAIHttpClientImpl(aaiHttpClientConfiguration); + closeableHttpClient = aaiHttpClient.getAAIHttpClient(); + aaiHost = aaiHttpClientConfiguration.aaiHost(); + aaiProtocol = aaiHttpClientConfiguration.aaiProtocol(); + aaiHostPortNumber = aaiHttpClientConfiguration.aaiHostPortNumber(); + } + + @Override + public Optional getHttpResponse(HttpRequestDetails httpRequestDetails) { + + Optional extendedDetails = Optional.empty(); + + final URI extendedURI = createAAIExtendedURI(httpRequestDetails.aaiAPIPath(), + httpRequestDetails.queryParameters()); + final HttpRequestBase request = createHttpRequest(extendedURI, httpRequestDetails); + + if (request == null) { + return Optional.empty(); + } + + for (Map.Entry headersEntry : httpRequestDetails.headers().entrySet()) { + request.addHeader(headersEntry.getKey(), headersEntry.getValue()); + } + + try { + extendedDetails = closeableHttpClient.execute(request, aaiResponseHandler()); + } catch (IOException e) { + logger.error("Exception while executing HTTP request: {}", e); + } + + if (extendedDetails.isPresent()) { + return extendedDetails; + } else { + return Optional.empty(); + } + } + + private URI createAAIExtendedURI(final String path, Map 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); + } + + try { + logger.info("Building extended URI"); + extendedURI = uriBuilder.build(); + } catch (URISyntaxException e) { + logger.error("Exception while building extended URI: {}", e); + } + + return extendedURI; + } + + private String createCustomQuery(@Nonnull final Map queryParams) { + final StringBuilder queryStringBuilder = new StringBuilder(""); + final Iterator> queryParamIterator = queryParams.entrySet().iterator(); + + while (queryParamIterator.hasNext()) { + final Map.Entry queryParamsEntry = queryParamIterator.next(); + queryStringBuilder.append(queryParamsEntry.getKey()).append("=").append(queryParamsEntry.getValue()); + if (queryParamIterator.hasNext()) { + queryStringBuilder.append("&"); + } + } + + return queryStringBuilder.toString(); + } + + private ResponseHandler> aaiResponseHandler() { + return httpResponse -> { + final int responseCode = httpResponse.getStatusLine().getStatusCode(); + final HttpEntity responseEntity = httpResponse.getEntity(); + + 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) : ""; + 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/org/onap/dcaegen2/services/service/AAIHttpClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClient.java new file mode 100644 index 00000000..c60027c2 --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClient.java @@ -0,0 +1,29 @@ +/*- + * ============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.apache.http.impl.client.CloseableHttpClient; + +@FunctionalInterface +public interface AAIHttpClient { + CloseableHttpClient getAAIHttpClient(); +} + diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClientImpl.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClientImpl.java new file mode 100644 index 00000000..90b551db --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClientImpl.java @@ -0,0 +1,93 @@ +/*- + * ============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.apache.http.auth.AuthScope; +import org.apache.http.auth.Credentials; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.CloseableHttpClient; +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 java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; + +public class AAIHttpClientImpl implements AAIHttpClient { + + Logger logger = LoggerFactory.getLogger(AAIHttpClientImpl.class); + + private AAIHttpClientConfiguration aaiHttpClientConfig; + + @Autowired + public AAIHttpClientImpl(AAIHttpClientConfiguration aaiHttpClientConfiguration) { + this.aaiHttpClientConfig = aaiHttpClientConfiguration; + } + + @Override + public CloseableHttpClient getAAIHttpClient() { + + final HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties(); + final boolean aaiIgnoreSSLCertificateErrors = aaiHttpClientConfig.aaiIgnoreSSLCertificateErrors(); + + TrustStrategy acceptingTrustStrategy = (cert, authType) -> true; + + if (aaiIgnoreSSLCertificateErrors) { + try { + 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: {}", e); + } + + httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); + } + + final String aaiUserName = aaiHttpClientConfig.aaiUserName(); + + final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + + if (aaiUserName != null) { + final String aaiHost = aaiHttpClientConfig.aaiHost(); + final Integer aaiHostPortNumber = aaiHttpClientConfig.aaiHostPortNumber(); + final String aaiUserPassword = aaiHttpClientConfig.aaiUserPassword(); + final AuthScope aaiHostPortAuthScope = new AuthScope(aaiHost, aaiHostPortNumber); + final Credentials aaiCredentials = new UsernamePasswordCredentials(aaiUserName, aaiUserPassword); + credentialsProvider.setCredentials(aaiHostPortAuthScope, aaiCredentials); + } + + httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); + + return httpClientBuilder.build(); + } +} 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 new file mode 100644 index 00000000..896e3068 --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java @@ -0,0 +1,45 @@ +/*- + * ============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.utils; + + +import org.immutables.value.Value; + +import java.io.Serializable; +import java.util.Map; + +@Value.Immutable(prehash = true) +@Value.Style(stagedBuilder = true) +public abstract class HttpRequestDetails implements Serializable { + + private static final long serialVersionUID = 1L; + + @Value.Parameter + public abstract String aaiAPIPath(); + + @Value.Parameter + public abstract Map queryParameters(); + + @Value.Parameter + public abstract Map headers(); + + @Value.Parameter + public abstract RequestVerbs requestVerb(); +} diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpUtils.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpUtils.java new file mode 100644 index 00000000..eac7f83a --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpUtils.java @@ -0,0 +1,33 @@ +/*- + * ============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.utils; + +import org.apache.http.HttpStatus; + +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/main/java/org/onap/dcaegen2/services/utils/RequestVerbs.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/RequestVerbs.java new file mode 100644 index 00000000..f350aa48 --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/RequestVerbs.java @@ -0,0 +1,28 @@ +/*- + * ============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.utils; + +public enum RequestVerbs { + GET, + PUT; + + private RequestVerbs() {}; +} diff --git a/prh-aai-client/src/main/java/services/config/AAIHttpClientConfiguration.java b/prh-aai-client/src/main/java/services/config/AAIHttpClientConfiguration.java deleted file mode 100644 index d5a9281c..00000000 --- a/prh-aai-client/src/main/java/services/config/AAIHttpClientConfiguration.java +++ /dev/null @@ -1,53 +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.config; - - -import java.io.Serializable; -import org.immutables.value.Value; -import org.springframework.stereotype.Component; - -@Component -@Value.Immutable(prehash = true) -@Value.Style(stagedBuilder = true) -public abstract class AAIHttpClientConfiguration implements Serializable { - - private static final long serialVersionUID = 1L; - - @Value.Parameter - public abstract String aaiHost(); - - @Value.Parameter - public abstract Integer aaiHostPortNumber(); - - @Value.Parameter - public abstract String aaiProtocol(); - - @Value.Parameter - public abstract String aaiUserName(); - - @Value.Parameter - public abstract String aaiUserPassword(); - - @Value.Parameter - public abstract Boolean aaiIgnoreSSLCertificateErrors(); - -} diff --git a/prh-aai-client/src/main/java/services/service/AAIExtendedHttpClient.java b/prh-aai-client/src/main/java/services/service/AAIExtendedHttpClient.java deleted file mode 100644 index 41cf87e5..00000000 --- a/prh-aai-client/src/main/java/services/service/AAIExtendedHttpClient.java +++ /dev/null @@ -1,27 +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 java.util.Map; - -public interface AAIExtendedHttpClient { - String getExtendedDetails(String aaiAPIPath, Map queryParams, Map headers); -} diff --git a/prh-aai-client/src/main/java/services/service/AAIExtendedHttpClientImpl.java b/prh-aai-client/src/main/java/services/service/AAIExtendedHttpClientImpl.java deleted file mode 100644 index 7a5a9f8b..00000000 --- a/prh-aai-client/src/main/java/services/service/AAIExtendedHttpClientImpl.java +++ /dev/null @@ -1,142 +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 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.utils.URIBuilder; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.util.EntityUtils; -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; - -public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { - - private final CloseableHttpClient closeableHttpClient; - private final String aaiHost; - private final String aaiProtocol; - private final Integer aaiHostPortNumber; - - @Autowired - public AAIExtendedHttpClientImpl (AAIHttpClientConfiguration aaiHttpClientConfiguration) { - final AAIHttpClient aaiHttpClient = new AAIHttpClientImpl(aaiHttpClientConfiguration); - closeableHttpClient = aaiHttpClient.getAAIHttpClient(); - aaiHost = aaiHttpClientConfiguration.aaiHost(); - aaiProtocol = aaiHttpClientConfiguration.aaiProtocol(); - aaiHostPortNumber = aaiHttpClientConfiguration.aaiHostPortNumber(); - } - - @Override - public String getExtendedDetails(final String aaiAPIPath, final Map queryParams, - final Map headers) { - final URI extendedURI = - createAAIExtendedURI(aaiProtocol, aaiHost, aaiHostPortNumber, aaiAPIPath, queryParams); - - if (extendedURI == null) { - return null; - } - - final HttpGet getRequest = new HttpGet(extendedURI); - - for (Map.Entry headersEntry : headers.entrySet()) { - getRequest.addHeader(headersEntry.getKey(), headersEntry.getValue()); - } - - Optional extendedDetails = Optional.empty(); - - try { - extendedDetails = closeableHttpClient.execute(getRequest, aaiResponseHandler()); - } catch (IOException ex) { - //ToDo loging - } - - // return response - if (extendedDetails.isPresent()) { - return extendedDetails.get(); - } else { - return null; - } - } - - private URI createAAIExtendedURI(final String protocol, final String hostName, final Integer portNumber, - final String path, Map queryParams) { - final URIBuilder uriBuilder = new URIBuilder().setScheme(protocol).setHost(hostName).setPort(portNumber) - .setPath(path); - - final String customQuery = createCustomQuery(queryParams); - if (StringUtils.isNoneBlank(customQuery)) { - uriBuilder.setCustomQuery(customQuery); - } - - URI extendedURI = null; - - try { - extendedURI = uriBuilder.build(); - } catch (URISyntaxException e) { - // ToDo loging - } - - return extendedURI; - } - - private String createCustomQuery(@Nonnull final Map queryParams) { - final StringBuilder queryStringBuilder = new StringBuilder(""); - final Iterator> queryParamIterator = queryParams.entrySet().iterator(); - while (queryParamIterator.hasNext()) { - final Map.Entry queryParamsEntry = queryParamIterator.next(); - queryStringBuilder.append(queryParamsEntry.getKey()); - queryStringBuilder.append("="); - queryStringBuilder.append(queryParamsEntry.getValue()); - if (queryParamIterator.hasNext()) { - queryStringBuilder.append("&"); - } - } - return queryStringBuilder.toString(); - } - - private ResponseHandler> aaiResponseHandler() { - return httpResponse -> { - final int responseCode = httpResponse.getStatusLine().getStatusCode(); - final HttpEntity responseEntity = httpResponse.getEntity(); - - if (HttpUtils.isSuccessfulResponseCode(responseCode) && null != responseEntity) { - final String aaiResponse = EntityUtils.toString(responseEntity); - return Optional.of(aaiResponse); - } else { - String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; - //ToDo loging - return Optional.empty(); - } - }; - } - -} diff --git a/prh-aai-client/src/main/java/services/service/AAIHttpClient.java b/prh-aai-client/src/main/java/services/service/AAIHttpClient.java deleted file mode 100644 index c1054d7b..00000000 --- a/prh-aai-client/src/main/java/services/service/AAIHttpClient.java +++ /dev/null @@ -1,28 +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 org.apache.http.impl.client.CloseableHttpClient; - -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/services/service/AAIHttpClientImpl.java deleted file mode 100644 index 39c91ddc..00000000 --- a/prh-aai-client/src/main/java/services/service/AAIHttpClientImpl.java +++ /dev/null @@ -1,93 +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 org.apache.http.auth.AuthScope; -import org.apache.http.auth.Credentials; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.CredentialsProvider; -import org.apache.http.conn.ssl.NoopHostnameVerifier; -import org.apache.http.impl.client.BasicCredentialsProvider; -import org.apache.http.impl.client.CloseableHttpClient; -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.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; -import java.security.NoSuchAlgorithmException; - -public class AAIHttpClientImpl implements AAIHttpClient { - - Logger logger = LoggerFactory.getLogger(AAIHttpClientImpl.class); - - private AAIHttpClientConfiguration aaiHttpClientConfig; - - @Autowired - public AAIHttpClientImpl(AAIHttpClientConfiguration aaiHttpClientConfiguration) { - this.aaiHttpClientConfig = aaiHttpClientConfiguration; - } - - @Override - public CloseableHttpClient getAAIHttpClient() { - - final HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties(); - final boolean aaiIgnoreSSLCertificateErrors = aaiHttpClientConfig.aaiIgnoreSSLCertificateErrors(); - - TrustStrategy acceptingTrustStrategy = (cert, authType) -> true; - - if (aaiIgnoreSSLCertificateErrors) { - try { - SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); - sslContextBuilder.loadTrustMaterial(null, acceptingTrustStrategy); - httpClientBuilder.setSSLContext(sslContextBuilder.build()); - - } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e ) { - logger.error("Exception while setting SSL Context for AAI HTTP Client."); - } - - httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); - } - - final String aaiUserName = aaiHttpClientConfig.aaiUserName(); - - final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - - if (aaiUserName != null) { - final String aaiHost = aaiHttpClientConfig.aaiHost(); - final Integer aaiHostPortNumber = aaiHttpClientConfig.aaiHostPortNumber(); - final String aaiUserPassword = aaiHttpClientConfig.aaiUserPassword(); - final AuthScope aaiHostPortAuthScope = new AuthScope(aaiHost, aaiHostPortNumber); - final Credentials aaiCredentials = new UsernamePasswordCredentials(aaiUserName, aaiUserPassword); - credentialsProvider.setCredentials(aaiHostPortAuthScope, aaiCredentials); - } - - httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); - - return httpClientBuilder.build(); - } -} diff --git a/prh-aai-client/src/main/java/services/utils/HttpUtils.java b/prh-aai-client/src/main/java/services/utils/HttpUtils.java deleted file mode 100644 index d4d5c880..00000000 --- a/prh-aai-client/src/main/java/services/utils/HttpUtils.java +++ /dev/null @@ -1,33 +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.utils; - -import org.apache.http.HttpStatus; - -public class HttpUtils implements HttpStatus { - - private HttpUtils() {} - - public static final String JSON_APPLICATION_TYPE = "application/json"; - - public static boolean isSuccessfulResponseCode(Integer statusCode) { - return statusCode >= 200 && statusCode < 300; - } -} 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 expectedResult = Optional.empty(); + + @BeforeAll + public static void init() throws NoSuchFieldException, IllegalAccessException { + + Map queryParams = new HashMap<>(); + queryParams.put("ipaddress-v4-oam", "11.22.33.44"); + + Map 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 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 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 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/org/onap/dcaegen2/services/service/utils/HttpUtilsTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpUtilsTest.java new file mode 100644 index 00000000..53b31da2 --- /dev/null +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpUtilsTest.java @@ -0,0 +1,42 @@ +/*- + * ============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.apache.http.HttpStatus; +import org.junit.Test; +import org.onap.dcaegen2.services.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 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 queryParams = new HashMap<>(); - queryParams.put("key1", "value1"); - Map 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); - } - - -} diff --git a/prh-aai-client/src/test/java/services/service/AAIHttpClientImplTest.java b/prh-aai-client/src/test/java/services/service/AAIHttpClientImplTest.java deleted file mode 100644 index 242e56d4..00000000 --- a/prh-aai-client/src/test/java/services/service/AAIHttpClientImplTest.java +++ /dev/null @@ -1,56 +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 org.junit.After; -import org.junit.Before; -import org.junit.Test; -import services.config.AAIHttpClientConfiguration; -import services.service.utils.HTTPConfiguration; - -import static org.junit.Assert.assertNotNull; - - -public class AAIHttpClientImplTest { - - private AAIHttpClientImpl aaiHttpClientImpl; - private AAIHttpClientConfiguration aaiHttpClientConfiguration; - - - @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()); - } - -} - diff --git a/prh-aai-client/src/test/java/services/service/utils/HTTPConfiguration.java b/prh-aai-client/src/test/java/services/service/utils/HTTPConfiguration.java deleted file mode 100644 index 82f656d9..00000000 --- a/prh-aai-client/src/test/java/services/service/utils/HTTPConfiguration.java +++ /dev/null @@ -1,62 +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.utils; - -import services.config.AAIHttpClientConfiguration; - -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 diff --git a/prh-aai-client/src/test/java/services/service/utils/HttpUtilsTest.java b/prh-aai-client/src/test/java/services/service/utils/HttpUtilsTest.java deleted file mode 100644 index d54b335b..00000000 --- a/prh-aai-client/src/test/java/services/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 services.service.utils; - -import org.apache.http.HttpStatus; -import org.junit.Test; -import services.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 -- cgit 1.2.3-korg