Skip to content

Commit

Permalink
Improved the NPE fix by providing empty maps when they are null or ab…
Browse files Browse the repository at this point in the history
…sent in the YAML. #568

Signed-off-by: David Venable <[email protected]>
  • Loading branch information
dlvenable committed Nov 12, 2021
1 parent 5c9e2a2 commit 003e68a
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ public Map<String, Object> mapAttributes(final List<LogstashAttribute> logstashA

Objects.requireNonNull(logstashAttributes);
Objects.requireNonNull(logstashAttributesMappings);
Objects.requireNonNull(logstashAttributesMappings.getMappedAttributeNames());
Objects.requireNonNull(logstashAttributesMappings.getAdditionalAttributes());

final Map<String, Object> pluginSettings = new LinkedHashMap<>();

if(logstashAttributesMappings.getAdditionalAttributes() != null) {
pluginSettings.putAll(logstashAttributesMappings.getAdditionalAttributes());
}
final Map<String, Object> pluginSettings = new LinkedHashMap<>(logstashAttributesMappings.getAdditionalAttributes());

final Map<String, String> mappedAttributeNames = logstashAttributesMappings.getMappedAttributeNames();
logstashAttributes.forEach(logstashAttribute -> {
if (mappedAttributeNames != null && mappedAttributeNames.containsKey(logstashAttribute.getAttributeName())) {
if (mappedAttributeNames.containsKey(logstashAttribute.getAttributeName())) {
pluginSettings.put(
mappedAttributeNames.get(logstashAttribute.getAttributeName()),
logstashAttribute.getAttributeValue().getValue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,25 @@
* @since 1.2
*/
public interface LogstashAttributesMappings {
/**
* A map of attribute names in the Logstash configuration to
* the correct property name in Data Prepper.
* <p>
* This should not return null and should return empty if not defined.
*
* @return A Map
* @since 1.2
*/
Map<String, String> getMappedAttributeNames();

/**
* A map of Data Prepper property names to values to set on those
* properties.
* <p>
* This should not return null and should return empty if not defined.
*
* @return A Map
* @since 1.2
*/
Map<String, Object> getAdditionalAttributes();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package org.opensearch.dataprepper.logstash.mapping;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.Nulls;

import java.util.Map;

Expand All @@ -23,9 +25,11 @@ class LogstashMappingModel implements LogstashAttributesMappings {
private String attributesMapperClass;

@JsonProperty
@JsonSetter(nulls = Nulls.AS_EMPTY)
private Map<String, String> mappedAttributeNames;

@JsonProperty
@JsonSetter(nulls = Nulls.AS_EMPTY)
private Map<String, Object> additionalAttributes;

public String getPluginName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,26 @@ void mapAttributes_throws_with_null_Mappings() {
() -> objectUnderTest.mapAttributes(logstashAttributes, null));
}

@Test
void mapAttributes_throws_with_null_Mappings_mappedAttributeNames() {
final DefaultLogstashPluginAttributesMapper objectUnderTest = createObjectUnderTest();

when(mappings.getMappedAttributeNames()).thenReturn(null);

assertThrows(NullPointerException.class,
() -> objectUnderTest.mapAttributes(logstashAttributes, mappings));
}

@Test
void mapAttributes_throws_with_null_Mappings_additionalAttributes() {
final DefaultLogstashPluginAttributesMapper objectUnderTest = createObjectUnderTest();

when(mappings.getAdditionalAttributes()).thenReturn(null);

assertThrows(NullPointerException.class,
() -> objectUnderTest.mapAttributes(logstashAttributes, mappings));
}

@Test
void mapAttributes_sets_mapped_attributes() {
final String dataPrepperAttribute = UUID.randomUUID().toString();
Expand Down Expand Up @@ -94,16 +114,4 @@ void mapAttributes_sets_additional_attributes_to_those_values() {
assertThat(actualPluginSettings, hasKey(additionalAttributeName));
assertThat(actualPluginSettings.get(additionalAttributeName), equalTo(additionalAttributeValue));
}

@Test
void mapAttributes_with_null_mappings_and_attributes() {
when(mappings.getMappedAttributeNames()).thenReturn(null);
when(mappings.getAdditionalAttributes()).thenReturn(null);

final Map<String, Object> actualPluginSettings =
createObjectUnderTest().mapAttributes(logstashAttributes, mappings);

assertThat(actualPluginSettings, notNullValue());
assertThat(actualPluginSettings.size(), equalTo(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,17 @@ void deserialize_from_JSON_should_have_expected_value() throws IOException {
assertThat(logstashMappingModel.getAdditionalAttributes().get("addA"), equalTo(true));
assertThat(logstashMappingModel.getAdditionalAttributes().get("addB"), equalTo("staticValueB"));
}

@Test
void deserialize_from_JSON_with_null_values_has_empty_lists() throws IOException {

final LogstashMappingModel logstashMappingModel = objectMapper.readValue(this.getClass().getResourceAsStream("sample-with-nulls.mapping.yaml"), LogstashMappingModel.class);

assertThat(logstashMappingModel.getPluginName(), equalTo("samplePlugin"));
assertThat(logstashMappingModel.getAttributesMapperClass(), equalTo("org.opensearch.dataprepper.Placeholder"));
assertThat(logstashMappingModel.getMappedAttributeNames(), notNullValue());
assertThat(logstashMappingModel.getMappedAttributeNames().size(), equalTo(0));
assertThat(logstashMappingModel.getAdditionalAttributes(), notNullValue());
assertThat(logstashMappingModel.getAdditionalAttributes().size(), equalTo(0));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pluginName: samplePlugin
attributesMapperClass: org.opensearch.dataprepper.Placeholder
mappedAttributeNames: null
additionalAttributes: null

0 comments on commit 003e68a

Please sign in to comment.