1
1
package com .fasterxml .jackson .dataformat .avro ;
2
2
3
+ import java .io .IOException ;
3
4
import java .util .*;
4
5
5
6
public class MapWithUnionTest extends AvroTestBase
@@ -14,9 +15,38 @@ public class MapWithUnionTest extends AvroTestBase
14
15
+ " ] \n "
15
16
+"}\n " );
16
17
18
+ // for [dataformats-binary#39]
19
+ final static String MAP_CONTAINER_SCHEMA_JSON = aposToQuotes ("{\n "
20
+ +" 'namespace': 'com.salesforce.conduit.avro',\n "
21
+ +" 'type': 'record',\n "
22
+ +" 'name': 'MapContainer',\n "
23
+ +" 'fields': [\n "
24
+ +" {'name':'props', \n "
25
+ +" 'type' : {\n "
26
+ +" 'type' : 'map', \n "
27
+ +" 'values': ['null','int','long','float','double','string','boolean',{'type':'map','values':['null','int','long','float','double','string','boolean']}]\n "
28
+ +" }\n "
29
+ +" }\n "
30
+ +" ]\n "
31
+ +"}" );
32
+ static class MapContainer {
33
+ public Map <String , Object > props ;
34
+
35
+ public MapContainer () {}
36
+ public MapContainer (Map <String , Object > p ) {
37
+ props = p ;
38
+ }
39
+ }
40
+
41
+ /*
42
+ /**********************************************************
43
+ /* Test methods
44
+ /**********************************************************
45
+ */
46
+
17
47
private final AvroMapper MAPPER = getMapper ();
18
48
19
- public void testRecordWithMap () throws Exception
49
+ public void testRootMapWithUnion () throws Exception
20
50
{
21
51
AvroSchema schema = MAPPER .schemaFrom (MAP_WITH_UNION_SCHEMA_JSON );
22
52
Map <String ,Object > input = new LinkedHashMap <String ,Object >();
@@ -33,4 +63,31 @@ public void testRecordWithMap() throws Exception
33
63
assertEquals ("123" , result .get ("a" ));
34
64
assertEquals ("foobar" , result .get ("xy" ));
35
65
}
66
+
67
+ public void testMapContainerWithNested () throws IOException
68
+ {
69
+ Map <String ,Object > map = new LinkedHashMap <String ,Object >();
70
+ map .put ("hello" , "world" );
71
+ Map <String ,String > otherMap = new LinkedHashMap <String ,String >();
72
+ otherMap .put ("foo" , "bar" );
73
+ otherMap .put ("zap" , "bing" );
74
+ map .put ("otherMap" , otherMap );
75
+ map .put ("goodbye" , "charlie" );
76
+ MapContainer event = new MapContainer (map );
77
+
78
+ AvroSchema avroSchema = MAPPER .schemaFrom (MAP_CONTAINER_SCHEMA_JSON );
79
+ byte [] serialized = MAPPER .writer (avroSchema ).writeValueAsBytes (event );
80
+
81
+ MapContainer deserialized = MAPPER .readerFor (MapContainer .class )
82
+ .with (avroSchema )
83
+ .readValue (serialized );
84
+ assertEquals (3 , deserialized .props .size ());
85
+ assertEquals ("world" , deserialized .props .get ("hello" ));
86
+ assertEquals ("charlie" , deserialized .props .get ("goodbye" ));
87
+ Object ob = deserialized .props .get ("otherMap" );
88
+ assertTrue (ob instanceof Map <?,?>);
89
+ Map <?,?> m = (Map <?,?>) ob ;
90
+ assertEquals ("bar" , m .get ("foo" ));
91
+ assertEquals ("bing" , m .get ("zap" ));
92
+ }
36
93
}
0 commit comments