diff options
author | a.sreekumar <ajith.sreekumar@est.tech> | 2019-05-14 15:10:11 +0000 |
---|---|---|
committer | a.sreekumar <ajith.sreekumar@est.tech> | 2019-05-14 15:10:11 +0000 |
commit | ceb3ced528288d9869d14930ca88907547a20c93 (patch) | |
tree | 4eaf8872ef8cb22509138ddd2d418832a1df8865 /models-sim/policy-models-sim-pdp/src/test/java | |
parent | cee63084a9ebe9f91dc7afa490d18572448e6668 (diff) |
Change-Id: I809e229bf363f3b18942b20521b05e2dc06643b4
Issue-ID: POLICY-1768
Signed-off-by: a.sreekumar <ajith.sreekumar@est.tech>
Diffstat (limited to 'models-sim/policy-models-sim-pdp/src/test/java')
10 files changed, 1092 insertions, 0 deletions
diff --git a/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/TestPdpSimulatorActivator.java b/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/TestPdpSimulatorActivator.java new file mode 100644 index 000000000..e75bb6d26 --- /dev/null +++ b/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/TestPdpSimulatorActivator.java @@ -0,0 +1,117 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.models.sim.pdp; + +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.FileInputStream; +import java.util.Properties; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.models.pdp.concepts.PdpStatus; +import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException; +import org.onap.policy.models.sim.pdp.parameters.CommonTestData; +import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterGroup; +import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterHandler; + +/** + * Class to perform unit test of {@link PdpSimulatorActivator}}. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class TestPdpSimulatorActivator { + + private PdpSimulatorActivator activator; + + /** + * Initializes an activator. + * + * @throws Exception if an error occurs + */ + @Before + public void setUp() throws Exception { + Registry.newRegistry(); + final String[] pdpSimulatorConfigParameters = { "-c", "src/test/resources/PdpSimulatorConfigParameters.json", + "-p", "src/test/resources/topic.properties" }; + final PdpSimulatorCommandLineArguments arguments = + new PdpSimulatorCommandLineArguments(pdpSimulatorConfigParameters); + final PdpSimulatorParameterGroup parGroup = new PdpSimulatorParameterHandler().getParameters(arguments); + + final Properties props = new Properties(); + final String propFile = arguments.getFullPropertyFilePath(); + try (FileInputStream stream = new FileInputStream(propFile)) { + props.load(stream); + } + + activator = new PdpSimulatorActivator(parGroup, props); + } + + /** + * Method for cleanup after each test. + * + * @throws Exception if an error occurs + */ + @After + public void teardown() throws Exception { + if (activator != null && activator.isAlive()) { + activator.terminate(); + } + } + + @Test + public void testPdpSimulatorActivator() throws PdpSimulatorException { + assertFalse(activator.isAlive()); + activator.initialize(); + assertTrue(activator.isAlive()); + assertTrue(activator.getParameterGroup().isValid()); + assertEquals(CommonTestData.PDP_SIMULATOR_GROUP_NAME, activator.getParameterGroup().getName()); + + // ensure items were added to the registry + assertNotNull(Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class)); + + // repeat - should throw an exception + assertThatIllegalStateException().isThrownBy(() -> activator.initialize()); + assertTrue(activator.isAlive()); + assertTrue(activator.getParameterGroup().isValid()); + } + + @Test + public void testTerminate() throws Exception { + activator.initialize(); + activator.terminate(); + assertFalse(activator.isAlive()); + + // ensure items have been removed from the registry + assertNull(Registry.getOrDefault(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class, null)); + + // repeat - should throw an exception + assertThatIllegalStateException().isThrownBy(() -> activator.terminate()); + assertFalse(activator.isAlive()); + } +} diff --git a/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/TestPdpSimulatorConstants.java b/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/TestPdpSimulatorConstants.java new file mode 100644 index 000000000..2bec22af3 --- /dev/null +++ b/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/TestPdpSimulatorConstants.java @@ -0,0 +1,37 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.models.sim.pdp; + +import org.junit.Test; +import org.powermock.reflect.Whitebox; + +/** + * Class to perform unit test of {@link PdpSimulatorConstants}}. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class TestPdpSimulatorConstants { + @Test + public void test() throws Exception { + // verify that constructor does not throw an exception + Whitebox.invokeConstructor(PdpSimulatorConstants.class); + } +} diff --git a/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/TestPdpSimulatorMain.java b/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/TestPdpSimulatorMain.java new file mode 100644 index 000000000..7a3239891 --- /dev/null +++ b/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/TestPdpSimulatorMain.java @@ -0,0 +1,106 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.models.sim.pdp; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException; +import org.onap.policy.models.sim.pdp.parameters.CommonTestData; + +/** + * Class to perform unit test of {@link PdpSimulatorMain}}. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class TestPdpSimulatorMain { + private PdpSimulatorMain pdpSimulator; + + /** + * Set up. + */ + @Before + public void setUp() { + Registry.newRegistry(); + } + + /** + * Shuts "main" down. + * + * @throws Exception if an error occurs + */ + @After + public void tearDown() throws Exception { + // shut down activator + final PdpSimulatorActivator activator = Registry.getOrDefault(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR, + PdpSimulatorActivator.class, null); + if (activator != null && activator.isAlive()) { + activator.terminate(); + } + } + + @Test + public void testPdpSimulator() throws PdpSimulatorException { + final String[] pdpSimulatorConfigParameters = { "-c", "src/test/resources/PdpSimulatorConfigParameters.json", + "-p", "src/test/resources/topic.properties" }; + pdpSimulator = new PdpSimulatorMain(pdpSimulatorConfigParameters); + assertTrue(pdpSimulator.getParameters().isValid()); + assertEquals(CommonTestData.PDP_SIMULATOR_GROUP_NAME, pdpSimulator.getParameters().getName()); + + // ensure items were added to the registry + assertNotNull(Registry.get(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR, PdpSimulatorActivator.class)); + + pdpSimulator.shutdown(); + } + + @Test + public void testPdpSimulator_NoArguments() { + final String[] pdpSimulatorConfigParameters = {}; + pdpSimulator = new PdpSimulatorMain(pdpSimulatorConfigParameters); + assertTrue(pdpSimulator.getParameters() == null); + } + + @Test + public void testPdpSimulator_InvalidArguments() { + final String[] pdpSimulatorConfigParameters = { "src/test/resourcesPdpSimulatorConfigParameters.json" }; + pdpSimulator = new PdpSimulatorMain(pdpSimulatorConfigParameters); + assertTrue(pdpSimulator.getParameters() == null); + } + + @Test + public void testPdpSimulator_Help() { + final String[] pdpSimulatorConfigParameters = { "-h" }; + PdpSimulatorMain.main(pdpSimulatorConfigParameters); + } + + @Test + public void testPdpSimulator_InvalidParameters() { + final String[] pdpSimulatorConfigParameters = + { "-c", "src/test/resources/PdpSimulatorConfigParameters_InvalidName.json" }; + pdpSimulator = new PdpSimulatorMain(pdpSimulatorConfigParameters); + assertTrue(pdpSimulator.getParameters() == null); + } +} diff --git a/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/comm/TestPdpStateChangeListener.java b/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/comm/TestPdpStateChangeListener.java new file mode 100644 index 000000000..8e3582e21 --- /dev/null +++ b/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/comm/TestPdpStateChangeListener.java @@ -0,0 +1,180 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.models.sim.pdp.comm; + +import static org.junit.Assert.assertEquals; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; +import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.models.pdp.concepts.PdpStateChange; +import org.onap.policy.models.pdp.concepts.PdpStatus; +import org.onap.policy.models.pdp.concepts.PdpUpdate; +import org.onap.policy.models.pdp.enums.PdpState; +import org.onap.policy.models.sim.pdp.PdpSimulatorActivator; +import org.onap.policy.models.sim.pdp.PdpSimulatorCommandLineArguments; +import org.onap.policy.models.sim.pdp.PdpSimulatorConstants; +import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException; +import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterGroup; +import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterHandler; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; + +/** + * Class to perform unit test of {@link PdpStateChangeListener}. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class TestPdpStateChangeListener { + private PdpUpdateListener pdpUpdateMessageListener; + private PdpStateChangeListener pdpStateChangeListener; + private static final CommInfrastructure INFRA = CommInfrastructure.NOOP; + private static final String TOPIC = "my-topic"; + private PdpSimulatorActivator activator; + + /** + * Method for setup before each test. + * + * @throws PdpSimulatorException if some error occurs while starting up the pdp simulator + * @throws FileNotFoundException if the file is missing + * @throws IOException if IO exception occurs + */ + @Before + public void setUp() throws PdpSimulatorException, FileNotFoundException, IOException { + pdpUpdateMessageListener = new PdpUpdateListener(); + pdpStateChangeListener = new PdpStateChangeListener(); + Registry.newRegistry(); + final String[] pdpSimulatorConfigParameters = { "-c", "src/test/resources/PdpSimulatorConfigParameters.json", + "-p", "src/test/resources/topic.properties" }; + final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments(); + PdpSimulatorParameterGroup pdpSimulatorParameterGroup; + // The arguments return a string if there is a message to print and we should + // exit + final String argumentMessage = arguments.parse(pdpSimulatorConfigParameters); + if (argumentMessage != null) { + return; + } + // Validate that the arguments are sane + arguments.validate(); + + // Read the parameters + pdpSimulatorParameterGroup = new PdpSimulatorParameterHandler().getParameters(arguments); + + // Read the properties + final Properties topicProperties = new Properties(); + final String propFile = arguments.getFullPropertyFilePath(); + try (FileInputStream stream = new FileInputStream(propFile)) { + topicProperties.load(stream); + } + activator = new PdpSimulatorActivator(pdpSimulatorParameterGroup, topicProperties); + Registry.register(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR, activator); + activator.initialize(); + } + + /** + * Method for cleanup after each test. + * + * @throws Exception if an error occurs + */ + @After + public void teardown() throws Exception { + + // clear the pdp simulator activator + if (activator != null && activator.isAlive()) { + activator.terminate(); + } + } + + /** + * Method to initiate a PdpUpdate. + * + * @param instance the instance id + * @return PdpUpdate the pdp update message + */ + private PdpUpdate performPdpUpdate(final String instance) { + final PdpUpdate pdpUpdateMsg = new PdpUpdate(); + pdpUpdateMsg.setDescription("dummy pdp status for test"); + pdpUpdateMsg.setPdpGroup("pdpGroup"); + pdpUpdateMsg.setPdpSubgroup("pdpSubgroup"); + pdpUpdateMsg.setName(instance); + final ToscaPolicy toscaPolicy = new ToscaPolicy(); + toscaPolicy.setType("apexpolicytype"); + toscaPolicy.setVersion("1.0"); + final Map<String, Object> propertiesMap = new LinkedHashMap<>(); + + String properties; + try { + properties = new String(Files.readAllBytes(Paths.get("src\\test\\resources\\dummyProperties.json")), + StandardCharsets.UTF_8); + propertiesMap.put("content", properties); + } catch (final IOException e) { + propertiesMap.put("content", ""); + } + toscaPolicy.setProperties(propertiesMap); + final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>(); + toscaPolicies.add(toscaPolicy); + pdpUpdateMsg.setPolicies(toscaPolicies); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg); + return pdpUpdateMsg; + } + + @Test + public void testPdpStateChangeMessageListener_passivetopassive() { + final PdpStatus pdpStatus = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT); + performPdpUpdate(pdpStatus.getName()); + final PdpStateChange pdpStateChangeMsg = new PdpStateChange(); + pdpStateChangeMsg.setState(PdpState.PASSIVE); + pdpStateChangeMsg.setPdpGroup("pdpGroup"); + pdpStateChangeMsg.setPdpSubgroup("pdpSubgroup"); + pdpStateChangeMsg.setName(pdpStatus.getName()); + pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg); + + assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState()); + } + + @Test + public void testPdpStateChangeMessageListener_activetoactive() { + final PdpStatus pdpStatus = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT); + performPdpUpdate(pdpStatus.getName()); + pdpStatus.setState(PdpState.ACTIVE); + final PdpStateChange pdpStateChangeMsg = new PdpStateChange(); + pdpStateChangeMsg.setState(PdpState.ACTIVE); + pdpStateChangeMsg.setPdpGroup("pdpGroup"); + pdpStateChangeMsg.setPdpSubgroup("pdpSubgroup"); + pdpStateChangeMsg.setName(pdpStatus.getName()); + pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg); + + assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState()); + } +} diff --git a/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/comm/TestPdpUpdateListener.java b/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/comm/TestPdpUpdateListener.java new file mode 100644 index 000000000..f3885f42d --- /dev/null +++ b/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/comm/TestPdpUpdateListener.java @@ -0,0 +1,148 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.models.sim.pdp.comm; + +import static org.junit.Assert.assertEquals; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; +import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.models.pdp.concepts.PdpStatus; +import org.onap.policy.models.pdp.concepts.PdpUpdate; +import org.onap.policy.models.sim.pdp.PdpSimulatorActivator; +import org.onap.policy.models.sim.pdp.PdpSimulatorCommandLineArguments; +import org.onap.policy.models.sim.pdp.PdpSimulatorConstants; +import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException; +import org.onap.policy.models.sim.pdp.handler.PdpMessageHandler; +import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterGroup; +import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterHandler; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; + +/** + * Class to perform unit test of {@link PdpUpdateListener}. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class TestPdpUpdateListener { + private PdpUpdateListener pdpUpdateMessageListener; + private static final CommInfrastructure INFRA = CommInfrastructure.NOOP; + private static final String TOPIC = "my-topic"; + private PdpSimulatorActivator activator; + + /** + * Method for setup before each test. + * + * @throws PdpSimulatorException if some error occurs while starting up the pdp simulator + * @throws FileNotFoundException if the file is missing + * @throws IOException if IO exception occurs + */ + @Before + public void setUp() throws PdpSimulatorException, FileNotFoundException, IOException { + Registry.newRegistry(); + final String[] pdpSimulatorConfigParameters = { "-c", "src/test/resources/PdpSimulatorConfigParameters.json", + "-p", "src/test/resources/topic.properties" }; + final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments(); + PdpSimulatorParameterGroup pdpSimulatorParameterGroup; + // The arguments return a string if there is a message to print and we should + // exit + final String argumentMessage = arguments.parse(pdpSimulatorConfigParameters); + if (argumentMessage != null) { + return; + } + // Validate that the arguments are sane + arguments.validate(); + + // Read the parameters + pdpSimulatorParameterGroup = new PdpSimulatorParameterHandler().getParameters(arguments); + + // Read the properties + final Properties topicProperties = new Properties(); + final String propFile = arguments.getFullPropertyFilePath(); + try (FileInputStream stream = new FileInputStream(propFile)) { + topicProperties.load(stream); + } + activator = new PdpSimulatorActivator(pdpSimulatorParameterGroup, topicProperties); + Registry.register(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR, activator); + activator.initialize(); + pdpUpdateMessageListener = new PdpUpdateListener(); + } + + /** + * Method for cleanup after each test. + * + * @throws Exception if an error occurs + */ + @After + public void teardown() throws Exception { + + // clear the pdp simulator activator + if (activator != null && activator.isAlive()) { + activator.terminate(); + } + } + + @Test + public void testPdpUpdateMssageListener() { + final PdpStatus pdpStatus = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT); + final PdpUpdate pdpUpdateMsg = new PdpUpdate(); + pdpUpdateMsg.setDescription("dummy pdp status for test"); + pdpUpdateMsg.setPdpGroup("pdpGroup"); + pdpUpdateMsg.setPdpSubgroup("pdpSubgroup"); + pdpUpdateMsg.setName(pdpStatus.getName()); + pdpUpdateMsg.setPdpHeartbeatIntervalMs(Long.valueOf(3000)); + final ToscaPolicy toscaPolicy = new ToscaPolicy(); + toscaPolicy.setType("apexpolicytype"); + toscaPolicy.setVersion("1.0"); + toscaPolicy.setName("apex policy name"); + final Map<String, Object> propertiesMap = new LinkedHashMap<>(); + String properties; + try { + properties = new String(Files.readAllBytes(Paths.get("src\\test\\resources\\dummyProperties.json")), + StandardCharsets.UTF_8); + propertiesMap.put("content", properties); + } catch (final IOException e) { + propertiesMap.put("content", ""); + } + toscaPolicy.setProperties(propertiesMap); + final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>(); + toscaPolicies.add(toscaPolicy); + pdpUpdateMsg.setPolicies(toscaPolicies); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg); + assertEquals(pdpStatus.getPdpGroup(), pdpUpdateMsg.getPdpGroup()); + assertEquals(pdpStatus.getPdpSubgroup(), pdpUpdateMsg.getPdpSubgroup()); + assertEquals(pdpStatus.getPolicies(), + new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies())); + } +} diff --git a/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/exception/TestExceptions.java b/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/exception/TestExceptions.java new file mode 100644 index 000000000..45845a941 --- /dev/null +++ b/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/exception/TestExceptions.java @@ -0,0 +1,38 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.models.sim.pdp.exception; + +import org.junit.Test; +import org.onap.policy.common.utils.test.ExceptionsTester; + +/** + * Class to perform unit test of {@link PdpSimulatorException PdpSimulatorRunTimeException}}. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class TestExceptions { + + @Test + public void test() { + new ExceptionsTester().test(PdpSimulatorException.class); + new ExceptionsTester().test(PdpSimulatorRunTimeException.class); + } +} diff --git a/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/parameters/CommonTestData.java b/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/parameters/CommonTestData.java new file mode 100644 index 000000000..4351645b8 --- /dev/null +++ b/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/parameters/CommonTestData.java @@ -0,0 +1,144 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.models.sim.pdp.parameters; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; + +/** + * Class to hold/create all parameters for test cases. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class CommonTestData { + + public static final String PDP_SIMULATOR_GROUP_NAME = "PdpSimulatorParameterGroup"; + public static final long TIME_INTERVAL = 2000; + public static final String PDP_NAME = "apex-pdp"; + public static final String VERSION = "0.0.1"; + public static final String PDP_TYPE = "apex"; + public static final String DESCRIPTION = "Pdp status for HealthCheck"; + public static final String POLICY_NAME = "onap.controllloop.operational.apex.BBS"; + public static final String POLICY_VERSION = "0.0.1"; + public static final List<ToscaPolicyTypeIdentifierParameters> SUPPORTED_POLICY_TYPES = + Arrays.asList(getSupportedPolicyTypes(POLICY_NAME, POLICY_VERSION)); + private static final String REST_SERVER_PASSWORD = "zb!XztG34"; + private static final String REST_SERVER_USER = "healthcheck"; + private static final int REST_SERVER_PORT = 6969; + private static final String REST_SERVER_HOST = "0.0.0.0"; + private static final boolean REST_SERVER_HTTPS = true; + private static final boolean REST_SERVER_AAF = false; + + public static final Coder coder = new StandardCoder(); + + /** + * Returns supported policy types for test cases. + * + * @return supported policy types + */ + public static ToscaPolicyTypeIdentifierParameters getSupportedPolicyTypes(final String name, final String version) { + final ToscaPolicyTypeIdentifierParameters policyTypeIdentParameters = new ToscaPolicyTypeIdentifierParameters(); + policyTypeIdentParameters.setName(name); + policyTypeIdentParameters.setVersion(version); + return policyTypeIdentParameters; + } + + /** + * Converts the contents of a map to a parameter class. + * + * @param source property map + * @param clazz class of object to be created from the map + * @return a new object represented by the map + */ + public <T extends ParameterGroup> T toObject(final Map<String, Object> source, final Class<T> clazz) { + try { + return coder.decode(coder.encode(source), clazz); + + } catch (final CoderException e) { + throw new RuntimeException("cannot create " + clazz.getName() + " from map", e); + } + } + + /** + * Returns a property map for a PdpSimulatorParameterGroup map for test cases. + * + * @param name name of the parameters + * + * @return a property map suitable for constructing an object + */ + public Map<String, Object> getPdpSimulatorParameterGroupMap(final String name) { + final Map<String, Object> map = new TreeMap<>(); + + map.put("name", name); + map.put("restServerParameters", getRestServerParametersMap(false)); + map.put("pdpStatusParameters", getPdpStatusParametersMap(false)); + + return map; + } + + /** + * Returns a property map for a RestServerParameters map for test cases. + * + * @param isEmpty boolean value to represent that object created should be empty or not + * @return a property map suitable for constructing an object + */ + public Map<String, Object> getRestServerParametersMap(final boolean isEmpty) { + final Map<String, Object> map = new TreeMap<>(); + map.put("https", REST_SERVER_HTTPS); + map.put("aaf", REST_SERVER_AAF); + + if (!isEmpty) { + map.put("host", REST_SERVER_HOST); + map.put("port", REST_SERVER_PORT); + map.put("userName", REST_SERVER_USER); + map.put("password", REST_SERVER_PASSWORD); + } + + return map; + } + + /** + * Returns a property map for a PdpStatusParameters map for test cases. + * + * @param isEmpty boolean value to represent that object created should be empty or not + * @return a property map suitable for constructing an object + */ + public Map<String, Object> getPdpStatusParametersMap(final boolean isEmpty) { + final Map<String, Object> map = new TreeMap<>(); + if (!isEmpty) { + map.put("timeIntervalMs", TIME_INTERVAL); + map.put("pdpName", PDP_NAME); + map.put("version", VERSION); + map.put("pdpType", PDP_TYPE); + map.put("description", DESCRIPTION); + map.put("supportedPolicyTypes", SUPPORTED_POLICY_TYPES); + } + + return map; + } +} diff --git a/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/parameters/TestPdpSimulatorParameterGroup.java b/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/parameters/TestPdpSimulatorParameterGroup.java new file mode 100644 index 000000000..b4a54b8ae --- /dev/null +++ b/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/parameters/TestPdpSimulatorParameterGroup.java @@ -0,0 +1,107 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.models.sim.pdp.parameters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.Map; + +import org.junit.Test; +import org.onap.policy.common.parameters.GroupValidationResult; + +/** + * Class to perform unit test of {@link PdpSimulatorParameterGroup}. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class TestPdpSimulatorParameterGroup { + CommonTestData commonTestData = new CommonTestData(); + + @Test + public void testPdpSimulatorParameterGroup_Named() { + final PdpSimulatorParameterGroup pdpSimulatorParameters = new PdpSimulatorParameterGroup("my-name"); + assertEquals("my-name", pdpSimulatorParameters.getName()); + } + + @Test + public void testPdpSimulatorParameterGroup() { + final PdpSimulatorParameterGroup pdpSimulatorParameters = commonTestData.toObject( + commonTestData.getPdpSimulatorParameterGroupMap(CommonTestData.PDP_SIMULATOR_GROUP_NAME), + PdpSimulatorParameterGroup.class); + final PdpStatusParameters pdpStatusParameters = pdpSimulatorParameters.getPdpStatusParameters(); + final GroupValidationResult validationResult = pdpSimulatorParameters.validate(); + assertTrue(validationResult.isValid()); + assertEquals(CommonTestData.PDP_SIMULATOR_GROUP_NAME, pdpSimulatorParameters.getName()); + assertEquals(CommonTestData.TIME_INTERVAL, pdpStatusParameters.getTimeIntervalMs()); + assertEquals(CommonTestData.PDP_TYPE, pdpStatusParameters.getPdpType()); + assertEquals(CommonTestData.DESCRIPTION, pdpStatusParameters.getDescription()); + assertEquals(CommonTestData.SUPPORTED_POLICY_TYPES, pdpStatusParameters.getSupportedPolicyTypes()); + } + + @Test + public void testPdpSimulatorParameterGroup_NullName() { + final PdpSimulatorParameterGroup pdpSimulatorParameters = commonTestData + .toObject(commonTestData.getPdpSimulatorParameterGroupMap(null), PdpSimulatorParameterGroup.class); + final GroupValidationResult validationResult = pdpSimulatorParameters.validate(); + assertFalse(validationResult.isValid()); + assertEquals(null, pdpSimulatorParameters.getName()); + assertTrue(validationResult.getResult().contains("is null")); + } + + @Test + public void testPdpSimulatorParameterGroup_EmptyName() { + final PdpSimulatorParameterGroup pdpSimulatorParameters = commonTestData + .toObject(commonTestData.getPdpSimulatorParameterGroupMap(""), PdpSimulatorParameterGroup.class); + final GroupValidationResult validationResult = pdpSimulatorParameters.validate(); + assertFalse(validationResult.isValid()); + assertEquals("", pdpSimulatorParameters.getName()); + assertTrue(validationResult.getResult().contains( + "field \"name\" type \"java.lang.String\" value \"\" INVALID, " + "must be a non-blank string")); + } + + @Test + public void testPdpSimulatorParameterGroup_SetName() { + final PdpSimulatorParameterGroup pdpSimulatorParameters = commonTestData.toObject( + commonTestData.getPdpSimulatorParameterGroupMap(CommonTestData.PDP_SIMULATOR_GROUP_NAME), + PdpSimulatorParameterGroup.class); + pdpSimulatorParameters.setName("PdpSimulatorNewGroup"); + final GroupValidationResult validationResult = pdpSimulatorParameters.validate(); + assertTrue(validationResult.isValid()); + assertEquals("PdpSimulatorNewGroup", pdpSimulatorParameters.getName()); + } + + @Test + public void testPdpSimulatorParameterGroup_EmptyPdpStatusParameters() { + final Map<String, Object> map = + commonTestData.getPdpSimulatorParameterGroupMap(CommonTestData.PDP_SIMULATOR_GROUP_NAME); + map.put("pdpStatusParameters", commonTestData.getPdpStatusParametersMap(true)); + final PdpSimulatorParameterGroup pdpSimulatorParameters = + commonTestData.toObject(map, PdpSimulatorParameterGroup.class); + final GroupValidationResult validationResult = pdpSimulatorParameters.validate(); + assertFalse(validationResult.isValid()); + assertTrue(validationResult.getResult() + .contains("\"org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterGroup\" INVALID, " + + "parameter group has status INVALID")); + } + +} diff --git a/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/parameters/TestPdpSimulatorParameterHandler.java b/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/parameters/TestPdpSimulatorParameterHandler.java new file mode 100644 index 000000000..e8e1f9036 --- /dev/null +++ b/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/parameters/TestPdpSimulatorParameterHandler.java @@ -0,0 +1,157 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.models.sim.pdp.parameters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.FileNotFoundException; + +import org.junit.Test; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.models.sim.pdp.PdpSimulatorCommandLineArguments; +import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException; + +/** + * Class to perform unit test of {@link PdpSimulatorParameterHandler}. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class TestPdpSimulatorParameterHandler { + + @Test + public void testParameterHandlerNoParameterFile() throws PdpSimulatorException { + final String[] emptyArgumentString = { "-c", "src/test/resources/NoParametersFile.json" }; + + final PdpSimulatorCommandLineArguments emptyArguments = new PdpSimulatorCommandLineArguments(); + emptyArguments.parse(emptyArgumentString); + + try { + new PdpSimulatorParameterHandler().getParameters(emptyArguments); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getCause() instanceof CoderException); + assertTrue(e.getCause().getCause() instanceof FileNotFoundException); + } + } + + @Test + public void testParameterHandlerEmptyParameters() throws PdpSimulatorException { + final String[] noArgumentString = { "-c", "src/test/resources/NoParameters.json" }; + + final PdpSimulatorCommandLineArguments noArguments = new PdpSimulatorCommandLineArguments(); + noArguments.parse(noArgumentString); + + try { + new PdpSimulatorParameterHandler().getParameters(noArguments); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().contains("no parameters found")); + } + } + + @Test + public void testParameterHandlerInvalidParameters() throws PdpSimulatorException { + final String[] invalidArgumentString = { "-c", "src/test/resources/InvalidParameters.json" }; + + final PdpSimulatorCommandLineArguments invalidArguments = new PdpSimulatorCommandLineArguments(); + invalidArguments.parse(invalidArgumentString); + + try { + new PdpSimulatorParameterHandler().getParameters(invalidArguments); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().startsWith("error reading parameters from")); + assertTrue(e.getCause() instanceof CoderException); + } + } + + @Test + public void testParameterHandlerNoParameters() throws PdpSimulatorException { + final String[] noArgumentString = { "-c", "src/test/resources/EmptyConfigParameters.json" }; + + final PdpSimulatorCommandLineArguments noArguments = new PdpSimulatorCommandLineArguments(); + noArguments.parse(noArgumentString); + + try { + new PdpSimulatorParameterHandler().getParameters(noArguments); + } catch (final Exception e) { + assertTrue(e.getMessage().contains("is null")); + } + } + + @Test + public void testPdpSimulatorParameterGroup() throws PdpSimulatorException { + final String[] pdpSimulatorConfigParameters = { "-c", "src/test/resources/PdpSimulatorConfigParameters.json" }; + + final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments(); + arguments.parse(pdpSimulatorConfigParameters); + + final PdpSimulatorParameterGroup parGroup = new PdpSimulatorParameterHandler().getParameters(arguments); + assertTrue(arguments.checkSetConfigurationFilePath()); + assertEquals(CommonTestData.PDP_SIMULATOR_GROUP_NAME, parGroup.getName()); + } + + @Test + public void testPdpSimulatorParameterGroup_InvalidName() throws PdpSimulatorException { + final String[] pdpSimulatorConfigParameters = {"-c", + "src/test/resources/PdpSimulatorConfigParameters_InvalidName.json"}; + + final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments(); + arguments.parse(pdpSimulatorConfigParameters); + + try { + new PdpSimulatorParameterHandler().getParameters(arguments); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().contains( + "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string")); + } + } + + @Test + public void testPdpSimulatorVersion() throws PdpSimulatorException { + final String[] pdpSimulatorConfigParameters = { "-v" }; + final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments(); + final String version = arguments.parse(pdpSimulatorConfigParameters); + assertTrue(version.startsWith("ONAP Policy-PDP simulator Service")); + } + + @Test + public void testPdpSimulatorHelp() throws PdpSimulatorException { + final String[] pdpSimulatorConfigParameters = { "-h" }; + final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments(); + final String help = arguments.parse(pdpSimulatorConfigParameters); + assertTrue(help.startsWith("usage:")); + } + + @Test + public void testPdpSimulatorInvalidOption() throws PdpSimulatorException { + final String[] pdpSimulatorConfigParameters = { "-d" }; + final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments(); + try { + arguments.parse(pdpSimulatorConfigParameters); + } catch (final Exception exp) { + assertTrue(exp.getMessage().startsWith("invalid command line arguments specified")); + } + } +} diff --git a/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/parameters/TestPdpStatusParameters.java b/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/parameters/TestPdpStatusParameters.java new file mode 100644 index 000000000..a48082901 --- /dev/null +++ b/models-sim/policy-models-sim-pdp/src/test/java/org/onap/policy/models/sim/pdp/parameters/TestPdpStatusParameters.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.models.sim.pdp.parameters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.policy.common.parameters.GroupValidationResult; + +/** + * Class to perform unit test of {@link PdpStatusParameters}. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class TestPdpStatusParameters { + private static CommonTestData testData = new CommonTestData(); + + @Test + public void test() throws Exception { + final PdpStatusParameters pdpStatusParameters = + testData.toObject(testData.getPdpStatusParametersMap(false), PdpStatusParameters.class); + final GroupValidationResult validationResult = pdpStatusParameters.validate(); + assertTrue(validationResult.isValid()); + assertEquals(CommonTestData.TIME_INTERVAL, pdpStatusParameters.getTimeIntervalMs()); + assertEquals(CommonTestData.PDP_TYPE, pdpStatusParameters.getPdpType()); + assertEquals(CommonTestData.DESCRIPTION, pdpStatusParameters.getDescription()); + assertEquals(CommonTestData.SUPPORTED_POLICY_TYPES, pdpStatusParameters.getSupportedPolicyTypes()); + } + + @Test + public void testValidate() throws Exception { + final PdpStatusParameters pdpStatusParameters = + testData.toObject(testData.getPdpStatusParametersMap(false), PdpStatusParameters.class); + final GroupValidationResult result = pdpStatusParameters.validate(); + assertNull(result.getResult()); + assertTrue(result.isValid()); + } +} |