4
4
package com .marklogic .spark .writer ;
5
5
6
6
import com .fasterxml .jackson .databind .JsonNode ;
7
- import com .fasterxml .jackson .databind .node .JsonNodeType ;
8
7
import com .marklogic .junit5 .XmlNode ;
9
8
import com .marklogic .spark .AbstractIntegrationTest ;
10
9
import com .marklogic .spark .Options ;
15
14
import static org .junit .jupiter .api .Assertions .assertEquals ;
16
15
import static org .junit .jupiter .api .Assertions .assertFalse ;
17
16
18
- class WriteNullValuesTest extends AbstractIntegrationTest {
17
+ class IgnoreNullValuesTest extends AbstractIntegrationTest {
19
18
20
19
@ Test
21
20
void jsonWithEmptyValues () {
@@ -29,21 +28,20 @@ void jsonWithEmptyValues() {
29
28
.option (Options .CLIENT_URI , makeClientUri ())
30
29
.option (Options .WRITE_PERMISSIONS , DEFAULT_PERMISSIONS )
31
30
.option (Options .WRITE_URI_TEMPLATE , "/a/{number}.json" )
32
- .option (Options .WRITE_JSON_SERIALIZATION_OPTION_PREFIX + "ignoreNullFields" , "false " )
31
+ .option (Options .WRITE_JSON_SERIALIZATION_OPTION_PREFIX + "ignoreNullFields" , "true " )
33
32
.mode (SaveMode .Append )
34
33
.save ();
35
34
36
35
JsonNode doc = readJsonDocument ("/a/1.json" );
37
36
assertEquals (1 , doc .get ("number" ).asInt ());
38
37
assertEquals ("blue" , doc .get ("color" ).asText ());
39
- assertEquals (JsonNodeType .NULL , doc .get ("flag" ).getNodeType ());
40
- assertEquals (3 , doc .size (), "The file path column should not be included in the serialization." );
38
+ assertEquals (2 , doc .size (), "The flag column should not be included in the serialization." );
41
39
42
40
doc = readJsonDocument ("/a/2.json" );
43
41
assertEquals (2 , doc .get ("number" ).asInt ());
44
42
assertEquals (" " , doc .get ("color" ).asText (), "Verifies that whitespace is retained by default." );
45
43
assertFalse (doc .get ("flag" ).asBoolean ());
46
- assertEquals (3 , doc .size (), "The file path column should not be included in the serialization." );
44
+ assertEquals (3 , doc .size ());
47
45
}
48
46
49
47
@ Test
@@ -58,14 +56,12 @@ void xmlWithEmptyValues() {
58
56
.option (Options .WRITE_PERMISSIONS , DEFAULT_PERMISSIONS )
59
57
.option (Options .WRITE_XML_ROOT_NAME , "test" )
60
58
.option (Options .WRITE_URI_TEMPLATE , "/a/{number}.xml" )
61
- .option (Options .WRITE_JSON_SERIALIZATION_OPTION_PREFIX + "ignoreNullFields" , "false " )
59
+ .option (Options .WRITE_JSON_SERIALIZATION_OPTION_PREFIX + "ignoreNullFields" , "true " )
62
60
.mode (SaveMode .Append )
63
61
.save ();
64
62
65
63
XmlNode doc = readXmlDocument ("/a/1.xml" );
66
- doc .assertElementValue (
67
- "The empty flag column should be retained due to ignoreNullFields=true" ,
68
- "/test/flag" , "" );
64
+ doc .assertElementMissing ("The empty flag column should be ignored" , "/test/flag" );
69
65
doc .assertElementValue ("/test/number" , "1" );
70
66
doc .assertElementValue ("/test/color" , "blue" );
71
67
@@ -87,7 +83,7 @@ void jsonLinesWithNestedFieldsConvertedToXml() {
87
83
.option (Options .WRITE_PERMISSIONS , DEFAULT_PERMISSIONS )
88
84
.option (Options .WRITE_XML_ROOT_NAME , "parent" )
89
85
.option (Options .WRITE_URI_TEMPLATE , "/a/{id}.xml" )
90
- .option (Options .WRITE_JSON_SERIALIZATION_OPTION_PREFIX + "ignoreNullFields" , "false " )
86
+ .option (Options .WRITE_JSON_SERIALIZATION_OPTION_PREFIX + "ignoreNullFields" , "true " )
91
87
.mode (SaveMode .Append )
92
88
.save ();
93
89
@@ -99,9 +95,8 @@ void jsonLinesWithNestedFieldsConvertedToXml() {
99
95
doc .assertElementValue ("/parent/id" , "1" );
100
96
101
97
doc = readXmlDocument ("/a/2.xml" );
102
- doc .assertElementValue (
103
- "'hello' is added even though it doesn't exist on the line. This is due to ignoreNullFields being false " +
104
- "and Spark adding 'hello' to the schema since it appears on the first line." ,
105
- "/parent/hello" , "" );
98
+ doc .assertElementMissing ("'hello' should not appear. Spark JSON will actually include it in the schema and " +
99
+ "give it a value of null. But with ignoreNullFields set to true, it should be discarded." ,
100
+ "/parent/hello" );
106
101
}
107
102
}
0 commit comments