DefaultCustomFieldsConverter { public void convert(StringBuilder appendTo, Map<String, String> mdcPropertiesMap, Object... arguments) { if (customFieldKeyNames.isEmpty()) { return; } Map<String, CustomField> customFields = getRegisteredCustomFields(arguments); Map<String, String> mdcCustomFields = getRegisteredMdcCustomFields(mdcPropertiesMap); if (!customFields.isEmpty() || !mdcCustomFields.isEmpty()) { try { ArrayComposer<ObjectComposer<JSONComposer<String>>> oc = startJson(appendTo); addCustomFields(oc, customFields, mdcCustomFields); finishJson(oc, appendTo); } catch (Exception ex) { LoggerFactory.getLogger(DefaultCustomFieldsConverter.class).error("Conversion failed ", ex); } } }  void setFieldName(String fieldName); void setCustomFieldKeyNames(List<String> customFieldKeyNames); void convert(StringBuilder appendTo, Map<String, String> mdcPropertiesMap, Object... arguments);  }
@Test public void emptyMdcAndArguments() { StringBuilder sb = new StringBuilder(); converter.convert(sb, Collections.emptyMap()); assertThat(sb, hasToString("")); }
@Test public void standardArgument() throws Exception { StringBuilder sb = new StringBuilder(); converter.convert(sb, Collections.emptyMap(), "an_argument"); assertThat(sb, hasToString("")); }
@Test public void singleCustomFieldArgumentEmbedded() throws Exception { StringBuilder sb = new StringBuilder(); converter.convert(sb, Collections.emptyMap(), customField(CUSTOM_KEY_0, CUSTOM_VALUE_0)); assertThat(unmarshalCustomFields(sb), contains(hasCustomField(CUSTOM_KEY_0, CUSTOM_VALUE_0, 0))); }
@SuppressWarnings("unchecked") @Test public void multipleCustomFieldArgumentEmbedded() throws Exception { StringBuilder sb = new StringBuilder(); converter.convert(sb, Collections.emptyMap(), customField(CUSTOM_KEY_1, CUSTOM_VALUE_1), customField(UNREGISTERED_KEY, UNREGISTERED_VALUE), customField(CUSTOM_KEY_0, CUSTOM_VALUE_0)); assertThat(unmarshalCustomFields(sb), containsInAnyOrder(hasCustomField(CUSTOM_KEY_0, CUSTOM_VALUE_0, 0), hasCustomField(CUSTOM_KEY_1, CUSTOM_VALUE_1, 1))); }
@Test public void singleMdcField() throws Exception { StringBuilder sb = new StringBuilder(); @SuppressWarnings("serial") Map<String, String> mdcFields = new HashMap<String, String>() { { put(CUSTOM_KEY_0, CUSTOM_VALUE_0); } }; converter.convert(sb, mdcFields); assertThat(unmarshalCustomFields(sb), contains(hasCustomField(CUSTOM_KEY_0, CUSTOM_VALUE_0, 0))); }
@SuppressWarnings("unchecked") @Test public void multipleMdcFields() throws Exception { StringBuilder sb = new StringBuilder(); @SuppressWarnings("serial") Map<String, String> mdcFields = new HashMap<String, String>() { { put(CUSTOM_KEY_1, CUSTOM_VALUE_1); put(UNREGISTERED_KEY, UNREGISTERED_VALUE); put(CUSTOM_KEY_0, CUSTOM_VALUE_0); } }; converter.convert(sb, mdcFields); assertThat(unmarshalCustomFields(sb), containsInAnyOrder(hasCustomField(CUSTOM_KEY_0, CUSTOM_VALUE_0, 0), hasCustomField(CUSTOM_KEY_1, CUSTOM_VALUE_1, 1))); }
@SuppressWarnings("unchecked") @Test public void argumentsTakePrecendenceOverMdc() throws Exception { StringBuilder sb = new StringBuilder(); @SuppressWarnings("serial") Map<String, String> mdcFields = new HashMap<String, String>() { { put(CUSTOM_KEY_0, CUSTOM_VALUE_0); put(CUSTOM_KEY_1, CUSTOM_VALUE_1); } }; converter.convert(sb, mdcFields, customField(CUSTOM_KEY_0, "preferred value")); assertThat(unmarshalCustomFields(sb), containsInAnyOrder(hasCustomField(CUSTOM_KEY_0, "preferred value", 0), hasCustomField(CUSTOM_KEY_1, CUSTOM_VALUE_1, 1))); }
@Test public void properlyEscapesValues() throws Exception { StringBuilder sb = new StringBuilder(); converter.convert(sb, Collections.emptyMap(), customField(CUSTOM_KEY_0, HACK_ATTEMPT)); assertThat(unmarshalCustomFields(sb), contains(hasCustomField(CUSTOM_KEY_0, HACK_ATTEMPT, 0))); }
@Test public void properlyEscapesMdcFields() throws Exception { StringBuilder sb = new StringBuilder(); @SuppressWarnings("serial") Map<String, String> mdcFields = new HashMap<String, String>() { { put(CUSTOM_KEY_0, HACK_ATTEMPT); } }; converter.convert(sb, mdcFields); assertThat(unmarshalCustomFields(sb), contains(hasCustomField(CUSTOM_KEY_0, HACK_ATTEMPT, 0))); }