11package com .sap .hcp .cf .logging .common .converter ;
22
3- import static com .sap .hcp .cf .logging .common .converter .UnmarshallUtilities . unmarshal ;
4- import static com .sap .hcp .cf .logging .common .converter .UnmarshallUtilities .unmarshalPrefixed ;
3+ import static com .sap .hcp .cf .logging .common .converter .CustomFieldMatchers . hasCustomField ;
4+ import static com .sap .hcp .cf .logging .common .converter .UnmarshallUtilities .unmarshalCustomFields ;
55import static com .sap .hcp .cf .logging .common .customfields .CustomField .customField ;
6- import static org .hamcrest .Matchers .allOf ;
7- import static org .hamcrest .Matchers .hasEntry ;
6+ import static org .hamcrest .Matchers .contains ;
7+ import static org .hamcrest .Matchers .containsInAnyOrder ;
88import static org .hamcrest .Matchers .hasToString ;
9+ import static org .hamcrest .Matchers .isEmptyString ;
910import static org .junit .Assert .assertThat ;
1011
12+ import java .util .Arrays ;
1113import java .util .Collections ;
1214import java .util .HashMap ;
1315import java .util .Map ;
1719
1820public class DefaultCustomFieldsConverterTest {
1921
22+ private static final String CUSTOM_KEY_0 = "custom_key_0" ;
23+ private static final String CUSTOM_VALUE_0 = "custom_value_0" ;
24+ private static final String CUSTOM_KEY_1 = "custom_key_1" ;
25+ private static final String CUSTOM_VALUE_1 = "custom_value_1" ;
26+ private static final String UNREGISTERED_KEY = "unregistered_key" ;
27+ private static final String UNREGISTERED_VALUE = "unregistered_value" ;
2028 private static final String HACK_ATTEMPT = "}{:\" ,\" " ;
2129 private DefaultCustomFieldsConverter converter ;
2230
2331 @ Before
2432 public void initConverter () {
2533 this .converter = new DefaultCustomFieldsConverter ();
34+ converter .setCustomFieldKeyNames (Arrays .asList (CUSTOM_KEY_0 , CUSTOM_KEY_1 ));
2635 }
2736
2837 @ Test
@@ -45,84 +54,121 @@ public void standardArgument() throws Exception {
4554 public void singleCustomFieldArgumentEmbedded () throws Exception {
4655 StringBuilder sb = new StringBuilder ();
4756
48- converter .convert (sb , Collections .emptyMap (), customField ("some key" , "some value" ));
57+ converter .convert (sb , Collections .emptyMap (), customField (CUSTOM_KEY_0 , CUSTOM_VALUE_0 ));
4958
50- assertThat (unmarshal (sb ), hasEntry ("some key" , "some value" ));
59+ assertThat (unmarshalCustomFields (sb ), contains (hasCustomField (CUSTOM_KEY_0 , CUSTOM_VALUE_0 , 0 )));
60+ }
61+
62+ @ SuppressWarnings ("unchecked" )
63+ @ Test
64+ public void multipleCustomFieldArgumentEmbedded () throws Exception {
65+ StringBuilder sb = new StringBuilder ();
66+
67+ converter .convert (sb , Collections .emptyMap (), customField (CUSTOM_KEY_1 , CUSTOM_VALUE_1 ),
68+ customField (UNREGISTERED_KEY , UNREGISTERED_VALUE ), customField (CUSTOM_KEY_0 , CUSTOM_VALUE_0 ));
69+
70+ assertThat (unmarshalCustomFields (sb ), containsInAnyOrder (hasCustomField (CUSTOM_KEY_0 , CUSTOM_VALUE_0 , 0 ),
71+ hasCustomField (CUSTOM_KEY_1 , CUSTOM_VALUE_1 , 1 )));
5172 }
5273
5374 @ Test
5475 public void singleCustomFieldArgumentPrefix () throws Exception {
5576 converter .setFieldName ("prefix" );
5677 StringBuilder sb = new StringBuilder ();
5778
58- converter .convert (sb , Collections .emptyMap (), customField ("some key" , "some value" ));
79+ converter .convert (sb , Collections .emptyMap (), customField (CUSTOM_KEY_0 , CUSTOM_VALUE_0 ));
5980
60- assertThat (unmarshalPrefixed (sb , "prefix" ), hasEntry ( "some key" , "some value" ));
81+ assertThat (unmarshalCustomFields (sb , "prefix" ), contains ( hasCustomField ( CUSTOM_KEY_0 , CUSTOM_VALUE_0 , 0 ) ));
6182 }
6283
6384 @ Test
6485 public void singleMdcField () throws Exception {
6586 StringBuilder sb = new StringBuilder ();
66-
87+
6788 @ SuppressWarnings ("serial" )
6889 Map <String , String > mdcFields = new HashMap <String , String >() {
6990 {
70- put ("some key" , "some value" );
71- }};
91+ put (CUSTOM_KEY_0 , CUSTOM_VALUE_0 );
92+ }
93+ };
7294
7395 converter .convert (sb , mdcFields );
7496
75- assertThat (unmarshal (sb ), hasEntry ( "some key" , "some value" ));
97+ assertThat (unmarshalCustomFields (sb ), contains ( hasCustomField ( CUSTOM_KEY_0 , CUSTOM_VALUE_0 , 0 ) ));
7698 }
7799
100+ @ SuppressWarnings ("unchecked" )
78101 @ Test
79- public void mergesMdcFieldsAndArguments () throws Exception {
102+ public void multipleMdcFields () throws Exception {
80103 StringBuilder sb = new StringBuilder ();
81104
82105 @ SuppressWarnings ("serial" )
83106 Map <String , String > mdcFields = new HashMap <String , String >() {
84107 {
85- put ("mdc key" , "mdc value" );
108+ put (CUSTOM_KEY_1 , CUSTOM_VALUE_1 );
109+ put (UNREGISTERED_KEY , UNREGISTERED_VALUE );
110+ put (CUSTOM_KEY_0 , CUSTOM_VALUE_0 );
86111 }
87112 };
88113
89- converter .convert (sb , mdcFields , customField ( "some key" , "some value" ) );
114+ converter .convert (sb , mdcFields );
90115
91- assertThat (unmarshal (sb ),
92- allOf ( hasEntry ( "some key" , "some value" ), hasEntry ( "mdc key" , "mdc value" )));
116+ assertThat (unmarshalCustomFields (sb ), containsInAnyOrder ( hasCustomField ( CUSTOM_KEY_0 , CUSTOM_VALUE_0 , 0 ),
117+ hasCustomField ( CUSTOM_KEY_1 , CUSTOM_VALUE_1 , 1 )));
93118 }
94119
120+ @ SuppressWarnings ("unchecked" )
95121 @ Test
96- public void properlyEscapesValues () throws Exception {
122+ public void argumentsTakePrecendenceOverMdc () throws Exception {
123+ StringBuilder sb = new StringBuilder ();
124+
125+ @ SuppressWarnings ("serial" )
126+ Map <String , String > mdcFields = new HashMap <String , String >() {
127+ {
128+ put (CUSTOM_KEY_0 , CUSTOM_VALUE_0 );
129+ put (CUSTOM_KEY_1 , CUSTOM_VALUE_1 );
130+ }
131+ };
132+
133+ converter .convert (sb , mdcFields , customField (CUSTOM_KEY_0 , "preferred value" ));
134+
135+ assertThat (unmarshalCustomFields (sb ), containsInAnyOrder (hasCustomField (CUSTOM_KEY_0 , "preferred value" , 0 ),
136+ hasCustomField (CUSTOM_KEY_1 , CUSTOM_VALUE_1 , 1 )));
137+ }
138+
139+ @ Test
140+ public void doesNotWriteJsonWhenNoFieldKeysAreConfigured () throws Exception {
97141 StringBuilder sb = new StringBuilder ();
98142
99- converter .convert (sb , Collections .emptyMap (), customField ("some key" , HACK_ATTEMPT ));
143+ converter .setCustomFieldKeyNames (Collections .emptyList ());
144+ converter .convert (sb , Collections .emptyMap (), customField (CUSTOM_KEY_0 , CUSTOM_VALUE_0 ));
100145
101- assertThat (unmarshal ( sb ), hasEntry ( "some key" , HACK_ATTEMPT ));
146+ assertThat (sb . toString ( ), isEmptyString ( ));
102147 }
103148
104149 @ Test
105- public void properlyEscapesKeys () throws Exception {
150+ public void properlyEscapesValues () throws Exception {
106151 StringBuilder sb = new StringBuilder ();
107152
108- converter .convert (sb , Collections .emptyMap (), customField (HACK_ATTEMPT , "some value" ));
153+ converter .convert (sb , Collections .emptyMap (), customField (CUSTOM_KEY_0 , HACK_ATTEMPT ));
109154
110- assertThat (unmarshal (sb ), hasEntry ( HACK_ATTEMPT , "some value" ));
155+ assertThat (unmarshalCustomFields (sb ), contains ( hasCustomField ( CUSTOM_KEY_0 , HACK_ATTEMPT , 0 ) ));
111156 }
112157
113158 @ Test
114159 public void properlyEscapesMdcFields () throws Exception {
115160 StringBuilder sb = new StringBuilder ();
116-
161+
117162 @ SuppressWarnings ("serial" )
118163 Map <String , String > mdcFields = new HashMap <String , String >() {
119164 {
120- put (HACK_ATTEMPT , HACK_ATTEMPT );
121- }};
165+ put (CUSTOM_KEY_0 , HACK_ATTEMPT );
166+ }
167+ };
122168
123169 converter .convert (sb , mdcFields );
124170
125- assertThat (unmarshal (sb ), hasEntry ( HACK_ATTEMPT , HACK_ATTEMPT ));
171+ assertThat (unmarshalCustomFields (sb ), contains ( hasCustomField ( CUSTOM_KEY_0 , HACK_ATTEMPT , 0 ) ));
126172
127173 }
128174
@@ -131,10 +177,10 @@ public void properlyEscapesFieldNames() throws Exception {
131177 converter .setFieldName (HACK_ATTEMPT );
132178 StringBuilder sb = new StringBuilder ();
133179
134- converter .convert (sb , Collections .emptyMap (), customField ("some key" , "some value" ));
180+ converter .convert (sb , Collections .emptyMap (), customField (CUSTOM_KEY_0 , CUSTOM_VALUE_0 ));
135181
136- assertThat (unmarshalPrefixed (sb , HACK_ATTEMPT ), hasEntry ("some key" , "some value" ));
182+ assertThat (unmarshalCustomFields (sb , HACK_ATTEMPT ),
183+ contains (hasCustomField (CUSTOM_KEY_0 , CUSTOM_VALUE_0 , 0 )));
137184 }
138185
139186}
140-
0 commit comments