Skip to content

Commit

Permalink
Solved issues #117 regarding negative cases
Browse files Browse the repository at this point in the history
  • Loading branch information
baubakg committed Jun 14, 2024
1 parent f30272d commit 1c519f6
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
public class StringParseFactory {

protected static final Logger log = LogManager.getLogger();
public static final String STD_DEFAULT_ENTRY_FILENAME = "no_file";

private StringParseFactory() {
throw new IllegalStateException("Utility class");
Expand Down Expand Up @@ -132,13 +133,13 @@ static <T extends StdLogEntry> void updateEntryMapWithParsedData(final String in
lt_entry.setParseDefinition(in_parseDefinition);
lt_entry.setValuesFromMap(lt_lineResult);

var lt_fileObject = new File(in_logFile != null ? in_logFile : "no_file");
var lt_fileObject = new File(in_logFile != null ? in_logFile : STD_DEFAULT_ENTRY_FILENAME);
if (in_parseDefinition.isStoreFileName()) {
lt_entry.setLogFileName(lt_fileObject.getName());
}

if (in_parseDefinition.isStoreFilePath()) {
lt_entry.setFilePath(in_logFile != null ? lt_fileObject.getParentFile().getPath() : in_logFile);
lt_entry.setFilePath(lt_fileObject.exists() ? lt_fileObject.getParentFile().getPath() : STD_DEFAULT_ENTRY_FILENAME);
}

final String lt_currentKey = lt_entry.makeKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,9 +573,57 @@ public void testCreateApacheProfileFile_storeFile()
for (GenericEntry lt_entry : l_entries.values()) {
System.out.println(lt_entry.fetchPrintOut());
}
}

@Test
public void testCreateApacheProfileFile_storePathButNotFile()
throws StringParseException {

//Create a parse definition

ParseDefinitionEntry l_verbDefinition2 = new ParseDefinitionEntry();

l_verbDefinition2.setTitle("verb");
l_verbDefinition2.setStart("\"");
l_verbDefinition2.setEnd(" /");

ParseDefinitionEntry l_apiDefinition = new ParseDefinitionEntry();

l_apiDefinition.setTitle("path");
l_apiDefinition.setStart(" /rest/head/");
l_apiDefinition.setEnd(" ");

ParseDefinition l_pDefinition = new ParseDefinition("SSL Log");
l_pDefinition.setDefinitionEntries(Arrays.asList(l_verbDefinition2, l_apiDefinition));
l_pDefinition.defineKeys(Arrays.asList(l_apiDefinition, l_verbDefinition2));
l_pDefinition.setStoreFileName(false);
l_pDefinition.setStoreFilePath(true);

final String apacheLogFile = "src/test/resources/logTests/apache/ssl_accessSmall.log";

Map<String, GenericEntry> l_entries = StringParseFactory
.extractLogEntryMap(Arrays.asList(apacheLogFile), l_pDefinition, GenericEntry.class);

assertThat(l_entries, is(notNullValue()));
assertThat("We should have entries", l_entries.size(), is(greaterThan(0)));
assertThat("We should have entries", l_entries.size(), is(lessThan(19)));
assertThat("We should have the key for amcDataSource",
l_entries.containsKey("amcDataSource/AMCDS745177#GET"));
assertThat(l_entries.get("amcDataSource/AMCDS745177#GET").getFileName(),
nullValue());
assertThat(l_entries.get("amcDataSource/AMCDS745177#GET").getFilePath(),
is(equalTo("src/test/resources/logTests/apache")));

l_entries.get("amcDataSource/AMCDS745177#GET").fetchHeaders().stream().forEach(System.out::println);
l_entries.get("amcDataSource/AMCDS745177#GET").fetchValuesAsList().stream().forEach(System.out::println);


for (GenericEntry lt_entry : l_entries.values()) {
System.out.println(lt_entry.fetchPrintOut());
}
}


@Test
public void testIncludingTheFileAsAnEntry()
throws StringParseException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,117 @@ public void testUpdateMap() throws StringParseException {

}

@Test
public void testUpdateMap_missingFile() throws StringParseException {

String l_accLogString = "INFO | 25-May-2020 03:29:28:097 | - (SOAPutils.java:434) - HEADER ACTION xtk:persist#NewInstance";

//Create a parse definition
ParseDefinitionEntry l_apiDefinition = new ParseDefinitionEntry();

l_apiDefinition.setTitle("API");
l_apiDefinition.setStart("HEADER ACTION ");
l_apiDefinition.setEnd("#");

ParseDefinitionEntry l_verbDefinition = new ParseDefinitionEntry();

l_verbDefinition.setTitle("verb");
l_verbDefinition.setStart("#");
l_verbDefinition.setEnd(null);

ParseDefinition l_definition = new ParseDefinition("Soap Call");
l_definition.setDefinitionEntries(Arrays.asList(l_apiDefinition, l_verbDefinition));

Map<String, GenericEntry> l_entryMap = new HashMap<String, GenericEntry>();

StringParseFactory.updateEntryMapWithParsedData(null, l_accLogString, l_definition, l_entryMap,
GenericEntry.class);

assertThat("We should have one entry in the map", l_entryMap.size(), is(equalTo(1)));

final GenericEntry l_entry = l_entryMap.values().iterator().next();

assertThat("We should have the Definition stored in the generic entry", l_entry.getParseDefinition(),
is(equalTo(l_definition)));
assertThat("The key should be correct", l_entry.getFrequence(), is(equalTo(1)));
assertThat("The key should be correct", l_entry.makeKey(), is(equalTo("xtk:persist#NewInstance")));

}

@Test
public void testUpdateMap_nullFileButAwaitingAFile() throws StringParseException {

String l_accLogString = "INFO | 25-May-2020 03:29:28:097 | - (SOAPutils.java:434) - HEADER ACTION xtk:persist#NewInstance";

//Create a parse definition
ParseDefinitionEntry l_apiDefinition = new ParseDefinitionEntry();

l_apiDefinition.setTitle("API");
l_apiDefinition.setStart("HEADER ACTION ");
l_apiDefinition.setEnd("#");

ParseDefinitionEntry l_verbDefinition = new ParseDefinitionEntry();

l_verbDefinition.setTitle("verb");
l_verbDefinition.setStart("#");
l_verbDefinition.setEnd(null);

ParseDefinition l_definition = new ParseDefinition("Soap Call");
l_definition.setStoreFileName(true);
l_definition.setStoreFilePath(true);
l_definition.setDefinitionEntries(Arrays.asList(l_apiDefinition, l_verbDefinition));

Map<String, GenericEntry> l_entryMap = new HashMap<String, GenericEntry>();

StringParseFactory.updateEntryMapWithParsedData(null, l_accLogString, l_definition, l_entryMap,
GenericEntry.class);

assertThat("We should have one entry in the map", l_entryMap.size(), is(equalTo(1)));

final GenericEntry l_entry = l_entryMap.values().iterator().next();

assertThat("We should still have a file", l_entry.getFileName(), is(equalTo(StringParseFactory.STD_DEFAULT_ENTRY_FILENAME)));
assertThat("We should still have a path", l_entry.getFilePath(), is(equalTo(StringParseFactory.STD_DEFAULT_ENTRY_FILENAME)));
}


@Test
public void testUpdateMap_nonExistingFileButAwaitingAPath() throws StringParseException {

String l_accLogString = "INFO | 25-May-2020 03:29:28:097 | - (SOAPutils.java:434) - HEADER ACTION xtk:persist#NewInstance";

//Create a parse definition
ParseDefinitionEntry l_apiDefinition = new ParseDefinitionEntry();

l_apiDefinition.setTitle("API");
l_apiDefinition.setStart("HEADER ACTION ");
l_apiDefinition.setEnd("#");

ParseDefinitionEntry l_verbDefinition = new ParseDefinitionEntry();

l_verbDefinition.setTitle("verb");
l_verbDefinition.setStart("#");
l_verbDefinition.setEnd(null);

ParseDefinition l_definition = new ParseDefinition("Soap Call");
l_definition.setStoreFileName(true);
l_definition.setStoreFilePath(true);
l_definition.setDefinitionEntries(Arrays.asList(l_apiDefinition, l_verbDefinition));

Map<String, GenericEntry> l_entryMap = new HashMap<String, GenericEntry>();

StringParseFactory.updateEntryMapWithParsedData("does not exist", l_accLogString, l_definition, l_entryMap,
GenericEntry.class);

assertThat("We should have one entry in the map", l_entryMap.size(), is(equalTo(1)));

final GenericEntry l_entry = l_entryMap.values().iterator().next();

assertThat("We should still have a file", l_entry.getFileName(), is(equalTo("does not exist")));
assertThat("We should still have a path", l_entry.getFilePath(), is(equalTo("does not exist")));

}

@Test
public void testDefinitionCompare() {

Expand Down

0 comments on commit 1c519f6

Please sign in to comment.