1
1
/*
2
- * Copyright 2016 the original author or authors.
2
+ * Copyright 2016-2017 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
24
24
import java .util .List ;
25
25
26
26
import org .apache .kafka .common .errors .SerializationException ;
27
+ import org .apache .kafka .common .header .Headers ;
28
+ import org .apache .kafka .common .header .internals .RecordHeaders ;
27
29
import org .apache .kafka .common .serialization .StringDeserializer ;
28
30
import org .apache .kafka .common .serialization .StringSerializer ;
29
31
import org .junit .Before ;
30
32
import org .junit .Test ;
31
33
34
+ import org .springframework .kafka .support .converter .AbstractJavaTypeMapper ;
32
35
import org .springframework .kafka .support .serializer .testentities .DummyEntity ;
33
36
34
37
import com .fasterxml .jackson .core .JsonParseException ;
35
38
36
39
/**
37
40
* @author Igor Stepanov
38
41
* @author Artem Bilan
42
+ * @author Yanming Zhou
39
43
*/
40
44
public class JsonSerializationTests {
41
45
@@ -47,6 +51,8 @@ public class JsonSerializationTests {
47
51
48
52
private JsonDeserializer <DummyEntity > jsonReader ;
49
53
54
+ private JsonDeserializer <DummyEntity > dummyEntityJsonDeserializer ;
55
+
50
56
private DummyEntity entity ;
51
57
52
58
private String topic ;
@@ -73,6 +79,7 @@ public void init() {
73
79
stringReader .configure (new HashMap <String , Object >(), false );
74
80
stringWriter = new StringSerializer ();
75
81
stringWriter .configure (new HashMap <String , Object >(), false );
82
+ dummyEntityJsonDeserializer = new DummyEntityJsonDeserializer ();
76
83
}
77
84
78
85
/*
@@ -83,6 +90,9 @@ public void init() {
83
90
@ Test
84
91
public void testDeserializeSerializedEntityEquals () {
85
92
assertThat (jsonReader .deserialize (topic , jsonWriter .serialize (topic , entity ))).isEqualTo (entity );
93
+ Headers headers = new RecordHeaders ();
94
+ headers .add (AbstractJavaTypeMapper .DEFAULT_CLASSID_FIELD_NAME , DummyEntity .class .getName ().getBytes ());
95
+ assertThat (dummyEntityJsonDeserializer .deserialize (topic , headers , jsonWriter .serialize (topic , entity ))).isEqualTo (entity );
86
96
}
87
97
88
98
/*
@@ -103,6 +113,18 @@ public void testDeserializeSerializedDummyException() {
103
113
catch (Exception e ) {
104
114
fail ("Expected SerializationException, not " + e .getClass ());
105
115
}
116
+ try {
117
+ Headers headers = new RecordHeaders ();
118
+ headers .add (AbstractJavaTypeMapper .DEFAULT_CLASSID_FIELD_NAME , "com.malware.DummyEntity" .getBytes ());
119
+ dummyEntityJsonDeserializer .deserialize (topic , headers , jsonWriter .serialize (topic , entity ));
120
+ fail ("Expected IllegalArgumentException" );
121
+ }
122
+ catch (IllegalArgumentException e ) {
123
+ assertThat (e .getMessage ()).contains ("not in the trusted packages" );
124
+ }
125
+ catch (Exception e ) {
126
+ fail ("Expected IllegalArgumentException, not " + e .getClass ());
127
+ }
106
128
}
107
129
108
130
@ Test
@@ -125,4 +147,8 @@ public void testDeserializedJsonNullEqualsNull() {
125
147
assertThat (jsonReader .deserialize (topic , null )).isEqualTo (null );
126
148
}
127
149
150
+ static class DummyEntityJsonDeserializer extends JsonDeserializer <DummyEntity > {
151
+
152
+ }
153
+
128
154
}
0 commit comments