Skip to content

Commit

Permalink
Bug fixes in the DefaultLogstashPluginAttributesMapper related to nul…
Browse files Browse the repository at this point in the history
…l values. Part of opensearch-project#568.

Signed-off-by: David Venable <[email protected]>
  • Loading branch information
dlvenable committed Nov 12, 2021
1 parent e04a40f commit 5c9e2a2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,28 @@
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) {
final Map<String, Object> pluginSettings = new LinkedHashMap<>(logstashAttributesMappings.getAdditionalAttributes());

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

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

if(logstashAttributesMappings.getAdditionalAttributes() != null) {
pluginSettings.putAll(logstashAttributesMappings.getAdditionalAttributes());
}

final Map<String, String> mappedAttributeNames = logstashAttributesMappings.getMappedAttributeNames();
logstashAttributes.forEach(logstashAttribute -> {
if (logstashAttributesMappings.getMappedAttributeNames().containsKey(logstashAttribute.getAttributeName())) {
if (mappedAttributeNames != null && 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 @@ -5,42 +5,74 @@

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_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,15 +84,26 @@ 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));
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));
}
}

0 comments on commit 5c9e2a2

Please sign in to comment.