diff options
author | a.sreekumar <ajith.sreekumar@est.tech> | 2019-03-26 13:54:45 +0000 |
---|---|---|
committer | a.sreekumar <ajith.sreekumar@est.tech> | 2019-03-26 13:54:45 +0000 |
commit | 1d86d11223e9c60ec65b737301b51ca9a42adff5 (patch) | |
tree | 16a96cb29b045c7ed0cc1c3cdafae8f573095922 /services/services-onappf/src/test/java | |
parent | cfcffbce70ddc3083e337f18377c0847f7233caa (diff) |
Adding services-onappf module to apex-pdp
1) Adding services-onappf module to apex-pdp.
2) Following the code base from policy/pap to build the module.
3) Currently PAP is using property & configuration file for bootstrap.
Continuing the same in this module as well.
4) Adding relevant test cases.
Change-Id: Id9740ea60ae3ecbd88e5d6d3586ee0dde1054cca
Issue-ID: POLICY-1452
Signed-off-by: a.sreekumar <ajith.sreekumar@est.tech>
Diffstat (limited to 'services/services-onappf/src/test/java')
6 files changed, 597 insertions, 0 deletions
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 new file mode 100644 index 000000000..3b7df8321 --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterActivator.java @@ -0,0 +1,114 @@ +/*- + * ============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.assertSame; +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; + + + +/** + * 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 { + 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()); + + // repeat - should throw an exception + assertThatIllegalStateException().isThrownBy(() -> activator.initialize()); + assertTrue(activator.isAlive()); + assertTrue(activator.getParameterGroup().isValid()); + } + + @Test + public void testGetCurrent_testSetCurrent() { + assertSame(activator, ApexStarterActivator.getCurrent()); + } + + @Test + public void testTerminate() throws Exception { + activator.initialize(); + activator.terminate(); + assertFalse(activator.isAlive()); + + // 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/TestApexStarterMain.java b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterMain.java new file mode 100644 index 000000000..5d52d85c5 --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterMain.java @@ -0,0 +1,91 @@ +/*- + * ============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.assertTrue; + +import org.junit.After; +import org.junit.Test; +import org.onap.policy.apex.starter.exception.ApexStarterException; +import org.onap.policy.apex.starter.parameters.CommonTestData; + +/** + * Class to perform unit test of {@link ApexStarterMain}}. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class TestApexStarterMain { + private ApexStarterMain apexStarter; + + /** + * Shuts "main" down. + * + * @throws Exception if an error occurs + */ + @After + public void tearDown() throws Exception { + // shut down activator + final ApexStarterActivator activator = ApexStarterActivator.getCurrent(); + 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()); + + 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/exception/TestExceptions.java b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/exception/TestExceptions.java new file mode 100644 index 000000000..63ebeb608 --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/exception/TestExceptions.java @@ -0,0 +1,38 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.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 new file mode 100644 index 000000000..08a33b94c --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/CommonTestData.java @@ -0,0 +1,92 @@ +/*- + * ============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.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 int APEX_STARTER_TIME_INTERVAL = 5; + + private static final Coder coder = new StandardCoder(); + + /** + * Converts the contents of a map to a parameter class. + * + * @param source property map + * @param clazz class of object to be created from the map + * @return a new object represented by the map + */ + public <T extends ParameterGroup> T toObject(final Map<String, Object> source, final Class<T> clazz) { + try { + return coder.decode(coder.encode(source), clazz); + + } catch (final CoderException e) { + throw new RuntimeException("cannot create " + clazz.getName() + " from map", e); + } + } + + /** + * Returns a property map for a ApexStarterParameterGroup map for test cases. + * + * @param name name of the parameters + * + * @return a property map suitable for constructing an object + */ + public Map<String, Object> getApexStarterParameterGroupMap(final String name) { + final Map<String, Object> map = new TreeMap<>(); + + map.put("name", name); + map.put("timeInterval", getTimeInterval(false)); + + return map; + } + + + + /** + * Determines whether to return null or a valid time interval + * + * @param isNullField flag to determine what to return + * @return time interval based on the flag + */ + public Object getTimeInterval(final boolean isNullField) { + if (isNullField) { + return null; + } else { + return APEX_STARTER_TIME_INTERVAL; + } + + } + +} 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 new file mode 100644 index 000000000..d22455214 --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestApexStarterParameterGroup.java @@ -0,0 +1,104 @@ +/*- + * ============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 GroupValidationResult validationResult = apexStarterParameters.validate(); + assertTrue(validationResult.isValid()); + assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, apexStarterParameters.getName()); + assertEquals(CommonTestData.APEX_STARTER_TIME_INTERVAL, apexStarterParameters.getTimeInterval()); + } + + @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()); + assertEquals(CommonTestData.APEX_STARTER_TIME_INTERVAL, apexStarterParameters.getTimeInterval()); + 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()); + assertEquals(CommonTestData.APEX_STARTER_TIME_INTERVAL, apexStarterParameters.getTimeInterval()); + 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_EmptyTimeInterval() { + final Map<String, Object> map = + commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME); + map.put("timeInterval", commonTestData.getTimeInterval(true)); + final ApexStarterParameterGroup apexStarterParameters = + commonTestData.toObject(map, ApexStarterParameterGroup.class); + final GroupValidationResult validationResult = apexStarterParameters.validate(); + assertFalse(validationResult.isValid()); + assertTrue(validationResult.getResult() + .contains("field \"timeInterval\" type \"int\" value \"0\" INVALID, must be >= 1") + && validationResult.getResult().contains("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 new file mode 100644 index 000000000..3889b2aa5 --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestApexStarterParameterHandler.java @@ -0,0 +1,158 @@ +/*- + * ============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()); + assertEquals(CommonTestData.APEX_STARTER_TIME_INTERVAL, parGroup.getTimeInterval()); + } + + @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")); + } + } +} |