From 3e559e25389b24d1cacedc8983334054e33faf14 Mon Sep 17 00:00:00 2001 From: wasala Date: Thu, 17 May 2018 08:16:09 +0200 Subject: Change package in prh-dmaap-client module Move package org.onap.dcaegen2.services to org.onap.dcaegen2.services.prh Change-Id: Idef63833dcbc9cd28dda6b893245fcd16df4d97d Issue-ID: DCAEGEN2-396 Signed-off-by: wasala --- .../config/DmaapConsumerConfiguration.java | 66 --------- .../services/config/DmaapCustomConfig.java | 70 ---------- .../config/DmaapPublisherConfiguration.java | 43 ------ .../prh/config/DmaapConsumerConfiguration.java | 66 +++++++++ .../services/prh/config/DmaapCustomConfig.java | 70 ++++++++++ .../prh/config/DmaapPublisherConfiguration.java | 43 ++++++ .../prh/response/DMaaPConsumerResponse.java | 32 +++++ .../prh/response/DMaaPConsumerResponseImpl.java | 72 ++++++++++ .../services/prh/response/DMaaPResponse.java | 32 +++++ .../services/prh/service/DmaapHttpClientImpl.java | 70 ++++++++++ .../dcaegen2/services/prh/service/HttpUtils.java | 31 +++++ .../ExtendedDmaapConsumerHttpClientImpl.java | 141 +++++++++++++++++++ .../ExtendedDmaapProducerHttpClientImpl.java | 149 +++++++++++++++++++++ .../services/response/DMaaPConsumerResponse.java | 32 ----- .../response/DMaaPConsumerResponseImpl.java | 72 ---------- .../dcaegen2/services/response/DMaaPResponse.java | 32 ----- .../services/service/DmaapHttpClientImpl.java | 70 ---------- .../onap/dcaegen2/services/service/HttpUtils.java | 31 ----- .../ExtendedDmaapConsumerHttpClientImpl.java | 141 ------------------- .../ExtendedDmaapProducerHttpClientImpl.java | 149 --------------------- .../ExtendedDmaapConsumerHttpClientImplTest.java | 96 +++++++++++++ .../ExtendedDmaapProducerHttpClientImplTest.java | 98 ++++++++++++++ .../ExtendedDmaapConsumerHttpClientImplTest.java | 96 ------------- .../ExtendedDmaapProducerHttpClientImplTest.java | 98 -------------- 24 files changed, 900 insertions(+), 900 deletions(-) delete mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapConsumerConfiguration.java delete mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapCustomConfig.java delete mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapPublisherConfiguration.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapConsumerConfiguration.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapCustomConfig.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapPublisherConfiguration.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPConsumerResponse.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPConsumerResponseImpl.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPResponse.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/DmaapHttpClientImpl.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/HttpUtils.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImpl.java delete mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPConsumerResponse.java delete mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPConsumerResponseImpl.java delete mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPResponse.java delete mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/DmaapHttpClientImpl.java delete mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/HttpUtils.java delete mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java delete mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImpl.java create mode 100644 prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/ExtendedDmaapConsumerHttpClientImplTest.java create mode 100644 prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImplTest.java delete mode 100644 prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/service/consumer/ExtendedDmaapConsumerHttpClientImplTest.java delete mode 100644 prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImplTest.java (limited to 'prh-dmaap-client') diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapConsumerConfiguration.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapConsumerConfiguration.java deleted file mode 100644 index 382dfcad..00000000 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapConsumerConfiguration.java +++ /dev/null @@ -1,66 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ -package org.onap.dcaegen2.services.config; - -import org.immutables.gson.Gson; -import org.immutables.value.Value; -import org.springframework.stereotype.Component; - -/** - * @author Przemysław Wąsala on 3/23/18 - */ -@Component -@Value.Immutable(prehash = true) -@Value.Style(builder = "new") -@Gson.TypeAdapters -public abstract class DmaapConsumerConfiguration implements DmaapCustomConfig { - - private static final long serialVersionUID = 1L; - - @Value.Parameter - public abstract String consumerId(); - - @Value.Parameter - public abstract String consumerGroup(); - - @Value.Parameter - public abstract Integer timeoutMS(); - - @Value.Parameter - public abstract Integer messageLimit(); - - - public interface Builder extends - DmaapCustomConfig.Builder { - - Builder consumerId(String consumerId); - - Builder consumerGroup(String consumerGroup); - - Builder timeoutMS(Integer timeoutMS); - - Builder messageLimit(Integer messageLimit); - } - - public static DmaapConsumerConfiguration.Builder builder() { - return ImmutableDmaapConsumerConfiguration.builder(); - } - -} \ No newline at end of file diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapCustomConfig.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapCustomConfig.java deleted file mode 100644 index 0c20989a..00000000 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapCustomConfig.java +++ /dev/null @@ -1,70 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ -package org.onap.dcaegen2.services.config; - -import java.io.Serializable; -import org.immutables.value.Value; - -/** - * @author Przemysław Wąsala on 3/28/18 - */ -public interface DmaapCustomConfig extends Serializable { - - @Value.Parameter - String dmaapHostName(); - - @Value.Parameter - Integer dmaapPortNumber(); - - @Value.Parameter - String dmaapTopicName(); - - @Value.Parameter - String dmaapProtocol(); - - @Value.Parameter - String dmaapUserName(); - - @Value.Parameter - String dmaapUserPassword(); - - @Value.Parameter - String dmaapContentType(); - - - interface Builder> { - - B dmaapHostName(String dmaapHostName); - - B dmaapPortNumber(Integer dmaapPortNumber); - - B dmaapTopicName(String dmaapTopicName); - - B dmaapProtocol(String dmaapProtocol); - - B dmaapUserName(String dmaapUserName); - - B dmaapUserPassword(String dmaapUserPassword); - - B dmaapContentType(String dmaapContentType); - - T build(); - } -} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapPublisherConfiguration.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapPublisherConfiguration.java deleted file mode 100644 index 6f5f17c4..00000000 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapPublisherConfiguration.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ -package org.onap.dcaegen2.services.config; - -import org.immutables.gson.Gson; -import org.immutables.value.Value; - -/** - * @author Przemysław Wąsala on 3/23/18 - */ -@Value.Immutable(prehash = true) -@Value.Style(builder = "new") -@Gson.TypeAdapters -public abstract class DmaapPublisherConfiguration implements DmaapCustomConfig { - - private static final long serialVersionUID = 1L; - - interface Builder extends - DmaapCustomConfig.Builder { - - } - - public static DmaapPublisherConfiguration.Builder builder() { - return ImmutableDmaapPublisherConfiguration.builder(); - } -} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapConsumerConfiguration.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapConsumerConfiguration.java new file mode 100644 index 00000000..89d3bd4c --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapConsumerConfiguration.java @@ -0,0 +1,66 @@ +/*- + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.dcaegen2.services.prh.config; + +import org.immutables.gson.Gson; +import org.immutables.value.Value; +import org.springframework.stereotype.Component; + +/** + * @author Przemysław Wąsala on 3/23/18 + */ +@Component +@Value.Immutable(prehash = true) +@Value.Style(builder = "new") +@Gson.TypeAdapters +public abstract class DmaapConsumerConfiguration implements DmaapCustomConfig { + + private static final long serialVersionUID = 1L; + + @Value.Parameter + public abstract String consumerId(); + + @Value.Parameter + public abstract String consumerGroup(); + + @Value.Parameter + public abstract Integer timeoutMS(); + + @Value.Parameter + public abstract Integer messageLimit(); + + + public interface Builder extends + DmaapCustomConfig.Builder { + + Builder consumerId(String consumerId); + + Builder consumerGroup(String consumerGroup); + + Builder timeoutMS(Integer timeoutMS); + + Builder messageLimit(Integer messageLimit); + } + + public static DmaapConsumerConfiguration.Builder builder() { + return ImmutableDmaapConsumerConfiguration.builder(); + } + +} \ No newline at end of file diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapCustomConfig.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapCustomConfig.java new file mode 100644 index 00000000..ecc9d211 --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapCustomConfig.java @@ -0,0 +1,70 @@ +/*- + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.dcaegen2.services.prh.config; + +import java.io.Serializable; +import org.immutables.value.Value; + +/** + * @author Przemysław Wąsala on 3/28/18 + */ +public interface DmaapCustomConfig extends Serializable { + + @Value.Parameter + String dmaapHostName(); + + @Value.Parameter + Integer dmaapPortNumber(); + + @Value.Parameter + String dmaapTopicName(); + + @Value.Parameter + String dmaapProtocol(); + + @Value.Parameter + String dmaapUserName(); + + @Value.Parameter + String dmaapUserPassword(); + + @Value.Parameter + String dmaapContentType(); + + + interface Builder> { + + B dmaapHostName(String dmaapHostName); + + B dmaapPortNumber(Integer dmaapPortNumber); + + B dmaapTopicName(String dmaapTopicName); + + B dmaapProtocol(String dmaapProtocol); + + B dmaapUserName(String dmaapUserName); + + B dmaapUserPassword(String dmaapUserPassword); + + B dmaapContentType(String dmaapContentType); + + T build(); + } +} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapPublisherConfiguration.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapPublisherConfiguration.java new file mode 100644 index 00000000..2e95463c --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapPublisherConfiguration.java @@ -0,0 +1,43 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.dcaegen2.services.prh.config; + +import org.immutables.gson.Gson; +import org.immutables.value.Value; + +/** + * @author Przemysław Wąsala on 3/23/18 + */ +@Value.Immutable(prehash = true) +@Value.Style(builder = "new") +@Gson.TypeAdapters +public abstract class DmaapPublisherConfiguration implements DmaapCustomConfig { + + private static final long serialVersionUID = 1L; + + interface Builder extends + DmaapCustomConfig.Builder { + + } + + public static DmaapPublisherConfiguration.Builder builder() { + return ImmutableDmaapPublisherConfiguration.builder(); + } +} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPConsumerResponse.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPConsumerResponse.java new file mode 100644 index 00000000..d2c83e73 --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPConsumerResponse.java @@ -0,0 +1,32 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.dcaegen2.services.prh.response; + +import java.util.List; + +/** + * @author Przemysław Wąsala on 4/17/18 + */ +public interface DMaaPConsumerResponse extends DMaaPResponse { + + + List getFetchedMessages(); + +} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPConsumerResponseImpl.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPConsumerResponseImpl.java new file mode 100644 index 00000000..ddac9cbf --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPConsumerResponseImpl.java @@ -0,0 +1,72 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.dcaegen2.services.prh.response; + +import static java.util.Collections.unmodifiableList; + +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.StringJoiner; +import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; + +/** + * @author Przemysław Wąsala on 4/17/18 + */ +public class DMaaPConsumerResponseImpl implements DMaaPConsumerResponse { + + + private final Integer responseCode; + private final String responseMessage; + private final List fetchedMessage; + + public DMaaPConsumerResponseImpl(@NonNull Integer responseCode, @NonNull String responseMessage, + @Nullable List fetchedMessage) { + this.responseCode = responseCode; + this.responseMessage = responseMessage; + this.fetchedMessage = Optional.ofNullable(fetchedMessage).orElse(Collections.emptyList()); + } + + + @Override + public String getResponseMessage() { + return responseMessage; + } + + @Override + public Integer getResponseCode() { + return responseCode; + } + + @Override + public List getFetchedMessages() { + return unmodifiableList(fetchedMessage); + } + + @Override + public String toString() { + return new StringJoiner(", ", this.getClass().getSimpleName() + "[", "]") + .add("responseCode=" + responseCode) + .add("responseMessage=" + responseMessage) + .add("fetchedMessage=" + fetchedMessage) + .toString(); + } +} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPResponse.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPResponse.java new file mode 100644 index 00000000..5bf199c0 --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPResponse.java @@ -0,0 +1,32 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.services.prh.response; + +/** + * @author Przemysław Wąsala on 4/17/18 + */ +public interface DMaaPResponse { + + Integer getResponseCode(); + + + String getResponseMessage(); +} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/DmaapHttpClientImpl.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/DmaapHttpClientImpl.java new file mode 100644 index 00000000..59c4317b --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/DmaapHttpClientImpl.java @@ -0,0 +1,70 @@ +/*- + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.services.prh.service; + +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.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.onap.dcaegen2.services.prh.config.DmaapCustomConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DmaapHttpClientImpl { + + private static Logger logger = LoggerFactory.getLogger(DmaapHttpClientImpl.class); + + private final String dmaapHostName; + private final Integer dmaapPortNumber; + private final String dmaapUserName; + private final String dmaapUserPassword; + + + public DmaapHttpClientImpl(DmaapCustomConfig configuration) { + this.dmaapHostName = configuration.dmaapHostName(); + this.dmaapPortNumber = configuration.dmaapPortNumber(); + this.dmaapUserName = configuration.dmaapUserName(); + this.dmaapUserPassword = configuration.dmaapUserPassword(); + } + + public CloseableHttpClient getHttpClient() { + + logger.info("Preparing closeable http client"); + + HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties(); + + final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + + if (dmaapUserName != null) { + final AuthScope dmaapHostPortAuthScope = new AuthScope(dmaapHostName, dmaapPortNumber); + final Credentials dmaapCredentials = new UsernamePasswordCredentials(dmaapUserName, dmaapUserPassword); + credentialsProvider.setCredentials(dmaapHostPortAuthScope, dmaapCredentials); + } + + httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); + + return httpClientBuilder.build(); + } +} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/HttpUtils.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/HttpUtils.java new file mode 100644 index 00000000..b96f80dd --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/HttpUtils.java @@ -0,0 +1,31 @@ +/*- + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.dcaegen2.services.prh.service; + +import org.apache.http.HttpStatus; + +public final class HttpUtils implements HttpStatus { + + private HttpUtils() {} + + public static boolean isSuccessfulResponseCode(Integer statusCode) { + return statusCode >= 200 && statusCode < 300; + } +} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java new file mode 100644 index 00000000..2b46f51b --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java @@ -0,0 +1,141 @@ +/*- + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.services.prh.service.consumer; + +import org.apache.http.HttpEntity; +import org.apache.http.client.ResponseHandler; +import org.apache.http.client.methods.HttpGet; +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.prh.config.DmaapConsumerConfiguration; +import org.onap.dcaegen2.services.prh.service.DmaapHttpClientImpl; +import org.onap.dcaegen2.services.prh.service.HttpUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Optional; + + +public class ExtendedDmaapConsumerHttpClientImpl { + + private static Logger logger = LoggerFactory.getLogger(ExtendedDmaapConsumerHttpClientImpl.class); + + private final CloseableHttpClient closeableHttpClient; + private final String dmaapHostName; + private final String dmaapProtocol; + private final Integer dmaapPortNumber; + private final String dmaapTopicName; + private final String consumerGroup; + private final String consumerId; + private final String dmaapContentType; + + + public ExtendedDmaapConsumerHttpClientImpl(DmaapConsumerConfiguration configuration) { + this.closeableHttpClient = new DmaapHttpClientImpl(configuration).getHttpClient(); + this.dmaapHostName = configuration.dmaapHostName(); + this.dmaapProtocol = configuration.dmaapProtocol(); + this.dmaapPortNumber = configuration.dmaapPortNumber(); + this.dmaapTopicName = configuration.dmaapTopicName(); + this.consumerGroup = configuration.consumerGroup(); + this.consumerId = configuration.consumerId(); + this.dmaapContentType = configuration.dmaapContentType(); + } + + public Optional getHttpConsumerResponse() { + + Optional extendedDetails = Optional.empty(); + Optional request = createRequest(); + + try { + extendedDetails = closeableHttpClient.execute(request.get(), dmaapConsumerResponseHandler()); + } catch (IOException | NullPointerException e) { + logger.error("Exception while executing HTTP request: {}", e); + } + + return extendedDetails; + } + + private static HttpRequestBase createHttpRequest(URI extendedURI) { + if (isExtendedURINotNull(extendedURI)) { + return new HttpGet(extendedURI); + } + + return null; + } + + private static Boolean isExtendedURINotNull(URI extendedURI) { + return extendedURI != null; + } + + private Optional createRequest() { + + Optional request = Optional.empty(); + final URI extendedURI = createDmaapConsumerExtendedURI(); + + if ("application/json".equals(dmaapContentType)) { + request = Optional.ofNullable(createHttpRequest(extendedURI)); + request.get().addHeader("Content-type", dmaapContentType); + } + + return request; + } + + private String createRequestPath() { + return dmaapTopicName + "/" + consumerGroup + "/" + consumerId; + } + + private URI createDmaapConsumerExtendedURI() { + try { + return new URIBuilder() + .setScheme(dmaapProtocol) + .setHost(dmaapHostName) + .setPort(dmaapPortNumber) + .setPath(createRequestPath()).build(); + } catch (URISyntaxException e) { + throw new RuntimeException("Exception while building extended URI: {}", e); + } + } + + private ResponseHandler> dmaapConsumerResponseHandler() { + return httpResponse -> { + final int responseCode = httpResponse.getStatusLine().getStatusCode(); + logger.info("Status code of operation: {}", responseCode); + final HttpEntity responseEntity = httpResponse.getEntity(); + + if (HttpUtils.isSuccessfulResponseCode(responseCode)) { + logger.info("HTTP response successful."); + final String dmaapResponse = EntityUtils.toString(responseEntity); + return Optional.of(dmaapResponse); + } else { + String dmaapResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; + logger.error("HTTP response not successful : {}", dmaapResponse); + return Optional.of(String.valueOf(responseCode)); + } + }; + } +} + + diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImpl.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImpl.java new file mode 100644 index 00000000..156c1891 --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImpl.java @@ -0,0 +1,149 @@ +/*- + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.services.prh.service.producer; + +import org.apache.http.HttpEntity; +import org.apache.http.client.ResponseHandler; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.util.EntityUtils; +import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration; +import org.onap.dcaegen2.services.prh.model.CommonFunctions; +import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; +import org.onap.dcaegen2.services.prh.service.DmaapHttpClientImpl; +import org.onap.dcaegen2.services.prh.service.HttpUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Optional; + +public class ExtendedDmaapProducerHttpClientImpl { + + private static Logger logger = LoggerFactory.getLogger(ExtendedDmaapProducerHttpClientImpl.class); + + private final CloseableHttpClient closeableHttpClient; + private final String dmaapHostName; + private final String dmaapProtocol; + private final Integer dmaapPortNumber; + private final String dmaapTopicName; + private final String dmaapContentType; + + + public ExtendedDmaapProducerHttpClientImpl(DmaapPublisherConfiguration configuration) { + this.closeableHttpClient = new DmaapHttpClientImpl(configuration).getHttpClient(); + this.dmaapHostName = configuration.dmaapHostName(); + this.dmaapProtocol = configuration.dmaapProtocol(); + this.dmaapPortNumber = configuration.dmaapPortNumber(); + this.dmaapTopicName = configuration.dmaapTopicName(); + this.dmaapContentType = configuration.dmaapContentType(); + } + + public Optional getHttpProducerResponse(ConsumerDmaapModel consumerDmaapModel) { + Optional extendedDetails = Optional.empty(); + Optional request = createRequest(consumerDmaapModel); + try { + extendedDetails = closeableHttpClient.execute(request.get(), dmaapProducerResponseHandler()); + } catch (IOException | NullPointerException e) { + logger.warn("Exception while executing HTTP request: ", e); + } + return extendedDetails; + } + + private Optional createStringEntity(Optional jsonBody) { + return Optional.of(parseJson(jsonBody).get()); + } + + private Optional parseJson(Optional jsonBody) { + Optional stringEntity = Optional.empty(); + try { + stringEntity = Optional.of(new StringEntity(jsonBody.get())); + } catch (UnsupportedEncodingException e) { + logger.warn("Exception while parsing JSON: ", e); + } + return stringEntity; + } + + private Optional createRequest(ConsumerDmaapModel consumerDmaapModel) { + Optional request = Optional.empty(); + final URI extendedURI = createDmaapPublisherExtendedURI(); + + if ("application/json".equals(dmaapContentType)) { + request = Optional.ofNullable(createRequest(extendedURI, consumerDmaapModel)); + request.get().addHeader("Content-type", dmaapContentType); + } + + return request; + } + + private URI createDmaapPublisherExtendedURI() { + URI extendedURI = null; + final URIBuilder uriBuilder = new URIBuilder() + .setScheme(dmaapProtocol) + .setHost(dmaapHostName) + .setPort(dmaapPortNumber) + .setPath(dmaapTopicName); + try { + extendedURI = uriBuilder.build(); + logger.trace("Building extended URI: {}", extendedURI); + } catch (URISyntaxException e) { + logger.warn("Exception while building extended URI: ", e); + } + return extendedURI; + } + + private HttpRequestBase createRequest(URI extendedURI, ConsumerDmaapModel consumerDmaapModel) { + if (extendedURI != null) { + return createHttpPost(extendedURI, Optional.ofNullable(CommonFunctions.createJsonBody(consumerDmaapModel))); + } else { + return null; + } + } + + private HttpPost createHttpPost(URI extendedURI, Optional jsonBody) { + HttpPost post = new HttpPost(extendedURI); + Optional stringEntity = createStringEntity(jsonBody); + post.setEntity(stringEntity.get()); + return post; + } + + private ResponseHandler> dmaapProducerResponseHandler() { + return httpResponse -> { + final int responseCode = httpResponse.getStatusLine().getStatusCode(); + final HttpEntity responseEntity = httpResponse.getEntity(); + + if (HttpUtils.isSuccessfulResponseCode(responseCode)) { + logger.trace("HTTP response successful."); + return Optional.of("" + responseCode); + } else { + String response = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; + logger.warn("HTTP response not successful : {}", response); + return Optional.of("" + responseCode); + } + }; + } +} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPConsumerResponse.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPConsumerResponse.java deleted file mode 100644 index a9e5c526..00000000 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPConsumerResponse.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ -package org.onap.dcaegen2.services.response; - -import java.util.List; - -/** - * @author Przemysław Wąsala on 4/17/18 - */ -public interface DMaaPConsumerResponse extends DMaaPResponse { - - - List getFetchedMessages(); - -} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPConsumerResponseImpl.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPConsumerResponseImpl.java deleted file mode 100644 index f3565edf..00000000 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPConsumerResponseImpl.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ -package org.onap.dcaegen2.services.response; - -import static java.util.Collections.unmodifiableList; - -import java.util.Collections; -import java.util.List; -import java.util.Optional; -import java.util.StringJoiner; -import org.springframework.lang.NonNull; -import org.springframework.lang.Nullable; - -/** - * @author Przemysław Wąsala on 4/17/18 - */ -public class DMaaPConsumerResponseImpl implements DMaaPConsumerResponse { - - - private final Integer responseCode; - private final String responseMessage; - private final List fetchedMessage; - - public DMaaPConsumerResponseImpl(@NonNull Integer responseCode, @NonNull String responseMessage, - @Nullable List fetchedMessage) { - this.responseCode = responseCode; - this.responseMessage = responseMessage; - this.fetchedMessage = Optional.ofNullable(fetchedMessage).orElse(Collections.emptyList()); - } - - - @Override - public String getResponseMessage() { - return responseMessage; - } - - @Override - public Integer getResponseCode() { - return responseCode; - } - - @Override - public List getFetchedMessages() { - return unmodifiableList(fetchedMessage); - } - - @Override - public String toString() { - return new StringJoiner(", ", this.getClass().getSimpleName() + "[", "]") - .add("responseCode=" + responseCode) - .add("responseMessage=" + responseMessage) - .add("fetchedMessage=" + fetchedMessage) - .toString(); - } -} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPResponse.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPResponse.java deleted file mode 100644 index 6528eec8..00000000 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPResponse.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.dcaegen2.services.response; - -/** - * @author Przemysław Wąsala on 4/17/18 - */ -public interface DMaaPResponse { - - Integer getResponseCode(); - - - String getResponseMessage(); -} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/DmaapHttpClientImpl.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/DmaapHttpClientImpl.java deleted file mode 100644 index f2917d02..00000000 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/DmaapHttpClientImpl.java +++ /dev/null @@ -1,70 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.dcaegen2.services.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.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.onap.dcaegen2.services.config.DmaapCustomConfig; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class DmaapHttpClientImpl { - - private static Logger logger = LoggerFactory.getLogger(DmaapHttpClientImpl.class); - - private final String dmaapHostName; - private final Integer dmaapPortNumber; - private final String dmaapUserName; - private final String dmaapUserPassword; - - - public DmaapHttpClientImpl(DmaapCustomConfig configuration) { - this.dmaapHostName = configuration.dmaapHostName(); - this.dmaapPortNumber = configuration.dmaapPortNumber(); - this.dmaapUserName = configuration.dmaapUserName(); - this.dmaapUserPassword = configuration.dmaapUserPassword(); - } - - public CloseableHttpClient getHttpClient() { - - logger.info("Preparing closeable http client"); - - HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties(); - - final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - - if (dmaapUserName != null) { - final AuthScope dmaapHostPortAuthScope = new AuthScope(dmaapHostName, dmaapPortNumber); - final Credentials dmaapCredentials = new UsernamePasswordCredentials(dmaapUserName, dmaapUserPassword); - credentialsProvider.setCredentials(dmaapHostPortAuthScope, dmaapCredentials); - } - - httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); - - return httpClientBuilder.build(); - } -} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/HttpUtils.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/HttpUtils.java deleted file mode 100644 index fb615bbf..00000000 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/HttpUtils.java +++ /dev/null @@ -1,31 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ -package org.onap.dcaegen2.services.service; - -import org.apache.http.HttpStatus; - -public final class HttpUtils implements HttpStatus { - - private HttpUtils() {} - - public static boolean isSuccessfulResponseCode(Integer statusCode) { - return statusCode >= 200 && statusCode < 300; - } -} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java deleted file mode 100644 index 1772104a..00000000 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java +++ /dev/null @@ -1,141 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.dcaegen2.services.service.consumer; - -import org.apache.http.HttpEntity; -import org.apache.http.client.ResponseHandler; -import org.apache.http.client.methods.HttpGet; -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.DmaapConsumerConfiguration; -import org.onap.dcaegen2.services.service.DmaapHttpClientImpl; -import org.onap.dcaegen2.services.service.HttpUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.Optional; - - -public class ExtendedDmaapConsumerHttpClientImpl { - - private static Logger logger = LoggerFactory.getLogger(ExtendedDmaapConsumerHttpClientImpl.class); - - private final CloseableHttpClient closeableHttpClient; - private final String dmaapHostName; - private final String dmaapProtocol; - private final Integer dmaapPortNumber; - private final String dmaapTopicName; - private final String consumerGroup; - private final String consumerId; - private final String dmaapContentType; - - - public ExtendedDmaapConsumerHttpClientImpl(DmaapConsumerConfiguration configuration) { - this.closeableHttpClient = new DmaapHttpClientImpl(configuration).getHttpClient(); - this.dmaapHostName = configuration.dmaapHostName(); - this.dmaapProtocol = configuration.dmaapProtocol(); - this.dmaapPortNumber = configuration.dmaapPortNumber(); - this.dmaapTopicName = configuration.dmaapTopicName(); - this.consumerGroup = configuration.consumerGroup(); - this.consumerId = configuration.consumerId(); - this.dmaapContentType = configuration.dmaapContentType(); - } - - public Optional getHttpConsumerResponse() { - - Optional extendedDetails = Optional.empty(); - Optional request = createRequest(); - - try { - extendedDetails = closeableHttpClient.execute(request.get(), dmaapConsumerResponseHandler()); - } catch (IOException | NullPointerException e) { - logger.error("Exception while executing HTTP request: {}", e); - } - - return extendedDetails; - } - - private static HttpRequestBase createHttpRequest(URI extendedURI) { - if (isExtendedURINotNull(extendedURI)) { - return new HttpGet(extendedURI); - } - - return null; - } - - private static Boolean isExtendedURINotNull(URI extendedURI) { - return extendedURI != null; - } - - private Optional createRequest() { - - Optional request = Optional.empty(); - final URI extendedURI = createDmaapConsumerExtendedURI(); - - if ("application/json".equals(dmaapContentType)) { - request = Optional.ofNullable(createHttpRequest(extendedURI)); - request.get().addHeader("Content-type", dmaapContentType); - } - - return request; - } - - private String createRequestPath() { - return dmaapTopicName + "/" + consumerGroup + "/" + consumerId; - } - - private URI createDmaapConsumerExtendedURI() { - try { - return new URIBuilder() - .setScheme(dmaapProtocol) - .setHost(dmaapHostName) - .setPort(dmaapPortNumber) - .setPath(createRequestPath()).build(); - } catch (URISyntaxException e) { - throw new RuntimeException("Exception while building extended URI: {}", e); - } - } - - private ResponseHandler> dmaapConsumerResponseHandler() { - return httpResponse -> { - final int responseCode = httpResponse.getStatusLine().getStatusCode(); - logger.info("Status code of operation: {}", responseCode); - final HttpEntity responseEntity = httpResponse.getEntity(); - - if (HttpUtils.isSuccessfulResponseCode(responseCode)) { - logger.info("HTTP response successful."); - final String dmaapResponse = EntityUtils.toString(responseEntity); - return Optional.of(dmaapResponse); - } else { - String dmaapResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; - logger.error("HTTP response not successful : {}", dmaapResponse); - return Optional.of(String.valueOf(responseCode)); - } - }; - } -} - - diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImpl.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImpl.java deleted file mode 100644 index 11d5a487..00000000 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImpl.java +++ /dev/null @@ -1,149 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.dcaegen2.services.service.producer; - -import org.apache.http.HttpEntity; -import org.apache.http.client.ResponseHandler; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.client.methods.HttpRequestBase; -import org.apache.http.client.utils.URIBuilder; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.util.EntityUtils; -import org.onap.dcaegen2.services.config.DmaapPublisherConfiguration; -import org.onap.dcaegen2.services.prh.model.CommonFunctions; -import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; -import org.onap.dcaegen2.services.service.DmaapHttpClientImpl; -import org.onap.dcaegen2.services.service.HttpUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.Optional; - -public class ExtendedDmaapProducerHttpClientImpl { - - private static Logger logger = LoggerFactory.getLogger(ExtendedDmaapProducerHttpClientImpl.class); - - private final CloseableHttpClient closeableHttpClient; - private final String dmaapHostName; - private final String dmaapProtocol; - private final Integer dmaapPortNumber; - private final String dmaapTopicName; - private final String dmaapContentType; - - - public ExtendedDmaapProducerHttpClientImpl(DmaapPublisherConfiguration configuration) { - this.closeableHttpClient = new DmaapHttpClientImpl(configuration).getHttpClient(); - this.dmaapHostName = configuration.dmaapHostName(); - this.dmaapProtocol = configuration.dmaapProtocol(); - this.dmaapPortNumber = configuration.dmaapPortNumber(); - this.dmaapTopicName = configuration.dmaapTopicName(); - this.dmaapContentType = configuration.dmaapContentType(); - } - - public Optional getHttpProducerResponse(ConsumerDmaapModel consumerDmaapModel) { - Optional extendedDetails = Optional.empty(); - Optional request = createRequest(consumerDmaapModel); - try { - extendedDetails = closeableHttpClient.execute(request.get(), dmaapProducerResponseHandler()); - } catch (IOException | NullPointerException e) { - logger.warn("Exception while executing HTTP request: ", e); - } - return extendedDetails; - } - - private Optional createStringEntity(Optional jsonBody) { - return Optional.of(parseJson(jsonBody).get()); - } - - private Optional parseJson(Optional jsonBody) { - Optional stringEntity = Optional.empty(); - try { - stringEntity = Optional.of(new StringEntity(jsonBody.get())); - } catch (UnsupportedEncodingException e) { - logger.warn("Exception while parsing JSON: ", e); - } - return stringEntity; - } - - private Optional createRequest(ConsumerDmaapModel consumerDmaapModel) { - Optional request = Optional.empty(); - final URI extendedURI = createDmaapPublisherExtendedURI(); - - if ("application/json".equals(dmaapContentType)) { - request = Optional.ofNullable(createRequest(extendedURI, consumerDmaapModel)); - request.get().addHeader("Content-type", dmaapContentType); - } - - return request; - } - - private URI createDmaapPublisherExtendedURI() { - URI extendedURI = null; - final URIBuilder uriBuilder = new URIBuilder() - .setScheme(dmaapProtocol) - .setHost(dmaapHostName) - .setPort(dmaapPortNumber) - .setPath(dmaapTopicName); - try { - extendedURI = uriBuilder.build(); - logger.trace("Building extended URI: {}", extendedURI); - } catch (URISyntaxException e) { - logger.warn("Exception while building extended URI: ", e); - } - return extendedURI; - } - - private HttpRequestBase createRequest(URI extendedURI, ConsumerDmaapModel consumerDmaapModel) { - if (extendedURI != null) { - return createHttpPost(extendedURI, Optional.ofNullable(CommonFunctions.createJsonBody(consumerDmaapModel))); - } else { - return null; - } - } - - private HttpPost createHttpPost(URI extendedURI, Optional jsonBody) { - HttpPost post = new HttpPost(extendedURI); - Optional stringEntity = createStringEntity(jsonBody); - post.setEntity(stringEntity.get()); - return post; - } - - private ResponseHandler> dmaapProducerResponseHandler() { - return httpResponse -> { - final int responseCode = httpResponse.getStatusLine().getStatusCode(); - final HttpEntity responseEntity = httpResponse.getEntity(); - - if (HttpUtils.isSuccessfulResponseCode(responseCode)) { - logger.trace("HTTP response successful."); - return Optional.of("" + responseCode); - } else { - String response = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; - logger.warn("HTTP response not successful : {}", response); - return Optional.of("" + responseCode); - } - }; - } -} diff --git a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/ExtendedDmaapConsumerHttpClientImplTest.java b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/ExtendedDmaapConsumerHttpClientImplTest.java new file mode 100644 index 00000000..39c10de5 --- /dev/null +++ b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/ExtendedDmaapConsumerHttpClientImplTest.java @@ -0,0 +1,96 @@ +/*- + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.services.prh.service.consumer; + +import org.apache.http.client.ResponseHandler; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.Optional; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + + +public class ExtendedDmaapConsumerHttpClientImplTest { + + private static ExtendedDmaapConsumerHttpClientImpl objectUnderTest; + + private static DmaapConsumerConfiguration configurationMock = mock(DmaapConsumerConfiguration.class); + private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class); + + private static final String JSON_MESSAGE = "{ \"responseFromDmaap\": \"Success\" }"; + + private static Optional expectedResult = Optional.empty(); + + @BeforeAll + public static void init() throws NoSuchFieldException, IllegalAccessException { + + when(configurationMock.dmaapHostName()).thenReturn("54.45.33.2"); + when(configurationMock.dmaapProtocol()).thenReturn("https"); + when(configurationMock.dmaapPortNumber()).thenReturn(1234); + when(configurationMock.dmaapUserName()).thenReturn("PRH"); + when(configurationMock.dmaapUserPassword()).thenReturn("PRH"); + when(configurationMock.dmaapContentType()).thenReturn("application/json"); + when(configurationMock.dmaapTopicName()).thenReturn("unauthenticated.SEC_OTHER_OUTPUT"); + when(configurationMock.consumerGroup()).thenReturn("OpenDCAE-c12"); + when(configurationMock.consumerId()).thenReturn("c12"); + + objectUnderTest = new ExtendedDmaapConsumerHttpClientImpl(configurationMock); + + setField(); + } + + + @Test + public void getHttpResponseGet_success() throws IOException { + expectedResult = Optional.of(JSON_MESSAGE); + + when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))) + .thenReturn(expectedResult); + + Optional actualResult = objectUnderTest.getHttpConsumerResponse(); + + Assertions.assertEquals(expectedResult.get(), actualResult.get()); + } + + @Test + public void getExtendedDetails_returnsNull() throws IOException { + when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))). + thenReturn(Optional.empty()); + Optional actualResult = objectUnderTest.getHttpConsumerResponse(); + Assertions.assertEquals(Optional.empty(),actualResult); + } + + + private static void setField() throws NoSuchFieldException, IllegalAccessException { + Field field = objectUnderTest.getClass().getDeclaredField("closeableHttpClient"); + field.setAccessible(true); + field.set(objectUnderTest, closeableHttpClientMock); + } +} diff --git a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImplTest.java b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImplTest.java new file mode 100644 index 00000000..3cb84206 --- /dev/null +++ b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImplTest.java @@ -0,0 +1,98 @@ +/*- + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.services.prh.service.producer; + +import org.apache.http.client.ResponseHandler; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.impl.client.CloseableHttpClient; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration; +import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; +import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModelForUnitTest; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.Optional; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + + +public class ExtendedDmaapProducerHttpClientImplTest { + + private static ExtendedDmaapProducerHttpClientImpl objectUnderTest; + + private static DmaapPublisherConfiguration configurationMock = mock(DmaapPublisherConfiguration.class); + private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class); + private static ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest(); + + private static Optional expectedResult = Optional.empty(); + private static final String RESPONSE_SUCCESS = "200"; + private static final String RESPONSE_FAILURE = "404"; + + @BeforeAll + public static void init() throws NoSuchFieldException, IllegalAccessException { + + when(configurationMock.dmaapHostName()).thenReturn("54.45.33.2"); + when(configurationMock.dmaapProtocol()).thenReturn("https"); + when(configurationMock.dmaapPortNumber()).thenReturn(1234); + when(configurationMock.dmaapUserName()).thenReturn("PRH"); + when(configurationMock.dmaapUserPassword()).thenReturn("PRH"); + when(configurationMock.dmaapContentType()).thenReturn("application/json"); + when(configurationMock.dmaapTopicName()).thenReturn("pnfReady"); + + objectUnderTest = new ExtendedDmaapProducerHttpClientImpl(configurationMock); + + setField(); + } + + + @Test + public void getHttpResponsePost_success() throws IOException { + expectedResult = Optional.of(RESPONSE_SUCCESS); + + when(closeableHttpClientMock.execute(any(HttpPost.class), any(ResponseHandler.class))) + .thenReturn(expectedResult); + + Optional actualResult = objectUnderTest.getHttpProducerResponse(consumerDmaapModel); + + Assertions.assertEquals(expectedResult.get(), actualResult.get()); + } + + @Test + public void getExtendedDetails_returnsFailure() throws IOException { + expectedResult = Optional.of(RESPONSE_FAILURE); + when(closeableHttpClientMock.execute(any(HttpPost.class), any(ResponseHandler.class))) + .thenReturn(Optional.empty()); + Optional actualResult = objectUnderTest.getHttpProducerResponse(consumerDmaapModel); + Assertions.assertEquals(Optional.empty(), actualResult); + } + + + private static void setField() throws NoSuchFieldException, IllegalAccessException { + Field field = objectUnderTest.getClass().getDeclaredField("closeableHttpClient"); + field.setAccessible(true); + field.set(objectUnderTest, closeableHttpClientMock); + } +} diff --git a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/service/consumer/ExtendedDmaapConsumerHttpClientImplTest.java b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/service/consumer/ExtendedDmaapConsumerHttpClientImplTest.java deleted file mode 100644 index 1df025ca..00000000 --- a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/service/consumer/ExtendedDmaapConsumerHttpClientImplTest.java +++ /dev/null @@ -1,96 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.dcaegen2.services.service.consumer; - -import org.apache.http.client.ResponseHandler; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.CloseableHttpClient; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.onap.dcaegen2.services.config.DmaapConsumerConfiguration; - -import java.io.IOException; -import java.lang.reflect.Field; -import java.util.Optional; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - - -public class ExtendedDmaapConsumerHttpClientImplTest { - - private static ExtendedDmaapConsumerHttpClientImpl objectUnderTest; - - private static DmaapConsumerConfiguration configurationMock = mock(DmaapConsumerConfiguration.class); - private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class); - - private static final String JSON_MESSAGE = "{ \"responseFromDmaap\": \"Success\" }"; - - private static Optional expectedResult = Optional.empty(); - - @BeforeAll - public static void init() throws NoSuchFieldException, IllegalAccessException { - - when(configurationMock.dmaapHostName()).thenReturn("54.45.33.2"); - when(configurationMock.dmaapProtocol()).thenReturn("https"); - when(configurationMock.dmaapPortNumber()).thenReturn(1234); - when(configurationMock.dmaapUserName()).thenReturn("PRH"); - when(configurationMock.dmaapUserPassword()).thenReturn("PRH"); - when(configurationMock.dmaapContentType()).thenReturn("application/json"); - when(configurationMock.dmaapTopicName()).thenReturn("unauthenticated.SEC_OTHER_OUTPUT"); - when(configurationMock.consumerGroup()).thenReturn("OpenDCAE-c12"); - when(configurationMock.consumerId()).thenReturn("c12"); - - objectUnderTest = new ExtendedDmaapConsumerHttpClientImpl(configurationMock); - - setField(); - } - - - @Test - public void getHttpResponseGet_success() throws IOException { - expectedResult = Optional.of(JSON_MESSAGE); - - when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))) - .thenReturn(expectedResult); - - Optional actualResult = objectUnderTest.getHttpConsumerResponse(); - - Assertions.assertEquals(expectedResult.get(), actualResult.get()); - } - - @Test - public void getExtendedDetails_returnsNull() throws IOException { - when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))). - thenReturn(Optional.empty()); - Optional actualResult = objectUnderTest.getHttpConsumerResponse(); - Assertions.assertEquals(Optional.empty(),actualResult); - } - - - private static void setField() throws NoSuchFieldException, IllegalAccessException { - Field field = objectUnderTest.getClass().getDeclaredField("closeableHttpClient"); - field.setAccessible(true); - field.set(objectUnderTest, closeableHttpClientMock); - } -} diff --git a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImplTest.java b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImplTest.java deleted file mode 100644 index 6e7d81f6..00000000 --- a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImplTest.java +++ /dev/null @@ -1,98 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.dcaegen2.services.service.producer; - -import org.apache.http.client.ResponseHandler; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.impl.client.CloseableHttpClient; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.onap.dcaegen2.services.config.DmaapPublisherConfiguration; -import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; -import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModelForUnitTest; - -import java.io.IOException; -import java.lang.reflect.Field; -import java.util.Optional; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - - -public class ExtendedDmaapProducerHttpClientImplTest { - - private static ExtendedDmaapProducerHttpClientImpl objectUnderTest; - - private static DmaapPublisherConfiguration configurationMock = mock(DmaapPublisherConfiguration.class); - private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class); - private static ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest(); - - private static Optional expectedResult = Optional.empty(); - private static final String RESPONSE_SUCCESS = "200"; - private static final String RESPONSE_FAILURE = "404"; - - @BeforeAll - public static void init() throws NoSuchFieldException, IllegalAccessException { - - when(configurationMock.dmaapHostName()).thenReturn("54.45.33.2"); - when(configurationMock.dmaapProtocol()).thenReturn("https"); - when(configurationMock.dmaapPortNumber()).thenReturn(1234); - when(configurationMock.dmaapUserName()).thenReturn("PRH"); - when(configurationMock.dmaapUserPassword()).thenReturn("PRH"); - when(configurationMock.dmaapContentType()).thenReturn("application/json"); - when(configurationMock.dmaapTopicName()).thenReturn("pnfReady"); - - objectUnderTest = new ExtendedDmaapProducerHttpClientImpl(configurationMock); - - setField(); - } - - - @Test - public void getHttpResponsePost_success() throws IOException { - expectedResult = Optional.of(RESPONSE_SUCCESS); - - when(closeableHttpClientMock.execute(any(HttpPost.class), any(ResponseHandler.class))) - .thenReturn(expectedResult); - - Optional actualResult = objectUnderTest.getHttpProducerResponse(consumerDmaapModel); - - Assertions.assertEquals(expectedResult.get(), actualResult.get()); - } - - @Test - public void getExtendedDetails_returnsFailure() throws IOException { - expectedResult = Optional.of(RESPONSE_FAILURE); - when(closeableHttpClientMock.execute(any(HttpPost.class), any(ResponseHandler.class))) - .thenReturn(Optional.empty()); - Optional actualResult = objectUnderTest.getHttpProducerResponse(consumerDmaapModel); - Assertions.assertEquals(Optional.empty(), actualResult); - } - - - private static void setField() throws NoSuchFieldException, IllegalAccessException { - Field field = objectUnderTest.getClass().getDeclaredField("closeableHttpClient"); - field.setAccessible(true); - field.set(objectUnderTest, closeableHttpClientMock); - } -} -- cgit 1.2.3-korg