From 5e4eb38ae737abd2054108b99bbd7b3649ec77d1 Mon Sep 17 00:00:00 2001 From: ramverma Date: Tue, 11 Sep 2018 12:38:03 +0100 Subject: Fix all sonar issues in distribution * Fixing all sonar issues in distribution * Adding more test cases to improve code coverage Change-Id: I296f96b66046e25c3c462632091f40565ff75478 Issue-ID: POLICY-1035 Signed-off-by: ramverma --- .../handling/sdc/SdcReceptionHandler.java | 54 ++-- ...eptionHandlerConfigurationParameterBuilder.java | 337 +++++++++++++++++++++ ...eceptionHandlerConfigurationParameterGroup.java | 160 ++-------- .../handling/sdc/TestSdcConfiguration.java | 88 ------ ...eceptionHandlerConfigurationParameterGroup.java | 159 ++++++++++ .../src/test/resources/handling-sdcInvalid.json | 2 +- 6 files changed, 548 insertions(+), 252 deletions(-) create mode 100644 plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandlerConfigurationParameterBuilder.java delete mode 100644 plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcConfiguration.java create mode 100644 plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcReceptionHandlerConfigurationParameterGroup.java (limited to 'plugins') diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandler.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandler.java index 0601f4b2..14b0f1ec 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandler.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandler.java @@ -66,7 +66,7 @@ public class SdcReceptionHandler extends AbstractReceptionHandler implements INo @Override protected void initializeReception(final String parameterGroupName) throws PluginInitializationException { - handlerParameters = (SdcReceptionHandlerConfigurationParameterGroup) ParameterService.get(parameterGroupName); + handlerParameters = ParameterService.get(parameterGroupName); initializeSdcClient(); startSdcClient(); } @@ -108,12 +108,7 @@ public class SdcReceptionHandler extends AbstractReceptionHandler implements INo sdcReceptionHandlerStatus = newStatus; break; case IDLE: - if (nbOfNotificationsOngoing > 1) { - --nbOfNotificationsOngoing; - } else { - nbOfNotificationsOngoing = 0; - sdcReceptionHandlerStatus = newStatus; - } + handleIdleStatusChange(newStatus); break; case BUSY: ++nbOfNotificationsOngoing; @@ -286,27 +281,24 @@ public class SdcReceptionHandler extends AbstractReceptionHandler implements INo private void sendDistributionStatus(final DistributionStatusType statusType, final String artifactUrl, final String distributionId, final DistributionStatusEnum status, final String errorReason) { - IDistributionClientResult clientResult = null; + IDistributionClientResult clientResult; final DistributionStatusMessageBuilder messageBuilder = new DistributionStatusMessageBuilder() .setArtifactUrl(artifactUrl).setConsumerId(sdcConfig.getConsumerID()).setDistributionId(distributionId) .setDistributionStatus(status).setTimestamp(System.currentTimeMillis()); final IDistributionStatusMessage message = new DistributionStatusMessage(messageBuilder); - switch (statusType) { - case DOWNLOAD: - if (errorReason != null) { - clientResult = distributionClient.sendDownloadStatus(message, errorReason); - } else { - clientResult = distributionClient.sendDownloadStatus(message); - } - break; - case DEPLOY: - if (errorReason != null) { - clientResult = distributionClient.sendDeploymentStatus(message, errorReason); - } else { - clientResult = distributionClient.sendDeploymentStatus(message); - } + if (DistributionStatusType.DOWNLOAD.equals(statusType)) { + if (errorReason != null) { + clientResult = distributionClient.sendDownloadStatus(message, errorReason); + } else { + clientResult = distributionClient.sendDownloadStatus(message); + } + } else { + if (errorReason != null) { + clientResult = distributionClient.sendDeploymentStatus(message, errorReason); + } else { + clientResult = distributionClient.sendDeploymentStatus(message); + } } - final StringBuilder loggerMessage = new StringBuilder(); loggerMessage.append("distribution status to SDC with values - ").append("DistributionId") .append(distributionId).append(" Artifact: ").append(artifactUrl).append(" StatusType: ") @@ -332,7 +324,7 @@ public class SdcReceptionHandler extends AbstractReceptionHandler implements INo */ private void sendComponentDoneStatus(final String distributionId, final DistributionStatusEnum status, final String errorReason) { - IDistributionClientResult clientResult = null; + IDistributionClientResult clientResult; final ComponentDoneStatusMessageBuilder messageBuilder = new ComponentDoneStatusMessageBuilder() .setConsumerId(sdcConfig.getConsumerID()).setDistributionId(distributionId) .setDistributionStatus(status).setTimestamp(System.currentTimeMillis()); @@ -357,4 +349,18 @@ public class SdcReceptionHandler extends AbstractReceptionHandler implements INo LOGGER.debug(loggerMessage); } } + + /** + * Handle the status change of {@link SdcReceptionHandler} to Idle. + * + * @param newStatus the new status + */ + private void handleIdleStatusChange(final SdcReceptionHandlerStatus newStatus) { + if (nbOfNotificationsOngoing > 1) { + --nbOfNotificationsOngoing; + } else { + nbOfNotificationsOngoing = 0; + sdcReceptionHandlerStatus = newStatus; + } + } } diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandlerConfigurationParameterBuilder.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandlerConfigurationParameterBuilder.java new file mode 100644 index 00000000..34eaf33e --- /dev/null +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandlerConfigurationParameterBuilder.java @@ -0,0 +1,337 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.distribution.reception.handling.sdc; + +import java.util.List; + +/** + * This class builds an instance of {@link SdcReceptionHandlerConfigurationParameterGroup} class. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class SdcReceptionHandlerConfigurationParameterBuilder { + + private boolean activeserverTlsAuth; + private boolean filterinEmptyResources; + private boolean useHttpsWithDmaap; + private int pollingTimeout; + private int pollingInterval; + private String user; + private String password; + private String consumerId; + private String consumerGroup; + private String asdcAddress; + private String environmentName; + private String keystorePath; + private String keystorePassword; + private List messageBusAddress; + private List artifactTypes; + + /** + * Set activeserverTlsAuth to this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @param activeserverTlsAuth the activeserverTlsAuth + */ + public SdcReceptionHandlerConfigurationParameterBuilder setActiveserverTlsAuth(final boolean activeserverTlsAuth) { + this.activeserverTlsAuth = activeserverTlsAuth; + return this; + } + + /** + * Set filterinEmptyResources to this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @param filterinEmptyResources the filterinEmptyResources + */ + public SdcReceptionHandlerConfigurationParameterBuilder setFilterinEmptyResources( + final boolean filterinEmptyResources) { + this.filterinEmptyResources = filterinEmptyResources; + return this; + } + + /** + * Set useHttpsWithDmaap to this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @param useHttpsWithDmaap the useHttpsWithDmaap + */ + public SdcReceptionHandlerConfigurationParameterBuilder setUseHttpsWithDmaap(final Boolean useHttpsWithDmaap) { + this.useHttpsWithDmaap = useHttpsWithDmaap; + return this; + } + + /** + * Set pollingInterval to this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @param pollingInterval the pollingInterval + */ + public SdcReceptionHandlerConfigurationParameterBuilder setPollingInterval(final int pollingInterval) { + this.pollingInterval = pollingInterval; + return this; + } + + /** + * Set pollingTimeout to this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @param pollingTimeout the pollingTimeout + */ + public SdcReceptionHandlerConfigurationParameterBuilder setPollingTimeout(final int pollingTimeout) { + this.pollingTimeout = pollingTimeout; + return this; + } + + /** + * Set asdcAddress to this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @param asdcAddress the asdcAddress + */ + public SdcReceptionHandlerConfigurationParameterBuilder setAsdcAddress(final String asdcAddress) { + this.asdcAddress = asdcAddress; + return this; + } + + /** + * Set user to this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @param user the user + */ + public SdcReceptionHandlerConfigurationParameterBuilder setUser(final String user) { + this.user = user; + return this; + } + + /** + * Set password to this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @param password the password + */ + public SdcReceptionHandlerConfigurationParameterBuilder setPassword(final String password) { + this.password = password; + return this; + } + + /** + * Set consumerId to this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @param consumerId the consumerId + */ + public SdcReceptionHandlerConfigurationParameterBuilder setConsumerId(final String consumerId) { + this.consumerId = consumerId; + return this; + } + + /** + * Set consumerGroup to this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @param consumerGroup the consumerGroup + */ + public SdcReceptionHandlerConfigurationParameterBuilder setConsumerGroup(final String consumerGroup) { + this.consumerGroup = consumerGroup; + return this; + } + + /** + * Set environmentName to this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @param environmentName the environmentName + */ + public SdcReceptionHandlerConfigurationParameterBuilder setEnvironmentName(final String environmentName) { + this.environmentName = environmentName; + return this; + } + + /** + * Set keystorePath to this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @param keystorePath the keystorePath + */ + public SdcReceptionHandlerConfigurationParameterBuilder setKeystorePath(final String keystorePath) { + this.keystorePath = keystorePath; + return this; + } + + /** + * Set keystorePassword to this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @param keystorePassword the keystorePassword + */ + public SdcReceptionHandlerConfigurationParameterBuilder setKeystorePassword(final String keystorePassword) { + this.keystorePassword = keystorePassword; + return this; + } + + /** + * Set messageBusAddress to this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @param messageBusAddress the messageBusAddress + */ + public SdcReceptionHandlerConfigurationParameterBuilder setMessageBusAddress(final List messageBusAddress) { + this.messageBusAddress = messageBusAddress; + return this; + } + + /** + * Set artifactTypes to this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @param artifactTypes the artifactTypes + */ + public SdcReceptionHandlerConfigurationParameterBuilder setArtifactTypes(final List artifactTypes) { + this.artifactTypes = artifactTypes; + return this; + } + + /** + * Returns the active server TlsAuth of this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @return the activeserverTlsAuth + */ + public boolean isActiveserverTlsAuth() { + return activeserverTlsAuth; + } + + /** + * Returns the isFilterinEmptyResources flag of this {@link SdcReceptionHandlerConfigurationParameterBuilder} + * instance. + * + * @return the isFilterinEmptyResources + */ + public boolean isFilterinEmptyResources() { + return filterinEmptyResources; + } + + /** + * Returns the isUseHttpsWithDmaap flag of this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @return the isUseHttpsWithDmaap + */ + public Boolean getIsUseHttpsWithDmaap() { + return useHttpsWithDmaap; + } + + /** + * Returns the polling interval of this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @return the pollingInterval + */ + public int getPollingInterval() { + return pollingInterval; + } + + /** + * Returns the polling timeout of this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @return the pollingTimeout + */ + public int getPollingTimeout() { + return pollingTimeout; + } + + /** + * Returns the asdc address of this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @return the asdcAddress + */ + public String getAsdcAddress() { + return asdcAddress; + } + + /** + * Returns the user of this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @return the user + */ + public String getUser() { + return user; + } + + /** + * Returns the password of this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @return the password + */ + public String getPassword() { + return password; + } + + /** + * Returns the consumer id of this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @return the consumerId + */ + public String getConsumerId() { + return consumerId; + } + + /** + * Returns the consumer group of this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @return the consumerGroup + */ + public String getConsumerGroup() { + return consumerGroup; + } + + /** + * Returns the environment name of this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @return the environmentName + */ + public String getEnvironmentName() { + return environmentName; + } + + /** + * Returns the keystore path of this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @return the keystorePath + */ + public String getKeystorePath() { + return keystorePath; + } + + /** + * Returns the keystore password of this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @return the keystorePassword + */ + public String getKeystorePassword() { + return keystorePassword; + } + + /** + * Returns the message bus address of this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @return the messageBusAddress + */ + public List getMessageBusAddress() { + return messageBusAddress; + } + + /** + * Returns the artifact types of this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance. + * + * @return the artifactTypes + */ + public List getArtifactTypes() { + return artifactTypes; + } + +} + + diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandlerConfigurationParameterGroup.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandlerConfigurationParameterGroup.java index 08fd707c..bbee652a 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandlerConfigurationParameterGroup.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandlerConfigurationParameterGroup.java @@ -34,7 +34,6 @@ import org.onap.policy.distribution.reception.parameters.ReceptionHandlerConfigu */ public class SdcReceptionHandlerConfigurationParameterGroup extends ReceptionHandlerConfigurationParameterGroup { - // Interface of IConfiguration item private String asdcAddress; private List messageBusAddress; private String user; @@ -56,22 +55,23 @@ public class SdcReceptionHandlerConfigurationParameterGroup extends ReceptionHan * * @param builder the SDC configuration builder */ - private SdcReceptionHandlerConfigurationParameterGroup(final SdcConfigurationBuilder builder) { - asdcAddress = builder.asdcAddress; - messageBusAddress = builder.messageBusAddress; - user = builder.user; - password = builder.password; - pollingInterval = builder.pollingInterval; - pollingTimeout = builder.pollingTimeout; - consumerId = builder.consumerId; - artifactTypes = builder.artifactTypes; - consumerGroup = builder.consumerGroup; - environmentName = builder.environmentName; - keyStorePath = builder.keystorePath; - keyStorePassword = builder.keystorePassword; - activeServerTlsAuth = builder.activeserverTlsAuth; - isFilterInEmptyResources = builder.isFilterinEmptyResources; - isUseHttpsWithDmaap = builder.isUseHttpsWithDmaap; + public SdcReceptionHandlerConfigurationParameterGroup( + final SdcReceptionHandlerConfigurationParameterBuilder builder) { + asdcAddress = builder.getAsdcAddress(); + messageBusAddress = builder.getMessageBusAddress(); + user = builder.getUser(); + password = builder.getPassword(); + pollingInterval = builder.getPollingInterval(); + pollingTimeout = builder.getPollingTimeout(); + consumerId = builder.getConsumerId(); + artifactTypes = builder.getArtifactTypes(); + consumerGroup = builder.getConsumerGroup(); + environmentName = builder.getEnvironmentName(); + keyStorePath = builder.getKeystorePath(); + keyStorePassword = builder.getKeystorePassword(); + activeServerTlsAuth = builder.isActiveserverTlsAuth(); + isFilterInEmptyResources = builder.isFilterinEmptyResources(); + isUseHttpsWithDmaap = builder.getIsUseHttpsWithDmaap(); } @@ -145,119 +145,6 @@ public class SdcReceptionHandlerConfigurationParameterGroup extends ReceptionHan super.setName(name + "_" + UUID.randomUUID().toString()); } - /** - * Inner static class is to used as a Builder. - * - */ - public static class SdcConfigurationBuilder { - private String asdcAddress; - private List messageBusAddress; - private String user; - private String password; - private int pollingInterval; - private int pollingTimeout; - private String consumerId; - private List artifactTypes; - private String consumerGroup; - private String environmentName; - private String keystorePath; - private String keystorePassword; - private boolean activeserverTlsAuth; - private boolean isFilterinEmptyResources; - private Boolean isUseHttpsWithDmaap; - - public SdcConfigurationBuilder setAsdcAddress(final String val) { - asdcAddress = val; - return this; - } - - public SdcConfigurationBuilder setMessageBusAddress(final List val) { - messageBusAddress = val; - return this; - } - - public SdcConfigurationBuilder setUser(final String val) { - user = val; - return this; - } - - public SdcConfigurationBuilder setPassword(final String val) { - password = val; - return this; - } - - public SdcConfigurationBuilder setPollingInterval(final int val) { - pollingInterval = val; - return this; - } - - public SdcConfigurationBuilder setPollingTimeout(final int val) { - pollingTimeout = val; - return this; - } - - public SdcConfigurationBuilder setConsumerId(final String val) { - consumerId = val; - return this; - } - - public SdcConfigurationBuilder setArtifactTypes(final List val) { - artifactTypes = val; - return this; - } - - public SdcConfigurationBuilder setConsumerGroup(final String val) { - consumerGroup = val; - return this; - } - - public SdcConfigurationBuilder setEnvironmentName(final String val) { - environmentName = val; - return this; - } - - public SdcConfigurationBuilder setKeystorePath(final String val) { - keystorePath = val; - return this; - } - - public SdcConfigurationBuilder setKeystorePassword(final String val) { - keystorePassword = val; - return this; - } - - public SdcConfigurationBuilder setActiveserverTlsAuth(final boolean val) { - activeserverTlsAuth = val; - return this; - } - - public SdcConfigurationBuilder setIsFilterinEmptyResources(final boolean val) { - isFilterinEmptyResources = val; - return this; - } - - public SdcConfigurationBuilder setIsUseHttpsWithDmaap(final Boolean val) { - isUseHttpsWithDmaap = val; - return this; - } - - /** - * Creates an instance of {@link SdcReceptionHandlerConfigurationParameterGroup}. - */ - public SdcReceptionHandlerConfigurationParameterGroup build() { - return new SdcReceptionHandlerConfigurationParameterGroup(this); - } - } - - /** - * {@inheritDoc} - */ - @Override - public String toString() { - return "name =" + getName() + ",TestParameters:[asdcAddress = " + asdcAddress + ", messageBusAddress = " - + messageBusAddress + ", user = " + user + "]"; - } - /** * {@inheritDoc} */ @@ -301,15 +188,10 @@ public class SdcReceptionHandlerConfigurationParameterGroup extends ReceptionHan */ private void validateStringListElement(final GroupValidationResult validationResult, final List element, final String elementName) { - if (element == null) { - validationResult.setResult(elementName, ValidationStatus.INVALID, - elementName + " must be a list of non-blank string"); - } else { - for (final String temp : element) { - if (!ParameterValidationUtils.validateStringParameter(temp)) { - validationResult.setResult(elementName, ValidationStatus.INVALID, - "the string of " + elementName + "must be a non-blank string"); - } + for (final String temp : element) { + if (!ParameterValidationUtils.validateStringParameter(temp)) { + validationResult.setResult(elementName, ValidationStatus.INVALID, + "the string of " + elementName + "must be a non-blank string"); } } } diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcConfiguration.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcConfiguration.java deleted file mode 100644 index 65d7ca10..00000000 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcConfiguration.java +++ /dev/null @@ -1,88 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Intel. 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.distribution.reception.handling.sdc; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - -import java.io.FileReader; -import java.io.IOException; -import java.util.Arrays; - -import org.junit.Test; -import org.onap.policy.common.parameters.GroupValidationResult; - -/** - * Class to perform unit test of {@link SdcConfiguration}. - * - */ -public class TestSdcConfiguration { - - @Test - public void testSdcConfiguration() throws IOException { - SdcReceptionHandlerConfigurationParameterGroup configParameters = null; - try { - final Gson gson = new GsonBuilder().create(); - configParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdc.json"), - SdcReceptionHandlerConfigurationParameterGroup.class); - } catch (final Exception e) { - fail("test should not thrown an exception here: " + e.getMessage()); - } - final GroupValidationResult validationResult = configParameters.validate(); - assertTrue(validationResult.isValid()); - final SdcConfiguration config = new SdcConfiguration(configParameters); - assertEquals(Arrays.asList("a.com", "b.com", "c.com"), config.getMsgBusAddress()); - assertEquals(Arrays.asList("TOSCA_CSAR", "HEAT"), config.getRelevantArtifactTypes()); - assertEquals("localhost", config.getAsdcAddress()); - assertEquals("policy", config.getUser()); - assertEquals("policy", config.getPassword()); - assertEquals(20, config.getPollingInterval()); - assertEquals(30, config.getPollingTimeout()); - assertEquals("policy-id", config.getConsumerID()); - assertEquals("policy-group", config.getConsumerGroup()); - assertEquals("TEST", config.getEnvironmentName()); - assertEquals("null", config.getKeyStorePath()); - assertEquals("null", config.getKeyStorePassword()); - assertEquals(false, config.activateServerTLSAuth()); - assertEquals(true, config.isFilterInEmptyResources()); - assertEquals(false, config.isUseHttpsWithDmaap()); - } - - @Test - public void testInvalidSdcConfiguration() throws IOException { - SdcReceptionHandlerConfigurationParameterGroup configParameters = null; - try { - final Gson gson = new GsonBuilder().create(); - configParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdcInvalid.json"), - SdcReceptionHandlerConfigurationParameterGroup.class); - } catch (final Exception e) { - fail("test should not thrown an exception here: " + e.getMessage()); - } - final GroupValidationResult validationResult = configParameters.validate(); - assertFalse(validationResult.isValid()); - - } -} diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcReceptionHandlerConfigurationParameterGroup.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcReceptionHandlerConfigurationParameterGroup.java new file mode 100644 index 00000000..df72d45c --- /dev/null +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcReceptionHandlerConfigurationParameterGroup.java @@ -0,0 +1,159 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Intel. 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.distribution.reception.handling.sdc; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import java.io.FileReader; +import java.io.IOException; +import java.util.Arrays; + +import org.junit.Test; +import org.onap.policy.common.parameters.GroupValidationResult; + +/** + * Class to perform unit test of {@link SdcConfiguration}. + * + */ +public class TestSdcReceptionHandlerConfigurationParameterGroup { + + @Test + public void testSdcConfiguration() throws IOException { + SdcReceptionHandlerConfigurationParameterGroup configParameters = null; + try { + final Gson gson = new GsonBuilder().create(); + configParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdc.json"), + SdcReceptionHandlerConfigurationParameterGroup.class); + } catch (final Exception e) { + fail("test should not thrown an exception here: " + e.getMessage()); + } + final GroupValidationResult validationResult = configParameters.validate(); + assertTrue(validationResult.isValid()); + final SdcConfiguration config = new SdcConfiguration(configParameters); + assertEquals(Arrays.asList("a.com", "b.com", "c.com"), config.getMsgBusAddress()); + assertEquals(Arrays.asList("TOSCA_CSAR", "HEAT"), config.getRelevantArtifactTypes()); + assertEquals("localhost", config.getAsdcAddress()); + assertEquals("policy", config.getUser()); + assertEquals("policy", config.getPassword()); + assertEquals(20, config.getPollingInterval()); + assertEquals(30, config.getPollingTimeout()); + assertEquals("policy-id", config.getConsumerID()); + assertEquals("policy-group", config.getConsumerGroup()); + assertEquals("TEST", config.getEnvironmentName()); + assertEquals("null", config.getKeyStorePath()); + assertEquals("null", config.getKeyStorePassword()); + assertEquals(false, config.activateServerTLSAuth()); + assertEquals(true, config.isFilterInEmptyResources()); + assertEquals(false, config.isUseHttpsWithDmaap()); + } + + @Test + public void testInvalidSdcConfiguration() throws IOException { + SdcReceptionHandlerConfigurationParameterGroup configParameters = null; + try { + final Gson gson = new GsonBuilder().create(); + configParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdcInvalid.json"), + SdcReceptionHandlerConfigurationParameterGroup.class); + } catch (final Exception e) { + fail("test should not thrown an exception here: " + e.getMessage()); + } + final GroupValidationResult validationResult = configParameters.validate(); + assertFalse(validationResult.isValid()); + } + + @Test + public void testSdcConfigurationBuilder() { + + final SdcReceptionHandlerConfigurationParameterBuilder builder = + new SdcReceptionHandlerConfigurationParameterBuilder().setAsdcAddress("localhost") + .setConsumerGroup("policy-group").setConsumerId("policy-id").setEnvironmentName("TEST") + .setKeystorePassword("password").setKeystorePath("dummyPath").setPassword("policy") + .setPollingInterval(10).setPollingTimeout(20).setUser("policy").setUseHttpsWithDmaap(false) + .setActiveserverTlsAuth(false).setFilterinEmptyResources(true) + .setArtifactTypes(Arrays.asList("TOSCA_CSAR")).setMessageBusAddress(Arrays.asList("localhost")); + final SdcReceptionHandlerConfigurationParameterGroup configParameters = + new SdcReceptionHandlerConfigurationParameterGroup(builder); + configParameters.setName("SDCConfiguration"); + + assertEquals(Arrays.asList("localhost"), configParameters.getMessageBusAddress()); + assertEquals(Arrays.asList("TOSCA_CSAR"), configParameters.getArtifactTypes()); + assertEquals("localhost", configParameters.getAsdcAddress()); + assertEquals("policy", configParameters.getUser()); + assertEquals("policy", configParameters.getPassword()); + assertEquals(10, configParameters.getPollingInterval()); + assertEquals(20, configParameters.getPollingTimeout()); + assertEquals("policy-id", configParameters.getConsumerId()); + assertEquals("policy-group", configParameters.getConsumerGroup()); + assertEquals("TEST", configParameters.getEnvironmentName()); + assertEquals("dummyPath", configParameters.getKeyStorePath()); + assertEquals("password", configParameters.getKeyStorePassword()); + assertEquals(false, configParameters.isActiveServerTlsAuth()); + assertEquals(true, configParameters.isFilterInEmptyResources()); + assertEquals(false, configParameters.isUseHttpsWithDmaap()); + } + + @Test + public void testSdcConfigurationWithNullList() { + + final SdcReceptionHandlerConfigurationParameterBuilder builder = + new SdcReceptionHandlerConfigurationParameterBuilder().setAsdcAddress("localhost") + .setConsumerGroup("policy-group").setConsumerId("policy-id").setEnvironmentName("TEST") + .setKeystorePassword("password").setKeystorePath("dummyPath").setPassword("policy") + .setPollingInterval(10).setPollingTimeout(20).setUser("policy").setUseHttpsWithDmaap(false) + .setActiveserverTlsAuth(false).setFilterinEmptyResources(true) + .setArtifactTypes(Arrays.asList("TOSCA_CSAR")).setMessageBusAddress(null); + final SdcReceptionHandlerConfigurationParameterGroup configParameters = + new SdcReceptionHandlerConfigurationParameterGroup(builder); + configParameters.setName("SDCConfiguration"); + + try { + configParameters.validate(); + fail("Test must throw an exception"); + } catch (final Exception exp) { + assertTrue(exp.getMessage().contains("collection parameter \"messageBusAddress\" is null")); + } + } + + @Test + public void testSdcConfigurationWithEmptyStringList() { + + final SdcReceptionHandlerConfigurationParameterBuilder builder = + new SdcReceptionHandlerConfigurationParameterBuilder().setAsdcAddress("localhost") + .setConsumerGroup("policy-group").setConsumerId("policy-id").setEnvironmentName("TEST") + .setKeystorePassword("password").setKeystorePath("dummyPath").setPassword("policy") + .setPollingInterval(10).setPollingTimeout(20).setUser("policy").setUseHttpsWithDmaap(false) + .setActiveserverTlsAuth(false).setFilterinEmptyResources(true) + .setArtifactTypes(Arrays.asList("")).setMessageBusAddress(Arrays.asList("localhost")); + final SdcReceptionHandlerConfigurationParameterGroup configParameters = + new SdcReceptionHandlerConfigurationParameterGroup(builder); + configParameters.setName("SDCConfiguration"); + + final GroupValidationResult validationResult = configParameters.validate(); + assertFalse(validationResult.isValid()); + assertTrue(validationResult.getResult().contains("must be a non-blank string")); + } +} diff --git a/plugins/reception-plugins/src/test/resources/handling-sdcInvalid.json b/plugins/reception-plugins/src/test/resources/handling-sdcInvalid.json index 112879f4..803fc412 100644 --- a/plugins/reception-plugins/src/test/resources/handling-sdcInvalid.json +++ b/plugins/reception-plugins/src/test/resources/handling-sdcInvalid.json @@ -1,6 +1,6 @@ { "name" : "parameterConfig1", - "asdcAddress": "localhost", + "asdcAddress": "", "messageBusAddress": [ "a.com", "b.com", -- cgit 1.2.3-korg