diff options
author | ramverma <ram.krishna.verma@ericsson.com> | 2018-09-13 16:31:35 +0100 |
---|---|---|
committer | ramverma <ram.krishna.verma@ericsson.com> | 2018-09-13 22:06:26 +0100 |
commit | e80efa0dbe903e976f5b2799144658c7ba02e534 (patch) | |
tree | 231204269e12123ea3577679bcf91de2a39500e9 /plugins/reception-plugins | |
parent | 31def9f82514182e6761fac5ecae70333155aabc (diff) |
Adding policy decoder to extract file from csar
* Adding decoder configuration parameters infrastructure to support
plugin based architecture. Adding a new policy decoder after this will be just
creating a new decoder class and its corresponding parameter class.
* Adding a new decoder which extracts policy file from given csar. It is
written in a generic way to extract file for any pdp like apex, drools.
* Adding configuration parameters for the new decoder. The policy file name and
policy type is passed as parameter to the decoder.
* Fixing few broken package declaration in pdpx decoder tests.
* Adding test cases for all code changes.
Change-Id: I95e68cebce0f9747ca63b090f9b9116ce8836939
Issue-ID: POLICY-1101
Signed-off-by: ramverma <ram.krishna.verma@ericsson.com>
Diffstat (limited to 'plugins/reception-plugins')
16 files changed, 376 insertions, 36 deletions
diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpx.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpx.java index 0cd7bc16..6c4f285a 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpx.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpx.java @@ -5,15 +5,15 @@ * 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========================================================= */ @@ -22,26 +22,18 @@ package org.onap.policy.distribution.reception.decoding.pdpx; import java.io.File; import java.util.ArrayList; -import java.util.List; - import java.util.Collection; -import java.util.Collections; - +import java.util.List; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; - -import org.onap.policy.distribution.model.PolicyInput; 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; - import org.onap.sdc.tosca.parser.api.ISdcCsarHelper; -import org.onap.sdc.tosca.parser.impl.SdcCsarHelperImpl; import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory; - import org.onap.sdc.toscaparser.api.NodeTemplate; -import org.onap.sdc.toscaparser.api.elements.Metadata; /** * Decodes PDP-X policies from a TOSCA file. @@ -51,16 +43,16 @@ public class PolicyDecoderCsarPdpx implements PolicyDecoder<Csar, PdpxPolicy> { private static final Logger LOGGER = FlexLogger.getLogger(PolicyDecoderCsarPdpx.class); @Override - public Collection<PdpxPolicy> decode(Csar csar) throws PolicyDecodingException { - // logic for generating the policies from the CSAR. - List<PdpxPolicy> lPdpxPolicy = new ArrayList<>(); - ISdcCsarHelper sdcCsarHelper = parseCsar(csar); - List<NodeTemplate> lnodeVf = sdcCsarHelper.getServiceVfList(); + public Collection<PdpxPolicy> decode(final Csar csar) throws PolicyDecodingException { + // logic for generating the policies from the CSAR. + final List<PdpxPolicy> lPdpxPolicy = new ArrayList<>(); + final ISdcCsarHelper sdcCsarHelper = parseCsar(csar); + final List<NodeTemplate> lnodeVf = sdcCsarHelper.getServiceVfList(); LOGGER.debug("the size of Vf = " + lnodeVf.size()); - ExtractFromNode extractFromNode = new ExtractFromNode(); + final ExtractFromNode extractFromNode = new ExtractFromNode(); extractFromNode.setSdcCsarHelper(sdcCsarHelper); - for ( NodeTemplate node : lnodeVf) { - PdpxPolicy ret = extractFromNode.extractInfo(node); + for (final NodeTemplate node : lnodeVf) { + final PdpxPolicy ret = extractFromNode.extractInfo(node); if (ret != null) { lPdpxPolicy.add(ret); } @@ -69,34 +61,37 @@ public class PolicyDecoderCsarPdpx implements PolicyDecoder<Csar, PdpxPolicy> { } @Override - public boolean canHandle(PolicyInput policyInput) { + public boolean canHandle(final PolicyInput policyInput) { return policyInput.getClass().isAssignableFrom(Csar.class); } - /** + /** * Parse the input Csar by SDC tosca tool. * * @param csar represents the service TOSCA Csar * @return the object to represents the content of input csar * @throws PolicyDecodingException if parse fails */ - public ISdcCsarHelper parseCsar(Csar csar) throws PolicyDecodingException { - ISdcCsarHelper sdcCsarHelper ; + public ISdcCsarHelper parseCsar(final Csar csar) throws PolicyDecodingException { + ISdcCsarHelper sdcCsarHelper; try { - SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();//Autoclosable + final SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();// Autoclosable LOGGER.debug("tosca File Path = " + csar.getCsarPath()); - File spoolFile = new File(csar.getCsarPath()); + final File spoolFile = new File(csar.getCsarPath()); sdcCsarHelper = factory.getSdcCsarHelper(spoolFile.getAbsolutePath()); - } catch (Exception e) { - LOGGER.error("Exception got in parseTosca",e); - throw new PolicyDecodingException ("Exception caught when passing the csar file to the parser ", e); + } catch (final Exception e) { + LOGGER.error("Exception got in parseTosca", e); + throw new PolicyDecodingException("Exception caught when passing the csar file to the parser ", e); } return sdcCsarHelper; } + @Override + public void configure(final String parameterGroupName) {} + } diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderToscaPdpx.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderToscaPdpx.java index fa993579..40b33359 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderToscaPdpx.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderToscaPdpx.java @@ -5,15 +5,15 @@ * 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========================================================= */ @@ -22,6 +22,7 @@ package org.onap.policy.distribution.reception.decoding.pdpx; import java.util.Collection; import java.util.Collections; + import org.onap.policy.distribution.model.PolicyInput; import org.onap.policy.distribution.model.Tosca; import org.onap.policy.distribution.reception.decoding.PolicyDecoder; @@ -32,14 +33,17 @@ import org.onap.policy.distribution.reception.decoding.PolicyDecoder; public class PolicyDecoderToscaPdpx implements PolicyDecoder<Tosca, PdpxPolicy> { @Override - public Collection<PdpxPolicy> decode(Tosca tosca) { + public Collection<PdpxPolicy> decode(final Tosca tosca) { // Add logic for generating the policies from the TOSCA return Collections.emptySet(); } @Override - public boolean canHandle(PolicyInput policyInput) { + public boolean canHandle(final PolicyInput policyInput) { return policyInput.getClass().isAssignableFrom(Tosca.class); } + @Override + public void configure(final String parameterGroupName) {} + } diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicy.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicy.java new file mode 100644 index 00000000..9516ad70 --- /dev/null +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicy.java @@ -0,0 +1,94 @@ +/*- + * ============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.decoding.policy.file; + +import java.io.IOException; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Enumeration; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +import org.apache.commons.io.IOUtils; +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.model.Csar; +import org.onap.policy.distribution.model.PolicyAsString; +import org.onap.policy.distribution.model.PolicyInput; +import org.onap.policy.distribution.reception.decoding.PolicyDecoder; +import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; + +/** + * This class extracts policy files from a CSAR file. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class PolicyDecoderFileInCsarToPolicy implements PolicyDecoder<Csar, PolicyAsString> { + + private static final Logger LOGGER = FlexLogger.getLogger(PolicyDecoderFileInCsarToPolicy.class); + PolicyDecoderFileInCsarToPolicyParameterGroup decoderParameters; + + /** + * {@inheritDoc}. + */ + @Override + public void configure(final String parameterGroupName) { + decoderParameters = ParameterService.get(parameterGroupName); + } + + /** + * {@inheritDoc}. + */ + @Override + public boolean canHandle(final PolicyInput policyInput) { + return policyInput.getClass().isAssignableFrom(Csar.class); + } + + /** + * {@inheritDoc}. + */ + @Override + public Collection<PolicyAsString> decode(final Csar csar) throws PolicyDecodingException { + final Collection<PolicyAsString> policyList = new ArrayList<>(); + + try (ZipFile zipFile = new ZipFile(csar.getCsarPath())) { + final Enumeration<? extends ZipEntry> entries = zipFile.entries(); + while (entries.hasMoreElements()) { + final ZipEntry entry = entries.nextElement(); + if (entry.getName().contains(decoderParameters.getPolicyFileName())) { + final StringWriter writer = new StringWriter(); + IOUtils.copy(zipFile.getInputStream(entry), writer, "UTF-8"); + final PolicyAsString poilcy = new PolicyAsString(decoderParameters.getPolicyFileName(), + decoderParameters.getPolicyType(), writer.toString()); + policyList.add(poilcy); + } + } + } catch (final IOException exp) { + final String message = "Failed decoding the policy"; + LOGGER.error(message, exp); + throw new PolicyDecodingException(message, exp); + } + + return policyList; + } +} diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyParameterGroup.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyParameterGroup.java new file mode 100644 index 00000000..958180db --- /dev/null +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyParameterGroup.java @@ -0,0 +1,70 @@ +/*- + * ============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.decoding.policy.file; + +import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.ValidationStatus; +import org.onap.policy.common.utils.validation.ParameterValidationUtils; +import org.onap.policy.distribution.reception.parameters.PolicyDecoderConfigurationParameterGroup; + +/** + * Holds the parameters for the{@link PolicyDecoderFileInCsarToPolicy}. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class PolicyDecoderFileInCsarToPolicyParameterGroup extends PolicyDecoderConfigurationParameterGroup { + + private String policyFileName; + private String policyType; + + /** + * Constructor for instantiating {@link PolicyDecoderFileInCsarToPolicyParameterGroup} class. + * + * @param policyFileName the policy file name + * @param policyType the policy type + */ + public PolicyDecoderFileInCsarToPolicyParameterGroup(final String policyFileName, final String policyType) { + this.policyFileName = policyFileName; + this.policyType = policyType; + } + + public String getPolicyFileName() { + return policyFileName; + } + + public String getPolicyType() { + return policyType; + } + + @Override + public GroupValidationResult validate() { + final GroupValidationResult validationResult = new GroupValidationResult(this); + if (!ParameterValidationUtils.validateStringParameter(policyFileName)) { + validationResult.setResult("policyFileName", ValidationStatus.INVALID, + "must be a non-blank string containing the policy file name"); + } + if (!ParameterValidationUtils.validateStringParameter(policyType)) { + validationResult.setResult("policyType", ValidationStatus.INVALID, + "must be a non-blank string containing the policy type"); + } + return validationResult; + } +} diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestAttribute.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestAttribute.java index f11a7ecd..f11a7ecd 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestAttribute.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestAttribute.java diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestContent.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestContent.java index a329a3ee..a329a3ee 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestContent.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestContent.java diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestDirective.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestDirective.java index 48d11d32..48d11d32 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestDirective.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestDirective.java diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestFlavorFeature.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestFlavorFeature.java index 47b4e343..47b4e343 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestFlavorFeature.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestFlavorFeature.java diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestFlavorProperty.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestFlavorProperty.java index b5a535d7..b5a535d7 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestFlavorProperty.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestFlavorProperty.java diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestHpaFeatureAttribute.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestHpaFeatureAttribute.java index 40c30eb7..40c30eb7 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestHpaFeatureAttribute.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestHpaFeatureAttribute.java diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestPolicyDecoderCsarPdpx.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestPolicyDecoderCsarPdpx.java index 1ce5786c..1ce5786c 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestPolicyDecoderCsarPdpx.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestPolicyDecoderCsarPdpx.java diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyParameterGroupTest.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyParameterGroupTest.java new file mode 100644 index 00000000..0ea08905 --- /dev/null +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyParameterGroupTest.java @@ -0,0 +1,65 @@ +/*- + * ============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.decoding.policy.file; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.onap.policy.common.parameters.ValidationStatus; +import org.onap.policy.distribution.reception.decoding.policy.file.PolicyDecoderFileInCsarToPolicyParameterGroup; + +/** + * Class to perform unit test of {@link PolicyDecoderFileInCsarToPolicyParameterGroup}. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class PolicyDecoderFileInCsarToPolicyParameterGroupTest { + + @Test + public void testConstructorAndGetters() { + final PolicyDecoderFileInCsarToPolicyParameterGroup configurationParameters = + new PolicyDecoderFileInCsarToPolicyParameterGroup("SamplePolicy", "APEX"); + configurationParameters.setName("myConfiguration"); + + assertEquals("myConfiguration", configurationParameters.getName()); + assertEquals("SamplePolicy", configurationParameters.getPolicyFileName()); + assertEquals("APEX", configurationParameters.getPolicyType()); + assertEquals(ValidationStatus.CLEAN, configurationParameters.validate().getStatus()); + } + + @Test + public void testInvalidPolicyFileName() { + final PolicyDecoderFileInCsarToPolicyParameterGroup configurationParameters = + new PolicyDecoderFileInCsarToPolicyParameterGroup("", "APEX"); + configurationParameters.setName("myConfiguration"); + + assertEquals(ValidationStatus.INVALID, configurationParameters.validate().getStatus()); + } + + @Test + public void testInvalidPolicyType() { + final PolicyDecoderFileInCsarToPolicyParameterGroup configurationParameters = + new PolicyDecoderFileInCsarToPolicyParameterGroup("SamplePolicy", ""); + configurationParameters.setName("myConfiguration"); + + assertEquals(ValidationStatus.INVALID, configurationParameters.validate().getStatus()); + } +} diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyTest.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyTest.java new file mode 100644 index 00000000..e40ab3fe --- /dev/null +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyTest.java @@ -0,0 +1,109 @@ +/*- + * ============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.decoding.policy.file; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.IOException; +import java.util.Collection; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.policy.common.parameters.ParameterService; +import org.onap.policy.distribution.model.Csar; +import org.onap.policy.distribution.model.PolicyAsString; + +/** + * Class to perform unit test of {@link PolicyDecoderFileInCsarToPolicy}. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +@RunWith(MockitoJUnitRunner.class) +public class PolicyDecoderFileInCsarToPolicyTest { + + private static final String POLICY_FILE_NAME = "SamplePolicyModelJAVASCRIPT"; + private static final String POLICY_TYPE = "APEX"; + private static final String GROUP_NAME = "apexPdpDecoderConfiguration"; + + /** + * Set up. + */ + @BeforeClass + public static void setUp() { + final PolicyDecoderFileInCsarToPolicyParameterGroup configurationParameters = + new PolicyDecoderFileInCsarToPolicyParameterGroup(POLICY_FILE_NAME, POLICY_TYPE); + configurationParameters.setName(GROUP_NAME); + ParameterService.register(configurationParameters); + } + + /** + * Tear down. + */ + @AfterClass + public static void tearDown() { + ParameterService.deregister(GROUP_NAME); + } + + @Test + public void testDecodePolicy() { + + final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy(); + decoder.configure(GROUP_NAME); + + final File file = new File("src/test/resources/sampleTestService.csar"); + final Csar csar = new Csar(file.getAbsolutePath()); + + try { + decoder.canHandle(csar); + final Collection<PolicyAsString> policyHolders = decoder.decode(csar); + for (final PolicyAsString policy : policyHolders) { + assertEquals(POLICY_FILE_NAME, policy.getPolicyName()); + assertEquals(POLICY_TYPE, policy.getPolicyType()); + } + } catch (final Exception exp) { + fail("Test must not throw an exception"); + } + } + + @Test + public void testDecodePolicyError() throws IOException { + + final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy(); + decoder.configure(GROUP_NAME); + + final File file = new File("unknown.csar"); + final Csar csar = new Csar(file.getAbsolutePath()); + + try { + decoder.canHandle(csar); + decoder.decode(csar); + fail("Test must throw an exception"); + } catch (final Exception exp) { + assertTrue(exp.getMessage().contains("Failed decoding the policy")); + } + } +} 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 index 6a33e787..fd61c43d 100644 --- 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 @@ -64,4 +64,7 @@ public class DummyDecoder implements PolicyDecoder<Csar, DummyPolicy> { public DummyPolicy getDecodedPolicy() { return decodedPolicy; } + + @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 c4020a41..231c50ff 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 @@ -322,7 +322,7 @@ public class TestSdcReceptionHandler { 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"); + "org.onap.policy.distribution.reception.handling.sdc.DummyDecoder", "DummyDecoderConfiguration"); policyDecoders.put("DummyDecoderKey", pDParameters); return policyDecoders; } diff --git a/plugins/reception-plugins/src/test/resources/sampleTestService.csar b/plugins/reception-plugins/src/test/resources/sampleTestService.csar Binary files differnew file mode 100644 index 00000000..387f1bae --- /dev/null +++ b/plugins/reception-plugins/src/test/resources/sampleTestService.csar |