22
22
import static org .hamcrest .CoreMatchers .equalTo ;
23
23
import static org .hamcrest .MatcherAssert .assertThat ;
24
24
import static org .junit .jupiter .api .Assertions .assertEquals ;
25
- import static org .junit .jupiter .api .Assertions .assertFalse ;
26
25
import static org .junit .jupiter .api .Assertions .assertNotEquals ;
26
+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
27
27
import static org .junit .jupiter .api .Assertions .assertTrue ;
28
+ import static org .junit .jupiter .api .Assertions .assertFalse ;
29
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
30
+ import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
31
+
28
32
29
33
import org .junit .jupiter .api .BeforeEach ;
30
34
@@ -48,32 +52,68 @@ void test_basicJsonDecoder() {
48
52
String stringValue = UUID .randomUUID ().toString ();
49
53
Random r = new Random ();
50
54
int intValue = r .nextInt ();
51
- String inputString = "[{\" key1\" :\" " + stringValue + "\" , \" key2\" :" + intValue + "}]" ;
55
+ String inputString = "[{\" key1\" :\" " + stringValue + "\" , \" key2\" :" + intValue + "}]" ;
52
56
try {
53
57
jsonDecoder .parse (new ByteArrayInputStream (inputString .getBytes ()), null , (record ) -> {
54
58
receivedRecord = record ;
55
59
});
56
- } catch (Exception e ){}
57
-
60
+ } catch (Exception e ) {
61
+ }
62
+
58
63
assertNotEquals (receivedRecord , null );
59
64
Map <String , Object > map = receivedRecord .getData ().toMap ();
60
65
assertThat (map .get ("key1" ), equalTo (stringValue ));
61
66
assertThat (map .get ("key2" ), equalTo (intValue ));
62
67
}
63
68
69
+ @ Test
70
+ void test_basicJsonDecoder_exceedingMaxEventLength_throwsException () {
71
+ String largeString = "x" .repeat (200 );
72
+ String inputString = "[{\" key1\" :\" " + largeString + "\" }]" ;
73
+
74
+ jsonDecoder = new JsonDecoder (null , null , null , 100 );
75
+
76
+ Exception exception = assertThrows (Exception .class , () -> {
77
+ jsonDecoder .parse (new ByteArrayInputStream (inputString .getBytes ()), null , (record ) -> {
78
+ receivedRecord = record ;
79
+ });
80
+ });
81
+
82
+ assertEquals ("String value length (200) exceeds the maximum allowed (100, from `StreamReadConstraints.getMaxStringLength()`)" , exception .getMessage ());
83
+ }
84
+
85
+ @ Test
86
+ void test_basicJsonDecoder_withMaxEventLength () {
87
+ String validString = "Short string" ;
88
+ String inputString = "[{\" key1\" :\" " + validString + "\" }]" ;
89
+
90
+ jsonDecoder = new JsonDecoder (null , null , null , 100 );
91
+
92
+ assertDoesNotThrow (() -> {
93
+ jsonDecoder .parse (new ByteArrayInputStream (inputString .getBytes ()), null , (record ) -> {
94
+ receivedRecord = record ;
95
+ });
96
+ });
97
+
98
+ assertNotNull (receivedRecord );
99
+ Map <String , Object > map = receivedRecord .getData ().toMap ();
100
+ assertThat (map .get ("key1" ), equalTo (validString ));
101
+ }
102
+
64
103
@ Test
65
104
void test_basicJsonDecoder_withTimeReceived () {
66
105
String stringValue = UUID .randomUUID ().toString ();
67
106
Random r = new Random ();
68
107
int intValue = r .nextInt ();
69
- String inputString = "[{\" key1\" :\" " + stringValue + "\" , \" key2\" :" + intValue + "}]" ;
108
+ String inputString = "[{\" key1\" :\" " + stringValue + "\" , \" key2\" :" + intValue + "}]" ;
70
109
final Instant now = Instant .now ();
71
110
try {
72
111
jsonDecoder .parse (new ByteArrayInputStream (inputString .getBytes ()), now , (record ) -> {
73
112
receivedRecord = record ;
74
113
receivedTime = record .getData ().getEventHandle ().getInternalOriginationTime ();
75
114
});
76
- } catch (Exception e ){}
115
+ } catch (Exception e ) {
116
+ }
77
117
78
118
assertNotEquals (receivedRecord , null );
79
119
Map <String , Object > map = receivedRecord .getData ().toMap ();
@@ -91,21 +131,23 @@ class JsonDecoderWithInputConfig {
91
131
private static final int numKeyPerRecord = 3 ;
92
132
private Map <String , Object > jsonObject ;
93
133
private final String key_name = "logEvents" ;
134
+ private final Integer maxEventLength = 20000000 ;
94
135
95
136
@ BeforeEach
96
137
void setup () {
97
138
objectMapper = new ObjectMapper ();
98
- for (int i = 0 ; i < 10 ; i ++) {
139
+ for (int i = 0 ; i < 10 ; i ++) {
99
140
includeKeys .add (UUID .randomUUID ().toString ());
100
141
includeMetadataKeys .add (UUID .randomUUID ().toString ());
101
142
}
102
143
jsonObject = generateJsonWithSpecificKeys (includeKeys , includeMetadataKeys , key_name , numKeyRecords , numKeyPerRecord );
103
144
}
145
+
104
146
@ Test
105
147
void test_basicJsonDecoder_withInputConfig () throws IOException {
106
148
final Instant now = Instant .now ();
107
149
List <Record <Event >> records = new ArrayList <>();
108
- jsonDecoder = new JsonDecoder (key_name , includeKeys , includeMetadataKeys );
150
+ jsonDecoder = new JsonDecoder (key_name , includeKeys , includeMetadataKeys , maxEventLength );
109
151
jsonDecoder .parse (createInputStream (jsonObject ), now , (record ) -> {
110
152
records .add (record );
111
153
receivedTime = record .getData ().getEventHandle ().getInternalOriginationTime ();
@@ -118,10 +160,10 @@ void test_basicJsonDecoder_withInputConfig() throws IOException {
118
160
Map <String , Object > dataMap = record .getData ().toMap ();
119
161
Map <String , Object > metadataMap = record .getData ().getMetadata ().getAttributes ();
120
162
121
- for (String includeKey : includeKeys ) {
163
+ for (String includeKey : includeKeys ) {
122
164
assertThat (dataMap .get (includeKey ), equalTo (jsonObject .get (includeKey )));
123
165
}
124
- for (String includeMetadataKey : includeMetadataKeys ) {
166
+ for (String includeMetadataKey : includeMetadataKeys ) {
125
167
assertThat (metadataMap .get (includeMetadataKey ), equalTo (jsonObject .get (includeMetadataKey )));
126
168
}
127
169
});
@@ -133,7 +175,7 @@ void test_basicJsonDecoder_withInputConfig() throws IOException {
133
175
void test_basicJsonDecoder_withInputConfig_withoutEvents_empty_metadata_keys () throws IOException {
134
176
final Instant now = Instant .now ();
135
177
List <Record <Event >> records = new ArrayList <>();
136
- jsonDecoder = new JsonDecoder ("" , includeKeys , Collections .emptyList ());
178
+ jsonDecoder = new JsonDecoder ("" , includeKeys , Collections .emptyList (), maxEventLength );
137
179
jsonDecoder .parse (createInputStream (jsonObject ), now , (record ) -> {
138
180
records .add (record );
139
181
receivedTime = record .getData ().getEventHandle ().getInternalOriginationTime ();
@@ -145,7 +187,7 @@ void test_basicJsonDecoder_withInputConfig_withoutEvents_empty_metadata_keys() t
145
187
void test_basicJsonDecoder_withInputConfig_withoutEvents_null_include_metadata_keys () throws IOException {
146
188
final Instant now = Instant .now ();
147
189
List <Record <Event >> records = new ArrayList <>();
148
- jsonDecoder = new JsonDecoder ("" , includeKeys , null );
190
+ jsonDecoder = new JsonDecoder ("" , includeKeys , null , maxEventLength );
149
191
jsonDecoder .parse (createInputStream (jsonObject ), now , (record ) -> {
150
192
records .add (record );
151
193
receivedTime = record .getData ().getEventHandle ().getInternalOriginationTime ();
@@ -158,7 +200,7 @@ void test_basicJsonDecoder_withInputConfig_withoutEvents_null_include_metadata_k
158
200
void test_basicJsonDecoder_withInputConfig_withoutEvents_empty_include_keys () throws IOException {
159
201
final Instant now = Instant .now ();
160
202
List <Record <Event >> records = new ArrayList <>();
161
- jsonDecoder = new JsonDecoder ("" , Collections .emptyList (), includeMetadataKeys );
203
+ jsonDecoder = new JsonDecoder ("" , Collections .emptyList (), includeMetadataKeys , maxEventLength );
162
204
jsonDecoder .parse (createInputStream (jsonObject ), now , (record ) -> {
163
205
records .add (record );
164
206
receivedTime = record .getData ().getEventHandle ().getInternalOriginationTime ();
@@ -170,7 +212,7 @@ void test_basicJsonDecoder_withInputConfig_withoutEvents_empty_include_keys() th
170
212
void test_basicJsonDecoder_withInputConfig_withoutEvents_null_include_keys () throws IOException {
171
213
final Instant now = Instant .now ();
172
214
List <Record <Event >> records = new ArrayList <>();
173
- jsonDecoder = new JsonDecoder ("" , null , includeMetadataKeys );
215
+ jsonDecoder = new JsonDecoder ("" , null , includeMetadataKeys , maxEventLength );
174
216
jsonDecoder .parse (createInputStream (jsonObject ), now , (record ) -> {
175
217
records .add (record );
176
218
receivedTime = record .getData ().getEventHandle ().getInternalOriginationTime ();
@@ -187,17 +229,17 @@ private Map<String, Object> generateJsonWithSpecificKeys(final List<String> incl
187
229
final Map <String , Object > jsonObject = new LinkedHashMap <>();
188
230
final List <Map <String , Object >> innerObjects = new ArrayList <>();
189
231
190
- for (String includeKey : includeKeys ) {
232
+ for (String includeKey : includeKeys ) {
191
233
jsonObject .put (includeKey , UUID .randomUUID ().toString ());
192
234
}
193
235
194
- for (String includeMetadataKey : includeMetadataKeys ) {
236
+ for (String includeMetadataKey : includeMetadataKeys ) {
195
237
jsonObject .put (includeMetadataKey , UUID .randomUUID ().toString ());
196
238
}
197
239
198
- for (int i = 0 ; i < numKeyRecords ; i ++) {
240
+ for (int i = 0 ; i < numKeyRecords ; i ++) {
199
241
final Map <String , Object > innerJsonMap = new LinkedHashMap <>();
200
- for (int j = 0 ; j < numKeyPerRecord ; j ++) {
242
+ for (int j = 0 ; j < numKeyPerRecord ; j ++) {
201
243
innerJsonMap .put (UUID .randomUUID ().toString (), UUID .randomUUID ().toString ());
202
244
}
203
245
innerObjects .add (innerJsonMap );
0 commit comments