diff options
18 files changed, 341 insertions, 182 deletions
diff --git a/site-manager/pom.xml b/site-manager/pom.xml index 84c38b47..9a27d839 100644 --- a/site-manager/pom.xml +++ b/site-manager/pom.xml @@ -20,124 +20,158 @@ --> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> + <modelVersion>4.0.0</modelVersion> - <artifactId>site-manager</artifactId> - <packaging>jar</packaging> + <artifactId>site-manager</artifactId> + <packaging>jar</packaging> - <parent> - <groupId>org.onap.policy.common</groupId> - <artifactId>common-modules</artifactId> - <version>1.2.0-SNAPSHOT</version> - </parent> + <parent> + <groupId>org.onap.policy.common</groupId> + <artifactId>common-modules</artifactId> + <version>1.2.0-SNAPSHOT</version> + </parent> - <name>site-manager</name> + <name>site-manager</name> - <build> - <plugins> - <plugin> - <artifactId>maven-resources-plugin</artifactId> - <version>2.6</version> - <executions> - <execution> - <id>copy-resources</id> - <goals> - <goal>copy-resources</goal> - </goals> - <phase>validate</phase> - <configuration> - <outputDirectory>target/files</outputDirectory> - <resources> - <resource> - <directory>src/main/files</directory> - <filtering>true</filtering> - </resource> - </resources> - </configuration> - </execution> - </executions> - </plugin> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-shade-plugin</artifactId> - <version>1.4</version> - <executions> - <execution> - <phase>package</phase> - <goals> - <goal>shade</goal> - </goals> - <configuration> - <artifactSet> - <excludes> - <exclude>org.eclipse.persistence:javax.persistence</exclude> - </excludes> - </artifactSet> - </configuration> - </execution> - </executions> - </plugin> - <plugin> - <artifactId>maven-assembly-plugin</artifactId> - <version>2.6</version> - <executions> - <execution> - <id>zipfile</id> - <goals> - <goal>single</goal> - </goals> - <phase>package</phase> - <configuration> - <attach>true</attach> - <finalName>${project.artifactId}-${project.version}</finalName> - <descriptors> - <descriptor>src/assembly/assemble_zip.xml</descriptor> - </descriptors> - <appendAssemblyId>false</appendAssemblyId> - </configuration> - </execution> - </executions> - </plugin> - </plugins> - </build> + <build> + <plugins> + <plugin> + <artifactId>maven-checkstyle-plugin</artifactId> + <executions> + <execution> + <id>onap-java-style</id> + <goals> + <goal>check</goal> + </goals> + <phase>process-sources</phase> + <configuration> + <!-- Use Google Java Style Guide: https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml + with minor changes --> + <configLocation>onap-checkstyle/onap-java-style.xml</configLocation> + <!-- <sourceDirectory> is needed so that checkstyle + ignores the generated sources directory --> + <sourceDirectory>${project.build.sourceDirectory}</sourceDirectory> + <includeResources>true</includeResources> + <includeTestSourceDirectory>true</includeTestSourceDirectory> + <includeTestResources>true</includeTestResources> + <consoleOutput>true</consoleOutput> + <failOnViolation>true</failOnViolation> + <violationSeverity>warning</violationSeverity> + </configuration> + </execution> + </executions> + <dependencies> + <dependency> + <groupId>org.onap.oparent</groupId> + <artifactId>checkstyle</artifactId> + <version>0.1.1</version> + <scope>compile</scope> + </dependency> + </dependencies> + </plugin> + <plugin> + <artifactId>maven-resources-plugin</artifactId> + <version>2.6</version> + <executions> + <execution> + <id>copy-resources</id> + <goals> + <goal>copy-resources</goal> + </goals> + <phase>validate</phase> + <configuration> + <outputDirectory>target/files</outputDirectory> + <resources> + <resource> + <directory>src/main/files</directory> + <filtering>true</filtering> + </resource> + </resources> + </configuration> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-shade-plugin</artifactId> + <version>1.4</version> + <executions> + <execution> + <phase>package</phase> + <goals> + <goal>shade</goal> + </goals> + <configuration> + <artifactSet> + <excludes> + <exclude>org.eclipse.persistence:javax.persistence</exclude> + </excludes> + </artifactSet> + </configuration> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-assembly-plugin</artifactId> + <version>2.6</version> + <executions> + <execution> + <id>zipfile</id> + <goals> + <goal>single</goal> + </goals> + <phase>package</phase> + <configuration> + <attach>true</attach> + <finalName>${project.artifactId}-${project.version}</finalName> + <descriptors> + <descriptor>src/assembly/assemble_zip.xml</descriptor> + </descriptors> + <appendAssemblyId>false</appendAssemblyId> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> - <dependencies> - <dependency> - <groupId>org.eclipse.persistence</groupId> - <artifactId>javax.persistence</artifactId> - </dependency> - <dependency> - <groupId>org.onap.policy.common</groupId> - <artifactId>integrity-monitor</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>org.mariadb.jdbc</groupId> - <artifactId>mariadb-java-client</artifactId> - <version>${mariadb.jdbc.version}</version> - </dependency> - <dependency> - <groupId>commons-cli</groupId> - <artifactId>commons-cli</artifactId> - <version>1.3</version> - </dependency> - <dependency> - <groupId>com.h2database</groupId> - <artifactId>h2</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.eclipse.persistence</groupId> - <artifactId>eclipselink</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.onap.policy.common</groupId> - <artifactId>utils-test</artifactId> - <version>${project.version}</version> - <scope>test</scope> - </dependency> - </dependencies> + <dependencies> + <dependency> + <groupId>org.eclipse.persistence</groupId> + <artifactId>javax.persistence</artifactId> + </dependency> + <dependency> + <groupId>org.onap.policy.common</groupId> + <artifactId>integrity-monitor</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.mariadb.jdbc</groupId> + <artifactId>mariadb-java-client</artifactId> + <version>${mariadb.jdbc.version}</version> + </dependency> + <dependency> + <groupId>commons-cli</groupId> + <artifactId>commons-cli</artifactId> + <version>1.3</version> + </dependency> + <dependency> + <groupId>com.h2database</groupId> + <artifactId>h2</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.eclipse.persistence</groupId> + <artifactId>eclipselink</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.onap.policy.common</groupId> + <artifactId>utils-test</artifactId> + <version>${project.version}</version> + <scope>test</scope> + </dependency> + </dependencies> </project> diff --git a/site-manager/src/main/java/org/onap/policy/common/sitemanager/Main.java b/site-manager/src/main/java/org/onap/policy/common/sitemanager/Main.java index 780e14f0..a415c229 100644 --- a/site-manager/src/main/java/org/onap/policy/common/sitemanager/Main.java +++ b/site-manager/src/main/java/org/onap/policy/common/sitemanager/Main.java @@ -32,15 +32,19 @@ package org.onap.policy.common.sitemanager; import static org.onap.policy.common.sitemanager.utils.Constants.OPERATIONAL_PERSISTENCE_UNIT; import static org.onap.policy.common.sitemanager.utils.Constants.SITE_MANAGER_PROPERTIES_PROPERTY_NAME; import static org.onap.policy.common.sitemanager.utils.ErrorMessages.HELP_STRING; +import static org.onap.policy.common.sitemanager.utils.ErrorMessages.NO_MATCHING_ENTRIES; 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.UNLOCK; import static org.onap.policy.common.sitemanager.utils.JmxOpProcessor.jmxOp; + import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.Set; import java.util.TreeSet; + import org.onap.policy.common.im.jpa.ResourceRegistrationEntity; import org.onap.policy.common.im.jpa.StateManagementEntity; import org.onap.policy.common.sitemanager.data.service.DatabaseAccessService; @@ -76,7 +80,7 @@ public class Main { } /** - * This is the main entry point + * This is the main entry point. * * @param args these are command-line arguments to 'siteManager' */ @@ -85,6 +89,12 @@ public class Main { new Main().process(args, printable); } + /** + * Process command-line arguments. + * + * @param args args these are command-line arguments to 'siteManager' + * @param printable {@link Printable} callback to print statement + */ public void process(final String[] args, final Printable printable) { try { final CommandLineHelper commandLineHelper = new CommandLineHelper(args, printable); @@ -149,7 +159,7 @@ public class Main { } if (resourceRegistrationTable.isEmpty()) { - final String message = arg0 + ": No matching entries"; + final String message = arg0 + NO_MATCHING_ENTRIES; printable.println(message); throw new NoMatchingEntryFoundException(message); } @@ -179,6 +189,8 @@ public class Main { accessService.refreshEntity(stateManagementTable.get(r.getResourceName())); } } + } catch (final NoMatchingEntryFoundException exception) { + throw exception; } catch (final Exception exception) { printable.println(exception.getMessage()); } @@ -188,11 +200,13 @@ public class Main { } /** - * Compare two strings, either of which may be null + * Compare two strings, either of which may be null. * * @param first the first string * @param second the second string - * @return a negative value if s1<s2, 0 if they are equal, and positive if s1>s2 + * + * @return a negative value if s1 is less then s2, 0 if they are equal, and positive if s1 is + * greater then s2 */ private static int stringCompare(final String first, final String second) { if (first == null ^ second == null) { @@ -212,11 +226,12 @@ public class Main { * * @param current this is an array of length 7, containing the current maximum lengths of each * column in the tabular dump - * @param s this is an array of length 7, containing the current String entry for each column + * @param values this is an array of length 7, containing the current String entry for each + * column */ - private static void updateLengths(final int[] current, final String[] s) { - for (int i = 0; i < 7; i += 1) { - final String str = s[i]; + private static void updateLengths(final int[] current, final String[] values) { + for (int i = 0; i < 7; i++) { + final String str = values[i]; final int newLength = (str == null ? 4 : str.length()); if (current[i] < newLength) { // this column needs to be expanded @@ -226,28 +241,29 @@ public class Main { } /** - * Ordered display -- dump out all of the entries, in + * Ordered display -- dump out all of the entries in log. */ static void display() { - final TreeSet<String[]> treeset = new TreeSet<>((final String[] r1, final String[] r2) -> { + final Set<String[]> treeset = new TreeSet<>((final String[] r1, final String[] r2) -> { int rval = 0; // the first 3 columns are 'Site', 'NodeType', and 'ResourceName', // and are used to sort the entries for (int i = 0; i < 3; i += 1) { - if ((rval = stringCompare(r1[i], r2[i])) != 0) + if ((rval = stringCompare(r1[i], r2[i])) != 0) { break; + } } return (rval); }); final String[] labels = new String[] {"Site", "NodeType", "ResourceName", "AdminState", "OpState", - "AvailStatus", "StandbyStatus"}; + "AvailStatus", "StandbyStatus"}; final String[] underlines = new String[] {"----", "--------", "------------", "----------", "-------", - "-----------", "-------------"}; + "-----------", "-------------"}; // each column needs to be at least wide enough to fit the column label - final int[] lengths = new int[7]; + final int[] lengths = new int[labels.length]; updateLengths(lengths, labels); // Go through the 'resourceRegistrationTable', and generate the @@ -259,7 +275,7 @@ public class Main { // these are the entries to be displayed for this row final String[] values = new String[] {r.getSite(), r.getNodeType(), r.getResourceName(), s.getAdminState(), - s.getOpState(), s.getAvailStatus(), s.getStandbyStatus()}; + s.getOpState(), s.getAvailStatus(), s.getStandbyStatus()}; treeset.add(values); updateLengths(lengths, values); diff --git a/site-manager/src/main/java/org/onap/policy/common/sitemanager/data/service/DatabaseAccessService.java b/site-manager/src/main/java/org/onap/policy/common/sitemanager/data/service/DatabaseAccessService.java index 513ba174..4b29359a 100644 --- a/site-manager/src/main/java/org/onap/policy/common/sitemanager/data/service/DatabaseAccessService.java +++ b/site-manager/src/main/java/org/onap/policy/common/sitemanager/data/service/DatabaseAccessService.java @@ -32,22 +32,23 @@ public interface DatabaseAccessService extends AutoCloseable { <T> List<T> execute(final Class<T> clazz, final String query); /** - * Get {@link StateManagementEntity} entities from database + * Get {@link StateManagementEntity} entities from database. * - * @param rOption resource name (optional) - * @param sOption site name (optional) + * @param resourceOption resource name (optional) + * @param stateOption site name (optional) * @return list of {@link StateManagementEntity} entities found */ - List<StateManagementEntity> getStateManagementEntities(final String rOption, final String sOption); + List<StateManagementEntity> getStateManagementEntities(final String resourceOption, final String stateOption); /** - * Get {@link ResourceRegistrationEntity} entities from database + * Get {@link ResourceRegistrationEntity} entities from database. * - * @param rOption resource name (optional) - * @param sOption site name (optional) + * @param resourceOption resource name (optional) + * @param stateOption site name (optional) * @return list of {@link ResourceRegistrationEntity} entities found */ - List<ResourceRegistrationEntity> getResourceRegistrationEntities(final String rOption, final String sOption); + List<ResourceRegistrationEntity> getResourceRegistrationEntities(final String resourceOption, + final String stateOption); <T> void persist(final Collection<T> entities); diff --git a/site-manager/src/main/java/org/onap/policy/common/sitemanager/data/service/DatabaseAccessServiceImpl.java b/site-manager/src/main/java/org/onap/policy/common/sitemanager/data/service/DatabaseAccessServiceImpl.java index ee98fc29..769b164b 100644 --- a/site-manager/src/main/java/org/onap/policy/common/sitemanager/data/service/DatabaseAccessServiceImpl.java +++ b/site-manager/src/main/java/org/onap/policy/common/sitemanager/data/service/DatabaseAccessServiceImpl.java @@ -27,13 +27,16 @@ import static org.onap.policy.common.sitemanager.utils.Constants.STATE_MANAGEMEN import static org.onap.policy.common.sitemanager.utils.Constants.WHERE_R_RESOURCE_NAME; import static org.onap.policy.common.sitemanager.utils.Constants.WHERE_R_SITE_NAME; import static org.onap.policy.common.sitemanager.utils.Constants.WHERE_S_RESOURCE_NAME; + import java.util.Collection; import java.util.List; import java.util.Properties; + import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import javax.persistence.TypedQuery; + import org.onap.policy.common.im.jpa.ResourceRegistrationEntity; import org.onap.policy.common.im.jpa.StateManagementEntity; import org.onap.policy.common.utils.jpa.EntityTransCloser; @@ -47,7 +50,7 @@ public class DatabaseAccessServiceImpl implements DatabaseAccessService { this.entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName, properties); this.entityManager = entityManagerFactory.createEntityManager(); } - + public DatabaseAccessServiceImpl(final EntityManagerFactory entityManagerFactory) { this.entityManagerFactory = entityManagerFactory; this.entityManager = entityManagerFactory.createEntityManager(); @@ -69,11 +72,12 @@ public class DatabaseAccessServiceImpl implements DatabaseAccessService { } @Override - public List<StateManagementEntity> getStateManagementEntities(final String rOption, final String sOption) { - if (rOption != null) { + public List<StateManagementEntity> getStateManagementEntities(final String resourceOption, + final String stateOption) { + if (resourceOption != null) { final String query = STATE_MANAGEMENT_QUERY + WHERE_S_RESOURCE_NAME + RESOURCE_NAME; - return execute(StateManagementEntity.class, query, RESOURCE_NAME, rOption); - } else if (sOption != null) { + return execute(StateManagementEntity.class, query, RESOURCE_NAME, resourceOption); + } else if (stateOption != null) { return execute(StateManagementEntity.class, STATE_MANAGEMENT_QUERY); } return execute(StateManagementEntity.class, STATE_MANAGEMENT_QUERY); @@ -81,14 +85,14 @@ public class DatabaseAccessServiceImpl implements DatabaseAccessService { } @Override - public List<ResourceRegistrationEntity> getResourceRegistrationEntities(final String rOption, - final String sOption) { - if (rOption != null) { + public List<ResourceRegistrationEntity> getResourceRegistrationEntities(final String resourceOption, + final String stateOption) { + if (resourceOption != null) { final String query = RESOURCE_REGISTRATION_QUERY + WHERE_R_RESOURCE_NAME + RESOURCE_NAME; - return execute(ResourceRegistrationEntity.class, query, RESOURCE_NAME, rOption); - } else if (sOption != null) { + return execute(ResourceRegistrationEntity.class, query, RESOURCE_NAME, resourceOption); + } else if (stateOption != null) { final String query = RESOURCE_REGISTRATION_QUERY + WHERE_R_SITE_NAME + SITE_NAME; - return execute(ResourceRegistrationEntity.class, query, SITE_NAME, sOption); + return execute(ResourceRegistrationEntity.class, query, SITE_NAME, stateOption); } return execute(ResourceRegistrationEntity.class, RESOURCE_REGISTRATION_QUERY); } diff --git a/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/CommandLineHelper.java b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/CommandLineHelper.java index 58db7bc8..cfc68785 100644 --- a/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/CommandLineHelper.java +++ b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/CommandLineHelper.java @@ -21,6 +21,7 @@ package org.onap.policy.common.sitemanager.utils; import java.util.List; + import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.DefaultParser; @@ -76,6 +77,11 @@ public class CommandLineHelper { } } + /** + * validate given command line arguments. + * + * @return true if valid + */ public boolean isValid() { // fetch options, and remaining arguments final String sOption = commandLine.getOptionValue(SITE_ARGUMENT_NAME); diff --git a/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/ErrorMessages.java b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/ErrorMessages.java index 81145462..54695c65 100644 --- a/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/ErrorMessages.java +++ b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/ErrorMessages.java @@ -49,6 +49,8 @@ public class ErrorMessages { public static final String NO_COMMAND_SPECIFIED = "No command specified"; + public static final String NO_MATCHING_ENTRIES = ": No matching entries"; + public static final String HELP_STRING = "Usage:\n" + " siteManager show [ -s <site> | -r <resourceName> ] :\n" + " display node information\n" + " siteManager setAdminState { -s <site> | -r <resourceName> }" + " <new-state> :\n" + " update admin state on selected nodes\n" diff --git a/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/ExtraCommandLineArgument.java b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/ExtraCommandLineArgument.java index a28deece..55dfea7e 100644 --- a/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/ExtraCommandLineArgument.java +++ b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/ExtraCommandLineArgument.java @@ -119,6 +119,12 @@ public enum ExtraCommandLineArgument { return value; } + /** + * Get {@link ExtraCommandLineArgument} for given command line argument. + * + * @param value the input command line argument + * @return {@link ExtraCommandLineArgument} + */ public static ExtraCommandLineArgument getExtraCommandLineArgument(final String value) { for (final ExtraCommandLineArgument argument : ExtraCommandLineArgument.values()) { if (argument.getValue().equals(value)) { diff --git a/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/JmxOpProcessor.java b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/JmxOpProcessor.java index 2e354d3b..33a2238a 100644 --- a/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/JmxOpProcessor.java +++ b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/JmxOpProcessor.java @@ -38,10 +38,11 @@ public class JmxOpProcessor { } /** - * Process a 'lock' or 'unlock' operation on a single 'ResourceRegistrationEntity' + * Process a 'lock' or 'unlock' operation on a single {@link ResourceRegistrationEntity}. * * @param action this is the string "lock" or "unlock" - * @param resourceRegistrationEntity this is the ResourceRegistrationEntity to lock or unlock + * @param resourceRegistrationEntity this is the {@link ResourceRegistrationEntity} to lock or + * unlock */ public static void jmxOp(final String action, final ResourceRegistrationEntity resourceRegistrationEntity, final Printable printable) { diff --git a/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/PersistenceUnitPropertiesProvider.java b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/PersistenceUnitPropertiesProvider.java index 5d7abe7d..ffc48694 100644 --- a/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/PersistenceUnitPropertiesProvider.java +++ b/site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/PersistenceUnitPropertiesProvider.java @@ -26,12 +26,14 @@ import static org.onap.policy.common.sitemanager.utils.Constants.JDBC_URL_PROPER import static org.onap.policy.common.sitemanager.utils.Constants.JDBC_USER_PROPERTY_NAME; import static org.onap.policy.common.sitemanager.utils.ErrorMessages.SITE_MANAGER_PROPERY_FILE_MISSING_PROPERTY; import static org.onap.policy.common.sitemanager.utils.ErrorMessages.SITE_MANAGER_PROPERY_FILE_NOT_DEFINED_MESSAGE; + import java.io.BufferedReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Properties; + import org.onap.policy.common.sitemanager.exception.MissingPropertyException; import org.onap.policy.common.sitemanager.exception.PropertyFileProcessingException; @@ -41,6 +43,21 @@ public class PersistenceUnitPropertiesProvider { super(); } + /** + * Parser and validate properties in give property file. <br> + * valid and mandatory property name + * + * <ul> + * <li>javax.persistence.jdbc.driver</li> + * <li>javax.persistence.jdbc.url</li> + * <li>javax.persistence.jdbc.user</li> + * <li>javax.persistence.jdbc.password</li> + * </ul> + * + * @param propertiesFileName the properties filename + * @param printable {@link Printable} + * @return {@link Properties} + */ public static Properties getProperties(final String propertiesFileName, final Printable printable) { if (propertiesFileName == null) { printable.println(SITE_MANAGER_PROPERY_FILE_NOT_DEFINED_MESSAGE); @@ -61,18 +78,6 @@ public class PersistenceUnitPropertiesProvider { return properties; } - private static boolean isNotValid(final Properties properties, final String... values) { - if (values == null || values.length == 0) { - return true; - } - for (final String val : values) { - if (properties.get(val) == null) { - return true; - } - } - return false; - } - private static Properties getProperties(final Path filePath, final Printable printable) { if (!filePath.toFile().exists()) { printable.println(SITE_MANAGER_PROPERY_FILE_NOT_DEFINED_MESSAGE); @@ -91,4 +96,16 @@ public class PersistenceUnitPropertiesProvider { throw new PropertyFileProcessingException("Exception loading properties: ", exception); } } + + private static boolean isNotValid(final Properties properties, final String... values) { + if (values == null || values.length == 0) { + return true; + } + for (final String val : values) { + if (properties.get(val) == null) { + return true; + } + } + return false; + } } 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); |