Removed checkstyle warnings 01/39701/2
authoreikrwaq <waqas.ikram@ericsson.com>
Wed, 28 Mar 2018 14:50:57 +0000 (15:50 +0100)
committereikrwaq <waqas.ikram@ericsson.com>
Wed, 28 Mar 2018 14:53:34 +0000 (15:53 +0100)
Change-Id: I350bbd414477fe0b480c1bc4e7fa60b1e63842a7
Issue-ID: POLICY-712
Signed-off-by: eikrwaq <waqas.ikram@ericsson.com>
18 files changed:
site-manager/pom.xml
site-manager/src/main/java/org/onap/policy/common/sitemanager/Main.java
site-manager/src/main/java/org/onap/policy/common/sitemanager/data/service/DatabaseAccessService.java
site-manager/src/main/java/org/onap/policy/common/sitemanager/data/service/DatabaseAccessServiceImpl.java
site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/CommandLineHelper.java
site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/ErrorMessages.java
site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/ExtraCommandLineArgument.java
site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/JmxOpProcessor.java
site-manager/src/main/java/org/onap/policy/common/sitemanager/utils/PersistenceUnitPropertiesProvider.java
site-manager/src/test/java/org/onap/policy/common/sitemanager/MainTest.java
site-manager/src/test/java/org/onap/policy/common/sitemanager/MainTestRunner.java
site-manager/src/test/java/org/onap/policy/common/sitemanager/exception/IllegalCommandLineArgumentExceptionTest.java
site-manager/src/test/java/org/onap/policy/common/sitemanager/exception/MissingPropertyExceptionTest.java
site-manager/src/test/java/org/onap/policy/common/sitemanager/exception/NoMatchingEntryFoundExceptionTest.java
site-manager/src/test/java/org/onap/policy/common/sitemanager/exception/PropertyFileProcessingExceptionTest.java
site-manager/src/test/java/org/onap/policy/common/sitemanager/utils/CommandLineHelperTest.java
site-manager/src/test/java/org/onap/policy/common/sitemanager/utils/ExtraCommandLineArgumentTest.java
site-manager/src/test/java/org/onap/policy/common/sitemanager/utils/PersistenceUnitPropertiesProviderTest.java

index 84c38b4..9a27d83 100644 (file)
   -->
 
 <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>
index 780e14f..a415c22 100644 (file)
@@ -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);
index 513ba17..4b29359 100644 (file)
@@ -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);
 
index ee98fc2..769b164 100644 (file)
@@ -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);
     }
index 58db7bc..cfc6878 100644 (file)
@@ -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);
index 8114546..54695c6 100644 (file)
@@ -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"
index a28deec..55dfea7 100644 (file)
@@ -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)) {
index 2e354d3..33a2238 100644 (file)
@@ -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) {
index 5d7abe7..ffc4869 100644 (file)
@@ -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;
+    }
 }
index d8cb5f5..e9a596c 100644 (file)
  * 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();
@@ -187,6 +203,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 {
 
@@ -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);
 
@@ -368,6 +401,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;
     }
index f763d4a..bc0e736 100644 (file)
@@ -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) {
index 73a5691..0aaa423 100644 (file)
@@ -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 {
index 7a9963f..715036a 100644 (file)
@@ -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 {
index 1b7bfa3..f49ce05 100644 (file)
@@ -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 {
index 5289a81..490ebb3 100644 (file)
@@ -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 {
index d15c1dc..9d58baf 100644 (file)
@@ -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;
 
index b1b465b..16f9d09 100644 (file)
@@ -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 {
index 5f4e4eb..1694cf1 100644 (file)
 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);