Skip to content

Commit

Permalink
Make using the EventFactory easier by adding a test package and class…
Browse files Browse the repository at this point in the history
… 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 <[email protected]>
  • Loading branch information
dlvenable authored Mar 7, 2024
1 parent ccce150 commit 2c018b8
Show file tree
Hide file tree
Showing 27 changed files with 276 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ public interface BaseEventBuilder<T extends Event> {
* @since 2.2
*/
BaseEventBuilder<T> withData(final Object data);

T build();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ public interface LogEventBuilder extends BaseEventBuilder<Log> {
* @return a log event
* @since 2.2
*/
public Log build();
Log build();
}
2 changes: 2 additions & 0 deletions data-prepper-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,7 +80,7 @@ class PipelineTransformerTests {
@Mock
private CircuitBreakerManager circuitBreakerManager;

private DefaultEventFactory eventFactory;
private EventFactory eventFactory;

private DefaultAcknowledgementSetManager acknowledgementSetManager;

Expand All @@ -88,15 +89,15 @@ 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();

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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -44,10 +44,6 @@ class DefaultPluginFactoryIT {
private String objectPluginName;
private String pipelineName;

private DefaultEventFactory eventFactory;

private DefaultAcknowledgementSetManager acknowledgementSetManager;

@BeforeEach
void setUp() {
pluginName = "test_plugin";
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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());
Expand Down
17 changes: 17 additions & 0 deletions data-prepper-event/build.gradle
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends Event> implements BaseEventBuilder<T>{
abstract class DefaultBaseEventBuilder<T extends Event> implements BaseEventBuilder<T> {
private EventMetadata eventMetadata;
private Object data;
private String eventType;
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,16 +22,16 @@ DefaultBaseEventBuilder createNew() {
return new DefaultEventBuilder();
}

public static class DefaultEventBuilder extends DefaultBaseEventBuilder<Event> implements EventBuilder {
public static class DefaultEventBuilder extends DefaultBaseEventBuilder<Event> 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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Class<?>, DefaultEventBuilderFactory> classToFactoryMap;
private final Map<Class<?>, DefaultEventBuilderFactory> classToFactoryMap;

@Inject
public DefaultEventFactory(Collection< DefaultEventBuilderFactory> factories) {
DefaultEventFactory(final Collection<DefaultEventBuilderFactory> factories) {
classToFactoryMap = factories.stream()
.collect(Collectors.toMap(DefaultEventBuilderFactory::getEventClass, v -> v));
.collect(Collectors.toMap(DefaultEventBuilderFactory::getEventClass, v -> v));
}

@Override
public <T extends Event, B extends BaseEventBuilder<T>> B eventBuilder(Class<B> eventBuilderClass) throws UnsupportedOperationException {
public <T extends Event, B extends BaseEventBuilder<T>> B eventBuilder(final Class<B> eventBuilderClass) throws UnsupportedOperationException {
if (!classToFactoryMap.containsKey(eventBuilderClass)) {
throw new UnsupportedOperationException("Unsupported class");
}

return (B) classToFactoryMap.get(eventBuilderClass).createNew();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,16 +22,16 @@ DefaultBaseEventBuilder createNew() {
return new DefaultLogEventBuilder();
}

public static class DefaultLogEventBuilder extends DefaultBaseEventBuilder<Log> implements LogEventBuilder {
public static class DefaultLogEventBuilder extends DefaultBaseEventBuilder<Log> 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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
Loading

0 comments on commit 2c018b8

Please sign in to comment.