Add Junit to policy/engine LogParser package
[policy/engine.git] / LogParser / src / test / java / org / onap / xacml / parser / ParseLogTest.java
index b02a143..91de464 100644 (file)
@@ -31,24 +31,34 @@ import java.io.IOException;
 import java.io.InputStreamReader;
 import java.util.Date;
 import java.util.Properties;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.Mockito;
 import org.onap.policy.common.im.AdministrativeStateException;
 import org.onap.policy.common.im.IntegrityMonitor;
+import org.onap.policy.common.im.IntegrityMonitorException;
 import org.onap.policy.common.im.StandbyStatusException;
 import org.onap.policy.common.logging.flexlogger.FlexLogger;
 import org.onap.policy.common.logging.flexlogger.Logger;
 import org.onap.xacml.parser.LogEntryObject.LOGTYPE;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
 
-
+@PrepareForTest({ParseLogTest.class, IntegrityMonitor.class})
+@RunWith(PowerMockRunner.class)
 public class ParseLogTest {
        
        private static Logger logger = FlexLogger.getLogger(ParseLogTest.class);
        private Properties config = new Properties();
        private String configFile;
+       private String configFileDebug;
+       private String configFileError;
        private String testFile1;
        private String testFile2;
        private IntegrityMonitor im;
@@ -57,6 +67,7 @@ public class ParseLogTest {
        public void setUp() throws Exception {
                System.setProperty("com.sun.management.jmxremote.port", "9998");
                im = Mockito.mock(IntegrityMonitor.class);
+               String regex = "^\\/[a-zA-Z]\\:\\/";
                
                try {
                        Mockito.doNothing().when(im).startTransaction();
@@ -66,9 +77,22 @@ public class ParseLogTest {
                Mockito.doNothing().when(im).endTransaction();
                ClassLoader classLoader = getClass().getClassLoader();
                configFile = classLoader.getResource("test_config.properties").getFile();
-               if(configFile.startsWith("/C:/")){
+               configFileDebug = classLoader.getResource("test_config_debug.properties").getFile();
+               configFileError = classLoader.getResource("test_config_error.properties").getFile();
+               Pattern pattern = Pattern.compile(regex);
+               Matcher matcher = pattern.matcher(configFile);
+               Matcher matcherDebug = pattern.matcher(configFileDebug);
+               Matcher matcherError = pattern.matcher(configFileError);
+               
+               if (matcher.find()) {
                        configFile = configFile.substring(1);
                }
+               if (matcherDebug.find()) {
+                   configFileDebug = configFileDebug.substring(1);
+               }
+               if (matcherError.find()) {
+                   configFileError = configFileError.substring(1);
+               }
                testFile1 = classLoader.getResource("LineTest.txt").getFile();
                testFile2 = classLoader.getResource("LineTest2.txt").getFile();
                
@@ -116,8 +140,8 @@ public class ParseLogTest {
                
                config = new Properties();
                config.put("RESOURCE_NAME", "logparser_pap01");
-               config.put("JDBC_DRIVER" ,"com.mysql.jdbc.Driver");
-               config.put("JDBC_URL", "jdbc:mysql://localhost:3306/");
+               config.put("JDBC_DRIVER" ,"org.mariadb.jdbc.Driver");
+               config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/");
                config.put("JDBC_USER", "root");
                config.put("JDBC_PASSWORD", "password");
                config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi");
@@ -134,6 +158,56 @@ public class ParseLogTest {
                logger.debug("testGetPropertiesValue: exit");
        }
 
+       @Test
+       public void testGetPropertiesValue_1() {
+
+               logger.debug("testGetPropertiesValue: enter");
+
+               config = new Properties();
+               config.put("RESOURCE_NAME", "logparser_pap01");
+               config.put("JDBC_DRIVER", "org.mariadb.jdbc.Driver");
+               config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/");
+               config.put("JDBC_USER", "root");
+               config.put("JDBC_PASSWORD", "password");
+               config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi");
+               config.put("SERVER", "password");
+               config.put("JDBC_PASSWORD", "https://localhost:9091/pap/");
+               config.put("LOGTYPE", "PAP");
+               config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\debug\\pap-rest.log");
+               config.put("PARSERLOGPATH", "IntegrityMonitor.log");
+
+               final Properties returnConfig = ParseLog.getPropertiesValue(configFileDebug);
+               logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig);
+               assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));
+
+               logger.debug("testGetPropertiesValue_1: exit");
+           }
+
+       @Test
+       public void testGetPropertiesValue_2() {
+
+               logger.debug("testGetPropertiesValue: enter");
+
+               config = new Properties();
+               config.put("RESOURCE_NAME", "logparser_pap01");
+               config.put("JDBC_DRIVER", "org.mariadb.jdbc.Driver");
+               config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/");
+               config.put("JDBC_USER", "root");
+               config.put("JDBC_PASSWORD", "password");
+               config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi");
+               config.put("SERVER", "password");
+               config.put("JDBC_PASSWORD", "https://localhost:9091/pap/");
+               config.put("LOGTYPE", "PAP");
+               config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\error\\pap-rest.log");
+               config.put("PARSERLOGPATH", "IntegrityMonitor.log");
+
+               final Properties returnConfig = ParseLog.getPropertiesValue(configFileError);
+               logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig);
+               assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));
+
+               logger.debug("testGetPropertiesValue_2: exit");
+           }
+
        @Test
        public void testGetPropertiesFail() {   
                
@@ -482,7 +556,7 @@ public class ParseLogTest {
                im = Mockito.mock(IntegrityMonitor.class);
                try {
                        Mockito.doNothing().when(im).startTransaction();
-               } catch (StandbyStatusException | AdministrativeStateException e) {
+               } catch (IntegrityMonitorException e) {
                        fail();
                }
                Mockito.doNothing().when(im).endTransaction();
@@ -490,4 +564,39 @@ public class ParseLogTest {
                
                logger.debug("testProcess: exit");
        }
+
+       @Test
+       public void testMain() {
+               try {
+                   ParseLog.main(new String[] {});
+               } catch (final Exception e) {
+                   logger.debug("exception occured while executing the test: exit");
+               }
+       }
+       
+       @Test
+    public void testMainDebug() {
+        try {
+            final Properties returnConfig = ParseLog.getPropertiesValue(configFileDebug);
+            PowerMockito.mockStatic(IntegrityMonitor.class);
+            Mockito.when(IntegrityMonitor.getInstance("test", returnConfig)).thenReturn(im);
+            ParseLog.main(new String[] {});
+            Thread.sleep(30000);
+        } catch (final Exception e) {
+            logger.debug("exception occured while executing the test: exit");
+        }
+    }
+
+    @Test
+    public void testMainError() {
+        try {
+            final Properties returnConfig = ParseLog.getPropertiesValue(configFileError);
+            PowerMockito.mockStatic(IntegrityMonitor.class);
+            Mockito.when(IntegrityMonitor.getInstance("test", returnConfig)).thenReturn(im);
+            ParseLog.main(new String[] {});
+            Thread.sleep(30000);
+        } catch (final Exception e) {
+            logger.debug("exception occured while executing the test: exit");
+        }
+    }
 }
\ No newline at end of file