diff options
Diffstat (limited to 'plugins')
6 files changed, 58 insertions, 44 deletions
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 index 7f1e53ae..4b58d2da 100644 --- 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 @@ -22,20 +22,21 @@ 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.parameters.ParameterService; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; 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.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,10 +45,11 @@ import org.slf4j.LoggerFactory; * * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) */ -public class PolicyDecoderFileInCsarToPolicy implements PolicyDecoder<Csar, ToscaPolicy> { +public class PolicyDecoderFileInCsarToPolicy implements PolicyDecoder<Csar, ToscaEntity> { private static final Logger LOGGER = LoggerFactory.getLogger(PolicyDecoderFileInCsarToPolicy.class); - PolicyDecoderFileInCsarToPolicyParameterGroup decoderParameters; + private PolicyDecoderFileInCsarToPolicyParameterGroup decoderParameters; + private StandardCoder coder; /** * {@inheritDoc}. @@ -55,6 +57,7 @@ public class PolicyDecoderFileInCsarToPolicy implements PolicyDecoder<Csar, Tosc @Override public void configure(final String parameterGroupName) { decoderParameters = ParameterService.get(parameterGroupName); + coder = new StandardCoder(); } /** @@ -69,24 +72,21 @@ public class PolicyDecoderFileInCsarToPolicy implements PolicyDecoder<Csar, Tosc * {@inheritDoc}. */ @Override - public Collection<ToscaPolicy> decode(final Csar csar) throws PolicyDecodingException { - final Collection<ToscaPolicy> policyList = new ArrayList<>(); + public Collection<ToscaEntity> decode(final Csar csar) throws PolicyDecodingException { + final Collection<ToscaEntity> 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 ToscaPolicy policy = new ToscaPolicy(); - policy.setName(decoderParameters.getPolicyFileName()); - policy.setType(decoderParameters.getPolicyType()); - policy.setDescription(writer.toString()); + if (entry.getName().contains(decoderParameters.getPolicyTypeFileName()) + || entry.getName().contains(decoderParameters.getPolicyFileName())) { + final ToscaServiceTemplate policy = + coder.decode(zipFile.getInputStream(entry), ToscaServiceTemplate.class); policyList.add(policy); } } - } catch (final IOException exp) { + } catch (final IOException | CoderException exp) { final String message = "Failed decoding the policy"; LOGGER.error(message, exp); throw new PolicyDecodingException(message, exp); 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 index 958180db..1df563c3 100644 --- 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 @@ -33,25 +33,25 @@ import org.onap.policy.distribution.reception.parameters.PolicyDecoderConfigurat public class PolicyDecoderFileInCsarToPolicyParameterGroup extends PolicyDecoderConfigurationParameterGroup { private String policyFileName; - private String policyType; + private String policyTypeFileName; /** * Constructor for instantiating {@link PolicyDecoderFileInCsarToPolicyParameterGroup} class. * * @param policyFileName the policy file name - * @param policyType the policy type + * @param policyTypeFileName the policy type file name */ - public PolicyDecoderFileInCsarToPolicyParameterGroup(final String policyFileName, final String policyType) { + public PolicyDecoderFileInCsarToPolicyParameterGroup(final String policyFileName, final String policyTypeFileName) { this.policyFileName = policyFileName; - this.policyType = policyType; + this.policyTypeFileName = policyTypeFileName; } public String getPolicyFileName() { return policyFileName; } - public String getPolicyType() { - return policyType; + public String getPolicyTypeFileName() { + return policyTypeFileName; } @Override @@ -61,10 +61,6 @@ public class PolicyDecoderFileInCsarToPolicyParameterGroup extends PolicyDecoder 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/decoding/policy/file/PolicyDecoderFileInCsarToPolicyParameterGroupTest.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyParameterGroupTest.java index 0ea08905..2dd09985 100644 --- 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 @@ -24,7 +24,6 @@ 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}. @@ -36,30 +35,30 @@ public class PolicyDecoderFileInCsarToPolicyParameterGroupTest { @Test public void testConstructorAndGetters() { final PolicyDecoderFileInCsarToPolicyParameterGroup configurationParameters = - new PolicyDecoderFileInCsarToPolicyParameterGroup("SamplePolicy", "APEX"); + new PolicyDecoderFileInCsarToPolicyParameterGroup("SamplePolicy", "SamplePolicyType"); configurationParameters.setName("myConfiguration"); assertEquals("myConfiguration", configurationParameters.getName()); assertEquals("SamplePolicy", configurationParameters.getPolicyFileName()); - assertEquals("APEX", configurationParameters.getPolicyType()); + assertEquals("SamplePolicyType", configurationParameters.getPolicyTypeFileName()); assertEquals(ValidationStatus.CLEAN, configurationParameters.validate().getStatus()); } @Test public void testInvalidPolicyFileName() { final PolicyDecoderFileInCsarToPolicyParameterGroup configurationParameters = - new PolicyDecoderFileInCsarToPolicyParameterGroup("", "APEX"); + new PolicyDecoderFileInCsarToPolicyParameterGroup("", "SamplePolicyType"); configurationParameters.setName("myConfiguration"); assertEquals(ValidationStatus.INVALID, configurationParameters.validate().getStatus()); } @Test - public void testInvalidPolicyType() { + public void testEmptyPolicyType() { final PolicyDecoderFileInCsarToPolicyParameterGroup configurationParameters = new PolicyDecoderFileInCsarToPolicyParameterGroup("SamplePolicy", ""); configurationParameters.setName("myConfiguration"); - assertEquals(ValidationStatus.INVALID, configurationParameters.validate().getStatus()); + assertEquals(ValidationStatus.CLEAN, 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 index f3f0217b..2d0ba3ba 100644 --- 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 @@ -34,8 +34,9 @@ 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.common.utils.coder.CoderException; import org.onap.policy.distribution.model.Csar; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; /** * Class to perform unit test of {@link PolicyDecoderFileInCsarToPolicy}. @@ -45,8 +46,8 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; @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 POLICY_FILE_NAME = "apex_ddf_policy"; + private static final String POLICY_TYPE_FILE_NAME = "apex_ddf_policy_type"; private static final String GROUP_NAME = "apexPdpDecoderConfiguration"; /** @@ -55,7 +56,7 @@ public class PolicyDecoderFileInCsarToPolicyTest { @BeforeClass public static void setUp() { final PolicyDecoderFileInCsarToPolicyParameterGroup configurationParameters = - new PolicyDecoderFileInCsarToPolicyParameterGroup(POLICY_FILE_NAME, POLICY_TYPE); + new PolicyDecoderFileInCsarToPolicyParameterGroup(POLICY_FILE_NAME, POLICY_TYPE_FILE_NAME); configurationParameters.setName(GROUP_NAME); ParameterService.register(configurationParameters); } @@ -74,23 +75,20 @@ public class PolicyDecoderFileInCsarToPolicyTest { final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy(); decoder.configure(GROUP_NAME); - final File file = new File("src/test/resources/sampleTestService.csar"); + final File file = new File("src/test/resources/service-Sampleservice.csar"); final Csar csar = new Csar(file.getAbsolutePath()); try { - decoder.canHandle(csar); - final Collection<ToscaPolicy> policyHolders = decoder.decode(csar); - for (final ToscaPolicy policy : policyHolders) { - assertEquals(POLICY_FILE_NAME, policy.getName()); - assertEquals(POLICY_TYPE, policy.getType()); - } + assertTrue(decoder.canHandle(csar)); + final Collection<ToscaEntity> policyHolders = decoder.decode(csar); + assertEquals(2, policyHolders.size()); } catch (final Exception exp) { fail("Test must not throw an exception"); } } @Test - public void testDecodePolicyError() throws IOException { + public void testDecodePolicyZipError() { final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy(); decoder.configure(GROUP_NAME); @@ -99,10 +97,31 @@ public class PolicyDecoderFileInCsarToPolicyTest { final Csar csar = new Csar(file.getAbsolutePath()); try { - decoder.canHandle(csar); + assertTrue(decoder.canHandle(csar)); decoder.decode(csar); fail("Test must throw an exception"); } catch (final Exception exp) { + assertTrue(exp.getCause() instanceof IOException); + assertTrue(exp.getMessage().contains("Failed decoding the policy")); + } + } + + + @Test + public void testDecodePolicyCoderError() { + + final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy(); + decoder.configure(GROUP_NAME); + + final File file = new File("src/test/resources/service-Sampleservice-test.csar"); + final Csar csar = new Csar(file.getAbsolutePath()); + + try { + assertTrue(decoder.canHandle(csar)); + decoder.decode(csar); + fail("Test must throw an exception"); + } catch (final Exception exp) { + assertTrue(exp.getCause() instanceof CoderException); assertTrue(exp.getMessage().contains("Failed decoding the policy")); } } diff --git a/plugins/reception-plugins/src/test/resources/service-Sampleservice-test.csar b/plugins/reception-plugins/src/test/resources/service-Sampleservice-test.csar Binary files differnew file mode 100644 index 00000000..f7360cd2 --- /dev/null +++ b/plugins/reception-plugins/src/test/resources/service-Sampleservice-test.csar diff --git a/plugins/reception-plugins/src/test/resources/service-Sampleservice.csar b/plugins/reception-plugins/src/test/resources/service-Sampleservice.csar Binary files differnew file mode 100644 index 00000000..bb457e6f --- /dev/null +++ b/plugins/reception-plugins/src/test/resources/service-Sampleservice.csar |