20
20
import org .junit .jupiter .params .provider .ArgumentsSource ;
21
21
import org .mockito .Mock ;
22
22
import org .mockito .junit .jupiter .MockitoExtension ;
23
+ import com .google .common .collect .ImmutableMap ;
23
24
24
25
import java .util .Collections ;
25
26
import java .util .HashMap ;
@@ -53,7 +54,7 @@ private TruncateProcessor createObjectUnderTest() {
53
54
@ ArgumentsSource (TruncateArgumentsProvider .class )
54
55
void testTruncateProcessor (final Object messageValue , final Integer startAt , final Integer truncateLength , final Object truncatedMessage ) {
55
56
56
- when (config .getEntries ()).thenReturn (Collections .singletonList (createEntry (List .of ("message" ), startAt , truncateLength , null )));
57
+ when (config .getEntries ()).thenReturn (Collections .singletonList (createEntry (List .of ("message" ), startAt , truncateLength , null , false )));
57
58
final TruncateProcessor truncateProcessor = createObjectUnderTest ();
58
59
final Record <Event > record = createEvent ("message" , messageValue );
59
60
final List <Record <Event >> truncatedRecords = (List <Record <Event >>) truncateProcessor .doExecute (Collections .singletonList (record ));
@@ -64,8 +65,8 @@ void testTruncateProcessor(final Object messageValue, final Integer startAt, fin
64
65
@ ParameterizedTest
65
66
@ ArgumentsSource (MultipleTruncateArgumentsProvider .class )
66
67
void testTruncateProcessorMultipleEntries (final Object messageValue , final Integer startAt1 , final Integer truncateLength1 , final Integer startAt2 , final Integer truncateLength2 , final Object truncatedMessage1 , final Object truncatedMessage2 ) {
67
- TruncateProcessorConfig .Entry entry1 = createEntry (List .of ("message1" ), startAt1 , truncateLength1 , null );
68
- TruncateProcessorConfig .Entry entry2 = createEntry (List .of ("message2" ), startAt2 , truncateLength2 , null );
68
+ TruncateProcessorConfig .Entry entry1 = createEntry (List .of ("message1" ), startAt1 , truncateLength1 , null , false );
69
+ TruncateProcessorConfig .Entry entry2 = createEntry (List .of ("message2" ), startAt2 , truncateLength2 , null , false );
69
70
when (config .getEntries ()).thenReturn (List .of (entry1 , entry2 ));
70
71
final Record <Event > record1 = createEvent ("message1" , messageValue );
71
72
final Record <Event > record2 = createEvent ("message2" , messageValue );
@@ -82,7 +83,7 @@ void test_event_is_the_same_when_truncateWhen_condition_returns_false() {
82
83
final String truncateWhen = UUID .randomUUID ().toString ();
83
84
final String message = UUID .randomUUID ().toString ();
84
85
85
- when (config .getEntries ()).thenReturn (Collections .singletonList (createEntry (List .of ("message" ), null , 5 , truncateWhen )));
86
+ when (config .getEntries ()).thenReturn (Collections .singletonList (createEntry (List .of ("message" ), null , 5 , truncateWhen , false )));
86
87
87
88
final TruncateProcessor truncateProcessor = createObjectUnderTest ();
88
89
final Record <Event > record = createEvent ("message" , message );
@@ -92,8 +93,32 @@ void test_event_is_the_same_when_truncateWhen_condition_returns_false() {
92
93
assertThat (truncatedRecords .get (0 ).getData ().toMap (), equalTo (record .getData ().toMap ()));
93
94
}
94
95
95
- private TruncateProcessorConfig .Entry createEntry (final List <String > sourceKeys , final Integer startAt , final Integer length , final String truncateWhen ) {
96
- return new TruncateProcessorConfig .Entry (sourceKeys , startAt , length , truncateWhen );
96
+ @ Test
97
+ void test_event_with_all_fields_truncated () {
98
+ when (config .getEntries ()).thenReturn (Collections .singletonList (createEntry (null , null , 5 , null , false )));
99
+ final TruncateProcessor truncateProcessor = createObjectUnderTest ();
100
+ final Record <Event > record = createEventWithMultipleKeys (Map .of ("key1" , "aaaaa12345" , "key2" , "bbbbb12345" , "key3" , "ccccccc12345" ));
101
+ final List <Record <Event >> truncatedRecords = (List <Record <Event >>) truncateProcessor .doExecute (Collections .singletonList (record ));
102
+ Event event = truncatedRecords .get (0 ).getData ();
103
+ assertThat (event .get ("key1" , String .class ), equalTo ("aaaaa" ));
104
+ assertThat (event .get ("key2" , String .class ), equalTo ("bbbbb" ));
105
+ assertThat (event .get ("key3" , String .class ), equalTo ("ccccc" ));
106
+ }
107
+
108
+ @ Test
109
+ void test_event_with_all_fields_truncated_recursively () {
110
+ when (config .getEntries ()).thenReturn (Collections .singletonList (createEntry (null , null , 5 , null , true )));
111
+ final TruncateProcessor truncateProcessor = createObjectUnderTest ();
112
+ final Record <Event > record = createEventWithMultipleKeys (ImmutableMap .of ("key1" , "aaaaa12345" , "key2" , ImmutableMap .of ("key3" , "bbbbb12345" , "key4" , ImmutableMap .of ("key5" , "ccccccc12345" ))));
113
+ final List <Record <Event >> truncatedRecords = (List <Record <Event >>) truncateProcessor .doExecute (Collections .singletonList (record ));
114
+ Event event = truncatedRecords .get (0 ).getData ();
115
+ assertThat (event .get ("key1" , String .class ), equalTo ("aaaaa" ));
116
+ assertThat (event .get ("key2/key3" , String .class ), equalTo ("bbbbb" ));
117
+ assertThat (event .get ("key2/key4/key5" , String .class ), equalTo ("ccccc" ));
118
+ }
119
+
120
+ private TruncateProcessorConfig .Entry createEntry (final List <String > sourceKeys , final Integer startAt , final Integer length , final String truncateWhen , final boolean recurse ) {
121
+ return new TruncateProcessorConfig .Entry (sourceKeys , startAt , length , truncateWhen , recurse );
97
122
}
98
123
99
124
private Record <Event > createEvent (final String key , final Object value ) {
@@ -105,6 +130,13 @@ private Record<Event> createEvent(final String key, final Object value) {
105
130
.build ());
106
131
}
107
132
133
+ private Record <Event > createEventWithMultipleKeys (final Map <String , Object > data ) {
134
+ return new Record <>(JacksonEvent .builder ()
135
+ .withEventType ("event" )
136
+ .withData (data )
137
+ .build ());
138
+ }
139
+
108
140
static class TruncateArgumentsProvider implements ArgumentsProvider {
109
141
110
142
@ Override
0 commit comments