1
1
package org .mihkel .avro .io ;
2
2
3
+ import java .io .IOException ;
4
+
3
5
import org .apache .avro .Schema ;
4
6
import org .apache .avro .generic .GenericDatumReader ;
5
7
import org .apache .avro .generic .GenericRecord ;
@@ -60,4 +62,67 @@ private void checkNumeric(String type, Object value) throws Exception {
60
62
Assert .assertEquals (200 , in .readLong ());
61
63
in .skipArray ();
62
64
}
65
+
66
+ @ Test
67
+ public void testNullsAreInferred () throws IOException {
68
+ String w = "{\" type\" :\" record\" ,\" name\" :\" R\" ,\" fields\" :[{\" type\" :[\" null\" ,\" long\" ],\" name\" :\" a\" ,\" default\" :null}]}" ;
69
+ GenericRecord record = readRecord (w , "{}" );
70
+
71
+ Assert .assertNull (record .get ("a" ));
72
+ }
73
+
74
+ @ Test
75
+ public void testDefaultValuesAreInferred () throws IOException {
76
+ String w = "{\" type\" :\" record\" ,\" name\" :\" R\" ,\" fields\" :[{\" type\" :\" long\" ,\" name\" :\" a\" ,\" default\" :7}]}" ;
77
+ GenericRecord record = readRecord (w , "{}" );
78
+
79
+ Assert .assertEquals (7L , record .get ("a" ));
80
+ }
81
+
82
+ @ Test
83
+ public void testNestedNullsAreInferred () throws IOException {
84
+ String w = "{\" type\" :\" record\" ,\" name\" :\" R\" ,\" fields\" :[{\" name\" :\" S\" ,\" type\" :" +
85
+ "{\" type\" :\" record\" ,\" name\" :\" S\" ,\" fields\" :[{\" type\" :[\" null\" ,\" long\" ],\" name\" :\" a\" ,\" default\" :null},{\" type\" :\" long\" ,\" name\" :\" b\" }]}}]}" ;
86
+ String data = "{\" S\" : {\" b\" :1}}" ;
87
+ GenericRecord record = ((GenericRecord )readRecord (w , data ).get ("S" ));
88
+ Assert .assertNull (record .get ("a" ));
89
+ }
90
+
91
+ @ Test
92
+ public void testArraysCanBeNull () throws IOException {
93
+ String w = "{\" type\" :\" record\" ,\" name\" :\" R\" ,\" fields\" :[{\" type\" :[\" null\" ,{\" type\" :\" array\" ,\" items\" :\" long\" }],\" name\" :\" A\" ,\" default\" :null}]}" ;
94
+ String data = "{}" ;
95
+ GenericRecord record = readRecord (w , data );
96
+ Assert .assertNull (record .get ("A" ));
97
+ }
98
+
99
+ @ Test
100
+ public void testRecordCanBeNull () throws IOException {
101
+ String w = "{\" type\" :\" record\" ,\" name\" :\" R\" ,\" namespace\" :\" com.playtech.bex.massupdate.api\" ,\" fields\" :" +
102
+ "[{\" name\" :\" S\" ,\" type\" :[\" null\" ,{\" type\" :\" record\" ,\" name\" :\" S\" ,\" fields\" :[{\" name\" :\" A\" ,\" type\" :\" long\" }]}],\" default\" :null}]}" ;
103
+ String data = "{}" ;
104
+ GenericRecord record = readRecord (w , data );
105
+ Assert .assertNull (record .get ("S" ));
106
+ }
107
+
108
+ @ Test
109
+ public void testWtf () throws IOException {
110
+ String w = "{\" type\" :\" record\" ,\" name\" :\" wrapper\" ,\" fields\" :[{\" name\" :\" data\" ,\" type\" :" +
111
+ "{\" type\" :\" array\" ,\" items\" :{\" type\" :\" record\" ,\" name\" :\" r1\" ,\" fields\" :" +
112
+ "[{\" name\" :\" r1\" ,\" type\" :{\" type\" :\" array\" ,\" items\" :{\" type\" :\" record\" ,\" name\" :\" sr2\" ,\" fields\" :" +
113
+ "[{\" name\" :\" sr2\" ,\" type\" :\" string\" }]}}},{\" name\" :\" r2\" ,\" type\" :{\" type\" :\" array\" ,\" items\" :" +
114
+ "{\" type\" :\" record\" ,\" name\" :\" r2\" ,\" fields\" :[{\" name\" :\" notfound1\" ,\" type\" :[\" null\" ," +
115
+ "{\" type\" :\" array\" ,\" items\" :\" string\" }],\" default\" :null},{\" name\" :\" notfound2\" ,\" type\" :" +
116
+ "[\" null\" ,{\" type\" :\" array\" ,\" items\" :\" string\" }],\" default\" :null}]}}}]}}}]}" ;
117
+ String data = "{\" data\" :[{\" r1\" :[],\" r2\" :[{\" notfound1\" :{\" array\" :[\" val1\" ,\" val2\" ]}}]}]}" ;
118
+ GenericRecord record = readRecord (w , data );
119
+ Assert .assertNull (record .get ("S" ));
120
+ }
121
+
122
+ public GenericRecord readRecord (String schemaString , String jsonData ) throws IOException {
123
+ Schema schema = Schema .parse (schemaString );
124
+ Decoder decoder = new ExtendedJsonDecoder (schema , jsonData );
125
+ DatumReader <GenericRecord > datumReader = new GenericDatumReader <GenericRecord >(schema );
126
+ return datumReader .read (null , decoder );
127
+ }
63
128
}
0 commit comments