From 56591de41b05c013a0775a06ee926d8d37767d24 Mon Sep 17 00:00:00 2001 From: baubakg Date: Mon, 2 Oct 2023 13:59:56 +0200 Subject: [PATCH] Fixed issue #67 --- .../tests/logparser/LogDataFactory.java | 53 ++++++++- .../campaign/tests/logparser/LogDataTest.java | 103 +++++++++++++++++- .../simpleParseDefinitionLogDataFactory.json | 1 + 3 files changed, 150 insertions(+), 7 deletions(-) create mode 100644 src/test/resources/parseDefinitions/simpleParseDefinitionLogDataFactory.json diff --git a/src/main/java/com/adobe/campaign/tests/logparser/LogDataFactory.java b/src/main/java/com/adobe/campaign/tests/logparser/LogDataFactory.java index 785052b..db708df 100644 --- a/src/main/java/com/adobe/campaign/tests/logparser/LogDataFactory.java +++ b/src/main/java/com/adobe/campaign/tests/logparser/LogDataFactory.java @@ -200,12 +200,61 @@ public static LogData generateLogData(List in_filePathList public static LogData generateLogData(String in_rootDir, String in_fileFilter, ParseDefinition in_parseDefinition) throws StringParseException, InstantiationException, IllegalAccessException { + List l_foundFilesList = findFilePaths(in_rootDir, in_fileFilter); + return generateLogData(l_foundFilesList, in_parseDefinition); + + } + + + /** + * A factory method for LogData. By default we create GenricEntries. Given a root directory path and a wildcard for + * finding files, it generates a LogDataObject containing all the data the log parser finds in the files matching + * the search query defined as a json + *

+ * Author : gandomi + * + * @param in_rootDir A starting directory to start our file search + * @param in_fileFilter A wild card pattern to start our searches + * @param in_jsonParseDefinitionFilePath The file path of a ParseDefinition + * @return A LogData Object containing the found entries from the logs + * @throws ParseDefinitionImportExportException Thrown if there is a problem with the given parseDefinition file + * @throws InstantiationException if this {@code Class} represents an abstract class, an interface, an + * array class, a primitive type, or void; or if the class has no + * nullary constructor; or if the instantiation fails for some other + * reason. + * @throws IllegalAccessException if the class or its nullary constructor is not accessible. + * @throws StringParseException When there are logical rules when parsing the given string + */ + public static LogData generateLogData(String in_rootDir, String in_fileFilter, + String in_jsonParseDefinitionFilePath) throws InstantiationException, IllegalAccessException, + StringParseException, ParseDefinitionImportExportException { + + return LogDataFactory.generateLogData(in_rootDir, in_fileFilter, + ParseDefinitionFactory.importParseDefinition(in_jsonParseDefinitionFilePath)); + } + + /** + * Given a root directory and a filter, we return a list of files that correspond to our search mechanism + * + * @param in_rootDir A starting directory to start our file search + * @param in_fileFilter A wild card pattern to start our searches + * @return a list of file paths that can be used for + */ + public static List findFilePaths(String in_rootDir, String in_fileFilter) { File l_rootDir = new File(in_rootDir); + + if (!l_rootDir.exists()) { + throw new IllegalArgumentException("The given root path directory " + in_rootDir + " does not exist."); + } + + if (!l_rootDir.isDirectory()) { + throw new IllegalArgumentException("The given root path " + in_rootDir + " is not a directory."); + } + Iterator l_foundFilesIterator = FileUtils.iterateFiles(l_rootDir, new WildcardFileFilter(in_fileFilter), TrueFileFilter.INSTANCE); List l_foundFilesList = new ArrayList<>(); l_foundFilesIterator.forEachRemaining(f -> l_foundFilesList.add(f.getAbsolutePath())); - return generateLogData(l_foundFilesList, in_parseDefinition); - + return l_foundFilesList; } } diff --git a/src/test/java/com/adobe/campaign/tests/logparser/LogDataTest.java b/src/test/java/com/adobe/campaign/tests/logparser/LogDataTest.java index 2f941b3..2d07425 100644 --- a/src/test/java/com/adobe/campaign/tests/logparser/LogDataTest.java +++ b/src/test/java/com/adobe/campaign/tests/logparser/LogDataTest.java @@ -18,10 +18,7 @@ import java.io.File; import java.io.FileFilter; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; +import java.util.*; import org.apache.commons.io.FileUtils; import org.apache.commons.io.filefilter.TrueFileFilter; @@ -1158,7 +1155,7 @@ public void testNestedFileAccess() l_apiDefinition.setStart(" /rest/head/"); l_apiDefinition.setEnd(" "); - ParseDefinition l_pDefinition = new ParseDefinition("SSL Log"); + ParseDefinition l_pDefinition = new ParseDefinition("Simple log"); l_pDefinition.setDefinitionEntries(Arrays.asList(l_verbDefinition2, l_apiDefinition)); l_pDefinition.defineKeys(Arrays.asList(l_apiDefinition, l_verbDefinition2)); @@ -1220,7 +1217,103 @@ public void testNestedFileAccess() GenericEntry l_logDataItem2_2 = l_logData2.get(l_searchItem2); assertThat("We should only have one entry for the "+l_searchItem2, l_logDataItem2_2.getFrequence(), Matchers.equalTo(2)); + } + + @Test + public void testLogDataFactoryWithJSONFileForParseDefinitionAndSearchFile() + throws InstantiationException, IllegalAccessException, StringParseException, + ParseDefinitionImportExportException { + + String l_rootPath = "src/test/resources/nestedDirs/"; + String l_fileFilter = "simple*.log"; + + final String l_jsonPath = "src/test/resources/parseDefinitions/simpleParseDefinitionLogDataFactory.json"; + + LogData l_logData = LogDataFactory.generateLogData(l_rootPath, l_fileFilter, + l_jsonPath); + + String l_searchItem1 = "extAccount/destroySharedAudience#GET"; + String l_searchItem2 = "extAccount/importSharedAudience#GET"; + + + assertThat("We should have the key for amcDataSource", + l_logData.getEntries().containsKey(l_searchItem1)); + + GenericEntry l_logDataItem2_1 = l_logData.get(l_searchItem1); + assertThat("We should only have one entry for the "+l_searchItem1, l_logDataItem2_1.getFrequence(), equalTo(1)); + + assertThat("We should have the key for amcDataSource", + l_logData.getEntries().containsKey(l_searchItem2)); + + GenericEntry l_logDataItem2_2 = l_logData.get(l_searchItem2); + assertThat("We should only have one entry for the "+l_searchItem2, l_logDataItem2_2.getFrequence(), equalTo(2)); + } + + + /******************** Search file tests ***********************/ + + @Test + public void testFileSearch() { + String l_fileFilter = "simple*.log"; + String l_rootPath = "src/test/resources/nestedDirs/"; + + List l_foundFilePaths = LogDataFactory.findFilePaths(l_rootPath, l_fileFilter); + + assertThat("We should have found 2 files", l_foundFilePaths, + Matchers.containsInAnyOrder(Matchers.endsWith("dirA/simpleLog.log"), + Matchers.endsWith("dirB/simpleLog.log"))); + } + + @Test + public void testFileSearch_negative() { + String l_fileFilter = "simple*.log"; + String l_rootPath = "src/test/resources/nonexistantDir/"; + Assert.assertThrows(IllegalArgumentException.class, () -> LogDataFactory.findFilePaths(l_rootPath, l_fileFilter)); + } + + @Test + public void testFileSearch_negative2() { + String l_fileFilter = "simple*.log"; + String l_rootPath = "src/test/resources/testng.xml"; + Assert.assertThrows(IllegalArgumentException.class, () -> LogDataFactory.findFilePaths(l_rootPath, l_fileFilter)); + } + + @Test + public void testFileSearch_negative3() { + String l_fileFilter=null; + String l_rootPath = "src/test/resources/nestedDirs"; + Assert.assertThrows(IllegalArgumentException.class, () -> LogDataFactory.findFilePaths(l_rootPath, l_fileFilter)); } + + @Test + public void testFileSearch_negative4() { + String l_fileFilter="nonExistantFile.notLog"; + String l_rootPath = "src/test/resources/nestedDirs"; + List l_foundFilePaths = LogDataFactory.findFilePaths(l_rootPath, l_fileFilter); + + assertThat("We should have found no files", l_foundFilePaths.size(), + Matchers.equalTo(0)); + } + + @Test + public void testFileSearch_negative5() { + String l_fileFilter = "simple*.log"; + String l_rootPath = "null"; + + Assert.assertThrows(IllegalArgumentException.class, () -> LogDataFactory.findFilePaths(l_rootPath, l_fileFilter)); + } + + @Test + public void testFileSearch_specific() { + String l_fileFilter = "simpleLog.log"; + String l_rootPath = "src/test/resources/nestedDirs"; + + List l_foundFilePaths = LogDataFactory.findFilePaths(l_rootPath, l_fileFilter); + assertThat("We should have found 2 files", l_foundFilePaths, + Matchers.containsInAnyOrder(Matchers.endsWith("dirA/simpleLog.log"), + Matchers.endsWith("dirB/simpleLog.log"))); + } + } diff --git a/src/test/resources/parseDefinitions/simpleParseDefinitionLogDataFactory.json b/src/test/resources/parseDefinitions/simpleParseDefinitionLogDataFactory.json new file mode 100644 index 0000000..01c423e --- /dev/null +++ b/src/test/resources/parseDefinitions/simpleParseDefinitionLogDataFactory.json @@ -0,0 +1 @@ +{"title":"Simple log","definitionEntries":[{"title":"verb","start":"\"","end":" /","caseSensitive":true,"trimQuotes":false,"toPreserve":true},{"title":"path","start":" /rest/head/","end":" ","caseSensitive":true,"trimQuotes":false,"toPreserve":true}],"keyPadding":"#","keyOrder":["path","verb"]} \ No newline at end of file