diff options
Diffstat (limited to 'plugins/forwarding-plugins/src/test')
2 files changed, 271 insertions, 0 deletions
diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroupTest.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroupTest.java new file mode 100644 index 00000000..ca0efb5c --- /dev/null +++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroupTest.java @@ -0,0 +1,69 @@ +/*- + * ============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.forwarding.apex.pdp; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.policy.common.parameters.ValidationStatus; + +public class ApexPdpPolicyForwarderParameterGroupTest { + + @Test + public void testBuilderAndGetters() { + final ApexPdpPolicyForwarderParameterBuilder builder = new ApexPdpPolicyForwarderParameterBuilder(); + builder.setHostname("10.10.10.10").setPort(1234).setIgnoreConflicts(false).setForceUpdate(true); + final ApexPdpPolicyForwarderParameterGroup configurationParameters = + new ApexPdpPolicyForwarderParameterGroup(builder); + configurationParameters.setName("myConfiguration"); + + assertEquals("myConfiguration", configurationParameters.getName()); + assertTrue(configurationParameters.isForceUpdate()); + assertEquals("10.10.10.10", configurationParameters.getHostname()); + assertEquals(1234, configurationParameters.getPort()); + assertFalse(configurationParameters.isIgnoreConflicts()); + assertEquals(ValidationStatus.CLEAN, configurationParameters.validate().getStatus()); + } + + @Test + public void testInvalidHostName() { + final ApexPdpPolicyForwarderParameterBuilder builder = new ApexPdpPolicyForwarderParameterBuilder(); + builder.setHostname("").setPort(1234).setIgnoreConflicts(false).setForceUpdate(true); + final ApexPdpPolicyForwarderParameterGroup configurationParameters = + new ApexPdpPolicyForwarderParameterGroup(builder); + configurationParameters.setName("myConfiguration"); + + assertEquals(ValidationStatus.INVALID, configurationParameters.validate().getStatus()); + } + + @Test + public void testInvalidPort() { + final ApexPdpPolicyForwarderParameterBuilder builder = new ApexPdpPolicyForwarderParameterBuilder(); + builder.setHostname("10.10.10.10").setPort(-1).setIgnoreConflicts(false).setForceUpdate(true); + final ApexPdpPolicyForwarderParameterGroup configurationParameters = + new ApexPdpPolicyForwarderParameterGroup(builder); + configurationParameters.setName("myConfiguration"); + + assertEquals(ValidationStatus.INVALID, configurationParameters.validate().getStatus()); + } +} diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderTest.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderTest.java new file mode 100644 index 00000000..abf5b508 --- /dev/null +++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderTest.java @@ -0,0 +1,202 @@ +/*- + * ============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.forwarding.apex.pdp; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Collection; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +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.apex.core.deployment.EngineServiceFacade; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.ParameterService; +import org.onap.policy.distribution.forwarding.PolicyForwardingException; +import org.onap.policy.distribution.model.ApexPdpPolicy; +import org.onap.policy.distribution.model.Policy; + +@RunWith(MockitoJUnitRunner.class) +public class ApexPdpPolicyForwarderTest { + + private static final String HOST_NAME = "10.10.10.10"; + private static final int PORT = 1234; + private static final boolean IGNORE_CONFLICTS = false; + private static final boolean FORCE_UPDATE = true; + private static final String GROUP_NAME = "apexPdpConfiguration"; + + @Mock + EngineServiceFacade engineServiceFacade; + + /** + * Set up. + */ + @BeforeClass + public static void setUp() { + final ApexPdpPolicyForwarderParameterBuilder builder = new ApexPdpPolicyForwarderParameterBuilder(); + builder.setHostname(HOST_NAME).setPort(PORT).setIgnoreConflicts(IGNORE_CONFLICTS).setForceUpdate(FORCE_UPDATE); + final ParameterGroup parameterGroup = new ApexPdpPolicyForwarderParameterGroup(builder); + parameterGroup.setName(GROUP_NAME); + ParameterService.register(parameterGroup); + } + + /** + * Tear down. + */ + @AfterClass + public static void tearDown() { + ParameterService.deregister(GROUP_NAME); + } + + @Test + public void testForwardPolicy() throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, + NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { + + final FileInputStream fis = new FileInputStream(File.createTempFile("policy1", null)); + final Collection<Policy> policies = new ArrayList<>(); + final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder(); + forwarder.configure(GROUP_NAME); + + final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade"); + forwarderField.setAccessible(true); + forwarderField.set(forwarder, engineServiceFacade); + + final ApexPdpPolicy policy = new ApexPdpPolicy("policy", fis); + policies.add(policy); + + try { + forwarder.forward(policies); + verify(engineServiceFacade, times(1)).init(); + verify(engineServiceFacade, times(1)).deployModel("policy", fis, IGNORE_CONFLICTS, FORCE_UPDATE); + } catch (final Exception exp) { + fail("Test must not throw an exception"); + } + } + + @Test + public void testForwardPolicyError() + throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException, + SecurityException, IllegalArgumentException, IllegalAccessException { + + final FileInputStream fis = new FileInputStream(File.createTempFile("policy1", null)); + final Collection<Policy> policies = new ArrayList<>(); + final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder(); + forwarder.configure(GROUP_NAME); + + Mockito.doThrow(new ApexException("Failed")).when(engineServiceFacade).deployModel("policy1", fis, + IGNORE_CONFLICTS, FORCE_UPDATE); + + final Field decodersField = forwarder.getClass().getDeclaredField("engineServiceFacade"); + decodersField.setAccessible(true); + decodersField.set(forwarder, engineServiceFacade); + + final ApexPdpPolicy policy1 = new ApexPdpPolicy("policy1", fis); + policies.add(policy1); + + try { + forwarder.forward(policies); + fail("Test must throw an exception"); + } catch (final Exception exp) { + assertTrue(exp.getMessage().contains("Error sending policy to apex-pdp engine")); + } + + } + + @Test + public void testForwardMoreThanOnePolicy() + throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException, + SecurityException, IllegalArgumentException, IllegalAccessException { + + final Collection<Policy> policies = new ArrayList<>(); + final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder(); + forwarder.configure(GROUP_NAME); + + final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade"); + forwarderField.setAccessible(true); + forwarderField.set(forwarder, engineServiceFacade); + + final ApexPdpPolicy policy1 = + new ApexPdpPolicy("policy1", new FileInputStream(File.createTempFile("policy1", null))); + policies.add(policy1); + + final ApexPdpPolicy policy2 = + new ApexPdpPolicy("policy2", new FileInputStream(File.createTempFile("policy2", null))); + policies.add(policy2); + + try { + forwarder.forward(policies); + fail("Test must throw an exception"); + } catch (final Exception exp) { + assertTrue(exp.getMessage().contains("More than one apex policy cannot be forwarded to an apex engine")); + } + } + + @Test + public void testForwardUnsupportedPolicy() + throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException, + SecurityException, IllegalArgumentException, IllegalAccessException { + + final Collection<Policy> policies = new ArrayList<>(); + final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder(); + forwarder.configure(GROUP_NAME); + + final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade"); + forwarderField.setAccessible(true); + forwarderField.set(forwarder, engineServiceFacade); + + final Policy policy = new UnsupportedPolicy(); + policies.add(policy); + + try { + forwarder.forward(policies); + fail("Test must throw an exception"); + } catch (final Exception exp) { + assertTrue(exp.getMessage().contains("Ignoring the policy as it is not an apex-pdp policy")); + } + } + + class UnsupportedPolicy implements Policy { + + @Override + public String getPolicyName() { + return "unsupported"; + } + + @Override + public String getPolicyType() { + return "unsupported"; + } + } +} |