From 1c519f659f66b170704486af403933fbb83189bb Mon Sep 17 00:00:00 2001 From: baubakg Date: Fri, 14 Jun 2024 23:51:45 +0200 Subject: [PATCH] Solved issues #117 regarding negative cases --- .../logparser/core/StringParseFactory.java | 5 +- .../tests/logparser/core/ParseTesting.java | 48 ++++++++ .../tests/logparser/core/TestLogParsing.java | 111 ++++++++++++++++++ 3 files changed, 162 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/adobe/campaign/tests/logparser/core/StringParseFactory.java b/src/main/java/com/adobe/campaign/tests/logparser/core/StringParseFactory.java index 621e7c9..1e75cb5 100644 --- a/src/main/java/com/adobe/campaign/tests/logparser/core/StringParseFactory.java +++ b/src/main/java/com/adobe/campaign/tests/logparser/core/StringParseFactory.java @@ -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"); @@ -132,13 +133,13 @@ static 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(); diff --git a/src/test/java/com/adobe/campaign/tests/logparser/core/ParseTesting.java b/src/test/java/com/adobe/campaign/tests/logparser/core/ParseTesting.java index 7856582..66f1128 100644 --- a/src/test/java/com/adobe/campaign/tests/logparser/core/ParseTesting.java +++ b/src/test/java/com/adobe/campaign/tests/logparser/core/ParseTesting.java @@ -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 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 { diff --git a/src/test/java/com/adobe/campaign/tests/logparser/core/TestLogParsing.java b/src/test/java/com/adobe/campaign/tests/logparser/core/TestLogParsing.java index bcd0bc7..525d42a 100644 --- a/src/test/java/com/adobe/campaign/tests/logparser/core/TestLogParsing.java +++ b/src/test/java/com/adobe/campaign/tests/logparser/core/TestLogParsing.java @@ -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 l_entryMap = new HashMap(); + + 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 l_entryMap = new HashMap(); + + 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 l_entryMap = new HashMap(); + + 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() {