From b4a896e402ae53ceefcffa34654058ee38b4f444 Mon Sep 17 00:00:00 2001 From: yotamlevy3 Date: Tue, 29 Dec 2020 01:18:49 +0200 Subject: [PATCH 1/9] Fix Sawmill XML Processor Creates Empty Object --- sawmill-core/src/main/java/io/logz/sawmill/Doc.java | 6 +++++- .../main/java/io/logz/sawmill/processors/XmlProcessor.java | 7 +++++-- .../java/io/logz/sawmill/processors/XmlProcessorTest.java | 2 ++ sawmill-core/src/test/resources/xml_valid.xml | 1 + 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/sawmill-core/src/main/java/io/logz/sawmill/Doc.java b/sawmill-core/src/main/java/io/logz/sawmill/Doc.java index 0cf1c5ac..78a48254 100644 --- a/sawmill-core/src/main/java/io/logz/sawmill/Doc.java +++ b/sawmill-core/src/main/java/io/logz/sawmill/Doc.java @@ -32,7 +32,11 @@ public boolean hasField(String path, Class clazz) { public T getField(String path) { Optional field = getByPath(source, path); - checkState(field.isPresent(), "Couldn't resolve field in path [%s]", path); + try { + checkState(field.isPresent(), "Couldn't resolve field in path [%s]", path); + } catch (IllegalStateException e) { + return (T) e.getClass(); + } return (T) field.get(); } diff --git a/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java b/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java index 52a0d0ab..446dcbee 100644 --- a/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java +++ b/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java @@ -27,6 +27,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.stream.Collectors; import static java.util.Objects.requireNonNull; @@ -116,14 +117,16 @@ private Map extractNodes(Node parent) { String key = node.getNodeName(); Object value; - if (node.getChildNodes().getLength() == 1 && - node.getChildNodes().item(0).getNodeValue() != null) { + if (node.getChildNodes().getLength() == 1 + && node.getChildNodes().item(0) != null + && node.getChildNodes().item(0).getNodeValue() != null ) { value = node.getChildNodes().item(0).getNodeValue(); } else { value = extractNodes(node); } xmlNodes.compute(key, (k, oldVal) -> { + if (node.getChildNodes().item(0) == null) { return oldVal; } if (oldVal == null) return value; if (oldVal instanceof List) { ((List) oldVal).add(value); diff --git a/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java b/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java index eb44a610..40cfb0db 100644 --- a/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java +++ b/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java @@ -6,6 +6,7 @@ import io.logz.sawmill.ProcessResult; import io.logz.sawmill.exceptions.ProcessorConfigurationException; import org.apache.commons.io.IOUtils; +import org.assertj.core.error.OptionalShouldBeEmpty; import org.junit.Test; import java.io.IOException; @@ -47,6 +48,7 @@ public void testValidXml() { assertThat(processResult.isSucceeded()).isTrue(); assertThat((String) doc.getField("country.id")).isEqualTo("1"); assertThat((String) doc.getField("country.name")).isEqualTo("Israel"); + assertThat((Object) doc.getField("country.TestEmptyField")).isEqualTo(IllegalStateException.class); assertThat((List) doc.getField("country.cities.city")) .isEqualTo(Arrays.asList(ImmutableMap.of("name", "Jerusalem"), ImmutableMap.of("name", "Tel Aviv"))); diff --git a/sawmill-core/src/test/resources/xml_valid.xml b/sawmill-core/src/test/resources/xml_valid.xml index 470a2b9a..a7f4c269 100644 --- a/sawmill-core/src/test/resources/xml_valid.xml +++ b/sawmill-core/src/test/resources/xml_valid.xml @@ -1,6 +1,7 @@ 1 Israel + Jerusalem Tel Aviv From f55b1a89c6a0ed262d8ba2a80aca7c290c7ab0c6 Mon Sep 17 00:00:00 2001 From: yotamlevy3 Date: Tue, 29 Dec 2020 01:22:10 +0200 Subject: [PATCH 2/9] Fix Sawmill XML Processor Creates Empty Object --- .../src/main/java/io/logz/sawmill/processors/XmlProcessor.java | 1 - 1 file changed, 1 deletion(-) diff --git a/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java b/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java index 446dcbee..999a3c32 100644 --- a/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java +++ b/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java @@ -27,7 +27,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.stream.Collectors; import static java.util.Objects.requireNonNull; From 2a306c4869da5374989e1e2d9f6866b32a37a064 Mon Sep 17 00:00:00 2001 From: yotamlevy3 Date: Tue, 29 Dec 2020 01:28:07 +0200 Subject: [PATCH 3/9] removed unnecessary imports --- .../test/java/io/logz/sawmill/processors/XmlProcessorTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java b/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java index 40cfb0db..3020e8c0 100644 --- a/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java +++ b/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java @@ -6,7 +6,6 @@ import io.logz.sawmill.ProcessResult; import io.logz.sawmill.exceptions.ProcessorConfigurationException; import org.apache.commons.io.IOUtils; -import org.assertj.core.error.OptionalShouldBeEmpty; import org.junit.Test; import java.io.IOException; From 06cd88b8f2edb16d15a5f24371c4422b5c50d7a7 Mon Sep 17 00:00:00 2001 From: yotamlevy3 Date: Tue, 29 Dec 2020 17:53:50 +0200 Subject: [PATCH 4/9] make it configurable and test both possibilities --- .../logz/sawmill/processors/XmlProcessor.java | 13 +++++++--- .../sawmill/processors/XmlProcessorTest.java | 26 +++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java b/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java index 999a3c32..25557b2a 100644 --- a/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java +++ b/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java @@ -40,13 +40,15 @@ public class XmlProcessor implements Processor { private final String targetField; private final Map xpath; private final boolean storeXml; + private final boolean omitEmptyTagAsFieldEnabled; - public XmlProcessor(DocumentBuilderProvider documentBuilderProvider, String field, String targetField, Map xpath, boolean storeXml) { + public XmlProcessor(DocumentBuilderProvider documentBuilderProvider, String field, String targetField, Map xpath, boolean storeXml, boolean omitEmptyTagAsFieldEnabled) { this.documentBuilderProvider = requireNonNull(documentBuilderProvider); this.field = requireNonNull(field); this.targetField = targetField; this.xpath = xpath; this.storeXml = storeXml; + this.omitEmptyTagAsFieldEnabled = omitEmptyTagAsFieldEnabled; } @Override @@ -125,7 +127,7 @@ private Map extractNodes(Node parent) { } xmlNodes.compute(key, (k, oldVal) -> { - if (node.getChildNodes().item(0) == null) { return oldVal; } + if (node.getChildNodes().item(0) == null && omitEmptyTagAsFieldEnabled) { return oldVal; } if (oldVal == null) return value; if (oldVal instanceof List) { ((List) oldVal).add(value); @@ -165,7 +167,8 @@ public XmlProcessor create(Map config) { xmlConfig.getField(), xmlConfig.getTargetField(), xpath, - xmlConfig.isStoreXml()); + xmlConfig.isStoreXml(), + xmlConfig.isOmitEmptyTagAsFieldEnabled()); } } @@ -174,6 +177,7 @@ public static class Configuration implements Processor.Configuration { private String targetField; private Map xpath; private boolean storeXml = true; + private boolean omitEmptyTagAsFieldEnabled = true; public Configuration() { } @@ -192,5 +196,8 @@ public Map getXpath() { public boolean isStoreXml() { return storeXml; } + + public boolean isOmitEmptyTagAsFieldEnabled() { return omitEmptyTagAsFieldEnabled; } + } } diff --git a/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java b/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java index 3020e8c0..57121870 100644 --- a/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java +++ b/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java @@ -58,6 +58,32 @@ public void testValidXml() { assertThat((List) doc.getField("country.languages.language")).isEqualTo(Arrays.asList("Hebrew", "Arabic", "English")); } + @Test + public void testValidXmlWhenOmittingEmptyTagIsNotEnabled() { + String field = "xml"; + + Doc doc = createDoc(field, VALID_XML); + + Map config = createConfig("field", field, + "omitEmptyTagAsFieldEnabled", false, + "xpath", ImmutableMap.of("/country/languages/language[@type='official']/text()", "lang", + "/country/cities/city[2]/name/text()", "bestCity", + "/country/otherField", "nonExistsField", + "/country/TestEmptyField/text()", "")); + + XmlProcessor xmlProcessor = createProcessor(XmlProcessor.class, config); + + ProcessResult processResult = xmlProcessor.process(doc); + + assertThat(processResult.isSucceeded()).isTrue(); + assertThat((List) doc.getField("lang")).isEqualTo(Arrays.asList("Hebrew", "Arabic")); + assertThat((String) doc.getField("bestCity")).isEqualTo("Tel Aviv"); + assertThat(doc.hasField("nonExistsField")).isFalse(); + assertThat((Object) doc.getField("country.TestEmptyField").toString()).isEqualTo("{}"); + + } + + @Test public void testXPath() { String field = "xml"; From 22fa1b6716c846879f4d3c318429e40f548d4fed Mon Sep 17 00:00:00 2001 From: yotamlevy3 Date: Wed, 30 Dec 2020 11:53:38 +0200 Subject: [PATCH 5/9] return field and null in json --- .../src/main/java/io/logz/sawmill/processors/XmlProcessor.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java b/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java index 25557b2a..bdbd54fe 100644 --- a/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java +++ b/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java @@ -106,6 +106,7 @@ public ProcessResult process(Doc doc) { xmlNodes.forEach(doc::addField); } } + return ProcessResult.success(); } @@ -128,6 +129,7 @@ private Map extractNodes(Node parent) { xmlNodes.compute(key, (k, oldVal) -> { if (node.getChildNodes().item(0) == null && omitEmptyTagAsFieldEnabled) { return oldVal; } + if (node.getChildNodes().item(0) == null && !omitEmptyTagAsFieldEnabled) { return "null"; } if (oldVal == null) return value; if (oldVal instanceof List) { ((List) oldVal).add(value); From c169aa3206016fb1800d22753b801cd7d7b42802 Mon Sep 17 00:00:00 2001 From: yotamlevy3 Date: Wed, 30 Dec 2020 12:03:40 +0200 Subject: [PATCH 6/9] return field and null in json2 --- .../test/java/io/logz/sawmill/processors/XmlProcessorTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java b/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java index 57121870..bd052a6a 100644 --- a/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java +++ b/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java @@ -79,7 +79,7 @@ public void testValidXmlWhenOmittingEmptyTagIsNotEnabled() { assertThat((List) doc.getField("lang")).isEqualTo(Arrays.asList("Hebrew", "Arabic")); assertThat((String) doc.getField("bestCity")).isEqualTo("Tel Aviv"); assertThat(doc.hasField("nonExistsField")).isFalse(); - assertThat((Object) doc.getField("country.TestEmptyField").toString()).isEqualTo("{}"); + assertThat((Object) doc.getField("country.TestEmptyField")).isEqualTo("null"); } From 7cf1ac6828fab2d5f23eba697ef4be8cdef5bf2f Mon Sep 17 00:00:00 2001 From: yotamlevy3 Date: Wed, 30 Dec 2020 19:03:05 +0200 Subject: [PATCH 7/9] cr changes --- .../src/main/java/io/logz/sawmill/Doc.java | 7 ++++--- .../io/logz/sawmill/processors/XmlProcessor.java | 16 ++++++++-------- .../sawmill/processors/XmlProcessorTest.java | 4 ++-- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/sawmill-core/src/main/java/io/logz/sawmill/Doc.java b/sawmill-core/src/main/java/io/logz/sawmill/Doc.java index 78a48254..fcf45d8b 100644 --- a/sawmill-core/src/main/java/io/logz/sawmill/Doc.java +++ b/sawmill-core/src/main/java/io/logz/sawmill/Doc.java @@ -1,6 +1,5 @@ package io.logz.sawmill; -import static com.google.common.base.Preconditions.checkState; import org.apache.commons.collections4.MapUtils; import java.util.ArrayList; @@ -9,6 +8,8 @@ import java.util.Map; import java.util.Optional; +import static com.google.common.base.Preconditions.checkState; + public class Doc { private final Map source; @@ -34,8 +35,8 @@ public T getField(String path) { Optional field = getByPath(source, path); try { checkState(field.isPresent(), "Couldn't resolve field in path [%s]", path); - } catch (IllegalStateException e) { - return (T) e.getClass(); + } catch (Exception e) { + return (T) e.getMessage(); } return (T) field.get(); } diff --git a/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java b/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java index bdbd54fe..e546ede2 100644 --- a/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java +++ b/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java @@ -40,15 +40,15 @@ public class XmlProcessor implements Processor { private final String targetField; private final Map xpath; private final boolean storeXml; - private final boolean omitEmptyTagAsFieldEnabled; + private final boolean allowNullValue; - public XmlProcessor(DocumentBuilderProvider documentBuilderProvider, String field, String targetField, Map xpath, boolean storeXml, boolean omitEmptyTagAsFieldEnabled) { + public XmlProcessor(DocumentBuilderProvider documentBuilderProvider, String field, String targetField, Map xpath, boolean storeXml, boolean allowNullValue) { this.documentBuilderProvider = requireNonNull(documentBuilderProvider); this.field = requireNonNull(field); this.targetField = targetField; this.xpath = xpath; this.storeXml = storeXml; - this.omitEmptyTagAsFieldEnabled = omitEmptyTagAsFieldEnabled; + this.allowNullValue = allowNullValue; } @Override @@ -128,8 +128,8 @@ private Map extractNodes(Node parent) { } xmlNodes.compute(key, (k, oldVal) -> { - if (node.getChildNodes().item(0) == null && omitEmptyTagAsFieldEnabled) { return oldVal; } - if (node.getChildNodes().item(0) == null && !omitEmptyTagAsFieldEnabled) { return "null"; } + if (node.getChildNodes().item(0) == null && allowNullValue) { return oldVal; } + if (node.getChildNodes().item(0) == null && !allowNullValue) { return "null"; } if (oldVal == null) return value; if (oldVal instanceof List) { ((List) oldVal).add(value); @@ -170,7 +170,7 @@ public XmlProcessor create(Map config) { xmlConfig.getTargetField(), xpath, xmlConfig.isStoreXml(), - xmlConfig.isOmitEmptyTagAsFieldEnabled()); + xmlConfig.isAllowNullValue()); } } @@ -179,7 +179,7 @@ public static class Configuration implements Processor.Configuration { private String targetField; private Map xpath; private boolean storeXml = true; - private boolean omitEmptyTagAsFieldEnabled = true; + private boolean allowNullValue = true; public Configuration() { } @@ -199,7 +199,7 @@ public boolean isStoreXml() { return storeXml; } - public boolean isOmitEmptyTagAsFieldEnabled() { return omitEmptyTagAsFieldEnabled; } + public boolean isAllowNullValue() { return allowNullValue; } } } diff --git a/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java b/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java index bd052a6a..6315f3c1 100644 --- a/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java +++ b/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java @@ -47,7 +47,7 @@ public void testValidXml() { assertThat(processResult.isSucceeded()).isTrue(); assertThat((String) doc.getField("country.id")).isEqualTo("1"); assertThat((String) doc.getField("country.name")).isEqualTo("Israel"); - assertThat((Object) doc.getField("country.TestEmptyField")).isEqualTo(IllegalStateException.class); + assertThat((Object) doc.getField("country.TestEmptyField")).isEqualTo("Couldn't resolve field in path [country.TestEmptyField]"); assertThat((List) doc.getField("country.cities.city")) .isEqualTo(Arrays.asList(ImmutableMap.of("name", "Jerusalem"), ImmutableMap.of("name", "Tel Aviv"))); @@ -65,7 +65,7 @@ public void testValidXmlWhenOmittingEmptyTagIsNotEnabled() { Doc doc = createDoc(field, VALID_XML); Map config = createConfig("field", field, - "omitEmptyTagAsFieldEnabled", false, + "allowNullValue", false, "xpath", ImmutableMap.of("/country/languages/language[@type='official']/text()", "lang", "/country/cities/city[2]/name/text()", "bestCity", "/country/otherField", "nonExistsField", From 660ae686e4e0367bbd8e0feca4000c3a100af9f5 Mon Sep 17 00:00:00 2001 From: yotamlevy3 Date: Fri, 1 Jan 2021 00:30:11 +0200 Subject: [PATCH 8/9] ze ma yesh bentaim - not to merge --- .../src/main/java/io/logz/sawmill/Doc.java | 6 +----- .../io/logz/sawmill/processors/XmlProcessor.java | 15 ++++++++++++--- .../logz/sawmill/processors/XmlProcessorTest.java | 7 ++++--- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/sawmill-core/src/main/java/io/logz/sawmill/Doc.java b/sawmill-core/src/main/java/io/logz/sawmill/Doc.java index fcf45d8b..5d5ecfc3 100644 --- a/sawmill-core/src/main/java/io/logz/sawmill/Doc.java +++ b/sawmill-core/src/main/java/io/logz/sawmill/Doc.java @@ -33,11 +33,7 @@ public boolean hasField(String path, Class clazz) { public T getField(String path) { Optional field = getByPath(source, path); - try { - checkState(field.isPresent(), "Couldn't resolve field in path [%s]", path); - } catch (Exception e) { - return (T) e.getMessage(); - } + checkState(field.isPresent(), "Couldn't resolve field in path [%s]", path); return (T) field.get(); } diff --git a/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java b/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java index e546ede2..c2f7628c 100644 --- a/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java +++ b/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java @@ -113,6 +113,7 @@ public ProcessResult process(Doc doc) { private Map extractNodes(Node parent) { Map xmlNodes = new HashMap<>(); NodeList nodes = parent.getChildNodes(); + for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); @@ -128,8 +129,10 @@ private Map extractNodes(Node parent) { } xmlNodes.compute(key, (k, oldVal) -> { - if (node.getChildNodes().item(0) == null && allowNullValue) { return oldVal; } - if (node.getChildNodes().item(0) == null && !allowNullValue) { return "null"; } + if (node.getChildNodes().item(0) == null && !allowNullValue) { return oldVal; } + if (node.getChildNodes().item(0) == null && allowNullValue) { + return "null"; + } if (oldVal == null) return value; if (oldVal instanceof List) { ((List) oldVal).add(value); @@ -137,6 +140,12 @@ private Map extractNodes(Node parent) { } return new ArrayList<>(Arrays.asList(oldVal, value)); }); + + //putting actual null in map + if (node.getChildNodes().item(0) == null && allowNullValue) { + xmlNodes.replace(key, "null", null); + } + } return xmlNodes; @@ -179,7 +188,7 @@ public static class Configuration implements Processor.Configuration { private String targetField; private Map xpath; private boolean storeXml = true; - private boolean allowNullValue = true; + private boolean allowNullValue = false; public Configuration() { } diff --git a/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java b/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java index 6315f3c1..8b7766e1 100644 --- a/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java +++ b/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java @@ -47,7 +47,7 @@ public void testValidXml() { assertThat(processResult.isSucceeded()).isTrue(); assertThat((String) doc.getField("country.id")).isEqualTo("1"); assertThat((String) doc.getField("country.name")).isEqualTo("Israel"); - assertThat((Object) doc.getField("country.TestEmptyField")).isEqualTo("Couldn't resolve field in path [country.TestEmptyField]"); + assertThatThrownBy(() -> doc.getField("country.TestEmptyField")).isInstanceOf(IllegalStateException.class); assertThat((List) doc.getField("country.cities.city")) .isEqualTo(Arrays.asList(ImmutableMap.of("name", "Jerusalem"), ImmutableMap.of("name", "Tel Aviv"))); @@ -65,7 +65,7 @@ public void testValidXmlWhenOmittingEmptyTagIsNotEnabled() { Doc doc = createDoc(field, VALID_XML); Map config = createConfig("field", field, - "allowNullValue", false, + "allowNullValue", true, "xpath", ImmutableMap.of("/country/languages/language[@type='official']/text()", "lang", "/country/cities/city[2]/name/text()", "bestCity", "/country/otherField", "nonExistsField", @@ -79,7 +79,8 @@ public void testValidXmlWhenOmittingEmptyTagIsNotEnabled() { assertThat((List) doc.getField("lang")).isEqualTo(Arrays.asList("Hebrew", "Arabic")); assertThat((String) doc.getField("bestCity")).isEqualTo("Tel Aviv"); assertThat(doc.hasField("nonExistsField")).isFalse(); - assertThat((Object) doc.getField("country.TestEmptyField")).isEqualTo("null"); + assertThat((String) doc.getField("country.continent")).isEqualTo("Asia"); + assertThat((Object) doc.getField("country.TestEmptyField")).isNull(); } From 5a0550704b10e731c3ea5ed6fb0dd2c62db42b2e Mon Sep 17 00:00:00 2001 From: yotamlevy3 Date: Tue, 5 Jan 2021 16:41:51 +0200 Subject: [PATCH 9/9] without config --- .../logz/sawmill/processors/XmlProcessor.java | 20 ++----------- .../sawmill/processors/XmlProcessorTest.java | 28 +------------------ 2 files changed, 4 insertions(+), 44 deletions(-) diff --git a/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java b/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java index c2f7628c..2288156b 100644 --- a/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java +++ b/sawmill-core/src/main/java/io/logz/sawmill/processors/XmlProcessor.java @@ -40,15 +40,13 @@ public class XmlProcessor implements Processor { private final String targetField; private final Map xpath; private final boolean storeXml; - private final boolean allowNullValue; - public XmlProcessor(DocumentBuilderProvider documentBuilderProvider, String field, String targetField, Map xpath, boolean storeXml, boolean allowNullValue) { + public XmlProcessor(DocumentBuilderProvider documentBuilderProvider, String field, String targetField, Map xpath, boolean storeXml) { this.documentBuilderProvider = requireNonNull(documentBuilderProvider); this.field = requireNonNull(field); this.targetField = targetField; this.xpath = xpath; this.storeXml = storeXml; - this.allowNullValue = allowNullValue; } @Override @@ -129,10 +127,7 @@ private Map extractNodes(Node parent) { } xmlNodes.compute(key, (k, oldVal) -> { - if (node.getChildNodes().item(0) == null && !allowNullValue) { return oldVal; } - if (node.getChildNodes().item(0) == null && allowNullValue) { - return "null"; - } + if (node.getChildNodes().item(0) == null) { return oldVal; } if (oldVal == null) return value; if (oldVal instanceof List) { ((List) oldVal).add(value); @@ -141,11 +136,6 @@ private Map extractNodes(Node parent) { return new ArrayList<>(Arrays.asList(oldVal, value)); }); - //putting actual null in map - if (node.getChildNodes().item(0) == null && allowNullValue) { - xmlNodes.replace(key, "null", null); - } - } return xmlNodes; @@ -178,8 +168,7 @@ public XmlProcessor create(Map config) { xmlConfig.getField(), xmlConfig.getTargetField(), xpath, - xmlConfig.isStoreXml(), - xmlConfig.isAllowNullValue()); + xmlConfig.isStoreXml()); } } @@ -188,7 +177,6 @@ public static class Configuration implements Processor.Configuration { private String targetField; private Map xpath; private boolean storeXml = true; - private boolean allowNullValue = false; public Configuration() { } @@ -208,7 +196,5 @@ public boolean isStoreXml() { return storeXml; } - public boolean isAllowNullValue() { return allowNullValue; } - } } diff --git a/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java b/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java index 8b7766e1..8218c11d 100644 --- a/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java +++ b/sawmill-core/src/test/java/io/logz/sawmill/processors/XmlProcessorTest.java @@ -47,6 +47,7 @@ public void testValidXml() { assertThat(processResult.isSucceeded()).isTrue(); assertThat((String) doc.getField("country.id")).isEqualTo("1"); assertThat((String) doc.getField("country.name")).isEqualTo("Israel"); + assertThat(doc.hasField("country.TestEmptyField")).isFalse(); assertThatThrownBy(() -> doc.getField("country.TestEmptyField")).isInstanceOf(IllegalStateException.class); assertThat((List) doc.getField("country.cities.city")) .isEqualTo(Arrays.asList(ImmutableMap.of("name", "Jerusalem"), @@ -58,33 +59,6 @@ public void testValidXml() { assertThat((List) doc.getField("country.languages.language")).isEqualTo(Arrays.asList("Hebrew", "Arabic", "English")); } - @Test - public void testValidXmlWhenOmittingEmptyTagIsNotEnabled() { - String field = "xml"; - - Doc doc = createDoc(field, VALID_XML); - - Map config = createConfig("field", field, - "allowNullValue", true, - "xpath", ImmutableMap.of("/country/languages/language[@type='official']/text()", "lang", - "/country/cities/city[2]/name/text()", "bestCity", - "/country/otherField", "nonExistsField", - "/country/TestEmptyField/text()", "")); - - XmlProcessor xmlProcessor = createProcessor(XmlProcessor.class, config); - - ProcessResult processResult = xmlProcessor.process(doc); - - assertThat(processResult.isSucceeded()).isTrue(); - assertThat((List) doc.getField("lang")).isEqualTo(Arrays.asList("Hebrew", "Arabic")); - assertThat((String) doc.getField("bestCity")).isEqualTo("Tel Aviv"); - assertThat(doc.hasField("nonExistsField")).isFalse(); - assertThat((String) doc.getField("country.continent")).isEqualTo("Asia"); - assertThat((Object) doc.getField("country.TestEmptyField")).isNull(); - - } - - @Test public void testXPath() { String field = "xml";