2222import static org .hamcrest .CoreMatchers .equalTo ;
2323import static org .hamcrest .MatcherAssert .assertThat ;
2424import static org .junit .jupiter .api .Assertions .assertEquals ;
25- import static org .junit .jupiter .api .Assertions .assertFalse ;
2625import static org .junit .jupiter .api .Assertions .assertNotEquals ;
26+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
2727import 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+
2832
2933import org .junit .jupiter .api .BeforeEach ;
3034
@@ -48,32 +52,68 @@ void test_basicJsonDecoder() {
4852 String stringValue = UUID .randomUUID ().toString ();
4953 Random r = new Random ();
5054 int intValue = r .nextInt ();
51- String inputString = "[{\" key1\" :\" " + stringValue + "\" , \" key2\" :" + intValue + "}]" ;
55+ String inputString = "[{\" key1\" :\" " + stringValue + "\" , \" key2\" :" + intValue + "}]" ;
5256 try {
5357 jsonDecoder .parse (new ByteArrayInputStream (inputString .getBytes ()), null , (record ) -> {
5458 receivedRecord = record ;
5559 });
56- } catch (Exception e ){}
57-
60+ } catch (Exception e ) {
61+ }
62+
5863 assertNotEquals (receivedRecord , null );
5964 Map <String , Object > map = receivedRecord .getData ().toMap ();
6065 assertThat (map .get ("key1" ), equalTo (stringValue ));
6166 assertThat (map .get ("key2" ), equalTo (intValue ));
6267 }
6368
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+
64103 @ Test
65104 void test_basicJsonDecoder_withTimeReceived () {
66105 String stringValue = UUID .randomUUID ().toString ();
67106 Random r = new Random ();
68107 int intValue = r .nextInt ();
69- String inputString = "[{\" key1\" :\" " + stringValue + "\" , \" key2\" :" + intValue + "}]" ;
108+ String inputString = "[{\" key1\" :\" " + stringValue + "\" , \" key2\" :" + intValue + "}]" ;
70109 final Instant now = Instant .now ();
71110 try {
72111 jsonDecoder .parse (new ByteArrayInputStream (inputString .getBytes ()), now , (record ) -> {
73112 receivedRecord = record ;
74113 receivedTime = record .getData ().getEventHandle ().getInternalOriginationTime ();
75114 });
76- } catch (Exception e ){}
115+ } catch (Exception e ) {
116+ }
77117
78118 assertNotEquals (receivedRecord , null );
79119 Map <String , Object > map = receivedRecord .getData ().toMap ();
@@ -91,21 +131,23 @@ class JsonDecoderWithInputConfig {
91131 private static final int numKeyPerRecord = 3 ;
92132 private Map <String , Object > jsonObject ;
93133 private final String key_name = "logEvents" ;
134+ private final Integer maxEventLength = 20000000 ;
94135
95136 @ BeforeEach
96137 void setup () {
97138 objectMapper = new ObjectMapper ();
98- for (int i = 0 ; i < 10 ; i ++) {
139+ for (int i = 0 ; i < 10 ; i ++) {
99140 includeKeys .add (UUID .randomUUID ().toString ());
100141 includeMetadataKeys .add (UUID .randomUUID ().toString ());
101142 }
102143 jsonObject = generateJsonWithSpecificKeys (includeKeys , includeMetadataKeys , key_name , numKeyRecords , numKeyPerRecord );
103144 }
145+
104146 @ Test
105147 void test_basicJsonDecoder_withInputConfig () throws IOException {
106148 final Instant now = Instant .now ();
107149 List <Record <Event >> records = new ArrayList <>();
108- jsonDecoder = new JsonDecoder (key_name , includeKeys , includeMetadataKeys );
150+ jsonDecoder = new JsonDecoder (key_name , includeKeys , includeMetadataKeys , maxEventLength );
109151 jsonDecoder .parse (createInputStream (jsonObject ), now , (record ) -> {
110152 records .add (record );
111153 receivedTime = record .getData ().getEventHandle ().getInternalOriginationTime ();
@@ -118,10 +160,10 @@ void test_basicJsonDecoder_withInputConfig() throws IOException {
118160 Map <String , Object > dataMap = record .getData ().toMap ();
119161 Map <String , Object > metadataMap = record .getData ().getMetadata ().getAttributes ();
120162
121- for (String includeKey : includeKeys ) {
163+ for (String includeKey : includeKeys ) {
122164 assertThat (dataMap .get (includeKey ), equalTo (jsonObject .get (includeKey )));
123165 }
124- for (String includeMetadataKey : includeMetadataKeys ) {
166+ for (String includeMetadataKey : includeMetadataKeys ) {
125167 assertThat (metadataMap .get (includeMetadataKey ), equalTo (jsonObject .get (includeMetadataKey )));
126168 }
127169 });
@@ -133,7 +175,7 @@ void test_basicJsonDecoder_withInputConfig() throws IOException {
133175 void test_basicJsonDecoder_withInputConfig_withoutEvents_empty_metadata_keys () throws IOException {
134176 final Instant now = Instant .now ();
135177 List <Record <Event >> records = new ArrayList <>();
136- jsonDecoder = new JsonDecoder ("" , includeKeys , Collections .emptyList ());
178+ jsonDecoder = new JsonDecoder ("" , includeKeys , Collections .emptyList (), maxEventLength );
137179 jsonDecoder .parse (createInputStream (jsonObject ), now , (record ) -> {
138180 records .add (record );
139181 receivedTime = record .getData ().getEventHandle ().getInternalOriginationTime ();
@@ -145,7 +187,7 @@ void test_basicJsonDecoder_withInputConfig_withoutEvents_empty_metadata_keys() t
145187 void test_basicJsonDecoder_withInputConfig_withoutEvents_null_include_metadata_keys () throws IOException {
146188 final Instant now = Instant .now ();
147189 List <Record <Event >> records = new ArrayList <>();
148- jsonDecoder = new JsonDecoder ("" , includeKeys , null );
190+ jsonDecoder = new JsonDecoder ("" , includeKeys , null , maxEventLength );
149191 jsonDecoder .parse (createInputStream (jsonObject ), now , (record ) -> {
150192 records .add (record );
151193 receivedTime = record .getData ().getEventHandle ().getInternalOriginationTime ();
@@ -158,7 +200,7 @@ void test_basicJsonDecoder_withInputConfig_withoutEvents_null_include_metadata_k
158200 void test_basicJsonDecoder_withInputConfig_withoutEvents_empty_include_keys () throws IOException {
159201 final Instant now = Instant .now ();
160202 List <Record <Event >> records = new ArrayList <>();
161- jsonDecoder = new JsonDecoder ("" , Collections .emptyList (), includeMetadataKeys );
203+ jsonDecoder = new JsonDecoder ("" , Collections .emptyList (), includeMetadataKeys , maxEventLength );
162204 jsonDecoder .parse (createInputStream (jsonObject ), now , (record ) -> {
163205 records .add (record );
164206 receivedTime = record .getData ().getEventHandle ().getInternalOriginationTime ();
@@ -170,7 +212,7 @@ void test_basicJsonDecoder_withInputConfig_withoutEvents_empty_include_keys() th
170212 void test_basicJsonDecoder_withInputConfig_withoutEvents_null_include_keys () throws IOException {
171213 final Instant now = Instant .now ();
172214 List <Record <Event >> records = new ArrayList <>();
173- jsonDecoder = new JsonDecoder ("" , null , includeMetadataKeys );
215+ jsonDecoder = new JsonDecoder ("" , null , includeMetadataKeys , maxEventLength );
174216 jsonDecoder .parse (createInputStream (jsonObject ), now , (record ) -> {
175217 records .add (record );
176218 receivedTime = record .getData ().getEventHandle ().getInternalOriginationTime ();
@@ -187,17 +229,17 @@ private Map<String, Object> generateJsonWithSpecificKeys(final List<String> incl
187229 final Map <String , Object > jsonObject = new LinkedHashMap <>();
188230 final List <Map <String , Object >> innerObjects = new ArrayList <>();
189231
190- for (String includeKey : includeKeys ) {
232+ for (String includeKey : includeKeys ) {
191233 jsonObject .put (includeKey , UUID .randomUUID ().toString ());
192234 }
193235
194- for (String includeMetadataKey : includeMetadataKeys ) {
236+ for (String includeMetadataKey : includeMetadataKeys ) {
195237 jsonObject .put (includeMetadataKey , UUID .randomUUID ().toString ());
196238 }
197239
198- for (int i = 0 ; i < numKeyRecords ; i ++) {
240+ for (int i = 0 ; i < numKeyRecords ; i ++) {
199241 final Map <String , Object > innerJsonMap = new LinkedHashMap <>();
200- for (int j = 0 ; j < numKeyPerRecord ; j ++) {
242+ for (int j = 0 ; j < numKeyPerRecord ; j ++) {
201243 innerJsonMap .put (UUID .randomUUID ().toString (), UUID .randomUUID ().toString ());
202244 }
203245 innerObjects .add (innerJsonMap );
0 commit comments