CustomFieldsAdapter { public List<String> getExcludedFieldKeyNames() { Collection<String> contextFieldsKeys = LogContext.getContextFieldsKeys(); List<String> result = new ArrayList<>(customFields.size()); for (CustomField customField : customFields) { if (!customField.isRetainOriginal() && !contextFieldsKeys.contains(customField.getKey())) { result.add(customField.getKey()); } } return result; } CustomFieldsAdapter(CustomField... customFields); List<String> getCustomFieldKeyNames(); List<String> getExcludedFieldKeyNames();  }
@Test public void returnsEmptyExclusionsWithoutConfig() throws Exception { CustomFieldsAdapter adapter = new CustomFieldsAdapter(); assertThat(adapter.getExcludedFieldKeyNames(), is(empty())); }
@Test public void excludesGivenCustomFields() throws Exception { CustomFieldsAdapter adapter = new CustomFieldsAdapter(CustomField.newBuilder().setKey("this key").build(), CustomField.newBuilder().setKey("that key").build()); assertThat(adapter.getExcludedFieldKeyNames(), containsInAnyOrder("this key", "that key")); }
@Test public void removesCustomFieldsFromExclusionsWhenRetained() throws Exception { CustomFieldsAdapter adapter = new CustomFieldsAdapter( CustomField.newBuilder().setKey("this key").setRetainOriginal(true).build(), CustomField.newBuilder().setKey("that key").build()); assertThat(adapter.getExcludedFieldKeyNames(), contains("that key")); }
@Test public void neverExcludesLogContextFieldsEvenWhenConfigured() throws Exception { List<CustomField> customFields = new ArrayList<>(LogContext.getContextFieldsKeys().size()); for (String key : LogContext.getContextFieldsKeys()) { customFields.add(CustomField.newBuilder().setKey(key).build()); } CustomFieldsAdapter adapter = new CustomFieldsAdapter( customFields.toArray(new CustomField[customFields.size()])); assertThat(adapter.getExcludedFieldKeyNames(), is(empty())); }