Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPE bug fixes in the DefaultLogstashPluginAttributesMapper #574

Merged
merged 2 commits into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,26 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

class DefaultLogstashPluginAttributesMapper implements LogstashPluginAttributesMapper {
private static final Logger LOG = LoggerFactory.getLogger(DefaultLogstashPluginAttributesMapper.class);

@Override
public Map<String, Object> mapAttributes(final List<LogstashAttribute> logstashAttributes, final LogstashAttributesMappings logstashAttributesMappings) {

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

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

final Map<String, String> mappedAttributeNames = logstashAttributesMappings.getMappedAttributeNames();
logstashAttributes.forEach(logstashAttribute -> {
if (logstashAttributesMappings.getMappedAttributeNames().containsKey(logstashAttribute.getAttributeName())) {
if (mappedAttributeNames.containsKey(logstashAttribute.getAttributeName())) {
pluginSettings.put(
logstashAttributesMappings.getMappedAttributeNames().get(logstashAttribute.getAttributeName()),
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 @@ -5,42 +5,94 @@

package org.opensearch.dataprepper.logstash.mapping;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.opensearch.dataprepper.logstash.model.LogstashAttribute;
import org.opensearch.dataprepper.logstash.model.LogstashAttributeValue;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasKey;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class DefaultLogstashPluginAttributesMapperTest {
private DefaultLogstashPluginAttributesMapper createObjectUnderTest() {
return new DefaultLogstashPluginAttributesMapper();
}

@Test
void mapAttributes_sets_mapped_attributes() {
final String value = UUID.randomUUID().toString();
final String logstashAttributeName = UUID.randomUUID().toString();
private String logstashAttributeName;
private String value;
private List<LogstashAttribute> logstashAttributes;
private LogstashAttributesMappings mappings;

@BeforeEach
void setUp() {
value = UUID.randomUUID().toString();
logstashAttributeName = UUID.randomUUID().toString();
final LogstashAttribute logstashAttribute = mock(LogstashAttribute.class);
final LogstashAttributeValue logstashAttributeValue = mock(LogstashAttributeValue.class);
when(logstashAttributeValue.getValue()).thenReturn(value);
when(logstashAttribute.getAttributeName()).thenReturn(logstashAttributeName);
when(logstashAttribute.getAttributeValue()).thenReturn(logstashAttributeValue);


logstashAttributes = Collections.singletonList(logstashAttribute);
mappings = mock(LogstashAttributesMappings.class);
}

private DefaultLogstashPluginAttributesMapper createObjectUnderTest() {
return new DefaultLogstashPluginAttributesMapper();
}

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

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

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

assertThrows(NullPointerException.class,
() -> 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();
final LogstashAttributesMappings mappings = mock(LogstashAttributesMappings.class);

when(mappings.getMappedAttributeNames()).thenReturn(Collections.singletonMap(logstashAttributeName, dataPrepperAttribute));

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

assertThat(actualPluginSettings, notNullValue());
assertThat(actualPluginSettings.size(), equalTo(1));
Expand All @@ -52,11 +104,10 @@ void mapAttributes_sets_mapped_attributes() {
void mapAttributes_sets_additional_attributes_to_those_values() {
final String additionalAttributeName = UUID.randomUUID().toString();
final String additionalAttributeValue = UUID.randomUUID().toString();
final LogstashAttributesMappings mappings = mock(LogstashAttributesMappings.class);
when(mappings.getAdditionalAttributes()).thenReturn(Collections.singletonMap(additionalAttributeName, additionalAttributeValue));

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

assertThat(actualPluginSettings, notNullValue());
assertThat(actualPluginSettings.size(), equalTo(1));
Expand Down
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