From eabc69580026cad20f2ea5448f6cc7e604359d1f Mon Sep 17 00:00:00 2001 From: "a.sreekumar" Date: Thu, 11 Apr 2019 16:56:29 +0000 Subject: Changes for packaging services-onappf and some refactoring 1) Added the code for packaging PDP-A. 2) Updating the package names. 3) Updating the dependencies in services-onappf. Some dependencies can be removed as they are brought in while packaging. 4) Some other minor changes. Change-Id: Idf62d7854f3435806a9e8aa975138fbfd45e3ea8 Issue-ID: POLICY-1632 Signed-off-by: a.sreekumar --- .../services/onappf/TestApexStarterActivator.java | 120 ++++++++++++++ .../services/onappf/TestApexStarterConstants.java | 38 +++++ .../apex/services/onappf/TestApexStarterMain.java | 109 +++++++++++++ .../onappf/comm/TestPdpStateChangeListener.java | 173 +++++++++++++++++++++ .../onappf/comm/TestPdpUpdateListener.java | 142 +++++++++++++++++ .../services/onappf/exception/TestExceptions.java | 40 +++++ .../services/onappf/parameters/CommonTestData.java | 117 ++++++++++++++ .../parameters/TestApexStarterParameterGroup.java | 106 +++++++++++++ .../TestApexStarterParameterHandler.java | 159 +++++++++++++++++++ .../onappf/parameters/TestPdpStatusParameters.java | 59 +++++++ .../apex/starter/TestApexStarterActivator.java | 117 -------------- .../apex/starter/TestApexStarterConstants.java | 37 ----- .../policy/apex/starter/TestApexStarterMain.java | 106 ------------- .../starter/comm/TestPdpStateChangeListener.java | 171 -------------------- .../apex/starter/comm/TestPdpUpdateListener.java | 141 ----------------- .../apex/starter/exception/TestExceptions.java | 38 ----- .../apex/starter/parameters/CommonTestData.java | 116 -------------- .../parameters/TestApexStarterParameterGroup.java | 106 ------------- .../TestApexStarterParameterHandler.java | 157 ------------------- .../parameters/TestPdpStatusParameters.java | 58 ------- 20 files changed, 1063 insertions(+), 1047 deletions(-) create mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterActivator.java create mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterConstants.java create mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterMain.java create mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java create mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java create mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/exception/TestExceptions.java create mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/CommonTestData.java create mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestApexStarterParameterGroup.java create mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestApexStarterParameterHandler.java create mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestPdpStatusParameters.java delete mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterActivator.java delete mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterConstants.java delete mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterMain.java delete mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/starter/comm/TestPdpStateChangeListener.java delete mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/starter/comm/TestPdpUpdateListener.java delete mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/starter/exception/TestExceptions.java delete mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/CommonTestData.java delete mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestApexStarterParameterGroup.java delete mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestApexStarterParameterHandler.java delete mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestPdpStatusParameters.java (limited to 'services/services-onappf/src/test/java/org') diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterActivator.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterActivator.java new file mode 100644 index 000000000..238a89f66 --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterActivator.java @@ -0,0 +1,120 @@ +/*- + * ============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.apex.services.onappf; + +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.apex.services.onappf.ApexStarterActivator; +import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments; +import org.onap.policy.apex.services.onappf.ApexStarterConstants; +import org.onap.policy.apex.services.onappf.exception.ApexStarterException; +import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup; +import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler; +import org.onap.policy.apex.services.onappf.parameters.CommonTestData; +import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.models.pdp.concepts.PdpStatus; + +/** + * Class to perform unit test of {@link ApexStarterActivator}}. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class TestApexStarterActivator { + + private ApexStarterActivator activator; + + /** + * Initializes an activator. + * + * @throws Exception if an error occurs + */ + @Before + public void setUp() throws Exception { + Registry.newRegistry(); + final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json", + "-p", "src/test/resources/topic.properties" }; + final ApexStarterCommandLineArguments arguments = + new ApexStarterCommandLineArguments(apexStarterConfigParameters); + final ApexStarterParameterGroup parGroup = new ApexStarterParameterHandler().getParameters(arguments); + + final Properties props = new Properties(); + final String propFile = arguments.getFullPropertyFilePath(); + try (FileInputStream stream = new FileInputStream(propFile)) { + props.load(stream); + } + + activator = new ApexStarterActivator(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 testApexStarterActivator() throws ApexStarterException { + assertFalse(activator.isAlive()); + activator.initialize(); + assertTrue(activator.isAlive()); + assertTrue(activator.getParameterGroup().isValid()); + assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, activator.getParameterGroup().getName()); + + // ensure items were added to the registry + assertNotNull(Registry.get(ApexStarterConstants.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(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class, null)); + + // repeat - should throw an exception + assertThatIllegalStateException().isThrownBy(() -> activator.terminate()); + assertFalse(activator.isAlive()); + } +} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterConstants.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterConstants.java new file mode 100644 index 000000000..52ca94439 --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterConstants.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.apex.services.onappf; + +import org.junit.Test; +import org.onap.policy.apex.services.onappf.ApexStarterConstants; +import org.powermock.reflect.Whitebox; + +/** + * Class to perform unit test of {@link ApexStarterConstants}}. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class TestApexStarterConstants { + @Test + public void test() throws Exception { + // verify that constructor does not throw an exception + Whitebox.invokeConstructor(ApexStarterConstants.class); + } +} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterMain.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterMain.java new file mode 100644 index 000000000..c5c0fc483 --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterMain.java @@ -0,0 +1,109 @@ +/*- + * ============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.apex.services.onappf; + +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.apex.services.onappf.ApexStarterActivator; +import org.onap.policy.apex.services.onappf.ApexStarterConstants; +import org.onap.policy.apex.services.onappf.ApexStarterMain; +import org.onap.policy.apex.services.onappf.exception.ApexStarterException; +import org.onap.policy.apex.services.onappf.parameters.CommonTestData; +import org.onap.policy.common.utils.services.Registry; + +/** + * Class to perform unit test of {@link ApexStarterMain}}. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class TestApexStarterMain { + private ApexStarterMain apexStarter; + + /** + * 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 ApexStarterActivator activator = Registry.getOrDefault(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, + ApexStarterActivator.class, null); + if (activator != null && activator.isAlive()) { + activator.terminate(); + } + } + + @Test + public void testApexStarter() throws ApexStarterException { + final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json", + "-p", "src/test/resources/topic.properties" }; + apexStarter = new ApexStarterMain(apexStarterConfigParameters); + assertTrue(apexStarter.getParameters().isValid()); + assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, apexStarter.getParameters().getName()); + + // ensure items were added to the registry + assertNotNull(Registry.get(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, ApexStarterActivator.class)); + + apexStarter.shutdown(); + } + + @Test + public void testApexStarter_NoArguments() { + final String[] apexStarterConfigParameters = {}; + apexStarter = new ApexStarterMain(apexStarterConfigParameters); + assertTrue(apexStarter.getParameters() == null); + } + + @Test + public void testApexStarter_InvalidArguments() { + final String[] apexStarterConfigParameters = { "src/test/resources/ApexStarterConfigParameters.json" }; + apexStarter = new ApexStarterMain(apexStarterConfigParameters); + assertTrue(apexStarter.getParameters() == null); + } + + @Test + public void testApexStarter_Help() { + final String[] apexStarterConfigParameters = { "-h" }; + ApexStarterMain.main(apexStarterConfigParameters); + } + + @Test + public void testApexStarter_InvalidParameters() { + final String[] apexStarterConfigParameters = + { "-c", "src/test/resources/ApexStarterConfigParameters_InvalidName.json" }; + apexStarter = new ApexStarterMain(apexStarterConfigParameters); + assertTrue(apexStarter.getParameters() == null); + } +} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java new file mode 100644 index 000000000..34025a1ad --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java @@ -0,0 +1,173 @@ +/*- + * ============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.apex.services.onappf.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.apex.services.onappf.ApexStarterActivator; +import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments; +import org.onap.policy.apex.services.onappf.ApexStarterConstants; +import org.onap.policy.apex.services.onappf.comm.PdpStateChangeListener; +import org.onap.policy.apex.services.onappf.comm.PdpUpdateListener; +import org.onap.policy.apex.services.onappf.exception.ApexStarterException; +import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup; +import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler; +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.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 ApexStarterActivator activator; + + @Before + public void setUp() throws ApexStarterException, FileNotFoundException, IOException { + pdpUpdateMessageListener = new PdpUpdateListener(); + pdpStateChangeListener = new PdpStateChangeListener(); + Registry.newRegistry(); + final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json", + "-p", "src/test/resources/topic.properties" }; + final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments(); + ApexStarterParameterGroup apexStarterParameterGroup; + // The arguments return a string if there is a message to print and we should + // exit + final String argumentMessage = arguments.parse(apexStarterConfigParameters); + if (argumentMessage != null) { + return; + } + // Validate that the arguments are sane + arguments.validate(); + + // Read the parameters + apexStarterParameterGroup = new ApexStarterParameterHandler().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 ApexStarterActivator(apexStarterParameterGroup, topicProperties); + Registry.register(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, activator); + activator.initialize(); + } + + /** + * Method for cleanup after each test. + * + * @throws Exception if an error occurs + */ + @After + public void teardown() throws Exception { + + // clear the apex starter activator + if (activator != null && activator.isAlive()) { + activator.terminate(); + } + } + + /** + * @param instance + * @return + */ + 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 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 toscaPolicies = new ArrayList(); + toscaPolicies.add(toscaPolicy); + pdpUpdateMsg.setPolicies(toscaPolicies); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg); + return pdpUpdateMsg; + } + + @Test + public void testPdpStateChangeMessageListener_passivetopassive() { + final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.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(ApexStarterConstants.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/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java new file mode 100644 index 000000000..921cfd283 --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java @@ -0,0 +1,142 @@ +/*- + * ============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.apex.services.onappf.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.apex.services.onappf.ApexStarterActivator; +import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments; +import org.onap.policy.apex.services.onappf.ApexStarterConstants; +import org.onap.policy.apex.services.onappf.comm.PdpUpdateListener; +import org.onap.policy.apex.services.onappf.exception.ApexStarterException; +import org.onap.policy.apex.services.onappf.handler.PdpMessageHandler; +import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup; +import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler; +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.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 ApexStarterActivator activator; + + @Before + public void setUp() throws ApexStarterException, FileNotFoundException, IOException { + Registry.newRegistry(); + final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json", + "-p", "src/test/resources/topic.properties" }; + final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments(); + ApexStarterParameterGroup apexStarterParameterGroup; + // The arguments return a string if there is a message to print and we should + // exit + final String argumentMessage = arguments.parse(apexStarterConfigParameters); + if (argumentMessage != null) { + return; + } + // Validate that the arguments are sane + arguments.validate(); + + // Read the parameters + apexStarterParameterGroup = new ApexStarterParameterHandler().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 ApexStarterActivator(apexStarterParameterGroup, topicProperties); + Registry.register(ApexStarterConstants.REG_APEX_STARTER_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 apex starter activator + if (activator != null && activator.isAlive()) { + activator.terminate(); + } + } + + @Test + public void testPdpUpdateMssageListener() { + final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.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 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 toscaPolicies = new ArrayList(); + 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/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/exception/TestExceptions.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/exception/TestExceptions.java new file mode 100644 index 000000000..3a05e72b1 --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/exception/TestExceptions.java @@ -0,0 +1,40 @@ +/*- + * ============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.apex.services.onappf.exception; + +import org.junit.Test; +import org.onap.policy.apex.services.onappf.exception.ApexStarterException; +import org.onap.policy.apex.services.onappf.exception.ApexStarterRunTimeException; +import org.onap.policy.common.utils.test.ExceptionsTester; + +/** + * Class to perform unit test of {@link ApexStarterException ApexStarterRunTimeException}}. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class TestExceptions { + + @Test + public void test() { + new ExceptionsTester().test(ApexStarterException.class); + new ExceptionsTester().test(ApexStarterRunTimeException.class); + } +} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/CommonTestData.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/CommonTestData.java new file mode 100644 index 000000000..e4946dd2b --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/CommonTestData.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.apex.services.onappf.parameters; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +import org.onap.policy.apex.services.onappf.parameters.ToscaPolicyTypeIdentifierParameters; +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 APEX_STARTER_GROUP_NAME = "ApexStarterParameterGroup"; + 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 SUPPORTED_POLICY_TYPES = + Arrays.asList(getSupportedPolicyTypes(POLICY_NAME, POLICY_VERSION)); + + 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 toObject(final Map source, final Class 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 ApexStarterParameterGroup map for test cases. + * + * @param name name of the parameters + * + * @return a property map suitable for constructing an object + */ + public Map getApexStarterParameterGroupMap(final String name) { + final Map map = new TreeMap<>(); + + map.put("name", name); + map.put("pdpStatusParameters", getPdpStatusParametersMap(false)); + + 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 getPdpStatusParametersMap(final boolean isEmpty) { + final Map 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/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestApexStarterParameterGroup.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestApexStarterParameterGroup.java new file mode 100644 index 000000000..c843c6787 --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestApexStarterParameterGroup.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.apex.services.onappf.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 ApexStarterParameterGroup}. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class TestApexStarterParameterGroup { + CommonTestData commonTestData = new CommonTestData(); + + @Test + public void testApexStarterParameterGroup_Named() { + final ApexStarterParameterGroup apexStarterParameters = new ApexStarterParameterGroup("my-name"); + assertEquals("my-name", apexStarterParameters.getName()); + } + + @Test + public void testApexStarterParameterGroup() { + final ApexStarterParameterGroup apexStarterParameters = commonTestData.toObject( + commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME), + ApexStarterParameterGroup.class); + final PdpStatusParameters pdpStatusParameters = apexStarterParameters.getPdpStatusParameters(); + final GroupValidationResult validationResult = apexStarterParameters.validate(); + assertTrue(validationResult.isValid()); + assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, apexStarterParameters.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 testApexStarterParameterGroup_NullName() { + final ApexStarterParameterGroup apexStarterParameters = commonTestData + .toObject(commonTestData.getApexStarterParameterGroupMap(null), ApexStarterParameterGroup.class); + final GroupValidationResult validationResult = apexStarterParameters.validate(); + assertFalse(validationResult.isValid()); + assertEquals(null, apexStarterParameters.getName()); + assertTrue(validationResult.getResult().contains("is null")); + } + + @Test + public void testApexStarterParameterGroup_EmptyName() { + final ApexStarterParameterGroup apexStarterParameters = commonTestData + .toObject(commonTestData.getApexStarterParameterGroupMap(""), ApexStarterParameterGroup.class); + final GroupValidationResult validationResult = apexStarterParameters.validate(); + assertFalse(validationResult.isValid()); + assertEquals("", apexStarterParameters.getName()); + assertTrue(validationResult.getResult().contains( + "field \"name\" type \"java.lang.String\" value \"\" INVALID, " + "must be a non-blank string")); + } + + @Test + public void testApexStarterParameterGroup_SetName() { + final ApexStarterParameterGroup apexStarterParameters = commonTestData.toObject( + commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME), + ApexStarterParameterGroup.class); + apexStarterParameters.setName("ApexStarterNewGroup"); + final GroupValidationResult validationResult = apexStarterParameters.validate(); + assertTrue(validationResult.isValid()); + assertEquals("ApexStarterNewGroup", apexStarterParameters.getName()); + } + + @Test + public void testApexStarterParameterGroup_EmptyPdpStatusParameters() { + final Map map = + commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME); + map.put("pdpStatusParameters", commonTestData.getPdpStatusParametersMap(true)); + final ApexStarterParameterGroup apexStarterParameters = + commonTestData.toObject(map, ApexStarterParameterGroup.class); + final GroupValidationResult validationResult = apexStarterParameters.validate(); + assertFalse(validationResult.isValid()); + assertTrue(validationResult.getResult() + .contains("\"org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup\" INVALID, " + + "parameter group has status INVALID")); + } +} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestApexStarterParameterHandler.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestApexStarterParameterHandler.java new file mode 100644 index 000000000..f91baeb45 --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestApexStarterParameterHandler.java @@ -0,0 +1,159 @@ +/*- + * ============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.apex.services.onappf.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.apex.services.onappf.ApexStarterCommandLineArguments; +import org.onap.policy.apex.services.onappf.exception.ApexStarterException; +import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup; +import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler; +import org.onap.policy.common.utils.coder.CoderException; + +/** + * Class to perform unit test of {@link ApexStarterParameterHandler}. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class TestApexStarterParameterHandler { + + @Test + public void testParameterHandlerNoParameterFile() throws ApexStarterException { + final String[] emptyArgumentString = { "-c", "src/test/resources/NoParametersFile.json" }; + + final ApexStarterCommandLineArguments emptyArguments = new ApexStarterCommandLineArguments(); + emptyArguments.parse(emptyArgumentString); + + try { + new ApexStarterParameterHandler().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 ApexStarterException { + final String[] noArgumentString = { "-c", "src/test/resources/NoParameters.json" }; + + final ApexStarterCommandLineArguments noArguments = new ApexStarterCommandLineArguments(); + noArguments.parse(noArgumentString); + + try { + new ApexStarterParameterHandler().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 ApexStarterException { + final String[] invalidArgumentString = { "-c", "src/test/resources/InvalidParameters.json" }; + + final ApexStarterCommandLineArguments invalidArguments = new ApexStarterCommandLineArguments(); + invalidArguments.parse(invalidArgumentString); + + try { + new ApexStarterParameterHandler().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 ApexStarterException { + final String[] noArgumentString = { "-c", "src/test/resources/EmptyConfigParameters.json" }; + + final ApexStarterCommandLineArguments noArguments = new ApexStarterCommandLineArguments(); + noArguments.parse(noArgumentString); + + try { + new ApexStarterParameterHandler().getParameters(noArguments); + } catch (final Exception e) { + assertTrue(e.getMessage().contains("is null")); + } + } + + @Test + public void testApexStarterParameterGroup() throws ApexStarterException { + final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json" }; + + final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments(); + arguments.parse(apexStarterConfigParameters); + + final ApexStarterParameterGroup parGroup = new ApexStarterParameterHandler().getParameters(arguments); + assertTrue(arguments.checkSetConfigurationFilePath()); + assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, parGroup.getName()); + } + + @Test + public void testApexStarterParameterGroup_InvalidName() throws ApexStarterException { + final String[] apexStarterConfigParameters = + { "-c", "src/test/resources/ApexStarterConfigParameters_InvalidName.json" }; + + final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments(); + arguments.parse(apexStarterConfigParameters); + + try { + new ApexStarterParameterHandler().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 testApexStarterVersion() throws ApexStarterException { + final String[] apexStarterConfigParameters = { "-v" }; + final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments(); + final String version = arguments.parse(apexStarterConfigParameters); + assertTrue(version.startsWith("ONAP Policy Framework Apex Starter Service")); + } + + @Test + public void testApexStarterHelp() throws ApexStarterException { + final String[] apexStarterConfigParameters = { "-h" }; + final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments(); + final String help = arguments.parse(apexStarterConfigParameters); + assertTrue(help.startsWith("usage:")); + } + + @Test + public void testApexStarterInvalidOption() throws ApexStarterException { + final String[] apexStarterConfigParameters = { "-d" }; + final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments(); + try { + arguments.parse(apexStarterConfigParameters); + } catch (final Exception exp) { + assertTrue(exp.getMessage().startsWith("invalid command line arguments specified")); + } + } +} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestPdpStatusParameters.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestPdpStatusParameters.java new file mode 100644 index 000000000..e8a3f7ee0 --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestPdpStatusParameters.java @@ -0,0 +1,59 @@ +/*- + * ============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.apex.services.onappf.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.apex.services.onappf.parameters.PdpStatusParameters; +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()); + } +} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterActivator.java b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterActivator.java deleted file mode 100644 index 6da3a5eb6..000000000 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterActivator.java +++ /dev/null @@ -1,117 +0,0 @@ -/*- - * ============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.apex.starter; - -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.apex.starter.exception.ApexStarterException; -import org.onap.policy.apex.starter.parameters.ApexStarterParameterGroup; -import org.onap.policy.apex.starter.parameters.ApexStarterParameterHandler; -import org.onap.policy.apex.starter.parameters.CommonTestData; -import org.onap.policy.common.utils.services.Registry; -import org.onap.policy.models.pdp.concepts.PdpStatus; - -/** - * Class to perform unit test of {@link ApexStarterActivator}}. - * - * @author Ajith Sreekumar (ajith.sreekumar@est.tech) - */ -public class TestApexStarterActivator { - - private ApexStarterActivator activator; - - /** - * Initializes an activator. - * - * @throws Exception if an error occurs - */ - @Before - public void setUp() throws Exception { - Registry.newRegistry(); - final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json", - "-p", "src/test/resources/topic.properties" }; - final ApexStarterCommandLineArguments arguments = - new ApexStarterCommandLineArguments(apexStarterConfigParameters); - final ApexStarterParameterGroup parGroup = new ApexStarterParameterHandler().getParameters(arguments); - - final Properties props = new Properties(); - final String propFile = arguments.getFullPropertyFilePath(); - try (FileInputStream stream = new FileInputStream(propFile)) { - props.load(stream); - } - - activator = new ApexStarterActivator(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 testApexStarterActivator() throws ApexStarterException { - assertFalse(activator.isAlive()); - activator.initialize(); - assertTrue(activator.isAlive()); - assertTrue(activator.getParameterGroup().isValid()); - assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, activator.getParameterGroup().getName()); - - // ensure items were added to the registry - assertNotNull(Registry.get(ApexStarterConstants.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(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class, null)); - - // repeat - should throw an exception - assertThatIllegalStateException().isThrownBy(() -> activator.terminate()); - assertFalse(activator.isAlive()); - } -} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterConstants.java b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterConstants.java deleted file mode 100644 index 08e56cdf8..000000000 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterConstants.java +++ /dev/null @@ -1,37 +0,0 @@ -/*- - * ============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.apex.starter; - -import org.junit.Test; -import org.powermock.reflect.Whitebox; - -/** - * Class to perform unit test of {@link ApexStarterConstants}}. - * - * @author Ajith Sreekumar (ajith.sreekumar@est.tech) - */ -public class TestApexStarterConstants { - @Test - public void test() throws Exception { - // verify that constructor does not throw an exception - Whitebox.invokeConstructor(ApexStarterConstants.class); - } -} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterMain.java b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterMain.java deleted file mode 100644 index 3005e320a..000000000 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterMain.java +++ /dev/null @@ -1,106 +0,0 @@ -/*- - * ============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.apex.starter; - -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.apex.starter.exception.ApexStarterException; -import org.onap.policy.apex.starter.parameters.CommonTestData; -import org.onap.policy.common.utils.services.Registry; - -/** - * Class to perform unit test of {@link ApexStarterMain}}. - * - * @author Ajith Sreekumar (ajith.sreekumar@est.tech) - */ -public class TestApexStarterMain { - private ApexStarterMain apexStarter; - - /** - * 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 ApexStarterActivator activator = Registry.getOrDefault(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, - ApexStarterActivator.class, null); - if (activator != null && activator.isAlive()) { - activator.terminate(); - } - } - - @Test - public void testApexStarter() throws ApexStarterException { - final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json", - "-p", "src/test/resources/topic.properties" }; - apexStarter = new ApexStarterMain(apexStarterConfigParameters); - assertTrue(apexStarter.getParameters().isValid()); - assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, apexStarter.getParameters().getName()); - - // ensure items were added to the registry - assertNotNull(Registry.get(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, ApexStarterActivator.class)); - - apexStarter.shutdown(); - } - - @Test - public void testApexStarter_NoArguments() { - final String[] apexStarterConfigParameters = {}; - apexStarter = new ApexStarterMain(apexStarterConfigParameters); - assertTrue(apexStarter.getParameters() == null); - } - - @Test - public void testApexStarter_InvalidArguments() { - final String[] apexStarterConfigParameters = { "src/test/resources/ApexStarterConfigParameters.json" }; - apexStarter = new ApexStarterMain(apexStarterConfigParameters); - assertTrue(apexStarter.getParameters() == null); - } - - @Test - public void testApexStarter_Help() { - final String[] apexStarterConfigParameters = { "-h" }; - ApexStarterMain.main(apexStarterConfigParameters); - } - - @Test - public void testApexStarter_InvalidParameters() { - final String[] apexStarterConfigParameters = - { "-c", "src/test/resources/ApexStarterConfigParameters_InvalidName.json" }; - apexStarter = new ApexStarterMain(apexStarterConfigParameters); - assertTrue(apexStarter.getParameters() == null); - } -} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/comm/TestPdpStateChangeListener.java b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/comm/TestPdpStateChangeListener.java deleted file mode 100644 index d0a85f9f2..000000000 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/comm/TestPdpStateChangeListener.java +++ /dev/null @@ -1,171 +0,0 @@ -/*- - * ============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.apex.starter.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.apex.starter.ApexStarterActivator; -import org.onap.policy.apex.starter.ApexStarterCommandLineArguments; -import org.onap.policy.apex.starter.ApexStarterConstants; -import org.onap.policy.apex.starter.exception.ApexStarterException; -import org.onap.policy.apex.starter.parameters.ApexStarterParameterGroup; -import org.onap.policy.apex.starter.parameters.ApexStarterParameterHandler; -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.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 ApexStarterActivator activator; - - @Before - public void setUp() throws ApexStarterException, FileNotFoundException, IOException { - pdpUpdateMessageListener = new PdpUpdateListener(); - pdpStateChangeListener = new PdpStateChangeListener(); - Registry.newRegistry(); - final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json", - "-p", "src/test/resources/topic.properties" }; - final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments(); - ApexStarterParameterGroup apexStarterParameterGroup; - // The arguments return a string if there is a message to print and we should - // exit - final String argumentMessage = arguments.parse(apexStarterConfigParameters); - if (argumentMessage != null) { - return; - } - // Validate that the arguments are sane - arguments.validate(); - - // Read the parameters - apexStarterParameterGroup = new ApexStarterParameterHandler().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 ApexStarterActivator(apexStarterParameterGroup, topicProperties); - Registry.register(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, activator); - activator.initialize(); - } - - /** - * Method for cleanup after each test. - * - * @throws Exception if an error occurs - */ - @After - public void teardown() throws Exception { - - // clear the apex starter activator - if (activator != null && activator.isAlive()) { - activator.terminate(); - } - } - - /** - * @param instance - * @return - */ - 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 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 toscaPolicies = new ArrayList(); - toscaPolicies.add(toscaPolicy); - pdpUpdateMsg.setPolicies(toscaPolicies); - pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg); - return pdpUpdateMsg; - } - - @Test - public void testPdpStateChangeMessageListener_passivetopassive() { - final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.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(ApexStarterConstants.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/services/services-onappf/src/test/java/org/onap/policy/apex/starter/comm/TestPdpUpdateListener.java b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/comm/TestPdpUpdateListener.java deleted file mode 100644 index 2ffed2cb6..000000000 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/comm/TestPdpUpdateListener.java +++ /dev/null @@ -1,141 +0,0 @@ -/*- - * ============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.apex.starter.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.apex.starter.ApexStarterActivator; -import org.onap.policy.apex.starter.ApexStarterCommandLineArguments; -import org.onap.policy.apex.starter.ApexStarterConstants; -import org.onap.policy.apex.starter.exception.ApexStarterException; -import org.onap.policy.apex.starter.handler.PdpMessageHandler; -import org.onap.policy.apex.starter.parameters.ApexStarterParameterGroup; -import org.onap.policy.apex.starter.parameters.ApexStarterParameterHandler; -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.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 ApexStarterActivator activator; - - @Before - public void setUp() throws ApexStarterException, FileNotFoundException, IOException { - Registry.newRegistry(); - final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json", - "-p", "src/test/resources/topic.properties" }; - final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments(); - ApexStarterParameterGroup apexStarterParameterGroup; - // The arguments return a string if there is a message to print and we should - // exit - final String argumentMessage = arguments.parse(apexStarterConfigParameters); - if (argumentMessage != null) { - return; - } - // Validate that the arguments are sane - arguments.validate(); - - // Read the parameters - apexStarterParameterGroup = new ApexStarterParameterHandler().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 ApexStarterActivator(apexStarterParameterGroup, topicProperties); - Registry.register(ApexStarterConstants.REG_APEX_STARTER_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 apex starter activator - if (activator != null && activator.isAlive()) { - activator.terminate(); - } - } - - @Test - public void testPdpUpdateMssageListener() { - final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.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 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 toscaPolicies = new ArrayList(); - 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/services/services-onappf/src/test/java/org/onap/policy/apex/starter/exception/TestExceptions.java b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/exception/TestExceptions.java deleted file mode 100644 index 63ebeb608..000000000 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/exception/TestExceptions.java +++ /dev/null @@ -1,38 +0,0 @@ -/*- - * ============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.apex.starter.exception; - -import org.junit.Test; -import org.onap.policy.common.utils.test.ExceptionsTester; - -/** - * Class to perform unit test of {@link ApexStarterException ApexStarterRunTimeException}}. - * - * @author Ajith Sreekumar (ajith.sreekumar@est.tech) - */ -public class TestExceptions { - - @Test - public void test() { - new ExceptionsTester().test(ApexStarterException.class); - new ExceptionsTester().test(ApexStarterRunTimeException.class); - } -} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/CommonTestData.java b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/CommonTestData.java deleted file mode 100644 index d329d1f22..000000000 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/CommonTestData.java +++ /dev/null @@ -1,116 +0,0 @@ -/*- - * ============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.apex.starter.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 APEX_STARTER_GROUP_NAME = "ApexStarterParameterGroup"; - 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 SUPPORTED_POLICY_TYPES = - Arrays.asList(getSupportedPolicyTypes(POLICY_NAME, POLICY_VERSION)); - - 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 toObject(final Map source, final Class 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 ApexStarterParameterGroup map for test cases. - * - * @param name name of the parameters - * - * @return a property map suitable for constructing an object - */ - public Map getApexStarterParameterGroupMap(final String name) { - final Map map = new TreeMap<>(); - - map.put("name", name); - map.put("pdpStatusParameters", getPdpStatusParametersMap(false)); - - 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 getPdpStatusParametersMap(final boolean isEmpty) { - final Map 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/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestApexStarterParameterGroup.java b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestApexStarterParameterGroup.java deleted file mode 100644 index 8d5f1cef9..000000000 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestApexStarterParameterGroup.java +++ /dev/null @@ -1,106 +0,0 @@ -/*- - * ============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.apex.starter.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 ApexStarterParameterGroup}. - * - * @author Ajith Sreekumar (ajith.sreekumar@est.tech) - */ -public class TestApexStarterParameterGroup { - CommonTestData commonTestData = new CommonTestData(); - - @Test - public void testApexStarterParameterGroup_Named() { - final ApexStarterParameterGroup apexStarterParameters = new ApexStarterParameterGroup("my-name"); - assertEquals("my-name", apexStarterParameters.getName()); - } - - @Test - public void testApexStarterParameterGroup() { - final ApexStarterParameterGroup apexStarterParameters = commonTestData.toObject( - commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME), - ApexStarterParameterGroup.class); - final PdpStatusParameters pdpStatusParameters = apexStarterParameters.getPdpStatusParameters(); - final GroupValidationResult validationResult = apexStarterParameters.validate(); - assertTrue(validationResult.isValid()); - assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, apexStarterParameters.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 testApexStarterParameterGroup_NullName() { - final ApexStarterParameterGroup apexStarterParameters = commonTestData - .toObject(commonTestData.getApexStarterParameterGroupMap(null), ApexStarterParameterGroup.class); - final GroupValidationResult validationResult = apexStarterParameters.validate(); - assertFalse(validationResult.isValid()); - assertEquals(null, apexStarterParameters.getName()); - assertTrue(validationResult.getResult().contains("is null")); - } - - @Test - public void testApexStarterParameterGroup_EmptyName() { - final ApexStarterParameterGroup apexStarterParameters = commonTestData - .toObject(commonTestData.getApexStarterParameterGroupMap(""), ApexStarterParameterGroup.class); - final GroupValidationResult validationResult = apexStarterParameters.validate(); - assertFalse(validationResult.isValid()); - assertEquals("", apexStarterParameters.getName()); - assertTrue(validationResult.getResult().contains( - "field \"name\" type \"java.lang.String\" value \"\" INVALID, " + "must be a non-blank string")); - } - - @Test - public void testApexStarterParameterGroup_SetName() { - final ApexStarterParameterGroup apexStarterParameters = commonTestData.toObject( - commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME), - ApexStarterParameterGroup.class); - apexStarterParameters.setName("ApexStarterNewGroup"); - final GroupValidationResult validationResult = apexStarterParameters.validate(); - assertTrue(validationResult.isValid()); - assertEquals("ApexStarterNewGroup", apexStarterParameters.getName()); - } - - @Test - public void testApexStarterParameterGroup_EmptyPdpStatusParameters() { - final Map map = - commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME); - map.put("pdpStatusParameters", commonTestData.getPdpStatusParametersMap(true)); - final ApexStarterParameterGroup apexStarterParameters = - commonTestData.toObject(map, ApexStarterParameterGroup.class); - final GroupValidationResult validationResult = apexStarterParameters.validate(); - assertFalse(validationResult.isValid()); - assertTrue(validationResult.getResult() - .contains("\"org.onap.policy.apex.starter.parameters.ApexStarterParameterGroup\" INVALID, " - + "parameter group has status INVALID")); - } -} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestApexStarterParameterHandler.java b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestApexStarterParameterHandler.java deleted file mode 100644 index baa91d23a..000000000 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestApexStarterParameterHandler.java +++ /dev/null @@ -1,157 +0,0 @@ -/*- - * ============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.apex.starter.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.apex.starter.ApexStarterCommandLineArguments; -import org.onap.policy.apex.starter.exception.ApexStarterException; -import org.onap.policy.common.utils.coder.CoderException; - -/** - * Class to perform unit test of {@link ApexStarterParameterHandler}. - * - * @author Ajith Sreekumar (ajith.sreekumar@est.tech) - */ -public class TestApexStarterParameterHandler { - - @Test - public void testParameterHandlerNoParameterFile() throws ApexStarterException { - final String[] emptyArgumentString = { "-c", "src/test/resources/NoParametersFile.json" }; - - final ApexStarterCommandLineArguments emptyArguments = new ApexStarterCommandLineArguments(); - emptyArguments.parse(emptyArgumentString); - - try { - new ApexStarterParameterHandler().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 ApexStarterException { - final String[] noArgumentString = { "-c", "src/test/resources/NoParameters.json" }; - - final ApexStarterCommandLineArguments noArguments = new ApexStarterCommandLineArguments(); - noArguments.parse(noArgumentString); - - try { - new ApexStarterParameterHandler().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 ApexStarterException { - final String[] invalidArgumentString = { "-c", "src/test/resources/InvalidParameters.json" }; - - final ApexStarterCommandLineArguments invalidArguments = new ApexStarterCommandLineArguments(); - invalidArguments.parse(invalidArgumentString); - - try { - new ApexStarterParameterHandler().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 ApexStarterException { - final String[] noArgumentString = { "-c", "src/test/resources/EmptyConfigParameters.json" }; - - final ApexStarterCommandLineArguments noArguments = new ApexStarterCommandLineArguments(); - noArguments.parse(noArgumentString); - - try { - new ApexStarterParameterHandler().getParameters(noArguments); - } catch (final Exception e) { - assertTrue(e.getMessage().contains("is null")); - } - } - - @Test - public void testApexStarterParameterGroup() throws ApexStarterException { - final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json" }; - - final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments(); - arguments.parse(apexStarterConfigParameters); - - final ApexStarterParameterGroup parGroup = new ApexStarterParameterHandler().getParameters(arguments); - assertTrue(arguments.checkSetConfigurationFilePath()); - assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, parGroup.getName()); - } - - @Test - public void testApexStarterParameterGroup_InvalidName() throws ApexStarterException { - final String[] apexStarterConfigParameters = - { "-c", "src/test/resources/ApexStarterConfigParameters_InvalidName.json" }; - - final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments(); - arguments.parse(apexStarterConfigParameters); - - try { - new ApexStarterParameterHandler().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 testApexStarterVersion() throws ApexStarterException { - final String[] apexStarterConfigParameters = { "-v" }; - final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments(); - final String version = arguments.parse(apexStarterConfigParameters); - assertTrue(version.startsWith("ONAP Policy Framework Apex Starter Service")); - } - - @Test - public void testApexStarterHelp() throws ApexStarterException { - final String[] apexStarterConfigParameters = { "-h" }; - final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments(); - final String help = arguments.parse(apexStarterConfigParameters); - assertTrue(help.startsWith("usage:")); - } - - @Test - public void testApexStarterInvalidOption() throws ApexStarterException { - final String[] apexStarterConfigParameters = { "-d" }; - final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments(); - try { - arguments.parse(apexStarterConfigParameters); - } catch (final Exception exp) { - assertTrue(exp.getMessage().startsWith("invalid command line arguments specified")); - } - } -} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestPdpStatusParameters.java b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestPdpStatusParameters.java deleted file mode 100644 index 37cbbe021..000000000 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestPdpStatusParameters.java +++ /dev/null @@ -1,58 +0,0 @@ -/*- - * ============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.apex.starter.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()); - } -} -- cgit 1.2.3-korg