diff options
Diffstat (limited to 'main/src/test')
12 files changed, 455 insertions, 0 deletions
diff --git a/main/src/test/java/org/onap/policy/pap/main/TestExceptions.java b/main/src/test/java/org/onap/policy/pap/main/TestExceptions.java new file mode 100644 index 00000000..b45cdab7 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/TestExceptions.java @@ -0,0 +1,44 @@ +/*- + * ============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.pap.main; + +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; + +import org.junit.Test; + +/** + * Class to perform unit test of {@link PolicyPapException PolicyPapRuntimeException}}. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +public class TestExceptions { + + @Test + public void test() { + assertNotNull(new PolicyPapException("Message")); + assertNotNull(new PolicyPapException("Message", new IOException())); + + assertNotNull(new PolicyPapRuntimeException("Message")); + assertNotNull(new PolicyPapRuntimeException("Message", new IOException())); + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java b/main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java new file mode 100644 index 00000000..5e62ba68 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java @@ -0,0 +1,32 @@ +/*- + * ============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.pap.main.parameters; + +/** + * Class to hold/create all parameters for test cases. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +public class CommonTestData { + + public static final String PAP_GROUP_NAME = "PapGroup"; + +} diff --git a/main/src/test/java/org/onap/policy/pap/main/parameters/TestPapParameterGroup.java b/main/src/test/java/org/onap/policy/pap/main/parameters/TestPapParameterGroup.java new file mode 100644 index 00000000..086d7c22 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/parameters/TestPapParameterGroup.java @@ -0,0 +1,73 @@ +/*- + * ============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.pap.main.parameters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.policy.common.parameters.GroupValidationResult; + +/** + * Class to perform unit test of {@link PapParameterGroup}. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +public class TestPapParameterGroup { + + @Test + public void testPapParameterGroup() { + final PapParameterGroup papParameters = new PapParameterGroup(CommonTestData.PAP_GROUP_NAME); + final GroupValidationResult validationResult = papParameters.validate(); + assertTrue(validationResult.isValid()); + assertEquals(CommonTestData.PAP_GROUP_NAME, papParameters.getName()); + } + + @Test + public void testPapParameterGroup_NullName() { + final PapParameterGroup papParameters = new PapParameterGroup(null); + final GroupValidationResult validationResult = papParameters.validate(); + assertFalse(validationResult.isValid()); + assertEquals(null, papParameters.getName()); + assertTrue(validationResult.getResult().contains( + "field \"name\" type \"java.lang.String\" value \"null\" INVALID, " + "must be a non-blank string")); + } + + @Test + public void testPapParameterGroup_EmptyName() { + final PapParameterGroup papParameters = new PapParameterGroup(""); + final GroupValidationResult validationResult = papParameters.validate(); + assertFalse(validationResult.isValid()); + assertEquals("", papParameters.getName()); + assertTrue(validationResult.getResult().contains( + "field \"name\" type \"java.lang.String\" value \"\" INVALID, " + "must be a non-blank string")); + } + + @Test + public void testPapParameterGroup_SetName() { + final PapParameterGroup papParameters = new PapParameterGroup(CommonTestData.PAP_GROUP_NAME); + papParameters.setName("PapNewGroup"); + final GroupValidationResult validationResult = papParameters.validate(); + assertTrue(validationResult.isValid()); + assertEquals("PapNewGroup", papParameters.getName()); + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/parameters/TestPapParameterHandler.java b/main/src/test/java/org/onap/policy/pap/main/parameters/TestPapParameterHandler.java new file mode 100644 index 00000000..cb14748d --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/parameters/TestPapParameterHandler.java @@ -0,0 +1,166 @@ +/*- + * ============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.pap.main.parameters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.onap.policy.pap.main.PolicyPapException; +import org.onap.policy.pap.main.startstop.PapCommandLineArguments; + +/** + * Class to perform unit test of {@link PapParameterHandler}. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +public class TestPapParameterHandler { + + @Test + public void testParameterHandlerNoParameterFile() throws PolicyPapException { + final String[] noArgumentString = { "-c", "parameters/NoParameterFile.json" }; + + final PapCommandLineArguments noArguments = new PapCommandLineArguments(); + noArguments.parse(noArgumentString); + + try { + new PapParameterHandler().getParameters(noArguments); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().contains("FileNotFoundException")); + } + } + + @Test + public void testParameterHandlerEmptyParameters() throws PolicyPapException { + final String[] emptyArgumentString = { "-c", "parameters/EmptyParameters.json" }; + + final PapCommandLineArguments emptyArguments = new PapCommandLineArguments(); + emptyArguments.parse(emptyArgumentString); + + try { + new PapParameterHandler().getParameters(emptyArguments); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("no parameters found in \"parameters/EmptyParameters.json\"", e.getMessage()); + } + } + + @Test + public void testParameterHandlerInvalidParameters() throws PolicyPapException { + final String[] invalidArgumentString = { "-c", "parameters/InvalidParameters.json" }; + + final PapCommandLineArguments invalidArguments = new PapCommandLineArguments(); + invalidArguments.parse(invalidArgumentString); + + try { + new PapParameterHandler().getParameters(invalidArguments); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("error reading parameters from \"parameters/InvalidParameters.json\"\n" + + "(JsonSyntaxException):java.lang.IllegalStateException: " + + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name", e.getMessage()); + } + } + + @Test + public void testParameterHandlerNoParameters() throws PolicyPapException { + final String[] noArgumentString = { "-c", "parameters/NoParameters.json" }; + + final PapCommandLineArguments noArguments = new PapCommandLineArguments(); + noArguments.parse(noArgumentString); + + try { + new PapParameterHandler().getParameters(noArguments); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().contains( + "field \"name\" type \"java.lang.String\" value \"null\" INVALID, must be a non-blank string")); + } + } + + @Test + public void testParameterHandlerMinumumParameters() throws PolicyPapException { + final String[] minArgumentString = { "-c", "parameters/MinimumParameters.json" }; + + final PapCommandLineArguments minArguments = new PapCommandLineArguments(); + minArguments.parse(minArgumentString); + + final PapParameterGroup parGroup = new PapParameterHandler().getParameters(minArguments); + assertEquals(CommonTestData.PAP_GROUP_NAME, parGroup.getName()); + } + + @Test + public void testPapParameterGroup() throws PolicyPapException { + final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters.json" }; + + final PapCommandLineArguments arguments = new PapCommandLineArguments(); + arguments.parse(papConfigParameters); + + final PapParameterGroup parGroup = new PapParameterHandler().getParameters(arguments); + assertTrue(arguments.checkSetConfigurationFilePath()); + assertEquals(CommonTestData.PAP_GROUP_NAME, parGroup.getName()); + } + + @Test + public void testPapParameterGroup_InvalidName() throws PolicyPapException { + final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters_InvalidName.json" }; + + final PapCommandLineArguments arguments = new PapCommandLineArguments(); + arguments.parse(papConfigParameters); + + try { + new PapParameterHandler().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 testPapVersion() throws PolicyPapException { + final String[] papConfigParameters = { "-v" }; + final PapCommandLineArguments arguments = new PapCommandLineArguments(); + final String version = arguments.parse(papConfigParameters); + assertTrue(version.startsWith("ONAP Policy Framework PAP Service")); + } + + @Test + public void testPapHelp() throws PolicyPapException { + final String[] papConfigParameters = { "-h" }; + final PapCommandLineArguments arguments = new PapCommandLineArguments(); + final String help = arguments.parse(papConfigParameters); + assertTrue(help.startsWith("usage:")); + } + + @Test + public void testPapInvalidOption() throws PolicyPapException { + final String[] papConfigParameters = { "-d" }; + final PapCommandLineArguments arguments = new PapCommandLineArguments(); + try { + arguments.parse(papConfigParameters); + } catch (final Exception exp) { + assertTrue(exp.getMessage().startsWith("invalid command line arguments specified")); + } + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/startstop/TestMain.java b/main/src/test/java/org/onap/policy/pap/main/startstop/TestMain.java new file mode 100644 index 00000000..8bc544b0 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/startstop/TestMain.java @@ -0,0 +1,72 @@ +/*- + * ============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.pap.main.startstop; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.policy.pap.main.PolicyPapException; +import org.onap.policy.pap.main.parameters.CommonTestData; + +/** + * Class to perform unit test of {@link Main}}. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +public class TestMain { + + @Test + public void testMain() throws PolicyPapException { + final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters.json" }; + final Main main = new Main(papConfigParameters); + assertTrue(main.getParameters().isValid()); + assertEquals(CommonTestData.PAP_GROUP_NAME, main.getParameters().getName()); + main.shutdown(); + } + + @Test + public void testMain_NoArguments() { + final String[] papConfigParameters = {}; + final Main main = new Main(papConfigParameters); + assertTrue(main.getParameters() == null); + } + + @Test + public void testMain_InvalidArguments() { + final String[] papConfigParameters = { "parameters/PapConfigParameters.json" }; + final Main main = new Main(papConfigParameters); + assertTrue(main.getParameters() == null); + } + + @Test + public void testMain_Help() { + final String[] papConfigParameters = { "-h" }; + Main.main(papConfigParameters); + } + + @Test + public void testMain_InvalidParameters() { + final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters_InvalidName.json" }; + final Main main = new Main(papConfigParameters); + assertTrue(main.getParameters() == null); + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java b/main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java new file mode 100644 index 00000000..03c3413a --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java @@ -0,0 +1,54 @@ +/*- + * ============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.pap.main.startstop; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.policy.pap.main.PolicyPapException; +import org.onap.policy.pap.main.parameters.CommonTestData; +import org.onap.policy.pap.main.parameters.PapParameterGroup; +import org.onap.policy.pap.main.parameters.PapParameterHandler; + + +/** + * Class to perform unit test of {@link PapActivator}}. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +public class TestPapActivator { + + @Test + public void testPapActivator() throws PolicyPapException { + final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters.json" }; + + final PapCommandLineArguments arguments = new PapCommandLineArguments(papConfigParameters); + + final PapParameterGroup parGroup = new PapParameterHandler().getParameters(arguments); + + final PapActivator activator = new PapActivator(parGroup); + activator.initialize(); + assertTrue(activator.getParameterGroup().isValid()); + assertEquals(CommonTestData.PAP_GROUP_NAME, activator.getParameterGroup().getName()); + activator.terminate(); + } +} diff --git a/main/src/test/resources/parameters/EmptyParameters.json b/main/src/test/resources/parameters/EmptyParameters.json new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/main/src/test/resources/parameters/EmptyParameters.json diff --git a/main/src/test/resources/parameters/InvalidParameters.json b/main/src/test/resources/parameters/InvalidParameters.json new file mode 100644 index 00000000..de2040cc --- /dev/null +++ b/main/src/test/resources/parameters/InvalidParameters.json @@ -0,0 +1,3 @@ +{ + "name" : [] +}
\ No newline at end of file diff --git a/main/src/test/resources/parameters/MinimumParameters.json b/main/src/test/resources/parameters/MinimumParameters.json new file mode 100644 index 00000000..ec79d586 --- /dev/null +++ b/main/src/test/resources/parameters/MinimumParameters.json @@ -0,0 +1,3 @@ +{ + "name":"PapGroup" +} diff --git a/main/src/test/resources/parameters/NoParameters.json b/main/src/test/resources/parameters/NoParameters.json new file mode 100644 index 00000000..7a73a41b --- /dev/null +++ b/main/src/test/resources/parameters/NoParameters.json @@ -0,0 +1,2 @@ +{ +}
\ No newline at end of file diff --git a/main/src/test/resources/parameters/PapConfigParameters.json b/main/src/test/resources/parameters/PapConfigParameters.json new file mode 100644 index 00000000..88d00517 --- /dev/null +++ b/main/src/test/resources/parameters/PapConfigParameters.json @@ -0,0 +1,3 @@ +{ + "name":"PapGroup" +} diff --git a/main/src/test/resources/parameters/PapConfigParameters_InvalidName.json b/main/src/test/resources/parameters/PapConfigParameters_InvalidName.json new file mode 100644 index 00000000..1a466d01 --- /dev/null +++ b/main/src/test/resources/parameters/PapConfigParameters_InvalidName.json @@ -0,0 +1,3 @@ +{ + "name":" " +} |