diff --git a/data-prepper-logstash-configuration/src/main/java/org/opensearch/dataprepper/logstash/mapping/AttributesMapperCreator.java b/data-prepper-logstash-configuration/src/main/java/org/opensearch/dataprepper/logstash/mapping/AttributesMapperCreator.java new file mode 100644 index 0000000000..8e2f9d4677 --- /dev/null +++ b/data-prepper-logstash-configuration/src/main/java/org/opensearch/dataprepper/logstash/mapping/AttributesMapperCreator.java @@ -0,0 +1,34 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.logstash.mapping; + +import org.opensearch.dataprepper.logstash.exception.LogstashMappingException; + +import java.lang.reflect.Constructor; + +class AttributesMapperCreator { + + LogstashPluginAttributesMapper createMapperClass(final String attributesMapperClassName) { + final Class attributesMapperClass; + try { + attributesMapperClass = Class.forName(attributesMapperClassName); + } catch (final ClassNotFoundException ex) { + throw new LogstashMappingException("Unable to find Mapper class with name of " + attributesMapperClassName, ex); + } + + if(!LogstashPluginAttributesMapper.class.isAssignableFrom(attributesMapperClass)) { + throw new LogstashMappingException("The provided mapping class does not implement " + LogstashPluginAttributesMapper.class); + } + + try { + final Constructor defaultConstructor = attributesMapperClass.getConstructor(); + final Object instance = defaultConstructor.newInstance(); + return (LogstashPluginAttributesMapper) instance; + } catch (final Exception ex) { + throw new LogstashMappingException("Unable to create Mapper class with name of " + attributesMapperClassName, ex); + } + } +} diff --git a/data-prepper-logstash-configuration/src/main/java/org/opensearch/dataprepper/logstash/mapping/AttributesMapperProvider.java b/data-prepper-logstash-configuration/src/main/java/org/opensearch/dataprepper/logstash/mapping/AttributesMapperProvider.java new file mode 100644 index 0000000000..0094205e87 --- /dev/null +++ b/data-prepper-logstash-configuration/src/main/java/org/opensearch/dataprepper/logstash/mapping/AttributesMapperProvider.java @@ -0,0 +1,29 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.logstash.mapping; + +class AttributesMapperProvider { + private final AttributesMapperCreator attributesMapperCreator; + private final DefaultLogstashPluginAttributesMapper defaultLogstashPluginAttributesMapper; + + AttributesMapperProvider() { + this(new AttributesMapperCreator()); + } + + AttributesMapperProvider(final AttributesMapperCreator attributesMapperCreator) { + this.attributesMapperCreator = attributesMapperCreator; + defaultLogstashPluginAttributesMapper = new DefaultLogstashPluginAttributesMapper(); + } + + LogstashPluginAttributesMapper getAttributesMapper(final LogstashMappingModel mappingModel) { + final String attributesMapperClassName = mappingModel.getAttributesMapperClass(); + if(attributesMapperClassName == null) { + return defaultLogstashPluginAttributesMapper; + } + + return attributesMapperCreator.createMapperClass(attributesMapperClassName); + } +} diff --git a/data-prepper-logstash-configuration/src/main/java/org/opensearch/dataprepper/logstash/mapping/DefaultLogstashPluginAttributesMapper.java b/data-prepper-logstash-configuration/src/main/java/org/opensearch/dataprepper/logstash/mapping/DefaultLogstashPluginAttributesMapper.java new file mode 100644 index 0000000000..0f46f27b75 --- /dev/null +++ b/data-prepper-logstash-configuration/src/main/java/org/opensearch/dataprepper/logstash/mapping/DefaultLogstashPluginAttributesMapper.java @@ -0,0 +1,37 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.logstash.mapping; + +import org.opensearch.dataprepper.logstash.model.LogstashAttribute; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +class DefaultLogstashPluginAttributesMapper implements LogstashPluginAttributesMapper { + private static final Logger LOG = LoggerFactory.getLogger(DefaultLogstashPluginAttributesMapper.class); + + @Override + public Map mapAttributes(final List logstashAttributes, final LogstashAttributesMappings logstashAttributesMappings) { + final Map pluginSettings = new LinkedHashMap<>(logstashAttributesMappings.getAdditionalAttributes()); + + logstashAttributes.forEach(logstashAttribute -> { + if (logstashAttributesMappings.getMappedAttributeNames().containsKey(logstashAttribute.getAttributeName())) { + pluginSettings.put( + logstashAttributesMappings.getMappedAttributeNames().get(logstashAttribute.getAttributeName()), + logstashAttribute.getAttributeValue().getValue() + ); + } + else { + LOG.warn("Attribute name {} is not found in mapping file.", logstashAttribute.getAttributeName()); + } + }); + + return pluginSettings; + } +} diff --git a/data-prepper-logstash-configuration/src/main/java/org/opensearch/dataprepper/logstash/mapping/DefaultPluginMapper.java b/data-prepper-logstash-configuration/src/main/java/org/opensearch/dataprepper/logstash/mapping/DefaultPluginMapper.java index cb57f9eb9d..104252773f 100644 --- a/data-prepper-logstash-configuration/src/main/java/org/opensearch/dataprepper/logstash/mapping/DefaultPluginMapper.java +++ b/data-prepper-logstash-configuration/src/main/java/org/opensearch/dataprepper/logstash/mapping/DefaultPluginMapper.java @@ -5,12 +5,9 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import org.opensearch.dataprepper.logstash.exception.LogstashMappingException; import org.opensearch.dataprepper.logstash.model.LogstashPlugin; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.InputStream; -import java.util.LinkedHashMap; import java.util.Map; /** @@ -19,8 +16,16 @@ * @since 1.2 */ public class DefaultPluginMapper implements LogstashPluginMapper { - private static final Logger LOG = LoggerFactory.getLogger(DefaultPluginMapper.class); private final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); + private final AttributesMapperProvider attributesMapperProvider; + + public DefaultPluginMapper() { + this(new AttributesMapperProvider()); + } + + DefaultPluginMapper(final AttributesMapperProvider attributesMapperProvider) { + this.attributesMapperProvider = attributesMapperProvider; + } @Override public PluginModel mapPlugin(LogstashPlugin logstashPlugin) { @@ -43,19 +48,10 @@ public PluginModel mapPlugin(LogstashPlugin logstashPlugin) { if (logstashMappingModel.getPluginName() == null) { throw new LogstashMappingException("The mapping file " + mappingResourceName + " has a null value for 'pluginName'."); } - Map pluginSettings = new LinkedHashMap<>(logstashMappingModel.getAdditionalAttributes()); - - logstashPlugin.getAttributes().forEach(logstashAttribute -> { - if (logstashMappingModel.getMappedAttributeNames().containsKey(logstashAttribute.getAttributeName())) { - pluginSettings.put( - logstashMappingModel.getMappedAttributeNames().get(logstashAttribute.getAttributeName()), - logstashAttribute.getAttributeValue().getValue() - ); - } - else { - LOG.warn("Attribute name {} is not found in mapping file.", logstashAttribute.getAttributeName()); - } - }); + + final LogstashPluginAttributesMapper pluginAttributesMapper = attributesMapperProvider.getAttributesMapper(logstashMappingModel); + + final Map pluginSettings = pluginAttributesMapper.mapAttributes(logstashPlugin.getAttributes(), logstashMappingModel); return new PluginModel(logstashMappingModel.getPluginName(), pluginSettings); } diff --git a/data-prepper-logstash-configuration/src/main/java/org/opensearch/dataprepper/logstash/mapping/LogstashAttributesMappings.java b/data-prepper-logstash-configuration/src/main/java/org/opensearch/dataprepper/logstash/mapping/LogstashAttributesMappings.java new file mode 100644 index 0000000000..57b0360bec --- /dev/null +++ b/data-prepper-logstash-configuration/src/main/java/org/opensearch/dataprepper/logstash/mapping/LogstashAttributesMappings.java @@ -0,0 +1,19 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.logstash.mapping; + +import java.util.Map; + +/** + * Represents attribute mappings from Logstash into Data Prepper. + * + * @since 1.2 + */ +public interface LogstashAttributesMappings { + Map getMappedAttributeNames(); + + Map getAdditionalAttributes(); +} diff --git a/data-prepper-logstash-configuration/src/main/java/org/opensearch/dataprepper/logstash/mapping/LogstashMappingModel.java b/data-prepper-logstash-configuration/src/main/java/org/opensearch/dataprepper/logstash/mapping/LogstashMappingModel.java index 4782efb75f..cdbe6e444a 100644 --- a/data-prepper-logstash-configuration/src/main/java/org/opensearch/dataprepper/logstash/mapping/LogstashMappingModel.java +++ b/data-prepper-logstash-configuration/src/main/java/org/opensearch/dataprepper/logstash/mapping/LogstashMappingModel.java @@ -1,5 +1,12 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + package org.opensearch.dataprepper.logstash.mapping; +import com.fasterxml.jackson.annotation.JsonProperty; + import java.util.Map; /** @@ -7,25 +14,28 @@ * * @since 1.2 */ -public class LogstashMappingModel { +class LogstashMappingModel implements LogstashAttributesMappings { + @JsonProperty private String pluginName; - private Map mappedAttributeNames; - private Map additionalAttributes; - public LogstashMappingModel(final String pluginName, final Map mappedAttributeNames, - final Map additionalAttributes) { - this.pluginName = pluginName; - this.mappedAttributeNames = mappedAttributeNames; - this.additionalAttributes = additionalAttributes; - } + @JsonProperty + private String attributesMapperClass; - public LogstashMappingModel() {} + @JsonProperty + private Map mappedAttributeNames; + + @JsonProperty + private Map additionalAttributes; public String getPluginName() { return pluginName; } + public String getAttributesMapperClass() { + return attributesMapperClass; + } + public Map getMappedAttributeNames() { return mappedAttributeNames; } diff --git a/data-prepper-logstash-configuration/src/main/java/org/opensearch/dataprepper/logstash/mapping/LogstashPluginAttributesMapper.java b/data-prepper-logstash-configuration/src/main/java/org/opensearch/dataprepper/logstash/mapping/LogstashPluginAttributesMapper.java new file mode 100644 index 0000000000..543b3d509d --- /dev/null +++ b/data-prepper-logstash-configuration/src/main/java/org/opensearch/dataprepper/logstash/mapping/LogstashPluginAttributesMapper.java @@ -0,0 +1,29 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.logstash.mapping; + +import org.opensearch.dataprepper.logstash.model.LogstashAttribute; + +import java.util.List; +import java.util.Map; + +/** + * An interface for a class which is responsible for mapping attributes + * from a Logstash plugin into Data Prepper plugin settings. + * + * @since 1.2 + */ +public interface LogstashPluginAttributesMapper { + /** + * Map all logstashAttributes from a Logstash plugin. + * + * @param logstashAttributes All the Logstash logstashAttributes for the plugin + * @param logstashAttributesMappings The mappings for this Logstash plugin + * @return A map of Data Prepper plugin settings. + * @since 1.2 + */ + Map mapAttributes(List logstashAttributes, LogstashAttributesMappings logstashAttributesMappings); +} diff --git a/data-prepper-logstash-configuration/src/test/java/org/opensearch/dataprepper/logstash/mapping/AttributesMapperCreatorTest.java b/data-prepper-logstash-configuration/src/test/java/org/opensearch/dataprepper/logstash/mapping/AttributesMapperCreatorTest.java new file mode 100644 index 0000000000..5cd610480d --- /dev/null +++ b/data-prepper-logstash-configuration/src/test/java/org/opensearch/dataprepper/logstash/mapping/AttributesMapperCreatorTest.java @@ -0,0 +1,78 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.logstash.mapping; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.opensearch.dataprepper.logstash.exception.LogstashMappingException; +import org.opensearch.dataprepper.logstash.model.LogstashAttribute; + +import java.util.List; +import java.util.Map; + +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class AttributesMapperCreatorTest { + static class DoesNotImplement { + } + + public static class NoDefaultConstructor implements LogstashPluginAttributesMapper { + public NoDefaultConstructor(final String ignored) { } + @Override + public Map mapAttributes(List logstashAttributes, LogstashAttributesMappings logstashAttributesMappings) { + return null; + } + } + + public static class ThrowingConstructor implements LogstashPluginAttributesMapper { + public ThrowingConstructor() { + throw new RuntimeException("Intentional exception for testing."); + } + @Override + public Map mapAttributes(List logstashAttributes, LogstashAttributesMappings logstashAttributesMappings) { + return null; + } + } + + public static class ValidMapper implements LogstashPluginAttributesMapper { + @Override + public Map mapAttributes(final List logstashAttributes, final LogstashAttributesMappings logstashAttributesMappings) { + return null; + } + } + + private AttributesMapperCreator createObjectUnderTest() { + return new AttributesMapperCreator(); + } + + @Test + void createMapperClass_should_throw_if_class_does_not_exist() { + final AttributesMapperCreator objectUnderTest = createObjectUnderTest(); + + assertThrows(LogstashMappingException.class, + () -> objectUnderTest.createMapperClass("org.opensearch.dataprepper.logstash.DoesNotExist")); + } + + @ParameterizedTest + @ValueSource(classes = { + DoesNotImplement.class, NoDefaultConstructor.class, ThrowingConstructor.class + }) + void createMapperClass_should_throw_for_classes_which_cannot_be_constructor(final Class invalidClass) { + final AttributesMapperCreator objectUnderTest = createObjectUnderTest(); + + assertThrows(LogstashMappingException.class, + () -> objectUnderTest.createMapperClass(invalidClass.getName())); + } + + @Test + void createMapperClass_returns_new_instance() { + assertThat(createObjectUnderTest().createMapperClass(ValidMapper.class.getName()), + instanceOf(LogstashPluginAttributesMapper.class)); + } +} \ No newline at end of file diff --git a/data-prepper-logstash-configuration/src/test/java/org/opensearch/dataprepper/logstash/mapping/AttributesMapperProviderTest.java b/data-prepper-logstash-configuration/src/test/java/org/opensearch/dataprepper/logstash/mapping/AttributesMapperProviderTest.java new file mode 100644 index 0000000000..7d1fed263b --- /dev/null +++ b/data-prepper-logstash-configuration/src/test/java/org/opensearch/dataprepper/logstash/mapping/AttributesMapperProviderTest.java @@ -0,0 +1,54 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.logstash.mapping; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class AttributesMapperProviderTest { + + private AttributesMapperCreator attributesMapperCreator; + private LogstashMappingModel logstashMappingModel; + + @BeforeEach + void setUp() { + attributesMapperCreator = mock(AttributesMapperCreator.class); + logstashMappingModel = mock(LogstashMappingModel.class); + } + + private AttributesMapperProvider createObjectUnderTest() { + return new AttributesMapperProvider(attributesMapperCreator); + } + + @Test + void getAttributesMapper_should_return_Default_when_model_has_no_AttributesMapperClass() { + final LogstashPluginAttributesMapper attributesMapper = createObjectUnderTest().getAttributesMapper(logstashMappingModel); + + assertThat(attributesMapper, notNullValue()); + assertThat(attributesMapper, instanceOf(DefaultLogstashPluginAttributesMapper.class)); + } + + @Test + void getAttributesMapper_should_return_new_instance_for_AttributesMapperClass() { + final String className = UUID.randomUUID().toString(); + when(logstashMappingModel.getAttributesMapperClass()) + .thenReturn(className); + final LogstashPluginAttributesMapper expectedMapper = mock(LogstashPluginAttributesMapper.class); + when(attributesMapperCreator.createMapperClass(className)) + .thenReturn(expectedMapper); + + assertThat(createObjectUnderTest().getAttributesMapper(logstashMappingModel), equalTo(expectedMapper)); + } +} \ No newline at end of file diff --git a/data-prepper-logstash-configuration/src/test/java/org/opensearch/dataprepper/logstash/mapping/DefaultLogstashPluginAttributesMapperTest.java b/data-prepper-logstash-configuration/src/test/java/org/opensearch/dataprepper/logstash/mapping/DefaultLogstashPluginAttributesMapperTest.java new file mode 100644 index 0000000000..21c8871915 --- /dev/null +++ b/data-prepper-logstash-configuration/src/test/java/org/opensearch/dataprepper/logstash/mapping/DefaultLogstashPluginAttributesMapperTest.java @@ -0,0 +1,66 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.logstash.mapping; + +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.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.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(); + 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); + + final String dataPrepperAttribute = UUID.randomUUID().toString(); + final LogstashAttributesMappings mappings = mock(LogstashAttributesMappings.class); + when(mappings.getMappedAttributeNames()).thenReturn(Collections.singletonMap(logstashAttributeName, dataPrepperAttribute)); + + final Map actualPluginSettings = + createObjectUnderTest().mapAttributes(Collections.singletonList(logstashAttribute), mappings); + + assertThat(actualPluginSettings, notNullValue()); + assertThat(actualPluginSettings.size(), equalTo(1)); + assertThat(actualPluginSettings, hasKey(dataPrepperAttribute)); + assertThat(actualPluginSettings.get(dataPrepperAttribute), equalTo(value)); + } + + @Test + 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 actualPluginSettings = + createObjectUnderTest().mapAttributes(Collections.emptyList(), mappings); + + assertThat(actualPluginSettings, notNullValue()); + assertThat(actualPluginSettings.size(), equalTo(1)); + assertThat(actualPluginSettings, hasKey(additionalAttributeName)); + assertThat(actualPluginSettings.get(additionalAttributeName), equalTo(additionalAttributeValue)); + } +} \ No newline at end of file diff --git a/data-prepper-logstash-configuration/src/test/java/org/opensearch/dataprepper/logstash/mapping/DefaultPluginMapperTest.java b/data-prepper-logstash-configuration/src/test/java/org/opensearch/dataprepper/logstash/mapping/DefaultPluginMapperTest.java index ec2d9f2174..79be28b645 100644 --- a/data-prepper-logstash-configuration/src/test/java/org/opensearch/dataprepper/logstash/mapping/DefaultPluginMapperTest.java +++ b/data-prepper-logstash-configuration/src/test/java/org/opensearch/dataprepper/logstash/mapping/DefaultPluginMapperTest.java @@ -6,17 +6,36 @@ import org.opensearch.dataprepper.logstash.exception.LogstashMappingException; import org.opensearch.dataprepper.logstash.model.LogstashPlugin; +import java.util.Collections; +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.junit.Assert.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyList; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class DefaultPluginMapperTest { - private DefaultPluginMapper defaultPluginMapper; + private AttributesMapperProvider attributesMapperProvider; + private LogstashPluginAttributesMapper logstashPluginAttributesMapper; @BeforeEach - void createObjectUnderTest() { - defaultPluginMapper = new DefaultPluginMapper(); + void setUp() { + attributesMapperProvider = mock(AttributesMapperProvider.class); + + logstashPluginAttributesMapper = mock(LogstashPluginAttributesMapper.class); + when(attributesMapperProvider.getAttributesMapper(any(LogstashMappingModel.class))) + .thenReturn(logstashPluginAttributesMapper); + + } + + DefaultPluginMapper createObjectUnderTest() { + return new DefaultPluginMapper(attributesMapperProvider); } @Test @@ -24,8 +43,9 @@ void mapPlugin_without_mapping_file_throws_logstash_mapping_exception_Test() { LogstashPlugin logstashPlugin = TestDataProvider.invalidMappingResourceNameData(); String mappingResourceName = logstashPlugin.getPluginName() + ".mapping.yaml"; + final DefaultPluginMapper objectUnderTest = createObjectUnderTest(); Exception exception = assertThrows(LogstashMappingException.class, () -> - defaultPluginMapper.mapPlugin(logstashPlugin)); + objectUnderTest.mapPlugin(logstashPlugin)); String expectedMessage = "Unable to find mapping resource " + mappingResourceName; String actualMessage = exception.getMessage(); @@ -38,8 +58,9 @@ void mapPlugin_with_incorrect_mapping_file_throws_logstash_mapping_exception_Tes LogstashPlugin logstashPlugin = TestDataProvider.invalidMappingResourceData(); String mappingResourceName = logstashPlugin.getPluginName() + ".mapping.yaml"; + final DefaultPluginMapper objectUnderTest = createObjectUnderTest(); Exception exception = assertThrows(LogstashMappingException.class, () -> - defaultPluginMapper.mapPlugin(logstashPlugin)); + objectUnderTest.mapPlugin(logstashPlugin)); String expectedMessage = "Unable to parse mapping file " + mappingResourceName; String actualMessage = exception.getMessage(); @@ -52,8 +73,9 @@ void mapPlugin_without_plugin_name_in_mapping_file_throws_logstash_mapping_excep LogstashPlugin logstashPlugin = TestDataProvider.noPluginNameMappingResourceData(); String mappingResourceName = logstashPlugin.getPluginName() + ".mapping.yaml"; + final DefaultPluginMapper objectUnderTest = createObjectUnderTest(); Exception exception = assertThrows(LogstashMappingException.class, () -> - defaultPluginMapper.mapPlugin(logstashPlugin)); + objectUnderTest.mapPlugin(logstashPlugin)); String expectedMessage = "The mapping file " + mappingResourceName + " has a null value for 'pluginName'."; String actualMessage = exception.getMessage(); @@ -62,14 +84,29 @@ void mapPlugin_without_plugin_name_in_mapping_file_throws_logstash_mapping_excep } @Test - void mapPlugin_returns_plugin_model_Test() { - LogstashPlugin logstashPlugin = TestDataProvider.pluginData(); + void mapPlugin_should_return_PluginModel_with_correct_pluginName() { + final LogstashPlugin logstashPlugin = mock(LogstashPlugin.class); + when(logstashPlugin.getPluginName()).thenReturn("amazon_es"); - PluginModel actualPluginModel = defaultPluginMapper.mapPlugin(logstashPlugin); - PluginModel expectedPluginModel = TestDataProvider.getSamplePluginModel(); + final PluginModel pluginModel = createObjectUnderTest().mapPlugin(logstashPlugin); - assertThat(expectedPluginModel.getPluginName(), equalTo(actualPluginModel.getPluginName())); - assertThat(expectedPluginModel.getPluginSettings(), equalTo(actualPluginModel.getPluginSettings())); + assertThat(pluginModel, notNullValue()); + assertThat(pluginModel.getPluginName(), equalTo("opensearch")); } + @Test + void mapPlugin_should_return_PluginModel_with_mapped_attributes() { + final LogstashPlugin logstashPlugin = mock(LogstashPlugin.class); + when(logstashPlugin.getPluginName()).thenReturn("amazon_es"); + + final Map mappedPluginSettings = Collections.singletonMap(UUID.randomUUID().toString(), UUID.randomUUID().toString()); + when(logstashPluginAttributesMapper.mapAttributes(anyList(), any(LogstashAttributesMappings.class))) + .thenReturn(mappedPluginSettings); + + final PluginModel pluginModel = createObjectUnderTest().mapPlugin(logstashPlugin); + + assertThat(pluginModel, notNullValue()); + assertThat(pluginModel.getPluginSettings(), notNullValue()); + assertThat(pluginModel.getPluginSettings(), equalTo(mappedPluginSettings)); + } } \ No newline at end of file diff --git a/data-prepper-logstash-configuration/src/test/java/org/opensearch/dataprepper/logstash/mapping/LogstashMappingModelTest.java b/data-prepper-logstash-configuration/src/test/java/org/opensearch/dataprepper/logstash/mapping/LogstashMappingModelTest.java new file mode 100644 index 0000000000..cf13489c53 --- /dev/null +++ b/data-prepper-logstash-configuration/src/test/java/org/opensearch/dataprepper/logstash/mapping/LogstashMappingModelTest.java @@ -0,0 +1,44 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.logstash.mapping; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.MatcherAssert.assertThat; + +class LogstashMappingModelTest { + private ObjectMapper objectMapper; + + @BeforeEach + void setUp() { + objectMapper = new ObjectMapper(new YAMLFactory()); + + } + + @Test + void deserialize_from_JSON_should_have_expected_value() throws IOException { + + final LogstashMappingModel logstashMappingModel = objectMapper.readValue(this.getClass().getResourceAsStream("sample.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(2)); + assertThat(logstashMappingModel.getMappedAttributeNames().get("valueA"), equalTo("keyA")); + assertThat(logstashMappingModel.getMappedAttributeNames().get("valueB"), equalTo("keyB")); + assertThat(logstashMappingModel.getAdditionalAttributes(), notNullValue()); + assertThat(logstashMappingModel.getAdditionalAttributes().size(), equalTo(2)); + assertThat(logstashMappingModel.getAdditionalAttributes().get("addA"), equalTo(true)); + assertThat(logstashMappingModel.getAdditionalAttributes().get("addB"), equalTo("staticValueB")); + } +} \ No newline at end of file diff --git a/data-prepper-logstash-configuration/src/test/resources/org/opensearch/dataprepper/logstash/mapping/sample.mapping.yaml b/data-prepper-logstash-configuration/src/test/resources/org/opensearch/dataprepper/logstash/mapping/sample.mapping.yaml new file mode 100644 index 0000000000..244df2d033 --- /dev/null +++ b/data-prepper-logstash-configuration/src/test/resources/org/opensearch/dataprepper/logstash/mapping/sample.mapping.yaml @@ -0,0 +1,8 @@ +pluginName: samplePlugin +attributesMapperClass: org.opensearch.dataprepper.Placeholder +mappedAttributeNames: + valueA: keyA + valueB: keyB +additionalAttributes: + addA: true + addB: staticValueB \ No newline at end of file