+ * This method parses the JSON string into a map, removes the properties listed in
+ * {@link CustomLogProperties#getIgnoredProperties()}, and then converts the map back to a JSON string.
+ * If an exception occurs during this process, the original JSON string is returned.
+ *
+ *
+ * @param jsonString the input JSON string from which properties need to be removed
+ * @return a new JSON string with the ignored properties removed, or the original JSON string if an error occurs
+ */
+ public static String removeIgnoredPropertiesFromJson(String jsonString) {
+ try {
+ ObjectMapper objectMapper = new ObjectMapper();
+ Map map = objectMapper.readValue(jsonString, HashMap.class);
+ for(String propertyName : CustomLogProperties.getIgnoredProperties()) {
+ map.remove(propertyName);
+ }
+ return objectMapper.writeValueAsString(map);
+ } catch (Exception e) {
+ Log.error("Something went wrong when trying to remove ignored properties" +
+ " from the payload!");
+ return jsonString;
+ }
+ }
+
+ /**
+ * Retrieves custom log properties and removes any properties that are marked as ignored.
+ *
+ * This method fetches a map of custom log properties and then removes entries
+ * whose keys are listed in the ignored properties set. The resulting map contains
+ * only those properties that are not ignored.
+ *
+ * @return a map containing the custom log properties with the ignored properties removed.
+ */
+ public static Map getCustomLogPropertiesWithoutIgnored() {
+ Map customLogProps = CustomLogProperties.getCustomProperties();
+ for(String propertyName : CustomLogProperties.getIgnoredProperties()) {
+ customLogProps.remove(propertyName);
+ }
+ return customLogProps;
+ }
}
diff --git a/src/test/java/CustomLogPropertiesTest.java b/src/test/java/CustomLogPropertiesTest.java
new file mode 100644
index 0000000..d41e921
--- /dev/null
+++ b/src/test/java/CustomLogPropertiesTest.java
@@ -0,0 +1,78 @@
+
+import com.simple.logging.LoggingApplication;
+import com.simple.logging.application.model.CustomLogProperties;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+@SpringBootTest(classes = {LoggingApplication.class})
+class CustomLogPropertiesTest {
+
+ @AfterEach
+ public void tearDown() {
+ CustomLogProperties.clearCustomProperties();
+ CustomLogProperties.clearIgnoredProperties();
+ }
+
+ @Test
+ void testSetAndGetCustomProperty() {
+ CustomLogProperties.setCustomProperty("key1", "value1");
+ assertEquals("value1", CustomLogProperties.getCustomProperty("key1"));
+ }
+
+ @Test
+ void testSetCustomProperties() {
+ Map properties = new HashMap<>();
+ properties.put("key1", "value1");
+ properties.put("key2", "value2");
+ CustomLogProperties.setCustomProperties(properties);
+
+ assertEquals("value1", CustomLogProperties.getCustomProperty("key1"));
+ assertEquals("value2", CustomLogProperties.getCustomProperty("key2"));
+ }
+
+ @Test
+ void testGetCustomProperties() {
+ CustomLogProperties.setCustomProperty("key1", "value1");
+ CustomLogProperties.setCustomProperty("key2", "value2");
+
+ Map properties = CustomLogProperties.getCustomProperties();
+ assertEquals(2, properties.size());
+ assertEquals("value1", properties.get("key1"));
+ assertEquals("value2", properties.get("key2"));
+ }
+
+ @Test
+ void testClearCustomProperties() {
+ CustomLogProperties.setCustomProperty("key1", "value1");
+ CustomLogProperties.clearCustomProperties();
+
+ assertNull(CustomLogProperties.getCustomProperty("key1"));
+ assertTrue(CustomLogProperties.getCustomProperties().isEmpty());
+ }
+
+ @Test
+ void testAddAndGetIgnoredProperty() {
+ CustomLogProperties.addIgnoredProperty("prop1");
+ CustomLogProperties.addIgnoredProperty("prop2");
+
+ List ignoredProperties = CustomLogProperties.getIgnoredProperties();
+ assertEquals(2, ignoredProperties.size());
+ assertTrue(ignoredProperties.contains("prop1"));
+ assertTrue(ignoredProperties.contains("prop2"));
+ }
+
+ @Test
+ void testClearIgnoredProperties() {
+ CustomLogProperties.addIgnoredProperty("prop1");
+ CustomLogProperties.clearIgnoredProperties();
+
+ assertTrue(CustomLogProperties.getIgnoredProperties().isEmpty());
+ }
+}
diff --git a/src/test/java/LogUtilityTest.java b/src/test/java/LogUtilityTest.java
index fa5fe91..86e599d 100644
--- a/src/test/java/LogUtilityTest.java
+++ b/src/test/java/LogUtilityTest.java
@@ -1,4 +1,6 @@
+import com.fasterxml.jackson.databind.ObjectMapper;
import com.simple.logging.LoggingApplication;
+import com.simple.logging.application.model.CustomLogProperties;
import com.simple.logging.application.utility.LogUtility;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.io.TempDir;
@@ -9,7 +11,10 @@
import java.nio.file.*;
import java.time.LocalDate;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
+
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest(classes = {LoggingApplication.class})
@@ -23,6 +28,11 @@ void setUp() {
LogUtility.UtilityObjects.setObjects(tempDir.toString(), "testApp");
}
+ @AfterEach
+ public void tearDown() {
+ CustomLogProperties.clearIgnoredProperties();
+ }
+
@Test
void testMoveFile() throws IOException {
Path sourceFile = Files.createFile(tempDir.resolve("source.log"));
@@ -174,4 +184,39 @@ void testFindLogsForSpecificDateFailure() {
assertEquals(expectedFiles, filteredLogFiles);
}
+
+ @Test
+ void testRemoveIgnoredPropertiesFromJson() throws Exception {
+ String jsonString = "{\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":\"value3\"}";
+ CustomLogProperties.addIgnoredProperty("key2");
+
+ String result = LogUtility.removeIgnoredPropertiesFromJson(jsonString);
+
+ String expectedJsonString = "{\"key1\":\"value1\",\"key3\":\"value3\"}";
+ ObjectMapper objectMapper = new ObjectMapper();
+ Map resultMap = objectMapper.readValue(result, HashMap.class);
+ Map expectedMap = objectMapper.readValue(expectedJsonString, HashMap.class);
+ assertEquals(expectedMap, resultMap);
+ }
+
+ @Test
+ void testRemoveIgnoredPropertiesFromJsonWithNoIgnoredProperties() throws Exception {
+ String jsonString = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
+
+ String result = LogUtility.removeIgnoredPropertiesFromJson(jsonString);
+
+ ObjectMapper objectMapper = new ObjectMapper();
+ Map resultMap = objectMapper.readValue(result, HashMap.class);
+ Map expectedMap = objectMapper.readValue(jsonString, HashMap.class);
+ assertEquals(expectedMap, resultMap);
+ }
+
+ @Test
+ void testRemoveIgnoredPropertiesFromJsonWithException() {
+ String invalidJsonString = "{\"key1\":\"value1\",\"key2\":\"value2\""; // Invalid JSON
+
+ String result = LogUtility.removeIgnoredPropertiesFromJson(invalidJsonString);
+
+ assertEquals(invalidJsonString, result);
+ }
}
\ No newline at end of file