From 2c018b8b21d50a8e409216aed94ae8435876e4c9 Mon Sep 17 00:00:00 2001 From: David Venable Date: Thu, 7 Mar 2024 12:13:40 -0800 Subject: [PATCH] Make using the EventFactory easier by adding a test package and class to get a test EventFactory. It uses the actual EventFactory for now, but could vary in the future. Creates two now Gradle projects - one of the event factory and another for getting a test EventFactory. This also updates the file source to create Events using the EventFactory as the initial source. (#4110) Signed-off-by: David Venable --- .../model/event/BaseEventBuilder.java | 2 + .../model/event/LogEventBuilder.java | 2 +- data-prepper-core/build.gradle | 2 + .../event/EventBuilderFactory.java | 11 ----- .../parser/PipelineTransformerTests.java | 13 +++--- .../PeerForwarderAppConfigIT.java | 8 ++-- .../plugin/DefaultPluginFactoryIT.java | 16 +++----- .../dataprepper/plugin/ExtensionsIT.java | 6 +-- data-prepper-event/build.gradle | 17 ++++++++ .../core}/event/DefaultBaseEventBuilder.java | 18 ++++---- .../event/DefaultEventBuilderFactory.java | 12 +++--- .../core}/event/DefaultEventFactory.java | 22 +++++----- .../event/DefaultLogEventBuilderFactory.java | 12 +++--- .../core/event/EventBuilderFactory.java | 12 ++++++ .../event/DefaultBaseEventBuilderTests.java | 35 +++++++++------- .../DefaultEventBuilderFactoryTests.java | 21 +++++----- .../core}/event/DefaultEventFactoryTests.java | 23 ++++++----- .../DefaultLogEventBuilderFactoryTests.java | 21 +++++----- data-prepper-plugins/common/build.gradle | 1 + .../plugins/source/file/FileSource.java | 13 ++++-- .../plugins/source/file/FileSourceTests.java | 26 ++++++------ data-prepper-test-event/README.md | 5 +++ data-prepper-test-event/build.gradle | 12 ++++++ .../EventFactoryApplicationContextMarker.java | 13 ++++++ .../dataprepper/event/TestEventFactory.java | 41 +++++++++++++++++++ .../event/TestEventFactoryTest.java | 39 ++++++++++++++++++ settings.gradle | 2 + 27 files changed, 276 insertions(+), 129 deletions(-) delete mode 100644 data-prepper-core/src/main/java/org/opensearch/dataprepper/event/EventBuilderFactory.java create mode 100644 data-prepper-event/build.gradle rename {data-prepper-core/src/main/java/org/opensearch/dataprepper => data-prepper-event/src/main/java/org/opensearch/dataprepper/core}/event/DefaultBaseEventBuilder.java (88%) rename {data-prepper-core/src/main/java/org/opensearch/dataprepper => data-prepper-event/src/main/java/org/opensearch/dataprepper/core}/event/DefaultEventBuilderFactory.java (79%) rename {data-prepper-core/src/main/java/org/opensearch/dataprepper => data-prepper-event/src/main/java/org/opensearch/dataprepper/core}/event/DefaultEventFactory.java (68%) rename {data-prepper-core/src/main/java/org/opensearch/dataprepper => data-prepper-event/src/main/java/org/opensearch/dataprepper/core}/event/DefaultLogEventBuilderFactory.java (73%) create mode 100644 data-prepper-event/src/main/java/org/opensearch/dataprepper/core/event/EventBuilderFactory.java rename {data-prepper-core/src/test/java/org/opensearch/dataprepper => data-prepper-event/src/test/java/org/opensearch/dataprepper/core}/event/DefaultBaseEventBuilderTests.java (86%) rename {data-prepper-core/src/test/java/org/opensearch/dataprepper => data-prepper-event/src/test/java/org/opensearch/dataprepper/core}/event/DefaultEventBuilderFactoryTests.java (92%) rename {data-prepper-core/src/test/java/org/opensearch/dataprepper => data-prepper-event/src/test/java/org/opensearch/dataprepper/core}/event/DefaultEventFactoryTests.java (97%) rename {data-prepper-core/src/test/java/org/opensearch/dataprepper => data-prepper-event/src/test/java/org/opensearch/dataprepper/core}/event/DefaultLogEventBuilderFactoryTests.java (92%) create mode 100644 data-prepper-test-event/README.md create mode 100644 data-prepper-test-event/build.gradle create mode 100644 data-prepper-test-event/src/main/java/org/opensearch/dataprepper/core/event/EventFactoryApplicationContextMarker.java create mode 100644 data-prepper-test-event/src/main/java/org/opensearch/dataprepper/event/TestEventFactory.java create mode 100644 data-prepper-test-event/src/test/java/org/opensearch/dataprepper/event/TestEventFactoryTest.java diff --git a/data-prepper-api/src/main/java/org/opensearch/dataprepper/model/event/BaseEventBuilder.java b/data-prepper-api/src/main/java/org/opensearch/dataprepper/model/event/BaseEventBuilder.java index 7d7f50bff4..18014c2e4a 100644 --- a/data-prepper-api/src/main/java/org/opensearch/dataprepper/model/event/BaseEventBuilder.java +++ b/data-prepper-api/src/main/java/org/opensearch/dataprepper/model/event/BaseEventBuilder.java @@ -53,4 +53,6 @@ public interface BaseEventBuilder { * @since 2.2 */ BaseEventBuilder withData(final Object data); + + T build(); } diff --git a/data-prepper-api/src/main/java/org/opensearch/dataprepper/model/event/LogEventBuilder.java b/data-prepper-api/src/main/java/org/opensearch/dataprepper/model/event/LogEventBuilder.java index 3f9bdf2c81..4902e1a27f 100644 --- a/data-prepper-api/src/main/java/org/opensearch/dataprepper/model/event/LogEventBuilder.java +++ b/data-prepper-api/src/main/java/org/opensearch/dataprepper/model/event/LogEventBuilder.java @@ -15,5 +15,5 @@ public interface LogEventBuilder extends BaseEventBuilder { * @return a log event * @since 2.2 */ - public Log build(); + Log build(); } diff --git a/data-prepper-core/build.gradle b/data-prepper-core/build.gradle index f49f0d9bc9..95a83040da 100644 --- a/data-prepper-core/build.gradle +++ b/data-prepper-core/build.gradle @@ -14,6 +14,7 @@ sourceSets { dependencies { implementation project(':data-prepper-api') implementation project(':data-prepper-expression') + implementation project(':data-prepper-event') implementation project(':data-prepper-plugins:blocking-buffer') implementation project(':data-prepper-plugins:common') implementation project(':data-prepper-logstash-configuration') @@ -53,6 +54,7 @@ dependencies { testImplementation testLibs.junit.vintage testImplementation testLibs.mockito.inline testImplementation libs.commons.lang3 + testImplementation project(':data-prepper-test-event') testImplementation project(':data-prepper-api').sourceSets.test.output } diff --git a/data-prepper-core/src/main/java/org/opensearch/dataprepper/event/EventBuilderFactory.java b/data-prepper-core/src/main/java/org/opensearch/dataprepper/event/EventBuilderFactory.java deleted file mode 100644 index 897681e500..0000000000 --- a/data-prepper-core/src/main/java/org/opensearch/dataprepper/event/EventBuilderFactory.java +++ /dev/null @@ -1,11 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.dataprepper.event; - -abstract class EventBuilderFactory { - abstract Class getEventClass(); - abstract DefaultBaseEventBuilder createNew(); -} diff --git a/data-prepper-core/src/test/java/org/opensearch/dataprepper/parser/PipelineTransformerTests.java b/data-prepper-core/src/test/java/org/opensearch/dataprepper/parser/PipelineTransformerTests.java index 611dfaaf77..7b1607dc59 100644 --- a/data-prepper-core/src/test/java/org/opensearch/dataprepper/parser/PipelineTransformerTests.java +++ b/data-prepper-core/src/test/java/org/opensearch/dataprepper/parser/PipelineTransformerTests.java @@ -16,19 +16,20 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.opensearch.dataprepper.TestDataProvider; -import org.opensearch.dataprepper.model.breaker.CircuitBreaker; +import org.opensearch.dataprepper.acknowledgements.DefaultAcknowledgementSetManager; import org.opensearch.dataprepper.breaker.CircuitBreakerManager; +import org.opensearch.dataprepper.core.event.EventFactoryApplicationContextMarker; +import org.opensearch.dataprepper.model.breaker.CircuitBreaker; import org.opensearch.dataprepper.model.buffer.Buffer; import org.opensearch.dataprepper.model.configuration.PipelinesDataFlowModel; import org.opensearch.dataprepper.model.event.Event; +import org.opensearch.dataprepper.model.event.EventFactory; import org.opensearch.dataprepper.model.plugin.PluginFactory; import org.opensearch.dataprepper.model.record.Record; import org.opensearch.dataprepper.parser.model.DataPrepperConfiguration; import org.opensearch.dataprepper.peerforwarder.PeerForwarderConfiguration; import org.opensearch.dataprepper.peerforwarder.PeerForwarderProvider; import org.opensearch.dataprepper.peerforwarder.PeerForwarderReceiveBuffer; -import org.opensearch.dataprepper.event.DefaultEventFactory; -import org.opensearch.dataprepper.acknowledgements.DefaultAcknowledgementSetManager; import org.opensearch.dataprepper.pipeline.Pipeline; import org.opensearch.dataprepper.pipeline.router.RouterFactory; import org.opensearch.dataprepper.plugin.DefaultPluginFactory; @@ -79,7 +80,7 @@ class PipelineTransformerTests { @Mock private CircuitBreakerManager circuitBreakerManager; - private DefaultEventFactory eventFactory; + private EventFactory eventFactory; private DefaultAcknowledgementSetManager acknowledgementSetManager; @@ -88,7 +89,7 @@ class PipelineTransformerTests { @BeforeEach void setUp() { peerForwarderProvider = mock(PeerForwarderProvider.class); - eventFactory = mock(DefaultEventFactory.class); + eventFactory = mock(EventFactory.class); acknowledgementSetManager = mock(DefaultAcknowledgementSetManager.class); final AnnotationConfigApplicationContext publicContext = new AnnotationConfigApplicationContext(); publicContext.refresh(); @@ -96,7 +97,7 @@ void setUp() { final AnnotationConfigApplicationContext coreContext = new AnnotationConfigApplicationContext(); coreContext.setParent(publicContext); - coreContext.scan(DefaultEventFactory.class.getPackage().getName()); + coreContext.scan(EventFactoryApplicationContextMarker.class.getPackage().getName()); coreContext.scan(DefaultAcknowledgementSetManager.class.getPackage().getName()); diff --git a/data-prepper-core/src/test/java/org/opensearch/dataprepper/peerforwarder/PeerForwarderAppConfigIT.java b/data-prepper-core/src/test/java/org/opensearch/dataprepper/peerforwarder/PeerForwarderAppConfigIT.java index 897bdcfe66..a1db33f7d7 100644 --- a/data-prepper-core/src/test/java/org/opensearch/dataprepper/peerforwarder/PeerForwarderAppConfigIT.java +++ b/data-prepper-core/src/test/java/org/opensearch/dataprepper/peerforwarder/PeerForwarderAppConfigIT.java @@ -6,8 +6,10 @@ package org.opensearch.dataprepper.peerforwarder; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; -import org.opensearch.dataprepper.peerforwarder.discovery.DiscoveryMode; import org.junit.jupiter.api.Test; +import org.opensearch.dataprepper.acknowledgements.DefaultAcknowledgementSetManager; +import org.opensearch.dataprepper.core.event.EventFactoryApplicationContextMarker; +import org.opensearch.dataprepper.peerforwarder.discovery.DiscoveryMode; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -16,8 +18,6 @@ import static org.hamcrest.Matchers.equalTo; import static org.opensearch.dataprepper.peerforwarder.PeerForwarderConfiguration.DEFAULT_CERTIFICATE_FILE_PATH; import static org.opensearch.dataprepper.peerforwarder.PeerForwarderConfiguration.DEFAULT_PRIVATE_KEY_FILE_PATH; -import org.opensearch.dataprepper.event.DefaultEventFactory; -import org.opensearch.dataprepper.acknowledgements.DefaultAcknowledgementSetManager; class PeerForwarderAppConfigIT { @@ -26,7 +26,7 @@ private PeerForwarderConfiguration createObjectUnderTest() { applicationContext.scan(PeerForwarderConfiguration.class.getPackageName()); applicationContext.register(PeerForwarderAppConfig.class); applicationContext.register(InnerAppConfig.class); - applicationContext.scan(DefaultEventFactory.class.getPackage().getName()); + applicationContext.scan(EventFactoryApplicationContextMarker.class.getPackage().getName()); applicationContext.scan(DefaultAcknowledgementSetManager.class.getPackage().getName()); applicationContext.refresh(); diff --git a/data-prepper-core/src/test/java/org/opensearch/dataprepper/plugin/DefaultPluginFactoryIT.java b/data-prepper-core/src/test/java/org/opensearch/dataprepper/plugin/DefaultPluginFactoryIT.java index 943c582ac3..24a6082b11 100644 --- a/data-prepper-core/src/test/java/org/opensearch/dataprepper/plugin/DefaultPluginFactoryIT.java +++ b/data-prepper-core/src/test/java/org/opensearch/dataprepper/plugin/DefaultPluginFactoryIT.java @@ -5,17 +5,19 @@ package org.opensearch.dataprepper.plugin; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import org.opensearch.dataprepper.acknowledgements.DefaultAcknowledgementSetManager; +import org.opensearch.dataprepper.core.event.EventFactoryApplicationContextMarker; import org.opensearch.dataprepper.model.configuration.PipelinesDataFlowModel; import org.opensearch.dataprepper.model.configuration.PluginSetting; import org.opensearch.dataprepper.model.plugin.InvalidPluginConfigurationException; import org.opensearch.dataprepper.parser.model.DataPrepperConfiguration; -import org.opensearch.dataprepper.plugins.TestPlugin; import org.opensearch.dataprepper.plugins.TestObjectPlugin; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.opensearch.dataprepper.plugins.TestPlugin; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import java.util.HashMap; @@ -27,8 +29,6 @@ import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; -import org.opensearch.dataprepper.event.DefaultEventFactory; -import org.opensearch.dataprepper.acknowledgements.DefaultAcknowledgementSetManager; /** * Integration test of the plugin framework. These tests should not mock any portion @@ -44,10 +44,6 @@ class DefaultPluginFactoryIT { private String objectPluginName; private String pipelineName; - private DefaultEventFactory eventFactory; - - private DefaultAcknowledgementSetManager acknowledgementSetManager; - @BeforeEach void setUp() { pluginName = "test_plugin"; @@ -62,7 +58,7 @@ private DefaultPluginFactory createObjectUnderTest() { final AnnotationConfigApplicationContext coreContext = new AnnotationConfigApplicationContext(); coreContext.setParent(publicContext); - coreContext.scan(DefaultEventFactory.class.getPackage().getName()); + coreContext.scan(EventFactoryApplicationContextMarker.class.getPackage().getName()); coreContext.scan(DefaultAcknowledgementSetManager.class.getPackage().getName()); coreContext.scan(DefaultPluginFactory.class.getPackage().getName()); coreContext.register(PluginBeanFactoryProvider.class); diff --git a/data-prepper-core/src/test/java/org/opensearch/dataprepper/plugin/ExtensionsIT.java b/data-prepper-core/src/test/java/org/opensearch/dataprepper/plugin/ExtensionsIT.java index a498cd5f3b..c5e44016ec 100644 --- a/data-prepper-core/src/test/java/org/opensearch/dataprepper/plugin/ExtensionsIT.java +++ b/data-prepper-core/src/test/java/org/opensearch/dataprepper/plugin/ExtensionsIT.java @@ -15,7 +15,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.opensearch.dataprepper.acknowledgements.DefaultAcknowledgementSetManager; import org.opensearch.dataprepper.breaker.CircuitBreakerManager; -import org.opensearch.dataprepper.event.DefaultEventFactory; +import org.opensearch.dataprepper.core.event.EventFactoryApplicationContextMarker; import org.opensearch.dataprepper.model.acknowledgements.AcknowledgementSetManager; import org.opensearch.dataprepper.model.configuration.PluginSetting; import org.opensearch.dataprepper.model.event.EventFactory; @@ -25,9 +25,9 @@ import org.opensearch.dataprepper.parser.config.PipelineParserConfiguration; import org.opensearch.dataprepper.peerforwarder.PeerForwarderProvider; import org.opensearch.dataprepper.pipeline.router.RouterFactory; +import org.opensearch.dataprepper.plugins.TestPluginUsingExtension; import org.opensearch.dataprepper.plugins.TestPluginUsingExtensionWithConfig; import org.opensearch.dataprepper.plugins.test.TestExtension; -import org.opensearch.dataprepper.plugins.TestPluginUsingExtension; import org.opensearch.dataprepper.sourcecoordination.SourceCoordinatorFactory; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -79,7 +79,7 @@ void setUp() { coreContext = new AnnotationConfigApplicationContext(); coreContext.setParent(publicContext); - coreContext.scan(DefaultEventFactory.class.getPackage().getName()); + coreContext.scan(EventFactoryApplicationContextMarker.class.getPackage().getName()); coreContext.scan(DefaultAcknowledgementSetManager.class.getPackage().getName()); coreContext.scan(DefaultPluginFactory.class.getPackage().getName()); diff --git a/data-prepper-event/build.gradle b/data-prepper-event/build.gradle new file mode 100644 index 0000000000..3ec8fe936f --- /dev/null +++ b/data-prepper-event/build.gradle @@ -0,0 +1,17 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +dependencies { + implementation project(':data-prepper-api') + implementation 'javax.inject:javax.inject:1' + implementation 'javax.annotation:javax.annotation-api:1.3.2' + implementation(libs.spring.core) { + exclude group: 'commons-logging', module: 'commons-logging' + } + implementation(libs.spring.context) { + exclude group: 'commons-logging', module: 'commons-logging' + } + testImplementation libs.commons.lang3 +} diff --git a/data-prepper-core/src/main/java/org/opensearch/dataprepper/event/DefaultBaseEventBuilder.java b/data-prepper-event/src/main/java/org/opensearch/dataprepper/core/event/DefaultBaseEventBuilder.java similarity index 88% rename from data-prepper-core/src/main/java/org/opensearch/dataprepper/event/DefaultBaseEventBuilder.java rename to data-prepper-event/src/main/java/org/opensearch/dataprepper/core/event/DefaultBaseEventBuilder.java index d0aa67bbab..47a140d55d 100644 --- a/data-prepper-core/src/main/java/org/opensearch/dataprepper/event/DefaultBaseEventBuilder.java +++ b/data-prepper-event/src/main/java/org/opensearch/dataprepper/core/event/DefaultBaseEventBuilder.java @@ -3,18 +3,18 @@ * SPDX-License-Identifier: Apache-2.0 */ -package org.opensearch.dataprepper.event; +package org.opensearch.dataprepper.core.event; import org.opensearch.dataprepper.model.event.BaseEventBuilder; +import org.opensearch.dataprepper.model.event.DefaultEventMetadata; import org.opensearch.dataprepper.model.event.Event; import org.opensearch.dataprepper.model.event.EventMetadata; -import org.opensearch.dataprepper.model.event.DefaultEventMetadata; -import java.util.Map; -import java.util.HashMap; import java.time.Instant; +import java.util.HashMap; +import java.util.Map; -abstract class DefaultBaseEventBuilder implements BaseEventBuilder{ +abstract class DefaultBaseEventBuilder implements BaseEventBuilder { private EventMetadata eventMetadata; private Object data; private String eventType; @@ -36,10 +36,10 @@ public String getEventType() { public EventMetadata getEventMetadata() { if (this.eventMetadata == null) { this.eventMetadata = new DefaultEventMetadata.Builder() - .withEventType(eventType) - .withTimeReceived(timeReceived) - .withAttributes(attributes) - .build(); + .withEventType(eventType) + .withTimeReceived(timeReceived) + .withAttributes(attributes) + .build(); } return this.eventMetadata; } diff --git a/data-prepper-core/src/main/java/org/opensearch/dataprepper/event/DefaultEventBuilderFactory.java b/data-prepper-event/src/main/java/org/opensearch/dataprepper/core/event/DefaultEventBuilderFactory.java similarity index 79% rename from data-prepper-core/src/main/java/org/opensearch/dataprepper/event/DefaultEventBuilderFactory.java rename to data-prepper-event/src/main/java/org/opensearch/dataprepper/core/event/DefaultEventBuilderFactory.java index 563e426e70..7ac883e85c 100644 --- a/data-prepper-core/src/main/java/org/opensearch/dataprepper/event/DefaultEventBuilderFactory.java +++ b/data-prepper-event/src/main/java/org/opensearch/dataprepper/core/event/DefaultEventBuilderFactory.java @@ -3,10 +3,10 @@ * SPDX-License-Identifier: Apache-2.0 */ -package org.opensearch.dataprepper.event; +package org.opensearch.dataprepper.core.event; -import org.opensearch.dataprepper.model.event.EventBuilder; import org.opensearch.dataprepper.model.event.Event; +import org.opensearch.dataprepper.model.event.EventBuilder; import org.opensearch.dataprepper.model.event.JacksonEvent; import org.springframework.stereotype.Component; @@ -22,16 +22,16 @@ DefaultBaseEventBuilder createNew() { return new DefaultEventBuilder(); } - public static class DefaultEventBuilder extends DefaultBaseEventBuilder implements EventBuilder { + public static class DefaultEventBuilder extends DefaultBaseEventBuilder implements EventBuilder { public String getEventType() { return EVENT_TYPE; } public Event build() { return (Event) JacksonEvent.builder() - .withData(getData()) - .withEventType(EVENT_TYPE) - .build(); + .withData(getData()) + .withEventType(EVENT_TYPE) + .build(); } } } diff --git a/data-prepper-core/src/main/java/org/opensearch/dataprepper/event/DefaultEventFactory.java b/data-prepper-event/src/main/java/org/opensearch/dataprepper/core/event/DefaultEventFactory.java similarity index 68% rename from data-prepper-core/src/main/java/org/opensearch/dataprepper/event/DefaultEventFactory.java rename to data-prepper-event/src/main/java/org/opensearch/dataprepper/core/event/DefaultEventFactory.java index 1528cd831b..03d7d3f14b 100644 --- a/data-prepper-core/src/main/java/org/opensearch/dataprepper/event/DefaultEventFactory.java +++ b/data-prepper-event/src/main/java/org/opensearch/dataprepper/core/event/DefaultEventFactory.java @@ -3,34 +3,34 @@ * SPDX-License-Identifier: Apache-2.0 */ -package org.opensearch.dataprepper.event; +package org.opensearch.dataprepper.core.event; +import org.opensearch.dataprepper.model.event.BaseEventBuilder; import org.opensearch.dataprepper.model.event.Event; import org.opensearch.dataprepper.model.event.EventFactory; -import org.opensearch.dataprepper.model.event.BaseEventBuilder; -import java.util.Map; +import javax.inject.Inject; +import javax.inject.Named; import java.util.Collection; +import java.util.Map; import java.util.stream.Collectors; -import javax.inject.Named; -import javax.inject.Inject; @Named public class DefaultEventFactory implements EventFactory { - Map, DefaultEventBuilderFactory> classToFactoryMap; + private final Map, DefaultEventBuilderFactory> classToFactoryMap; @Inject - public DefaultEventFactory(Collection< DefaultEventBuilderFactory> factories) { + DefaultEventFactory(final Collection factories) { classToFactoryMap = factories.stream() - .collect(Collectors.toMap(DefaultEventBuilderFactory::getEventClass, v -> v)); + .collect(Collectors.toMap(DefaultEventBuilderFactory::getEventClass, v -> v)); } - + @Override - public > B eventBuilder(Class eventBuilderClass) throws UnsupportedOperationException { + public > B eventBuilder(final Class eventBuilderClass) throws UnsupportedOperationException { if (!classToFactoryMap.containsKey(eventBuilderClass)) { throw new UnsupportedOperationException("Unsupported class"); } - + return (B) classToFactoryMap.get(eventBuilderClass).createNew(); } } diff --git a/data-prepper-core/src/main/java/org/opensearch/dataprepper/event/DefaultLogEventBuilderFactory.java b/data-prepper-event/src/main/java/org/opensearch/dataprepper/core/event/DefaultLogEventBuilderFactory.java similarity index 73% rename from data-prepper-core/src/main/java/org/opensearch/dataprepper/event/DefaultLogEventBuilderFactory.java rename to data-prepper-event/src/main/java/org/opensearch/dataprepper/core/event/DefaultLogEventBuilderFactory.java index 882fea1d5e..d290900a87 100644 --- a/data-prepper-core/src/main/java/org/opensearch/dataprepper/event/DefaultLogEventBuilderFactory.java +++ b/data-prepper-event/src/main/java/org/opensearch/dataprepper/core/event/DefaultLogEventBuilderFactory.java @@ -3,11 +3,11 @@ * SPDX-License-Identifier: Apache-2.0 */ -package org.opensearch.dataprepper.event; +package org.opensearch.dataprepper.core.event; import org.opensearch.dataprepper.model.event.LogEventBuilder; -import org.opensearch.dataprepper.model.log.Log; import org.opensearch.dataprepper.model.log.JacksonLog; +import org.opensearch.dataprepper.model.log.Log; import org.springframework.stereotype.Component; @Component @@ -22,16 +22,16 @@ DefaultBaseEventBuilder createNew() { return new DefaultLogEventBuilder(); } - public static class DefaultLogEventBuilder extends DefaultBaseEventBuilder implements LogEventBuilder { + public static class DefaultLogEventBuilder extends DefaultBaseEventBuilder implements LogEventBuilder { public String getEventType() { return LOG_EVENT_TYPE; } public Log build() { return (Log) JacksonLog.builder() - .withData(getData()) - .withEventType(getEventType()) - .build(); + .withData(getData()) + .withEventType(getEventType()) + .build(); } } } diff --git a/data-prepper-event/src/main/java/org/opensearch/dataprepper/core/event/EventBuilderFactory.java b/data-prepper-event/src/main/java/org/opensearch/dataprepper/core/event/EventBuilderFactory.java new file mode 100644 index 0000000000..685c0599b5 --- /dev/null +++ b/data-prepper-event/src/main/java/org/opensearch/dataprepper/core/event/EventBuilderFactory.java @@ -0,0 +1,12 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.core.event; + +abstract class EventBuilderFactory { + abstract Class getEventClass(); + + abstract DefaultBaseEventBuilder createNew(); +} diff --git a/data-prepper-core/src/test/java/org/opensearch/dataprepper/event/DefaultBaseEventBuilderTests.java b/data-prepper-event/src/test/java/org/opensearch/dataprepper/core/event/DefaultBaseEventBuilderTests.java similarity index 86% rename from data-prepper-core/src/test/java/org/opensearch/dataprepper/event/DefaultBaseEventBuilderTests.java rename to data-prepper-event/src/test/java/org/opensearch/dataprepper/core/event/DefaultBaseEventBuilderTests.java index 1561b0b4cf..1a7828526d 100644 --- a/data-prepper-core/src/test/java/org/opensearch/dataprepper/event/DefaultBaseEventBuilderTests.java +++ b/data-prepper-event/src/test/java/org/opensearch/dataprepper/core/event/DefaultBaseEventBuilderTests.java @@ -3,22 +3,29 @@ * SPDX-License-Identifier: Apache-2.0 */ -package org.opensearch.dataprepper.event; +package org.opensearch.dataprepper.core.event; +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.jupiter.api.Test; import org.opensearch.dataprepper.model.event.DefaultEventMetadata; +import org.opensearch.dataprepper.model.event.Event; import org.opensearch.dataprepper.model.event.EventMetadata; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.CoreMatchers.not; -import org.junit.jupiter.api.Test; -import org.apache.commons.lang3.RandomStringUtils; +import org.opensearch.dataprepper.model.event.JacksonEvent; +import java.time.Instant; import java.util.Collections; import java.util.Map; -import java.time.Instant; + +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; class DefaultBaseEventBuilderTests { class TestDefaultBaseEventBuilder extends DefaultBaseEventBuilder { + @Override + public Event build() { + return JacksonEvent.builder().build(); + } } private DefaultBaseEventBuilder createObjectUnderTest() { @@ -42,7 +49,7 @@ void testDefaultBaseEventBuilderWithTypeDataAndAttributes() { defaultBaseEventBuilder.withEventType(testEventType); Map metadataAttributes = Collections.emptyMap(); defaultBaseEventBuilder.withEventMetadataAttributes(metadataAttributes); - + assertThat(defaultBaseEventBuilder.getTimeReceived(), not(equalTo(null))); assertThat(defaultBaseEventBuilder.getData(), equalTo(data)); assertThat(defaultBaseEventBuilder.getEventType(), equalTo(testEventType)); @@ -63,15 +70,15 @@ void testDefaultBaseEventBuilderWithEventMetadata() { Instant timeReceived = Instant.now(); Map attributes = Collections.emptyMap(); EventMetadata eventMetadata = new DefaultEventMetadata.Builder() - .withEventType(testEventType) - .withTimeReceived(timeReceived) - .withAttributes(attributes) - .build(); + .withEventType(testEventType) + .withTimeReceived(timeReceived) + .withAttributes(attributes) + .build(); defaultBaseEventBuilder.withEventMetadata(eventMetadata); defaultBaseEventBuilder.withData(data); - - assertThat(defaultBaseEventBuilder.getTimeReceived(),equalTo(timeReceived)); + + assertThat(defaultBaseEventBuilder.getTimeReceived(), equalTo(timeReceived)); assertThat(defaultBaseEventBuilder.getData(), equalTo(data)); assertThat(defaultBaseEventBuilder.getEventType(), equalTo(testEventType)); assertThat(defaultBaseEventBuilder.getEventMetadataAttributes(), equalTo(attributes)); diff --git a/data-prepper-core/src/test/java/org/opensearch/dataprepper/event/DefaultEventBuilderFactoryTests.java b/data-prepper-event/src/test/java/org/opensearch/dataprepper/core/event/DefaultEventBuilderFactoryTests.java similarity index 92% rename from data-prepper-core/src/test/java/org/opensearch/dataprepper/event/DefaultEventBuilderFactoryTests.java rename to data-prepper-event/src/test/java/org/opensearch/dataprepper/core/event/DefaultEventBuilderFactoryTests.java index cbcebfb01f..09295d0737 100644 --- a/data-prepper-core/src/test/java/org/opensearch/dataprepper/event/DefaultEventBuilderFactoryTests.java +++ b/data-prepper-event/src/test/java/org/opensearch/dataprepper/core/event/DefaultEventBuilderFactoryTests.java @@ -3,20 +3,21 @@ * SPDX-License-Identifier: Apache-2.0 */ -package org.opensearch.dataprepper.event; +package org.opensearch.dataprepper.core.event; -import org.opensearch.dataprepper.model.event.EventMetadata; -import org.opensearch.dataprepper.model.event.JacksonEvent; -import org.opensearch.dataprepper.model.event.EventBuilder; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.CoreMatchers.not; +import org.apache.commons.lang3.RandomStringUtils; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.apache.commons.lang3.RandomStringUtils; +import org.opensearch.dataprepper.model.event.EventBuilder; +import org.opensearch.dataprepper.model.event.EventMetadata; +import org.opensearch.dataprepper.model.event.JacksonEvent; -import java.util.Map; import java.util.Collections; +import java.util.Map; + +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; class DefaultEventBuilderFactoryTests { private DefaultEventBuilderFactory defaultEventBuilderFactory; @@ -44,7 +45,7 @@ public void testBasic() { String testValue = RandomStringUtils.randomAlphabetic(10); Map data = Map.of(testKey, testValue); Map attributes = Collections.emptyMap(); - EventBuilder eventBuilder = (EventBuilder)baseEventBuilder.withEventMetadataAttributes(attributes).withData(data); + EventBuilder eventBuilder = (EventBuilder) baseEventBuilder.withEventMetadataAttributes(attributes).withData(data); JacksonEvent event = (JacksonEvent) eventBuilder.build(); EventMetadata eventMetadata = event.getMetadata(); diff --git a/data-prepper-core/src/test/java/org/opensearch/dataprepper/event/DefaultEventFactoryTests.java b/data-prepper-event/src/test/java/org/opensearch/dataprepper/core/event/DefaultEventFactoryTests.java similarity index 97% rename from data-prepper-core/src/test/java/org/opensearch/dataprepper/event/DefaultEventFactoryTests.java rename to data-prepper-event/src/test/java/org/opensearch/dataprepper/core/event/DefaultEventFactoryTests.java index a42f24d122..0f54fab6db 100644 --- a/data-prepper-core/src/test/java/org/opensearch/dataprepper/event/DefaultEventFactoryTests.java +++ b/data-prepper-event/src/test/java/org/opensearch/dataprepper/core/event/DefaultEventFactoryTests.java @@ -3,22 +3,23 @@ * SPDX-License-Identifier: Apache-2.0 */ -package org.opensearch.dataprepper.event; +package org.opensearch.dataprepper.core.event; -import org.opensearch.dataprepper.model.event.BaseEventBuilder; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.hamcrest.Matchers.equalTo; -import org.junit.jupiter.api.Test; import org.junit.jupiter.api.BeforeEach; -import static org.mockito.Mockito.lenient; -import static org.mockito.Mockito.mock; -import org.mockito.Mock; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import org.opensearch.dataprepper.model.event.BaseEventBuilder; -import java.util.List; import java.util.ArrayList; +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; @ExtendWith(MockitoExtension.class) class DefaultEventFactoryTests { @@ -35,7 +36,7 @@ class DefaultEventFactoryTests { private Class class1; private Class class2; - + private DefaultEventFactory createObjectUnderTest() { return new DefaultEventFactory(factories); } diff --git a/data-prepper-core/src/test/java/org/opensearch/dataprepper/event/DefaultLogEventBuilderFactoryTests.java b/data-prepper-event/src/test/java/org/opensearch/dataprepper/core/event/DefaultLogEventBuilderFactoryTests.java similarity index 92% rename from data-prepper-core/src/test/java/org/opensearch/dataprepper/event/DefaultLogEventBuilderFactoryTests.java rename to data-prepper-event/src/test/java/org/opensearch/dataprepper/core/event/DefaultLogEventBuilderFactoryTests.java index cfe719891b..734458e01d 100644 --- a/data-prepper-core/src/test/java/org/opensearch/dataprepper/event/DefaultLogEventBuilderFactoryTests.java +++ b/data-prepper-event/src/test/java/org/opensearch/dataprepper/core/event/DefaultLogEventBuilderFactoryTests.java @@ -3,20 +3,21 @@ * SPDX-License-Identifier: Apache-2.0 */ -package org.opensearch.dataprepper.event; +package org.opensearch.dataprepper.core.event; -import org.opensearch.dataprepper.model.event.EventMetadata; -import org.opensearch.dataprepper.model.log.JacksonLog; -import org.opensearch.dataprepper.model.event.LogEventBuilder; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.CoreMatchers.not; +import org.apache.commons.lang3.RandomStringUtils; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.apache.commons.lang3.RandomStringUtils; +import org.opensearch.dataprepper.model.event.EventMetadata; +import org.opensearch.dataprepper.model.event.LogEventBuilder; +import org.opensearch.dataprepper.model.log.JacksonLog; -import java.util.Map; import java.util.Collections; +import java.util.Map; + +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; class DefaultLogEventBuilderFactoryTests { private DefaultLogEventBuilderFactory defaultLogEventBuilderFactory; @@ -44,7 +45,7 @@ public void testBasic() { String testValue = RandomStringUtils.randomAlphabetic(10); Map data = Map.of(testKey, testValue); Map attributes = Collections.emptyMap(); - LogEventBuilder eventBuilder = (LogEventBuilder)baseEventBuilder.withEventMetadataAttributes(attributes).withData(data); + LogEventBuilder eventBuilder = (LogEventBuilder) baseEventBuilder.withEventMetadataAttributes(attributes).withData(data); JacksonLog log = (JacksonLog) eventBuilder.build(); EventMetadata eventMetadata = log.getMetadata(); diff --git a/data-prepper-plugins/common/build.gradle b/data-prepper-plugins/common/build.gradle index 634379fffb..242b91aa32 100644 --- a/data-prepper-plugins/common/build.gradle +++ b/data-prepper-plugins/common/build.gradle @@ -22,6 +22,7 @@ dependencies { implementation 'org.apache.parquet:parquet-common:1.13.1' implementation 'org.xerial.snappy:snappy-java:1.1.10.5' testImplementation project(':data-prepper-plugins:blocking-buffer') + testImplementation project(':data-prepper-test-event') testImplementation libs.commons.io testImplementation testLibs.mockito.inline } diff --git a/data-prepper-plugins/common/src/main/java/org/opensearch/dataprepper/plugins/source/file/FileSource.java b/data-prepper-plugins/common/src/main/java/org/opensearch/dataprepper/plugins/source/file/FileSource.java index d658bdf897..dd8bf65f80 100644 --- a/data-prepper-plugins/common/src/main/java/org/opensearch/dataprepper/plugins/source/file/FileSource.java +++ b/data-prepper-plugins/common/src/main/java/org/opensearch/dataprepper/plugins/source/file/FileSource.java @@ -15,7 +15,8 @@ import org.opensearch.dataprepper.model.codec.InputCodec; import org.opensearch.dataprepper.model.configuration.PluginModel; import org.opensearch.dataprepper.model.configuration.PluginSetting; -import org.opensearch.dataprepper.model.event.JacksonEvent; +import org.opensearch.dataprepper.model.event.EventBuilder; +import org.opensearch.dataprepper.model.event.EventFactory; import org.opensearch.dataprepper.model.plugin.PluginFactory; import org.opensearch.dataprepper.model.record.Record; import org.opensearch.dataprepper.model.source.Source; @@ -45,12 +46,16 @@ public class FileSource implements Source> { private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); private final FileSourceConfig fileSourceConfig; private final FileStrategy fileStrategy; + private final EventFactory eventFactory; private boolean isStopRequested; private final int writeTimeout; @DataPrepperPluginConstructor - public FileSource(final FileSourceConfig fileSourceConfig, final PluginMetrics pluginMetrics, final PluginFactory pluginFactory) { + public FileSource( + final FileSourceConfig fileSourceConfig, final PluginMetrics pluginMetrics, final PluginFactory pluginFactory, + final EventFactory eventFactory) { + this.eventFactory = eventFactory; fileSourceConfig.validate(); this.fileSourceConfig = fileSourceConfig; this.isStopRequested = false; @@ -106,8 +111,8 @@ private Record getEventRecordFromLine(final String line) { break; } - return new Record<>(JacksonEvent - .builder() + return new Record<>( + eventFactory.eventBuilder(EventBuilder.class) .withEventType(fileSourceConfig.getRecordType()) .withData(structuredLine) .build()); diff --git a/data-prepper-plugins/common/src/test/java/org/opensearch/dataprepper/plugins/source/file/FileSourceTests.java b/data-prepper-plugins/common/src/test/java/org/opensearch/dataprepper/plugins/source/file/FileSourceTests.java index 6bc886b52e..86d596caa1 100644 --- a/data-prepper-plugins/common/src/test/java/org/opensearch/dataprepper/plugins/source/file/FileSourceTests.java +++ b/data-prepper-plugins/common/src/test/java/org/opensearch/dataprepper/plugins/source/file/FileSourceTests.java @@ -5,34 +5,35 @@ package org.opensearch.dataprepper.plugins.source.file; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.opensearch.dataprepper.event.TestEventFactory; import org.opensearch.dataprepper.metrics.PluginMetrics; import org.opensearch.dataprepper.model.buffer.Buffer; import org.opensearch.dataprepper.model.codec.InputCodec; import org.opensearch.dataprepper.model.configuration.PluginSetting; import org.opensearch.dataprepper.model.event.Event; -import org.opensearch.dataprepper.model.event.JacksonEvent; +import org.opensearch.dataprepper.model.event.EventBuilder; import org.opensearch.dataprepper.model.plugin.PluginFactory; import org.opensearch.dataprepper.model.record.Record; import org.opensearch.dataprepper.plugins.buffer.blockingbuffer.BlockingBuffer; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.util.List; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.UUID; import java.util.concurrent.TimeoutException; @@ -78,7 +79,7 @@ void setUp() { private FileSource createObjectUnderTest() { fileSourceConfig = OBJECT_MAPPER.convertValue(pluginSettings, FileSourceConfig.class); - return new FileSource(fileSourceConfig, pluginMetrics, pluginFactory); + return new FileSource(fileSourceConfig, pluginMetrics, pluginFactory, TestEventFactory.getTestEventFactory()); } @Nested @@ -252,8 +253,7 @@ private Record createRecordEventWithKeyValuePair(final String key, final final Map eventData = new HashMap<>(); eventData.put(key, value); - return new Record<>(JacksonEvent - .builder() + return new Record<>(TestEventFactory.getTestEventFactory().eventBuilder(EventBuilder.class) .withEventType("event") .withData(eventData) .build()); diff --git a/data-prepper-test-event/README.md b/data-prepper-test-event/README.md new file mode 100644 index 0000000000..fa9a405271 --- /dev/null +++ b/data-prepper-test-event/README.md @@ -0,0 +1,5 @@ +# Data Prepper Test Event + +This class provides test classes to help with the Data Prepper event model. + +See in particular the `TestEventFactory` class. diff --git a/data-prepper-test-event/build.gradle b/data-prepper-test-event/build.gradle new file mode 100644 index 0000000000..f731101c06 --- /dev/null +++ b/data-prepper-test-event/build.gradle @@ -0,0 +1,12 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +dependencies { + implementation project(':data-prepper-api') + implementation project(':data-prepper-event') + implementation(libs.spring.context) { + exclude group: 'commons-logging', module: 'commons-logging' + } +} diff --git a/data-prepper-test-event/src/main/java/org/opensearch/dataprepper/core/event/EventFactoryApplicationContextMarker.java b/data-prepper-test-event/src/main/java/org/opensearch/dataprepper/core/event/EventFactoryApplicationContextMarker.java new file mode 100644 index 0000000000..722c3197ed --- /dev/null +++ b/data-prepper-test-event/src/main/java/org/opensearch/dataprepper/core/event/EventFactoryApplicationContextMarker.java @@ -0,0 +1,13 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.core.event; + +/** + * Intended for Data Prepper core tests. Using this, tests that need to scan + * the Application Context can scan this class. + */ +public interface EventFactoryApplicationContextMarker { +} diff --git a/data-prepper-test-event/src/main/java/org/opensearch/dataprepper/event/TestEventFactory.java b/data-prepper-test-event/src/main/java/org/opensearch/dataprepper/event/TestEventFactory.java new file mode 100644 index 0000000000..932c9ca66a --- /dev/null +++ b/data-prepper-test-event/src/main/java/org/opensearch/dataprepper/event/TestEventFactory.java @@ -0,0 +1,41 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.event; + +import org.opensearch.dataprepper.core.event.EventFactoryApplicationContextMarker; +import org.opensearch.dataprepper.model.event.BaseEventBuilder; +import org.opensearch.dataprepper.model.event.Event; +import org.opensearch.dataprepper.model.event.EventFactory; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +/** + * An implementation of {@link EventFactory} that is useful for integration and unit tests + * in other projects. + */ +public class TestEventFactory implements EventFactory { + private static AnnotationConfigApplicationContext APPLICATION_CONTEXT; + private static EventFactory DEFAULT_EVENT_FACTORY; + private final EventFactory innerEventFactory; + + TestEventFactory(final EventFactory innerEventFactory) { + this.innerEventFactory = innerEventFactory; + } + + public static EventFactory getTestEventFactory() { + if(APPLICATION_CONTEXT == null) { + APPLICATION_CONTEXT = new AnnotationConfigApplicationContext(); + APPLICATION_CONTEXT.scan(EventFactoryApplicationContextMarker.class.getPackageName()); + APPLICATION_CONTEXT.refresh(); + DEFAULT_EVENT_FACTORY = APPLICATION_CONTEXT.getBean(EventFactory.class); + } + return new TestEventFactory(DEFAULT_EVENT_FACTORY); + } + + @Override + public > B eventBuilder(final Class eventBuilderClass) throws UnsupportedOperationException { + return innerEventFactory.eventBuilder(eventBuilderClass); + } +} diff --git a/data-prepper-test-event/src/test/java/org/opensearch/dataprepper/event/TestEventFactoryTest.java b/data-prepper-test-event/src/test/java/org/opensearch/dataprepper/event/TestEventFactoryTest.java new file mode 100644 index 0000000000..cdfeb4c4b1 --- /dev/null +++ b/data-prepper-test-event/src/test/java/org/opensearch/dataprepper/event/TestEventFactoryTest.java @@ -0,0 +1,39 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.event; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.opensearch.dataprepper.model.event.BaseEventBuilder; +import org.opensearch.dataprepper.model.event.EventFactory; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class TestEventFactoryTest { + @Mock + private EventFactory eventFactory; + + private TestEventFactory createObjectUnderTest() { + return new TestEventFactory(eventFactory); + } + + @Test + void eventBuilder_returns_EventBuilder_from_inner() { + final BaseEventBuilder innerEventBuilder = mock(BaseEventBuilder.class); + final Class inputClass = innerEventBuilder.getClass(); + when(eventFactory.eventBuilder(inputClass)) + .thenReturn(innerEventBuilder); + + assertThat(createObjectUnderTest().eventBuilder(inputClass), + equalTo(innerEventBuilder)); + } +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 567e4aa40b..80868f5cb9 100644 --- a/settings.gradle +++ b/settings.gradle @@ -94,6 +94,8 @@ dependencyResolutionManagement { include 'data-prepper-api' include 'data-prepper-plugins' +include 'data-prepper-event' +include 'data-prepper-test-event' include 'data-prepper-core' include 'data-prepper-main' include 'data-prepper-plugins:common'