Skip to content

Commit f83d8a3

Browse files
Refactor DefaultPropertiesConverterTest
1 parent fdd2624 commit f83d8a3

File tree

5 files changed

+231
-132
lines changed

5 files changed

+231
-132
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
11
package com.sap.hcp.cf.logging.common.converter;
22

33
import java.io.IOException;
4-
import java.util.Arrays;
5-
import java.util.HashMap;
6-
import java.util.List;
7-
import java.util.Map;
8-
import java.util.Map.Entry;
9-
10-
import org.slf4j.MDC;
114

125
import com.fasterxml.jackson.jr.ob.JSON;
136
import com.fasterxml.jackson.jr.ob.JSONObjectException;
147

158
public abstract class AbstractConverterTest {
169
protected static final String EMPTY = "";
17-
protected static final String SOME_KEY = "some_key";
18-
protected static final String SOME_VALUE = "some value";
1910
protected static final String STRANGE_SEQ = "}{:\",\"";
20-
protected static final String SOME_OTHER_KEY = "some_other_key";
21-
protected static final String SOME_OTHER_VALUE = "some other value";
2211
protected static final String TEST_MSG_NO_ARGS = "This is a test ";
2312

2413
protected String formatMsg(DefaultMessageConverter mc, String msg) {
@@ -27,55 +16,17 @@ protected String formatMsg(DefaultMessageConverter mc, String msg) {
2716
return sb.toString();
2817
}
2918

30-
protected String formatProps(DefaultPropertiesConverter pc) {
31-
StringBuilder sb = new StringBuilder();
32-
pc.convert(MDC.getCopyOfContextMap(), sb);
33-
return sb.toString();
34-
}
35-
3619
protected String formatStacktrace(DefaultStacktraceConverter dstc, Throwable t) {
3720
StringBuilder sb = new StringBuilder();
3821
dstc.convert(t, sb);
3922
return sb.toString();
4023
}
4124

42-
protected Map<String, Object> mdcMap() {
43-
return mdcMap(null);
44-
}
45-
46-
protected Map<String, Object> mdcMap(String[] exclusions) {
47-
Map<String, Object> result = new HashMap<String, Object>();
48-
List<String> exclusionList;
49-
if (exclusions == null) {
50-
exclusionList = Arrays.asList(new String[0]);
51-
} else {
52-
exclusionList = Arrays.asList(exclusions);
53-
}
54-
for (Entry<String, String> t: MDC.getCopyOfContextMap().entrySet()) {
55-
if (!exclusionList.contains(t.getKey())) {
56-
result.put(t.getKey(), t.getValue());
57-
}
58-
}
59-
return result;
60-
}
61-
6225
protected Object arrayElem(String serialized, int i) throws JSONObjectException, IOException {
6326
return arrayFrom(serialized)[i];
6427
}
6528

6629
protected Object[] arrayFrom(String serialized) throws JSONObjectException, IOException {
6730
return JSON.std.arrayFrom(serialized);
6831
}
69-
70-
protected Map<String, Object> mapFrom(String serialized) throws JSONObjectException, IOException {
71-
return mapFrom(serialized, true);
72-
}
73-
74-
protected Map<String, Object> mapFrom(String serialized, boolean wrap) throws JSONObjectException, IOException {
75-
if (wrap) {
76-
return JSON.std.mapFrom("{" + serialized + "}");
77-
} else {
78-
return JSON.std.mapFrom(serialized);
79-
}
80-
}
8132
}

cf-java-logging-support-core/src/test/java/com/sap/hcp/cf/logging/common/converter/DefaultCustomFieldsConverterTest.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package 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;
35
import static com.sap.hcp.cf.logging.common.customfields.CustomField.customField;
46
import static org.hamcrest.Matchers.allOf;
57
import static org.hamcrest.Matchers.hasEntry;
@@ -13,8 +15,6 @@
1315
import org.junit.Before;
1416
import org.junit.Test;
1517

16-
import com.fasterxml.jackson.jr.ob.JSON;
17-
1818
public class DefaultCustomFieldsConverterTest {
1919

2020
private static final String HACK_ATTEMPT = "}{:\",\"";
@@ -88,7 +88,8 @@ public void mergesMdcFieldsAndArguments() throws Exception {
8888

8989
converter.convert(sb, mdcFields, customField("some key", "some value"));
9090

91-
assertThat(unmarshal(sb), allOf(hasEntry("some key", "some value"), hasEntry("mdc key", "mdc value")));
91+
assertThat(unmarshal(sb),
92+
allOf(hasEntry("some key", "some value"), hasEntry("mdc key", "mdc value")));
9293
}
9394

9495
@Test
@@ -135,15 +136,5 @@ public void properlyEscapesFieldNames() throws Exception {
135136
assertThat(unmarshalPrefixed(sb, HACK_ATTEMPT), hasEntry("some key", "some value"));
136137
}
137138

138-
private static Map<String, Object> unmarshal(StringBuilder sb) throws Exception {
139-
return JSON.std.mapFrom("{" + sb.toString() + "}");
140-
}
141-
142-
@SuppressWarnings("unchecked")
143-
private static Map<String, Object> unmarshalPrefixed(StringBuilder sb, String prefix)
144-
throws Exception {
145-
return (Map<String, Object>) unmarshal(sb).get(prefix);
146-
}
147-
148139
}
149140

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
package com.sap.hcp.cf.logging.common.converter;
2+
3+
import static com.sap.hcp.cf.logging.common.converter.UnmarshallUtilities.unmarshal;
4+
import static org.hamcrest.Matchers.allOf;
5+
import static org.hamcrest.Matchers.hasEntry;
6+
import static org.hamcrest.Matchers.hasKey;
7+
import static org.hamcrest.Matchers.not;
8+
import static org.junit.Assert.assertThat;
9+
10+
import java.util.Arrays;
11+
import java.util.Collections;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
import org.hamcrest.Matcher;
16+
import org.junit.Before;
17+
import org.junit.Test;
18+
import org.slf4j.MDC;
19+
20+
import com.sap.hcp.cf.logging.common.Defaults;
21+
import com.sap.hcp.cf.logging.common.Fields;
22+
23+
public class DefaultPropertiesConverterTest {
24+
25+
private static final String HACK_ATTEMPT = "}{:\",\"";
26+
27+
private DefaultPropertiesConverter converter;
28+
29+
@Before
30+
public void initConverter() {
31+
this.converter = new DefaultPropertiesConverter();
32+
}
33+
34+
@Before
35+
public void cleadMdc() {
36+
MDC.clear();
37+
}
38+
39+
@Test
40+
public void emptyProperties() throws Exception {
41+
StringBuilder sb = new StringBuilder();
42+
43+
converter.convert(Collections.emptyMap(), sb);
44+
45+
assertThat(unmarshal(sb), hasDefaultProperties());
46+
}
47+
48+
@Test
49+
public void singleMdcEntry() throws Exception {
50+
StringBuilder sb = new StringBuilder();
51+
MDC.put("some key", "some value");
52+
53+
converter.convert(Collections.emptyMap(), sb);
54+
55+
assertThat(unmarshal(sb), hasEntry("some key", "some value"));
56+
}
57+
58+
@Test
59+
public void twoMdcEntries() throws Exception {
60+
StringBuilder sb = new StringBuilder();
61+
MDC.put("some key", "some value");
62+
MDC.put("other key", "other value");
63+
64+
converter.convert(Collections.emptyMap(), sb);
65+
66+
assertThat(unmarshal(sb),
67+
allOf(hasEntry("some key", "some value"), hasEntry("other key", "other value")));
68+
}
69+
70+
@Test
71+
public void singleExplicitEntry() throws Exception {
72+
StringBuilder sb = new StringBuilder();
73+
@SuppressWarnings("serial")
74+
Map<String, String> explicitFields = new HashMap<String, String>() {
75+
{
76+
put("explicit key", "explicit value");
77+
}
78+
};
79+
80+
converter.convert(explicitFields, sb);
81+
82+
assertThat(unmarshal(sb), hasEntry("explicit key", "explicit value"));
83+
}
84+
85+
@Test
86+
public void mergesDifferentMdcAndExplicitEntries() throws Exception {
87+
StringBuilder sb = new StringBuilder();
88+
@SuppressWarnings("serial")
89+
Map<String, String> explicitFields = new HashMap<String, String>() {
90+
{
91+
put("explicit key", "explicit value");
92+
}
93+
};
94+
MDC.put("some key", "some value");
95+
96+
converter.convert(explicitFields, sb);
97+
98+
assertThat(unmarshal(sb),
99+
allOf(hasEntry("some key", "some value"), hasEntry("explicit key", "explicit value")));
100+
}
101+
102+
@Test
103+
public void explicitValuesOverwritesMdc() throws Exception {
104+
StringBuilder sb = new StringBuilder();
105+
@SuppressWarnings("serial")
106+
Map<String, String> explicitFields = new HashMap<String, String>() {
107+
{
108+
put("some key", "explicit value");
109+
}
110+
};
111+
MDC.put("some key", "some value");
112+
113+
converter.convert(explicitFields, sb);
114+
115+
assertThat(unmarshal(sb), hasEntry("some key", "explicit value"));
116+
}
117+
118+
@Test
119+
public void dropsExclusions() throws Exception {
120+
StringBuilder sb = new StringBuilder();
121+
@SuppressWarnings("serial")
122+
Map<String, String> explicitFields = new HashMap<String, String>() {
123+
{
124+
put("excluded explicit key", "excluded explicit value");
125+
put("retained explicit key", "retained explicit value");
126+
}
127+
};
128+
MDC.put("retained mdc key", "retained mdc value");
129+
MDC.put("excluded mdc key", "excluded mdc value");
130+
131+
converter.setExclusions(Arrays.asList("excluded explicit key", "excluded mdc key"));
132+
converter.convert(explicitFields, sb);
133+
134+
assertThat(unmarshal(sb),
135+
allOf(hasEntry("retained mdc key", "retained mdc value"),
136+
not(hasEntry("excluded mdc key", "excluded mdc value")),
137+
hasEntry("retained explicit key", "retained explicit value"),
138+
not(hasEntry("excluded explicit key", "excluded explicit value"))));
139+
140+
}
141+
142+
@Test
143+
public void properlyEscapesKeys() throws Exception {
144+
StringBuilder sb = new StringBuilder();
145+
@SuppressWarnings("serial")
146+
Map<String, String> explicitFields = new HashMap<String, String>() {
147+
{
148+
put("explicit" + HACK_ATTEMPT, "explicit value");
149+
}
150+
};
151+
MDC.put("mdc" + HACK_ATTEMPT, "mdc value");
152+
153+
converter.convert(explicitFields, sb);
154+
155+
assertThat(unmarshal(sb),
156+
allOf(hasEntry("mdc" + HACK_ATTEMPT, "mdc value"),
157+
hasEntry("explicit" + HACK_ATTEMPT, "explicit value")));
158+
}
159+
160+
@Test
161+
public void properlyEscapesValues() throws Exception {
162+
StringBuilder sb = new StringBuilder();
163+
@SuppressWarnings("serial")
164+
Map<String, String> explicitFields = new HashMap<String, String>() {
165+
{
166+
put("explicit key", "explicit" + HACK_ATTEMPT);
167+
}
168+
};
169+
MDC.put("mdc key", "mdc" + HACK_ATTEMPT);
170+
171+
converter.convert(explicitFields, sb);
172+
173+
assertThat(unmarshal(sb),
174+
allOf(hasEntry("mdc key", "mdc" + HACK_ATTEMPT), hasEntry("explicit key", "explicit" + HACK_ATTEMPT)));
175+
}
176+
177+
@Test
178+
public void properlyEscapesExclusions() throws Exception {
179+
StringBuilder sb = new StringBuilder();
180+
@SuppressWarnings("serial")
181+
Map<String, String> explicitFields = new HashMap<String, String>() {
182+
{
183+
put("explicit" + HACK_ATTEMPT, "explicit value");
184+
}
185+
};
186+
MDC.put("mdc" + HACK_ATTEMPT, "mdc value");
187+
188+
converter.setExclusions(Arrays.asList("explicit" + HACK_ATTEMPT, "mdc" + HACK_ATTEMPT));
189+
converter.convert(explicitFields, sb);
190+
191+
assertThat(unmarshal(sb), allOf(not(hasEntry("mdc" + HACK_ATTEMPT, "mdc value")),
192+
not(hasEntry("explicit" + HACK_ATTEMPT, "explicit value"))));
193+
}
194+
195+
@SuppressWarnings("unchecked")
196+
private static Matcher<Map<String, Object>> hasDefaultProperties() {
197+
return allOf(hasEntry(Fields.CORRELATION_ID, Defaults.UNKNOWN), hasEntry(Fields.TENANT_ID, Defaults.UNKNOWN),
198+
hasEntry(Fields.COMPONENT_ID, Defaults.UNKNOWN), hasEntry(Fields.COMPONENT_NAME, Defaults.UNKNOWN),
199+
hasEntry(Fields.COMPONENT_TYPE, Defaults.COMPONENT_TYPE),
200+
hasEntry(Fields.COMPONENT_INSTANCE, Defaults.COMPONENT_INDEX),
201+
hasEntry(Fields.CONTAINER_ID, Defaults.UNKNOWN), hasEntry(Fields.ORGANIZATION_ID, Defaults.UNKNOWN),
202+
hasEntry(Fields.ORGANIZATION_NAME, Defaults.UNKNOWN), hasEntry(Fields.SPACE_ID, Defaults.UNKNOWN),
203+
hasEntry(Fields.SPACE_NAME, Defaults.UNKNOWN), not(hasKey(Fields.REQUEST_ID)));
204+
}
205+
}

0 commit comments

Comments
 (0)