From ecc059f29f8da065356571ef00c6cad595e298b3 Mon Sep 17 00:00:00 2001 From: ramverma Date: Fri, 31 Aug 2018 17:16:01 +0100 Subject: Adding code for managing life cycle of SDC Client * Adding init/start/stop methods in SDCReceptionHandler for managing lifecycle of SDC Client. * Adding a handler status enum to hold all the possible status values. * Adding test cases to cover code changes fully. Change-Id: Ib6f370485ff330538bfada6030c592629ed3fd1c Issue-ID: POLICY-956 Signed-off-by: ramverma --- .../handling/sdc/PssdConfigurationTest.java | 75 --------- .../handling/sdc/TestSdcConfiguration.java | 89 ++++++++++ .../handling/sdc/TestSdcReceptionHandler.java | 180 +++++++++++++++++++++ .../src/test/resources/handling-sdc.json | 6 +- 4 files changed, 272 insertions(+), 78 deletions(-) delete mode 100644 plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/PssdConfigurationTest.java create 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/TestSdcReceptionHandler.java (limited to 'plugins/reception-plugins/src/test') diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/PssdConfigurationTest.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/PssdConfigurationTest.java deleted file mode 100644 index 5c24a792..00000000 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/PssdConfigurationTest.java +++ /dev/null @@ -1,75 +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 org.junit.Test; -import org.onap.policy.common.parameters.GroupValidationResult; -import org.onap.policy.distribution.reception.parameters.PssdConfigurationParametersGroup; - -/*- - * Tests for PssdConfiguration class - * - */ -public class PssdConfigurationTest { - - @Test - public void testPssdConfigurationParametersGroup() throws IOException { - PssdConfigurationParametersGroup configParameters = null; - try { - final Gson gson = new GsonBuilder().create(); - configParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdc.json"), - PssdConfigurationParametersGroup.class); - } catch (final Exception e) { - fail("test should not thrown an exception here: " + e.getMessage()); - } - final GroupValidationResult validationResult = configParameters.validate(); - assertTrue(validationResult.isValid()); - final PssdConfiguration config = new PssdConfiguration(configParameters); - assertEquals(20, config.getPollingInterval()); - assertEquals(30, config.getPollingTimeout()); - } - - @Test - public void testInvalidPssdConfigurationParametersGroup() throws IOException { - PssdConfigurationParametersGroup configParameters = null; - try { - final Gson gson = new GsonBuilder().create(); - configParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdcInvalid.json"), - PssdConfigurationParametersGroup.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/TestSdcConfiguration.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcConfiguration.java new file mode 100644 index 00000000..e1ba00cf --- /dev/null +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcConfiguration.java @@ -0,0 +1,89 @@ +/*- + * ============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; +import org.onap.policy.distribution.reception.parameters.PssdConfigurationParametersGroup; + +/** + * Class to perform unit test of {@link SdcConfiguration}. + * + */ +public class TestSdcConfiguration { + + @Test + public void testSdcConfiguration() throws IOException { + PssdConfigurationParametersGroup configParameters = null; + try { + final Gson gson = new GsonBuilder().create(); + configParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdc.json"), + PssdConfigurationParametersGroup.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 { + PssdConfigurationParametersGroup configParameters = null; + try { + final Gson gson = new GsonBuilder().create(); + configParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdcInvalid.json"), + PssdConfigurationParametersGroup.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/TestSdcReceptionHandler.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcReceptionHandler.java new file mode 100644 index 00000000..02b83849 --- /dev/null +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcReceptionHandler.java @@ -0,0 +1,180 @@ +/*- + * ============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.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.any; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import java.io.FileReader; +import java.io.IOException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; +import org.onap.policy.common.parameters.ParameterService; +import org.onap.policy.distribution.reception.decoding.PluginInitializationException; +import org.onap.policy.distribution.reception.decoding.PluginTerminationException; +import org.onap.policy.distribution.reception.parameters.PssdConfigurationParametersGroup; +import org.onap.sdc.api.results.IDistributionClientResult; +import org.onap.sdc.impl.mock.DistributionClientStubImpl; +import org.onap.sdc.utils.DistributionActionResultEnum; + +/** + * Class to perform unit test of {@link SdcReceptionHandler}. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +@RunWith(MockitoJUnitRunner.class) +public class TestSdcReceptionHandler { + + private static final Logger LOGGER = FlexLogger.getLogger(TestSdcReceptionHandler.class); + + @Mock + private IDistributionClientResult successfulClientInitResult; + @Mock + private IDistributionClientResult failureClientInitResult; + @Mock + private DistributionClientStubImpl distributionClient; + + private PssdConfigurationParametersGroup pssdConfigParameters; + private SdcReceptionHandler sypHandler; + + /** + * Setup for the test cases. + * + * @throws IOException if it occurs + */ + @Before + public final void init() throws IOException { + final Gson gson = new GsonBuilder().create(); + pssdConfigParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdc.json"), + PssdConfigurationParametersGroup.class); + ParameterService.register(pssdConfigParameters); + final SdcReceptionHandler sdcHandler = new SdcReceptionHandler(); + sypHandler = Mockito.spy(sdcHandler); + Mockito.when(sypHandler.createSdcDistributionClient()).thenReturn(distributionClient); + Mockito.when(distributionClient.init(any(), any())).thenReturn(successfulClientInitResult); + Mockito.when(distributionClient.start()).thenReturn(successfulClientInitResult); + Mockito.when(distributionClient.stop()).thenReturn(successfulClientInitResult); + Mockito.when(successfulClientInitResult.getDistributionActionResult()) + .thenReturn(DistributionActionResultEnum.SUCCESS); + } + + @After + public void teardown() { + ParameterService.deregister(pssdConfigParameters); + } + + @Test + public final void testInitializeSdcClient() { + try { + sypHandler.initializeReception(pssdConfigParameters.getName()); + } catch (final PluginInitializationException exp) { + LOGGER.error(exp); + fail("Test should not throw any exception"); + } + } + + @Test + public final void testInitializeSdcClient_Again() throws PluginInitializationException { + sypHandler.initializeReception(pssdConfigParameters.getName()); + try { + sypHandler.initializeReception(pssdConfigParameters.getName()); + fail("Test must throw an exception here"); + } catch (final Exception exp) { + assertTrue(exp.getMessage().startsWith("The SDC Client is already initialized")); + } + } + + @Test + public final void testInitializeSdcClient_Failure() throws PluginInitializationException { + + Mockito.when(successfulClientInitResult.getDistributionActionResult()) + .thenReturn(DistributionActionResultEnum.FAIL); + try { + sypHandler.initializeReception(pssdConfigParameters.getName()); + fail("Test must throw an exception here"); + } catch (final Exception exp) { + assertTrue(exp.getMessage().startsWith("SDC client initialization failed with reason")); + } + } + + @Test + public final void testStartSdcClient_Failure() throws PluginInitializationException { + try { + Mockito.when(distributionClient.start()).thenReturn(failureClientInitResult); + Mockito.when(failureClientInitResult.getDistributionActionResult()) + .thenReturn(DistributionActionResultEnum.FAIL); + sypHandler.initializeReception(pssdConfigParameters.getName()); + + fail("Test must throw an exception here"); + } catch (final Exception exp) { + assertTrue(exp.getMessage().startsWith("SDC client start failed with reason")); + } + } + + @Test + public final void testStopSdcClient() { + try { + sypHandler.initializeReception(pssdConfigParameters.getName()); + sypHandler.destroy(); + } catch (final PluginInitializationException | PluginTerminationException exp) { + LOGGER.error(exp); + fail("Test should not throw any exception"); + } + + } + + @Test + public final void testStopSdcClientWithoutStart() { + try { + sypHandler.destroy(); + } catch (final PluginTerminationException exp) { + LOGGER.error(exp); + fail("Test should not throw any exception"); + } + + } + + @Test + public final void testStopSdcClient_Failure() throws PluginInitializationException { + + sypHandler.initializeReception(pssdConfigParameters.getName()); + Mockito.when(successfulClientInitResult.getDistributionActionResult()) + .thenReturn(DistributionActionResultEnum.FAIL); + try { + sypHandler.destroy(); + fail("Test must throw an exception here"); + } catch (final Exception exp) { + assertTrue(exp.getMessage().startsWith("SDC client stop failed with reason")); + } + } +} diff --git a/plugins/reception-plugins/src/test/resources/handling-sdc.json b/plugins/reception-plugins/src/test/resources/handling-sdc.json index c1ca23aa..b9e63fe0 100644 --- a/plugins/reception-plugins/src/test/resources/handling-sdc.json +++ b/plugins/reception-plugins/src/test/resources/handling-sdc.json @@ -6,8 +6,8 @@ "b.com", "c.com" ], - "user": "tbdsdc-1480", - "password": "tbdsdc-1480", + "user": "policy", + "password": "policy", "pollingInterval":20, "pollingTimeout":30, "consumerId": "policy-id", @@ -16,7 +16,7 @@ "HEAT" ], "consumerGroup": "policy-group", - "environmentName": "environmentName", + "environmentName": "TEST", "keystorePath": "null", "keystorePassword": "null", "activeserverTlsAuth": false, -- cgit 1.2.3-korg