Add Junit to policy/engine LogParser
[policy/engine.git] / LogParser / src / test / java / org / onap / xacml / parser / ParseLogTest.java
index 3cf3d18..09ffa42 100644 (file)
@@ -31,6 +31,8 @@ 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;
@@ -38,6 +40,7 @@ import org.junit.Test;
 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;
@@ -49,6 +52,8 @@ 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 +62,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 +72,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 +135,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 +153,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() {   
                
@@ -160,19 +229,6 @@ public class ParseLogTest {
                logger.debug("testParseDate: exit");
        }
 
-       @Test
-       public  void  testParseDateFail(){
-               
-               logger.debug("testParseDateFail: enter");
-               
-               String line = "2016-02-23 08:07:30";
-               Date returnValue = ParseLog.parseDate(line, "yyyy-MM-dd HH:mm:ss", true);
-               logger.debug("testParseDateFail: returnValue: " + returnValue);
-               assertEquals(null, returnValue);
-               
-               logger.debug("testParseDateFail: exit");
-       }
-
        @Test
        public void testPullLastLineRead(){
                
@@ -180,7 +236,7 @@ public class ParseLogTest {
                File file = new File(testFile1);
                String returnValue = null;
                try {
-                       returnValue = ParseLog.PullLastLineRead(file, "pap-rest.log");
+                       returnValue = ParseLog.pullLastLineRead(file, "pap-rest.log");
                        logger.debug("testPullLastLineRead: returnValue for pap-rest.log: " + returnValue);
                } catch (IOException e) {
                        fail();
@@ -188,7 +244,7 @@ public class ParseLogTest {
                assertEquals("52", returnValue);
                
                try {
-                       returnValue = ParseLog.PullLastLineRead(file, "debug.log");
+                       returnValue = ParseLog.pullLastLineRead(file, "debug.log");
                        logger.debug("testPullLastLineRead: returnValue for debug.log: " + returnValue);
                } catch (IOException e) {
                        fail();
@@ -196,7 +252,7 @@ public class ParseLogTest {
                assertEquals("17", returnValue);
                
                try {
-                       returnValue = ParseLog.PullLastLineRead(file, "error.log");
+                       returnValue = ParseLog.pullLastLineRead(file, "error.log");
                        logger.debug("testPullLastLineRead: returnValue for error.log: " + returnValue);
                } catch (IOException e) {
                        fail();
@@ -213,7 +269,7 @@ public class ParseLogTest {
                
                File file = new File("nonExistFile.txt");
                try {
-                       assertEquals(null, ParseLog.PullLastLineRead(file, "pap-rest"));
+                       assertEquals(null, ParseLog.pullLastLineRead(file, "pap-rest"));
                } catch (IOException e) {
                        fail();
                }
@@ -228,7 +284,7 @@ public class ParseLogTest {
                
                File file = new File(testFile2);
                try {
-                       assertEquals(null, ParseLog.PullLastLineRead(file, "pap-rest"));
+                       assertEquals(null, ParseLog.pullLastLineRead(file, "pap-rest"));
                } catch (IOException e) {
                        fail();
                }
@@ -495,7 +551,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();
@@ -503,4 +559,13 @@ 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");
+               }
+       }
 }
\ No newline at end of file