diff options
author | ramverma <ram.krishna.verma@ericsson.com> | 2018-09-05 17:19:10 +0100 |
---|---|---|
committer | ramverma <ram.krishna.verma@ericsson.com> | 2018-09-06 19:43:24 +0100 |
commit | 905324b15dd1a3e80397d514aa22873703d8f239 (patch) | |
tree | 0ff866aa2cd73b2505e699a44ee54234aa0fad7b /plugins/reception-plugins/src/test | |
parent | 9d3612f325ae52e41a0a6d9e794b42e98305d7ec (diff) |
Adding code for handling notifications from SDC
Change-Id: I45f28cbce8defead5e1f43ed81fa5ea6a2b2208b
Issue-ID: POLICY-974
Signed-off-by: ramverma <ram.krishna.verma@ericsson.com>
Diffstat (limited to 'plugins/reception-plugins/src/test')
-rw-r--r-- | plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyDecoder.java | 67 | ||||
-rw-r--r-- | plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyPolicy.java | 62 | ||||
-rw-r--r-- | plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyPolicyForwarder.java | 77 | ||||
-rw-r--r-- | plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcReceptionHandler.java | 127 | ||||
-rw-r--r-- | plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/ArtifactDownloadExceptionTest.java (renamed from plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/PssdDownloadExceptionTest.java) | 6 |
5 files changed, 336 insertions, 3 deletions
diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyDecoder.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyDecoder.java new file mode 100644 index 00000000..6a33e787 --- /dev/null +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyDecoder.java @@ -0,0 +1,67 @@ +/*- + * ============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.Arrays; +import java.util.Collection; + +import org.onap.policy.distribution.model.Csar; +import org.onap.policy.distribution.model.PolicyInput; +import org.onap.policy.distribution.reception.decoding.PolicyDecoder; +import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; + +/** + * Class to create a dummy decoder for test cases. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class DummyDecoder implements PolicyDecoder<Csar, DummyPolicy> { + + public static final String DUMMY_POLICY = "DummyPolicy"; + private DummyPolicy decodedPolicy; + + /** + * {@inheritDoc}. + */ + @Override + public boolean canHandle(final PolicyInput policyInput) { + return policyInput.getClass().isAssignableFrom(Csar.class); + } + + /** + * {@inheritDoc}. + */ + @Override + public Collection<DummyPolicy> decode(final Csar input) throws PolicyDecodingException { + final DummyPolicy dummyPolicy = new DummyPolicy(input.getCsarPath(), DUMMY_POLICY); + decodedPolicy = dummyPolicy; + return Arrays.asList(dummyPolicy); + } + + /** + * Returns the policy decoded by this decoder. + * + * @return the policy + */ + public DummyPolicy getDecodedPolicy() { + return decodedPolicy; + } +} diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyPolicy.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyPolicy.java new file mode 100644 index 00000000..f4c56ab0 --- /dev/null +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyPolicy.java @@ -0,0 +1,62 @@ +/*- + * ============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 org.onap.policy.distribution.model.Policy; + +/** + * Class to create a dummy policy for test cases. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class DummyPolicy implements Policy { + + private String policyName; + private String policyType; + + /** + * Constructor for instantiating {@link DummyPolicy} class. + * + * @param policyName the policy name + * @param policyType the policy type + */ + public DummyPolicy(final String policyName, final String policyType) { + super(); + this.policyName = policyName; + this.policyType = policyType; + } + + /** + * {@inheritDoc}. + */ + @Override + public String getPolicyName() { + return policyName; + } + + /** + * {@inheritDoc}. + */ + @Override + public String getPolicyType() { + return policyType; + } +} diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyPolicyForwarder.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyPolicyForwarder.java new file mode 100644 index 00000000..cd127e22 --- /dev/null +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyPolicyForwarder.java @@ -0,0 +1,77 @@ +/*- + * ============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.ArrayList; +import java.util.Collection; + +import org.onap.policy.distribution.forwarding.PolicyForwarder; +import org.onap.policy.distribution.forwarding.PolicyForwardingException; +import org.onap.policy.distribution.model.Policy; + +/** + * Class to create a dummy forwarder for test cases. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class DummyPolicyForwarder implements PolicyForwarder { + private int numberOfPoliciesReceived = 0; + private Collection<Policy> policiesReceived = new ArrayList<>(); + + /** + * {@inheritDoc}. + */ + @Override + public void forward(final Collection<Policy> policies) throws PolicyForwardingException { + numberOfPoliciesReceived += policies.size(); + policiesReceived.addAll(policies); + } + + /** + * Returns the number of policies received by this forwarder. + * + * @return the integer value + */ + public int getNumberOfPoliciesReceived() { + return numberOfPoliciesReceived; + } + + /** + * Checks if the forwarder has received a policy with given policy type. + * + * @param policyType the policy type + * @return the boolean result + */ + public boolean receivedPolicyWithGivenType(final String policyType) { + for (final Policy policy : policiesReceived) { + if (policy.getPolicyType().equals(policyType)) { + return true; + } + } + return false; + } + + /** + * {@inheritDoc}. + */ + @Override + public void configure(final String parameterGroupName) {} +} 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 index c876f99c..a648f807 100644 --- 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 @@ -20,6 +20,7 @@ package org.onap.policy.distribution.reception.handling.sdc; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Matchers.any; @@ -29,6 +30,12 @@ import com.google.gson.GsonBuilder; import java.io.FileReader; import java.io.IOException; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; import org.junit.After; import org.junit.Before; @@ -40,8 +47,19 @@ 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.forwarding.PolicyForwarder; +import org.onap.policy.distribution.forwarding.parameters.PolicyForwarderParameters; +import org.onap.policy.distribution.model.Csar; import org.onap.policy.distribution.reception.decoding.PluginInitializationException; import org.onap.policy.distribution.reception.decoding.PluginTerminationException; +import org.onap.policy.distribution.reception.decoding.PolicyDecoder; +import org.onap.policy.distribution.reception.handling.AbstractReceptionHandler; +import org.onap.policy.distribution.reception.handling.PluginHandler; +import org.onap.policy.distribution.reception.parameters.PluginHandlerParameters; +import org.onap.policy.distribution.reception.parameters.PolicyDecoderParameters; +import org.onap.sdc.api.notification.IArtifactInfo; +import org.onap.sdc.api.notification.INotificationData; +import org.onap.sdc.api.results.IDistributionClientDownloadResult; import org.onap.sdc.api.results.IDistributionClientResult; import org.onap.sdc.impl.mock.DistributionClientStubImpl; import org.onap.sdc.utils.DistributionActionResultEnum; @@ -55,6 +73,7 @@ import org.onap.sdc.utils.DistributionActionResultEnum; public class TestSdcReceptionHandler { private static final Logger LOGGER = FlexLogger.getLogger(TestSdcReceptionHandler.class); + private static final String DUMMY_SERVICE_CSAR = "dummyService.csar"; @Mock private IDistributionClientResult successfulClientInitResult; @@ -62,10 +81,17 @@ public class TestSdcReceptionHandler { private IDistributionClientResult failureClientInitResult; @Mock private DistributionClientStubImpl distributionClient; + @Mock + private IDistributionClientDownloadResult successfulClientDownloadResult; + @Mock + private INotificationData notificationData; + @Mock + private IArtifactInfo artifactInfo; private SdcReceptionHandlerConfigurationParameterGroup pssdConfigParameters; private SdcReceptionHandler sypHandler; + /** * Setup for the test cases. * @@ -79,12 +105,20 @@ public class TestSdcReceptionHandler { 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(distributionClient.download(any())).thenReturn(successfulClientDownloadResult); + Mockito.when(notificationData.getServiceArtifacts()).thenReturn(Arrays.asList(artifactInfo)); + Mockito.when(artifactInfo.getArtifactName()).thenReturn(DUMMY_SERVICE_CSAR); + Mockito.when(successfulClientDownloadResult.getArtifactPayload()).thenReturn(new byte[1]); Mockito.when(successfulClientInitResult.getDistributionActionResult()) .thenReturn(DistributionActionResultEnum.SUCCESS); + Mockito.when(successfulClientDownloadResult.getDistributionActionResult()) + .thenReturn(DistributionActionResultEnum.SUCCESS); + } @After @@ -176,4 +210,97 @@ public class TestSdcReceptionHandler { assertTrue(exp.getMessage().startsWith("SDC client stop failed with reason")); } } + + @Test + public void testNotificationCallBack() throws NoSuchFieldException, SecurityException, IllegalArgumentException, + IllegalAccessException, PluginInitializationException { + + final DummyDecoder policyDecoder = new DummyDecoder(); + final Collection<PolicyDecoder<Csar, DummyPolicy>> policyDecoders = new ArrayList<>(); + policyDecoders.add(policyDecoder); + + final DummyPolicyForwarder policyForwarder = new DummyPolicyForwarder(); + final Collection<PolicyForwarder> policyForwarders = new ArrayList<>(); + policyForwarders.add(policyForwarder); + + setUpPlugins(sypHandler, policyDecoders, policyForwarders); + sypHandler.initializeReception(pssdConfigParameters.getName()); + sypHandler.activateCallback(notificationData); + + assertEquals(DummyDecoder.DUMMY_POLICY, policyDecoder.getDecodedPolicy().getPolicyType()); + assertTrue(policyDecoder.getDecodedPolicy().getPolicyName().contains(DUMMY_SERVICE_CSAR)); + assertEquals(1, policyForwarder.getNumberOfPoliciesReceived()); + assertTrue(policyForwarder.receivedPolicyWithGivenType(DummyDecoder.DUMMY_POLICY)); + } + + @Test + public void testDownloadArtifactFailure() throws NoSuchFieldException, SecurityException, IllegalArgumentException, + IllegalAccessException, PluginInitializationException { + + Mockito.when(successfulClientDownloadResult.getDistributionActionResult()) + .thenReturn(DistributionActionResultEnum.FAIL); + + final DummyDecoder policyDecoder = new DummyDecoder(); + final Collection<PolicyDecoder<Csar, DummyPolicy>> policyDecoders = new ArrayList<>(); + policyDecoders.add(policyDecoder); + + final DummyPolicyForwarder policyForwarder = new DummyPolicyForwarder(); + final Collection<PolicyForwarder> policyForwarders = new ArrayList<>(); + policyForwarders.add(policyForwarder); + + setUpPlugins(sypHandler, policyDecoders, policyForwarders); + sypHandler.initializeReception(pssdConfigParameters.getName()); + sypHandler.activateCallback(notificationData); + + assertEquals(null, policyDecoder.getDecodedPolicy()); + assertEquals(0, policyForwarder.getNumberOfPoliciesReceived()); + } + + private void setUpPlugins(final AbstractReceptionHandler receptionHandler, + final Collection<PolicyDecoder<Csar, DummyPolicy>> decoders, final Collection<PolicyForwarder> forwarders) + throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, + PluginInitializationException { + final PluginHandlerParameters pluginParameters = getPluginHandlerParameters(); + pluginParameters.setName("DummyDistributionGroup"); + ParameterService.register(pluginParameters); + final PluginHandler pluginHandler = new PluginHandler(pluginParameters.getName()); + + final Field decodersField = pluginHandler.getClass().getDeclaredField("policyDecoders"); + decodersField.setAccessible(true); + decodersField.set(pluginHandler, decoders); + + final Field forwardersField = pluginHandler.getClass().getDeclaredField("policyForwarders"); + forwardersField.setAccessible(true); + forwardersField.set(pluginHandler, forwarders); + + final Field pluginHandlerField = AbstractReceptionHandler.class.getDeclaredField("pluginHandler"); + pluginHandlerField.setAccessible(true); + pluginHandlerField.set(receptionHandler, pluginHandler); + ParameterService.deregister(pluginParameters.getName()); + } + + private PluginHandlerParameters getPluginHandlerParameters() { + final Map<String, PolicyDecoderParameters> policyDecoders = getPolicyDecoders(); + final Map<String, PolicyForwarderParameters> policyForwarders = getPolicyForwarders(); + final PluginHandlerParameters pluginHandlerParameters = + new PluginHandlerParameters(policyDecoders, policyForwarders); + return pluginHandlerParameters; + } + + private Map<String, PolicyDecoderParameters> getPolicyDecoders() { + final Map<String, PolicyDecoderParameters> policyDecoders = new HashMap<String, PolicyDecoderParameters>(); + final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters("DummyDecoder", + "org.onap.policy.distribution.reception.handling.sdc.DummyDecoder"); + policyDecoders.put("DummyDecoderKey", pDParameters); + return policyDecoders; + } + + private Map<String, PolicyForwarderParameters> getPolicyForwarders() { + final Map<String, PolicyForwarderParameters> policyForwarders = + new HashMap<String, PolicyForwarderParameters>(); + final PolicyForwarderParameters pFParameters = new PolicyForwarderParameters("DummyForwarder", + "org.onap.policy.distribution.reception.handling.sdc.DummyPolicyForwarder", "DummyConfiguration"); + policyForwarders.put("DummyForwarderKey", pFParameters); + return policyForwarders; + } } diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/PssdDownloadExceptionTest.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/ArtifactDownloadExceptionTest.java index 9f76b375..6d275f7a 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/PssdDownloadExceptionTest.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/ArtifactDownloadExceptionTest.java @@ -26,11 +26,11 @@ import java.io.IOException; import org.junit.Test; -public class PssdDownloadExceptionTest { +public class ArtifactDownloadExceptionTest { @Test public void test() { - assertNotNull(new PssdDownloadException("Message")); - assertNotNull(new PssdDownloadException("Message", new IOException())); + assertNotNull(new ArtifactDownloadException("Message")); + assertNotNull(new ArtifactDownloadException("Message", new IOException())); } } |