Skip to content

Commit

Permalink
Fixed issue #67
Browse files Browse the repository at this point in the history
  • Loading branch information
baubakg committed Oct 2, 2023
1 parent ba3e682 commit 56591de
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,61 @@ public static LogData<GenericEntry> generateLogData(List<String> in_filePathList
public static LogData<GenericEntry> generateLogData(String in_rootDir, String in_fileFilter,
ParseDefinition in_parseDefinition)
throws StringParseException, InstantiationException, IllegalAccessException {
List<String> 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
* <p>
* 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<GenericEntry> 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<String> 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<File> l_foundFilesIterator = FileUtils.iterateFiles(l_rootDir, new WildcardFileFilter(in_fileFilter),
TrueFileFilter.INSTANCE);
List<String> l_foundFilesList = new ArrayList<>();
l_foundFilesIterator.forEachRemaining(f -> l_foundFilesList.add(f.getAbsolutePath()));
return generateLogData(l_foundFilesList, in_parseDefinition);

return l_foundFilesList;
}
}
103 changes: 98 additions & 5 deletions src/test/java/com/adobe/campaign/tests/logparser/LogDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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<GenericEntry> 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<String> 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<String> 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<String> 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")));
}

}
Original file line number Diff line number Diff line change
@@ -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"]}

0 comments on commit 56591de

Please sign in to comment.