forked from opensearch-project/data-prepper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support plugins defining the EventKey in the plugin configuration cla…
…sses. Data Prepper will deserialize the EventKey from the pipeline configuration and validate @notempty validations. Builds on the opensearch-project#1916. (opensearch-project#4635) Signed-off-by: David Venable <dlv@amazon.com>
- Loading branch information
Showing
15 changed files
with
431 additions
and
10 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
...epper-api/src/main/java/org/opensearch/dataprepper/model/event/EventKeyConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.model.event; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* An annotation for an {@link EventKey} used in a Data Prepper pipeline configuration. | ||
* <p> | ||
* Unless you need all actions on a configuration, you should use this annotation to | ||
* provide the most appropriate validation. | ||
* | ||
* @since 2.9 | ||
*/ | ||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.FIELD}) | ||
public @interface EventKeyConfiguration { | ||
/** | ||
* Defines the {@link EventKeyFactory.EventAction}s to use when creating the {@link EventKey} | ||
* for the configuration. | ||
* | ||
* @return The desired event actions. | ||
* @since 2.9 | ||
*/ | ||
EventKeyFactory.EventAction[] value(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...core/src/integrationTest/java/org/opensearch/dataprepper/plugins/SimpleCopyProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins; | ||
|
||
import org.opensearch.dataprepper.model.annotations.DataPrepperPlugin; | ||
import org.opensearch.dataprepper.model.annotations.DataPrepperPluginConstructor; | ||
import org.opensearch.dataprepper.model.event.Event; | ||
import org.opensearch.dataprepper.model.processor.Processor; | ||
import org.opensearch.dataprepper.model.record.Record; | ||
|
||
import java.util.Collection; | ||
|
||
@DataPrepperPlugin(name = "simple_copy_test", pluginType = Processor.class, pluginConfigurationType = SimpleCopyProcessorConfig.class) | ||
public class SimpleCopyProcessor implements Processor<Record<Event>, Record<Event>> { | ||
private final SimpleCopyProcessorConfig simpleCopyProcessorConfig; | ||
int count = 0; | ||
|
||
@DataPrepperPluginConstructor | ||
public SimpleCopyProcessor(final SimpleCopyProcessorConfig simpleCopyProcessorConfig) { | ||
this.simpleCopyProcessorConfig = simpleCopyProcessorConfig; | ||
} | ||
|
||
@Override | ||
public Collection<Record<Event>> execute(final Collection<Record<Event>> records) { | ||
for (final Record<Event> record : records) { | ||
final Object value = record.getData().get(simpleCopyProcessorConfig.getSource(), Object.class); | ||
record.getData().put(simpleCopyProcessorConfig.getTarget(), value); | ||
count++; | ||
} | ||
|
||
return records; | ||
} | ||
|
||
@Override | ||
public void prepareForShutdown() { | ||
|
||
} | ||
|
||
@Override | ||
public boolean isReadyForShutdown() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void shutdown() { | ||
|
||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...rc/integrationTest/java/org/opensearch/dataprepper/plugins/SimpleCopyProcessorConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins; | ||
|
||
import org.opensearch.dataprepper.model.event.EventKey; | ||
import org.opensearch.dataprepper.model.event.EventKeyConfiguration; | ||
import org.opensearch.dataprepper.model.event.EventKeyFactory; | ||
|
||
public class SimpleCopyProcessorConfig { | ||
@EventKeyConfiguration(EventKeyFactory.EventAction.GET) | ||
private EventKey source; | ||
private EventKey target; | ||
|
||
public EventKey getSource() { | ||
return source; | ||
} | ||
|
||
public EventKey getTarget() { | ||
return target; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...rc/main/java/org/opensearch/dataprepper/core/validators/NotEmptyValidatorForEventKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.core.validators; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import org.opensearch.dataprepper.model.event.EventKey; | ||
|
||
import jakarta.validation.constraints.NotEmpty; | ||
|
||
public class NotEmptyValidatorForEventKey implements ConstraintValidator<NotEmpty, EventKey> { | ||
@Override | ||
public boolean isValid(final EventKey eventKey, final ConstraintValidatorContext constraintValidatorContext) { | ||
if(eventKey == null) { | ||
return false; | ||
} | ||
return !eventKey.getKey().isEmpty(); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...parser/src/main/java/org/opensearch/dataprepper/pipeline/parser/EventKeyDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.pipeline.parser; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.BeanProperty; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.deser.ContextualDeserializer; | ||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||
import org.opensearch.dataprepper.model.event.EventKey; | ||
import org.opensearch.dataprepper.model.event.EventKeyConfiguration; | ||
import org.opensearch.dataprepper.model.event.EventKeyFactory; | ||
|
||
import java.io.IOException; | ||
|
||
public class EventKeyDeserializer extends StdDeserializer<EventKey> implements ContextualDeserializer { | ||
private final EventKeyFactory eventKeyFactory; | ||
private final EventKeyFactory.EventAction[] eventAction; | ||
|
||
/** | ||
* Constructs a new {@link EventKeyDeserializer} from an {@link EventKeyFactory}. | ||
* | ||
* @param eventKeyFactory The factory for creating {@link EventKey} objects. | ||
*/ | ||
public EventKeyDeserializer(final EventKeyFactory eventKeyFactory) { | ||
this(eventKeyFactory, new EventKeyFactory.EventAction[] {EventKeyFactory.EventAction.ALL}); | ||
} | ||
|
||
private EventKeyDeserializer(final EventKeyFactory eventKeyFactory, final EventKeyFactory.EventAction[] eventAction) { | ||
super(EventKey.class); | ||
this.eventKeyFactory = eventKeyFactory; | ||
this.eventAction = eventAction; | ||
} | ||
|
||
@Override | ||
public EventKey deserialize(final JsonParser parser, final DeserializationContext ctxt) throws IOException { | ||
final String eventKeyString = parser.getValueAsString(); | ||
|
||
return eventKeyFactory.createEventKey(eventKeyString, eventAction); | ||
} | ||
|
||
@Override | ||
public JsonDeserializer<?> createContextual(final DeserializationContext deserializationContext, final BeanProperty property) { | ||
if(property == null) | ||
return this; | ||
|
||
final EventKeyConfiguration eventKeyConfiguration = property.getAnnotation(EventKeyConfiguration.class); | ||
|
||
if(eventKeyConfiguration == null) | ||
return this; | ||
|
||
final EventKeyFactory.EventAction[] eventAction = eventKeyConfiguration.value(); | ||
|
||
return new EventKeyDeserializer(eventKeyFactory, eventAction); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...peline-parser/src/main/resources/META-INF/services/jakarta.validation.ConstraintValidator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# | ||
# Copyright OpenSearch Contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
org.opensearch.dataprepper.core.validators.NotEmptyValidatorForEventKey |
50 changes: 50 additions & 0 deletions
50
...est/java/org/opensearch/dataprepper/core/validators/NotEmptyValidatorForEventKeyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.core.validators; | ||
|
||
import jakarta.validation.ConstraintValidatorContext; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.opensearch.dataprepper.model.event.EventKey; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class NotEmptyValidatorForEventKeyTest { | ||
@Mock | ||
private EventKey eventKey; | ||
|
||
@Mock | ||
private ConstraintValidatorContext context; | ||
|
||
private NotEmptyValidatorForEventKey createObjectUnderTest() { | ||
return new NotEmptyValidatorForEventKey(); | ||
} | ||
|
||
@Test | ||
void isValid_returns_false_if_EventKey_is_empty() { | ||
assertThat(createObjectUnderTest().isValid(null, context), equalTo(false)); | ||
} | ||
|
||
@Test | ||
void isValid_returns_false_if_EventKey_getKey_is_empty() { | ||
when(eventKey.getKey()).thenReturn(""); | ||
assertThat(createObjectUnderTest().isValid(eventKey, context), equalTo(false)); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"/", "a", "/abcdefghijklmnopqrstuvwxyz"}) | ||
void isValid_returns_true_if_EventKey_getKey_is_not_empty(final String key) { | ||
when(eventKey.getKey()).thenReturn(key); | ||
assertThat(createObjectUnderTest().isValid(eventKey, context), equalTo(true)); | ||
} | ||
} |
Oops, something went wrong.