diff options
Diffstat (limited to 'site-manager/src/test/java')
9 files changed, 92 insertions, 20 deletions
diff --git a/site-manager/src/test/java/org/onap/policy/common/sitemanager/MainTest.java b/site-manager/src/test/java/org/onap/policy/common/sitemanager/MainTest.java index d8cb5f5f..e9a596c3 100644 --- a/site-manager/src/test/java/org/onap/policy/common/sitemanager/MainTest.java +++ b/site-manager/src/test/java/org/onap/policy/common/sitemanager/MainTest.java @@ -17,12 +17,14 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.onap.policy.common.sitemanager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; + import java.io.BufferedWriter; import java.io.File; import java.io.IOException; @@ -31,11 +33,13 @@ import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.Properties; + import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import javax.persistence.TypedQuery; + import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -57,6 +61,9 @@ import org.onap.policy.common.utils.jpa.EntityTransCloser; */ public class MainTest { + private static final String EXIT_CODE_TEST_EXCEPTION = "ExitCodeTestException must be thrown in MainTestRunner" + + " class when System.exit() is called in Main class"; + private static final String SITE_NAME = "SITE"; private static final String PROPERTY_FILE_NAME = "siteManagerPropertyFile.properties"; @@ -100,6 +107,12 @@ public class MainTest { private final MainTestRunner testRunner = new MainTestRunner(); + /** + * Before Test. + * + * @throws Exception throws exception if unable to create property file or + * {@link EntityManagerFactory} + */ @Before public void setUp() throws Exception { @@ -120,6 +133,9 @@ public class MainTest { testRunner.setUp(); } + /** + * Destroy. + */ @After public void destroy() { Main.stateManagementTable.clear(); @@ -188,6 +204,23 @@ public class MainTest { } @Test + public void test_process_setlockWithStateArgument() throws IOException { + persist(managerFactory.createEntityManager(), getStateManagementEntity()); + persist(managerFactory.createEntityManager(), getResourceRegistrationEntity()); + + final String[] args = new String[] {"lock", "-s", SITE_NAME}; + final PrintableImpl printable = new PrintableImpl(); + final Main objUnderTest = getMain(); + objUnderTest.process(args, printable); + + assertEquals(1, Main.resourceRegistrationTable.size()); + assertEquals(1, Main.stateManagementTable.size()); + final List<String> result = printable.getResult(); + assertFalse(result.isEmpty()); + + } + + @Test public void test_process_propertyFileNotAvailable() throws IOException { final File file = temporaryFolder.newFile("New" + PROPERTY_FILE_NAME); @@ -198,7 +231,7 @@ public class MainTest { try { final Main objUnderTest = getMain(); objUnderTest.process(args, printable); - fail("ExitCodeTestException must be thrown in MainTestRunner class when System.exit() is called in Main class"); + fail(EXIT_CODE_TEST_EXCEPTION); } catch (final ExitCodeTestException exitCodeTestException) { assertEquals(3, exitCodeTestException.exitCode); @@ -220,7 +253,7 @@ public class MainTest { try { final Main objUnderTest = getMain(); objUnderTest.process(args, printable); - fail("ExitCodeTestException must be thrown in MainTestRunner class when System.exit() is called in Main class"); + fail(EXIT_CODE_TEST_EXCEPTION); } catch (final ExitCodeTestException exitCodeTestException) { assertEquals(3, exitCodeTestException.exitCode); @@ -244,7 +277,7 @@ public class MainTest { try { final Main objUnderTest = getMain(); objUnderTest.process(args, printable); - fail("ExitCodeTestException must be thrown in MainTestRunner class when System.exit() is called in Main class"); + fail(EXIT_CODE_TEST_EXCEPTION); } catch (final ExitCodeTestException exitCodeTestException) { assertEquals(3, exitCodeTestException.exitCode); @@ -270,7 +303,7 @@ public class MainTest { try { final Main objUnderTest = getMain(); objUnderTest.process(args, printable); - fail("ExitCodeTestException must be thrown in MainTestRunner class when System.exit() is called in Main class"); + fail(EXIT_CODE_TEST_EXCEPTION); } catch (final ExitCodeTestException exitCodeTestException) { assertEquals(3, exitCodeTestException.exitCode); @@ -289,7 +322,7 @@ public class MainTest { final String[] args = new String[] {}; final Main objUnderTest = getMain(); objUnderTest.process(args, printable); - fail("ExitCodeTestException must be thrown in MainTestRunner class when System.exit() is called in Main class"); + fail(EXIT_CODE_TEST_EXCEPTION); } catch (final ExitCodeTestException exitCodeTestException) { assertEquals(2, exitCodeTestException.exitCode); @@ -312,7 +345,7 @@ public class MainTest { final String[] args = new String[] {"---", ""}; final Main objUnderTest = getMain(); objUnderTest.process(args, printable); - fail("ExitCodeTestException must be thrown in MainTestRunner class when System.exit() is called in Main class"); + fail(EXIT_CODE_TEST_EXCEPTION); } catch (final ExitCodeTestException exitCodeTestException) { assertEquals(1, exitCodeTestException.exitCode); @@ -333,7 +366,7 @@ public class MainTest { final String[] args = new String[] {"-h"}; final Main objUnderTest = getMain(); objUnderTest.process(args, printable); - fail("ExitCodeTestException must be thrown in MainTestRunner class when System.exit() is called in Main class"); + fail(EXIT_CODE_TEST_EXCEPTION); } catch (final ExitCodeTestException exitCodeTestException) { assertEquals(0, exitCodeTestException.exitCode); @@ -354,7 +387,7 @@ public class MainTest { final String[] args = new String[] {"setAdminState", "-s", RESOURCE_NAME}; final Main objUnderTest = getMain(); objUnderTest.process(args, printable); - fail("ExitCodeTestException must be thrown in MainTestRunner class when System.exit() is called in Main class"); + fail(EXIT_CODE_TEST_EXCEPTION); } catch (final ExitCodeTestException exitCodeTestException) { assertEquals(2, exitCodeTestException.exitCode); @@ -369,6 +402,27 @@ public class MainTest { } @Test + public void test_process_setAdmnStateWithValidArgs_printHelpAndErrorMessage() { + + final PrintableImpl printable = new PrintableImpl(); + try { + final String[] args = new String[] {"setAdminState", "-s", RESOURCE_NAME, RESOURCE_NAME}; + final Main objUnderTest = getMain(); + objUnderTest.process(args, printable); + fail(EXIT_CODE_TEST_EXCEPTION); + } catch (final ExitCodeTestException exitCodeTestException) { + assertEquals(4, exitCodeTestException.exitCode); + + } + assertTrue(Main.stateManagementTable.isEmpty()); + assertTrue(Main.resourceRegistrationTable.isEmpty()); + final List<String> actualMessages = printable.getResult(); + assertFalse(actualMessages.isEmpty()); + assertEquals(Arrays.asList("setAdminState" + ErrorMessages.NO_MATCHING_ENTRIES), actualMessages); + + } + + @Test public void test_process_extraShowArguments_printHelpAndErrorMessage() { final String[] args = new String[] {"show", RESOURCE_NAME}; @@ -427,7 +481,7 @@ public class MainTest { try { final Main objUnderTest = getMain(); objUnderTest.process(args, printable); - fail("ExitCodeTestException must be thrown in MainTestRunner class when System.exit() is called in Main class"); + fail(EXIT_CODE_TEST_EXCEPTION); } catch (final ExitCodeTestException exitCodeTestException) { assertEquals(2, exitCodeTestException.exitCode); @@ -448,6 +502,14 @@ public class MainTest { } } + /** + * execute given query. + * + * @param entityManager the {@link EntityManager} + * @param clazz the entity class + * @param query the query + * @return a list of the results + */ public <T> List<T> execute(final EntityManager entityManager, final Class<T> clazz, final String query) { try (final EntityMgrCloser entityMgrCloser = new EntityMgrCloser(entityManager);) { final TypedQuery<T> typedQuery = entityManager.createQuery(query, clazz); @@ -461,7 +523,7 @@ public class MainTest { DatabaseAccessService getDatabaseAccessService(final String persistenceUnitName, final Properties properties) { return new DatabaseAccessServiceImpl(managerFactory); - }; + } }; return objUnderTest; } diff --git a/site-manager/src/test/java/org/onap/policy/common/sitemanager/MainTestRunner.java b/site-manager/src/test/java/org/onap/policy/common/sitemanager/MainTestRunner.java index f763d4a4..bc0e7362 100644 --- a/site-manager/src/test/java/org/onap/policy/common/sitemanager/MainTestRunner.java +++ b/site-manager/src/test/java/org/onap/policy/common/sitemanager/MainTestRunner.java @@ -25,9 +25,12 @@ import java.security.Permission; public class MainTestRunner { private SecurityManager savedSecurityManager; - private final ExitCodeSecurityManager exitCodeSecurityManager = new ExitCodeSecurityManager();; + private final ExitCodeSecurityManager exitCodeSecurityManager = new ExitCodeSecurityManager(); private boolean isSetUp = false; + /** + * Set {@link SecurityManager} to {@link ExitCodeTestException}. + */ public void setUp() { if (!isSetUp) { savedSecurityManager = System.getSecurityManager(); @@ -36,6 +39,9 @@ public class MainTestRunner { } } + /** + * Restore save {@link SecurityManager}. + */ public void destroy() { if (isSetUp) { System.setSecurityManager(savedSecurityManager); @@ -56,12 +62,10 @@ public class MainTestRunner { private class ExitCodeSecurityManager extends SecurityManager { @Override - public void checkPermission(final Permission perm) { - } + public void checkPermission(final Permission perm) {} @Override - public void checkPermission(final Permission perm, final Object context) { - } + public void checkPermission(final Permission perm, final Object context) {} @Override public void checkExit(final int status) { diff --git a/site-manager/src/test/java/org/onap/policy/common/sitemanager/exception/IllegalCommandLineArgumentExceptionTest.java b/site-manager/src/test/java/org/onap/policy/common/sitemanager/exception/IllegalCommandLineArgumentExceptionTest.java index 73a56919..0aaa423f 100644 --- a/site-manager/src/test/java/org/onap/policy/common/sitemanager/exception/IllegalCommandLineArgumentExceptionTest.java +++ b/site-manager/src/test/java/org/onap/policy/common/sitemanager/exception/IllegalCommandLineArgumentExceptionTest.java @@ -21,8 +21,8 @@ package org.onap.policy.common.sitemanager.exception; import static org.junit.Assert.assertEquals; + import org.junit.Test; -import org.onap.policy.common.sitemanager.exception.IllegalCommandLineArgumentException; import org.onap.policy.common.utils.test.ExceptionsTester; public class IllegalCommandLineArgumentExceptionTest extends ExceptionsTester { diff --git a/site-manager/src/test/java/org/onap/policy/common/sitemanager/exception/MissingPropertyExceptionTest.java b/site-manager/src/test/java/org/onap/policy/common/sitemanager/exception/MissingPropertyExceptionTest.java index 7a9963f2..715036a7 100644 --- a/site-manager/src/test/java/org/onap/policy/common/sitemanager/exception/MissingPropertyExceptionTest.java +++ b/site-manager/src/test/java/org/onap/policy/common/sitemanager/exception/MissingPropertyExceptionTest.java @@ -21,8 +21,8 @@ package org.onap.policy.common.sitemanager.exception; import static org.junit.Assert.assertEquals; + import org.junit.Test; -import org.onap.policy.common.sitemanager.exception.MissingPropertyException; import org.onap.policy.common.utils.test.ExceptionsTester; public class MissingPropertyExceptionTest extends ExceptionsTester { diff --git a/site-manager/src/test/java/org/onap/policy/common/sitemanager/exception/NoMatchingEntryFoundExceptionTest.java b/site-manager/src/test/java/org/onap/policy/common/sitemanager/exception/NoMatchingEntryFoundExceptionTest.java index 1b7bfa32..f49ce055 100644 --- a/site-manager/src/test/java/org/onap/policy/common/sitemanager/exception/NoMatchingEntryFoundExceptionTest.java +++ b/site-manager/src/test/java/org/onap/policy/common/sitemanager/exception/NoMatchingEntryFoundExceptionTest.java @@ -21,8 +21,8 @@ package org.onap.policy.common.sitemanager.exception; import static org.junit.Assert.assertEquals; + import org.junit.Test; -import org.onap.policy.common.sitemanager.exception.NoMatchingEntryFoundException; import org.onap.policy.common.utils.test.ExceptionsTester; public class NoMatchingEntryFoundExceptionTest extends ExceptionsTester { diff --git a/site-manager/src/test/java/org/onap/policy/common/sitemanager/exception/PropertyFileProcessingExceptionTest.java b/site-manager/src/test/java/org/onap/policy/common/sitemanager/exception/PropertyFileProcessingExceptionTest.java index 5289a816..490ebb3e 100644 --- a/site-manager/src/test/java/org/onap/policy/common/sitemanager/exception/PropertyFileProcessingExceptionTest.java +++ b/site-manager/src/test/java/org/onap/policy/common/sitemanager/exception/PropertyFileProcessingExceptionTest.java @@ -21,8 +21,8 @@ package org.onap.policy.common.sitemanager.exception; import static org.junit.Assert.assertEquals; + import org.junit.Test; -import org.onap.policy.common.sitemanager.exception.PropertyFileProcessingException; import org.onap.policy.common.utils.test.ExceptionsTester; public class PropertyFileProcessingExceptionTest extends ExceptionsTester { 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 index d15c1dc9..9d58bafb 100644 --- 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 @@ -27,9 +27,11 @@ import static org.onap.policy.common.sitemanager.utils.ExtraCommandLineArgument. 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; 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 index b1b465b7..16f9d091 100644 --- 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 @@ -26,8 +26,10 @@ 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 { 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 index 5f4e4eb1..1694cf17 100644 --- 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 @@ -21,11 +21,13 @@ 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; @@ -73,7 +75,7 @@ public class PersistenceUnitPropertiesProviderTest { } @Test - public void test_getProperties_PropertyFileValidProperties_throwException() throws IOException { + public void test_getProperties_PropertyFileValidProperties() throws IOException { final Printable printable = new PrintableImpl(); final File file = temporaryFolder.newFile(PROPERTIES_FILE_NAME); |