diff options
Diffstat (limited to 'site-manager/src/test/java/org/onap/policy/common/sitemanager/utils')
4 files changed, 414 insertions, 0 deletions
diff --git a/site-manager/src/test/java/org/onap/policy/common/sitemanager/utils/CommandLineHelperTest.java b/site-manager/src/test/java/org/onap/policy/common/sitemanager/utils/CommandLineHelperTest.java new file mode 100644 index 00000000..d15c1dc9 --- /dev/null +++ b/site-manager/src/test/java/org/onap/policy/common/sitemanager/utils/CommandLineHelperTest.java @@ -0,0 +1,194 @@ +/*- + * ============LICENSE_START======================================================= + * site-manager + * ================================================================================ + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.sitemanager.utils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.onap.policy.common.sitemanager.utils.ExtraCommandLineArgument.LOCK; +import static org.onap.policy.common.sitemanager.utils.ExtraCommandLineArgument.SET_ADMIN_STATE; +import static org.onap.policy.common.sitemanager.utils.ExtraCommandLineArgument.SHOW; +import static org.onap.policy.common.sitemanager.utils.ExtraCommandLineArgument.UNLOCK; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import org.junit.Test; +import org.onap.policy.common.sitemanager.exception.IllegalCommandLineArgumentException; + +public class CommandLineHelperTest { + + private static final String STATE_NAME = "STATE_NAME"; + private static final String RESOURCE_NAME = "RESOURCE_NAME"; + + @Test + public void test_CommandLineHelper_emptyArgs() throws IOException { + final String[] args = new String[] {}; + final PrintableImpl printable = new PrintableImpl(); + final CommandLineHelper objUnderTest = new CommandLineHelper(args, printable); + + assertFalse(objUnderTest.isValid()); + assertEquals(Arrays.asList(ErrorMessages.NO_COMMAND_SPECIFIED), printable.getResult()); + } + + @Test + public void test_CommandLineHelper_NullArgs() throws IOException { + final String[] args = null; + final PrintableImpl printable = new PrintableImpl(); + final CommandLineHelper objUnderTest = new CommandLineHelper(args, printable); + + assertFalse(objUnderTest.isValid()); + assertEquals(Arrays.asList(ErrorMessages.NO_COMMAND_SPECIFIED), printable.getResult()); + } + + @Test + public void test_CommandLineHelper_ShowValidArgs() throws IOException { + final String[] args = new String[] {SHOW.getValue(), "-s", RESOURCE_NAME}; + final PrintableImpl printable = new PrintableImpl(); + final CommandLineHelper objUnderTest = new CommandLineHelper(args, printable); + + assertTrue(objUnderTest.isValid()); + assertTrue(printable.getResult().isEmpty()); + } + + @Test + public void test_CommandLineHelper_LockValidArgs() throws IOException { + final String[] args = new String[] {LOCK.getValue(), "-s", RESOURCE_NAME}; + final PrintableImpl printable = new PrintableImpl(); + final CommandLineHelper objUnderTest = new CommandLineHelper(args, printable); + + assertTrue(objUnderTest.isValid()); + assertTrue(printable.getResult().isEmpty()); + } + + @Test + public void test_CommandLineHelper_UnLockValidArgs() throws IOException { + final String[] args = new String[] {UNLOCK.getValue(), "-s", RESOURCE_NAME}; + final PrintableImpl printable = new PrintableImpl(); + final CommandLineHelper objUnderTest = new CommandLineHelper(args, printable); + + assertTrue(objUnderTest.isValid()); + assertTrue(printable.getResult().isEmpty()); + } + + @Test + public void test_CommandLineHelper_SetAdminStateValidArgs() throws IOException { + final String[] args = new String[] {SET_ADMIN_STATE.getValue(), RESOURCE_NAME, "-r", RESOURCE_NAME}; + final PrintableImpl printable = new PrintableImpl(); + final CommandLineHelper objUnderTest = new CommandLineHelper(args, printable); + + assertTrue(objUnderTest.isValid()); + assertTrue(printable.getResult().isEmpty()); + assertEquals(RESOURCE_NAME, objUnderTest.getResourceName()); + } + + @Test + public void test_CommandLineHelper_SetAdminStateWithStateValidArgs() throws IOException { + final String[] args = new String[] {SET_ADMIN_STATE.getValue(), RESOURCE_NAME, "-s", STATE_NAME}; + final PrintableImpl printable = new PrintableImpl(); + final CommandLineHelper objUnderTest = new CommandLineHelper(args, printable); + + assertTrue(objUnderTest.isValid()); + assertTrue(printable.getResult().isEmpty()); + assertEquals(STATE_NAME, objUnderTest.getSite()); + + assertEquals(Arrays.asList(SET_ADMIN_STATE.getValue(), RESOURCE_NAME), objUnderTest.getArgList()); + } + + @Test + public void test_CommandLineHelper_HelpValidArgs() throws IOException { + final String[] args = new String[] {"-h"}; + final PrintableImpl printable = new PrintableImpl(); + final CommandLineHelper objUnderTest = new CommandLineHelper(args, printable); + assertTrue(objUnderTest.isHelpArgumentSet()); + } + + @Test(expected = IllegalCommandLineArgumentException.class) + public void test_CommandLineHelper_invaidArgs_printHelp() throws IOException { + + final PrintableImpl printable = new PrintableImpl(); + + final String[] args = new String[] {"---", ""}; + final CommandLineHelper objUnderTest = new CommandLineHelper(args, printable); + + assertFalse(objUnderTest.isValid()); + + } + + @Test + public void test_CommandLineHelper_invaidArgsMissingAttributes_printHelp() throws IOException { + + final PrintableImpl printable = new PrintableImpl(); + final String[] args = new String[] {SET_ADMIN_STATE.getValue(), "-s", RESOURCE_NAME}; + final CommandLineHelper objUnderTest = new CommandLineHelper(args, printable); + + assertFalse(objUnderTest.isValid()); + final List<String> actualMessages = printable.getResult(); + + assertEquals(Arrays.asList(ErrorMessages.SET_ADMIN_STATE_MISSING_NEW_STATE_VALUE), actualMessages); + + } + + @Test + public void test_CommandLineHelper_invaidShowArgsExtraAttributes_printHelp() throws IOException { + + final PrintableImpl printable = new PrintableImpl(); + final String[] args = new String[] {SHOW.getValue(), RESOURCE_NAME}; + final CommandLineHelper objUnderTest = new CommandLineHelper(args, printable); + + assertFalse(objUnderTest.isValid()); + final List<String> actualMessages = printable.getResult(); + + assertEquals(Arrays.asList(ErrorMessages.SHOW_EXTRA_ARGUMENTS), actualMessages); + + } + + @Test + public void test_CommandLineHelper_invaidLockArgsExtraAttributes_printHelp() throws IOException { + + final PrintableImpl printable = new PrintableImpl(); + final String[] args = new String[] {LOCK.getValue(), RESOURCE_NAME}; + final CommandLineHelper objUnderTest = new CommandLineHelper(args, printable); + + assertFalse(objUnderTest.isValid()); + final List<String> actualMessages = printable.getResult(); + + assertEquals( + Arrays.asList(ErrorMessages.LOCK_EXTRA_ARGUMENTS, ErrorMessages.LOCK_EITHER_S_OR_R_OPTION_IS_NEEDED), + actualMessages); + + } + + @Test + public void test_CommandLineHelper_invaidUnLockArgsExtraAttributes_printHelp() throws IOException { + + final PrintableImpl printable = new PrintableImpl(); + final String[] args = new String[] {UNLOCK.getValue(), RESOURCE_NAME}; + final CommandLineHelper objUnderTest = new CommandLineHelper(args, printable); + + assertFalse(objUnderTest.isValid()); + final List<String> actualMessages = printable.getResult(); + + assertEquals(Arrays.asList(ErrorMessages.UNLOCK_EXTRA_ARGUMENTS, + ErrorMessages.UNLOCK_EITHER_S_OR_R_OPTION_IS_NEEDED), actualMessages); + + } + +} diff --git a/site-manager/src/test/java/org/onap/policy/common/sitemanager/utils/ExtraCommandLineArgumentTest.java b/site-manager/src/test/java/org/onap/policy/common/sitemanager/utils/ExtraCommandLineArgumentTest.java new file mode 100644 index 00000000..b1b465b7 --- /dev/null +++ b/site-manager/src/test/java/org/onap/policy/common/sitemanager/utils/ExtraCommandLineArgumentTest.java @@ -0,0 +1,84 @@ +/*- + * ============LICENSE_START======================================================= + * site-manager + * ================================================================================ + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.sitemanager.utils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.onap.policy.common.sitemanager.utils.ExtraCommandLineArgument.LOCK; +import static org.onap.policy.common.sitemanager.utils.ExtraCommandLineArgument.SET_ADMIN_STATE; +import static org.onap.policy.common.sitemanager.utils.ExtraCommandLineArgument.SHOW; +import java.io.IOException; +import java.util.Arrays; +import org.junit.Test; + +public class ExtraCommandLineArgumentTest { + + private static final String EMPTY_STRING = ""; + private static final String RESOURCE_NAME = "RESOURCE_NAME"; + + @Test + public void test_ExtraCommandLineArgument_ShowValidArgs() throws IOException { + final PrintableImpl printable = new PrintableImpl(); + + final ExtraCommandLineArgument objUnderTest = + ExtraCommandLineArgument.getExtraCommandLineArgument(SHOW.getValue()); + + assertTrue(objUnderTest.isValid(Arrays.asList(SHOW.getValue()), printable)); + assertTrue(printable.getResult().isEmpty()); + } + + @Test + public void test_ExtraCommandLineArgument_SetAdminStateValidArgs() throws IOException { + final PrintableImpl printable = new PrintableImpl(); + + final ExtraCommandLineArgument objUnderTest = + ExtraCommandLineArgument.getExtraCommandLineArgument(SET_ADMIN_STATE.getValue()); + + assertTrue(objUnderTest.isValid(Arrays.asList(SET_ADMIN_STATE.getValue(), RESOURCE_NAME), printable, true)); + assertTrue(printable.getResult().isEmpty()); + } + + @Test + public void test_ExtraCommandLineArgument_LockStateValidArgs() throws IOException { + final PrintableImpl printable = new PrintableImpl(); + + final ExtraCommandLineArgument objUnderTest = + ExtraCommandLineArgument.getExtraCommandLineArgument(LOCK.getValue()); + + assertTrue(objUnderTest.isValid(Arrays.asList(LOCK.getValue()), printable, true)); + assertTrue(printable.getResult().isEmpty()); + } + + @Test + public void test_ExtraCommandLineArgument_InValidArgs() throws IOException { + final PrintableImpl printable = new PrintableImpl(); + + final ExtraCommandLineArgument objUnderTest = + ExtraCommandLineArgument.getExtraCommandLineArgument(EMPTY_STRING); + + assertTrue(objUnderTest.equals(ExtraCommandLineArgument.INVALID)); + assertFalse(objUnderTest.isValid(Arrays.asList(EMPTY_STRING), printable, false)); + assertFalse(printable.getResult().isEmpty()); + assertEquals(Arrays.asList(ErrorMessages.UNKNOWN_COMMAND), printable.getResult()); + } + +} diff --git a/site-manager/src/test/java/org/onap/policy/common/sitemanager/utils/PersistenceUnitPropertiesProviderTest.java b/site-manager/src/test/java/org/onap/policy/common/sitemanager/utils/PersistenceUnitPropertiesProviderTest.java new file mode 100644 index 00000000..5f4e4eb1 --- /dev/null +++ b/site-manager/src/test/java/org/onap/policy/common/sitemanager/utils/PersistenceUnitPropertiesProviderTest.java @@ -0,0 +1,97 @@ +/*- + * ============LICENSE_START======================================================= + * site-manager + * ================================================================================ + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.sitemanager.utils; + +import static org.junit.Assert.assertEquals; +import java.io.BufferedWriter; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.Properties; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.onap.policy.common.sitemanager.exception.MissingPropertyException; +import org.onap.policy.common.sitemanager.exception.PropertyFileProcessingException; + +public class PersistenceUnitPropertiesProviderTest { + private static final String PROPERTIES_FILE_NAME = "file.properties"; + + private static final String COMMENTS = ""; + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Test(expected = PropertyFileProcessingException.class) + public void test_getProperties_null_throwException() { + final Printable printable = new PrintableImpl(); + + PersistenceUnitPropertiesProvider.getProperties(null, printable); + } + + @Test(expected = PropertyFileProcessingException.class) + public void test_getProperties_emptyString_throwException() { + final Printable printable = new PrintableImpl(); + + PersistenceUnitPropertiesProvider.getProperties("", printable); + } + + @Test(expected = MissingPropertyException.class) + public void test_getProperties_emptyPropertyFile_throwException() throws IOException { + final Printable printable = new PrintableImpl(); + final File file = temporaryFolder.newFile(PROPERTIES_FILE_NAME); + creatPropertyFile(file, new Properties()); + PersistenceUnitPropertiesProvider.getProperties(file.toString(), printable); + } + + @Test(expected = MissingPropertyException.class) + public void test_getProperties_PropertyFileWithMissingProperties_throwException() throws IOException { + final Printable printable = new PrintableImpl(); + final File file = temporaryFolder.newFile(PROPERTIES_FILE_NAME); + final Properties properties = new Properties(); + properties.put(Constants.JDBC_DRIVER_PROPERTY_NAME, "org.h2"); + creatPropertyFile(file, properties); + PersistenceUnitPropertiesProvider.getProperties(file.toString(), printable); + } + + @Test + public void test_getProperties_PropertyFileValidProperties_throwException() throws IOException { + final Printable printable = new PrintableImpl(); + final File file = temporaryFolder.newFile(PROPERTIES_FILE_NAME); + + final Properties properties = new Properties(); + properties.put(Constants.JDBC_DRIVER_PROPERTY_NAME, "Driver"); + properties.put(Constants.JDBC_URL_PROPERTY_NAME, "inMem:Database"); + properties.put(Constants.JDBC_USER_PROPERTY_NAME, "test"); + properties.put(Constants.JDBC_PASSWORD_PROPERTY_NAME, "test"); + creatPropertyFile(file, properties); + final Properties actualProperties = PersistenceUnitPropertiesProvider.getProperties(file.toString(), printable); + + assertEquals(properties, actualProperties); + } + + private void creatPropertyFile(final File file, final Properties properties) throws IOException { + try (final BufferedWriter bufferedWriter = Files.newBufferedWriter(file.toPath());) { + properties.store(bufferedWriter, COMMENTS); + } + } + +} diff --git a/site-manager/src/test/java/org/onap/policy/common/sitemanager/utils/PrintableImpl.java b/site-manager/src/test/java/org/onap/policy/common/sitemanager/utils/PrintableImpl.java new file mode 100644 index 00000000..dd0512fa --- /dev/null +++ b/site-manager/src/test/java/org/onap/policy/common/sitemanager/utils/PrintableImpl.java @@ -0,0 +1,39 @@ +/*- + * ============LICENSE_START======================================================= + * site-manager + * ================================================================================ + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.sitemanager.utils; + +import java.util.ArrayList; +import java.util.List; + +public class PrintableImpl implements Printable { + + private final List<String> result = new ArrayList<>(); + + @Override + public void println(final String value) { + result.add(value); + } + + public List<String> getResult() { + return result; + } + +} |