-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validate plugin configuration objects using JSR-303 validation #826
Merged
dlvenable
merged 1 commit into
opensearch-project:main
from
dlvenable:jsr-303-validation-709
Jan 5, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
...rc/main/java/com/amazon/dataprepper/model/plugin/InvalidPluginConfigurationException.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,18 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.amazon.dataprepper.model.plugin; | ||
|
||
/** | ||
* This exception is thrown when a plugin in configured with an invalid | ||
* configuration. | ||
* | ||
* @since 1.3 | ||
*/ | ||
public class InvalidPluginConfigurationException extends RuntimeException { | ||
public InvalidPluginConfigurationException(final String message) { | ||
super(message); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...est/java/com/amazon/dataprepper/model/plugin/InvalidPluginConfigurationExceptionTest.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,33 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.amazon.dataprepper.model.plugin; | ||
|
||
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.MatcherAssert.assertThat; | ||
|
||
class InvalidPluginConfigurationExceptionTest { | ||
private String message; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
message = UUID.randomUUID().toString(); | ||
} | ||
|
||
private InvalidPluginConfigurationException createObjectUnderTest() { | ||
return new InvalidPluginConfigurationException(message); | ||
} | ||
|
||
@Test | ||
void getMessage_returns_message() { | ||
assertThat(createObjectUnderTest().getMessage(), | ||
equalTo(message)); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,26 +5,65 @@ | |
|
||
package com.amazon.dataprepper.plugin; | ||
|
||
import com.amazon.dataprepper.model.annotations.DataPrepperPlugin; | ||
import com.amazon.dataprepper.model.configuration.PluginSetting; | ||
import com.amazon.dataprepper.model.plugin.InvalidPluginConfigurationException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import jakarta.validation.ConstraintViolation; | ||
import jakarta.validation.Validation; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How is the validation import used ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line 33:
|
||
import jakarta.validation.Validator; | ||
import jakarta.validation.ValidatorFactory; | ||
|
||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Converts and validates a plugin configuration. This class is responsible for taking a {@link PluginSetting} | ||
* and converting it to the plugin model type which should be denoted by {@link DataPrepperPlugin#pluginConfigurationType()} | ||
*/ | ||
class PluginConfigurationConverter { | ||
private final ObjectMapper objectMapper; | ||
private final Validator validator; | ||
|
||
PluginConfigurationConverter() { | ||
this.objectMapper = new ObjectMapper().setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); | ||
|
||
final ValidatorFactory validationFactory = Validation.buildDefaultValidatorFactory(); | ||
validator = validationFactory.getValidator(); | ||
} | ||
|
||
/** | ||
* Converts and validates to a plugin model type. The conversion happens via | ||
* Java Bean Validation 2.0. | ||
* | ||
* @param pluginConfigurationType the destination type | ||
* @param pluginSetting The source {@link PluginSetting} | ||
* @return The converted object of type pluginConfigurationType | ||
* @throws InvalidPluginConfigurationException - If the plugin configuration is invalid | ||
*/ | ||
public Object convert(final Class<?> pluginConfigurationType, final PluginSetting pluginSetting) { | ||
Objects.requireNonNull(pluginConfigurationType); | ||
Objects.requireNonNull(pluginSetting); | ||
|
||
if(pluginConfigurationType.equals(PluginSetting.class)) | ||
return pluginSetting; | ||
|
||
return objectMapper.convertValue(pluginSetting.getSettings(), pluginConfigurationType); | ||
final Object configuration = objectMapper.convertValue(pluginSetting.getSettings(), pluginConfigurationType); | ||
|
||
final Set<ConstraintViolation<Object>> constraintViolations = validator.validate(configuration); | ||
|
||
if(!constraintViolations.isEmpty()) { | ||
final String violationsString = constraintViolations.stream() | ||
.map(v -> v.getPropertyPath().toString() + " " + v.getMessage()) | ||
.collect(Collectors.joining(". ")); | ||
|
||
final String exceptionMessage = String.format("Plugin %s in pipeline %s is configured incorrectly: %s", | ||
pluginSetting.getName(), pluginSetting.getPipelineName(), violationsString); | ||
throw new InvalidPluginConfigurationException(exceptionMessage); | ||
} | ||
|
||
return configuration; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
data-prepper-core/src/test/java/com/amazon/dataprepper/plugin/DefaultPluginFactoryIT.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,88 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.amazon.dataprepper.plugin; | ||
|
||
import com.amazon.dataprepper.model.configuration.PluginSetting; | ||
import com.amazon.dataprepper.model.plugin.InvalidPluginConfigurationException; | ||
import com.amazon.dataprepper.plugins.TestPlugin; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
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.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
/** | ||
* Integration test of the plugin framework. These tests should not mock any portion | ||
* of the plugin framework. But, they may mock inputs when appropriate. | ||
*/ | ||
class DefaultPluginFactoryIT { | ||
|
||
private String pluginName; | ||
private String pipelineName; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
pluginName = "test_plugin"; | ||
pipelineName = UUID.randomUUID().toString(); | ||
} | ||
|
||
private DefaultPluginFactory createObjectUnderTest() { | ||
return new DefaultPluginFactory(); | ||
} | ||
|
||
@Test | ||
void loadPlugin_should_return_a_new_plugin_instance_with_the_expected_configuration() { | ||
|
||
final String requiredStringValue = UUID.randomUUID().toString(); | ||
final String optionalStringValue = UUID.randomUUID().toString(); | ||
|
||
final Map<String, Object> pluginSettingMap = new HashMap<>(); | ||
pluginSettingMap.put("required_string", requiredStringValue); | ||
pluginSettingMap.put("optional_string", optionalStringValue); | ||
final PluginSetting pluginSetting = createPluginSettings(pluginSettingMap); | ||
|
||
final TestPluggableInterface plugin = createObjectUnderTest().loadPlugin(TestPluggableInterface.class, pluginSetting); | ||
|
||
assertThat(plugin, instanceOf(TestPlugin.class)); | ||
|
||
final TestPlugin testPlugin = (TestPlugin) plugin; | ||
|
||
final TestPluginConfiguration configuration = testPlugin.getConfiguration(); | ||
|
||
assertThat(configuration.getRequiredString(), equalTo(requiredStringValue)); | ||
assertThat(configuration.getOptionalString(), equalTo(optionalStringValue)); | ||
} | ||
|
||
@Test | ||
void loadPlugin_should_throw_when_a_plugin_configuration_is_invalid() { | ||
final String optionalStringValue = UUID.randomUUID().toString(); | ||
|
||
final Map<String, Object> pluginSettingMap = new HashMap<>(); | ||
pluginSettingMap.put("optional_string", optionalStringValue); | ||
final PluginSetting pluginSetting = createPluginSettings(pluginSettingMap); | ||
|
||
final DefaultPluginFactory objectUnderTest = createObjectUnderTest(); | ||
|
||
final InvalidPluginConfigurationException actualException = assertThrows(InvalidPluginConfigurationException.class, | ||
() -> objectUnderTest.loadPlugin(TestPluggableInterface.class, pluginSetting)); | ||
|
||
assertThat(actualException.getMessage(), notNullValue()); | ||
assertThat(actualException.getMessage(), equalTo("Plugin test_plugin in pipeline " + pipelineName + " is configured incorrectly: requiredString must not be null")); | ||
} | ||
|
||
private PluginSetting createPluginSettings(final Map<String, Object> pluginSettingMap) { | ||
final PluginSetting pluginSetting = new PluginSetting(pluginName, pluginSettingMap); | ||
pluginSetting.setPipelineName(pipelineName); | ||
return pluginSetting; | ||
} | ||
} |
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
9 changes: 9 additions & 0 deletions
9
data-prepper-core/src/test/java/com/amazon/dataprepper/plugin/TestPluggableInterface.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,9 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.amazon.dataprepper.plugin; | ||
|
||
public interface TestPluggableInterface { | ||
} |
31 changes: 31 additions & 0 deletions
31
data-prepper-core/src/test/java/com/amazon/dataprepper/plugin/TestPluginConfiguration.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,31 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.amazon.dataprepper.plugin; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
|
||
public class TestPluginConfiguration { | ||
@NotNull | ||
private String requiredString; | ||
|
||
private String optionalString; | ||
|
||
public String getRequiredString() { | ||
return requiredString; | ||
} | ||
|
||
public void setRequiredString(final String requiredString) { | ||
this.requiredString = requiredString; | ||
} | ||
|
||
public String getOptionalString() { | ||
return optionalString; | ||
} | ||
|
||
public void setOptionalString(final String optionalString) { | ||
this.optionalString = optionalString; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious since we were talking about these dependencies recently. Originally you had thought that the Glassfish dependency was unnecessary because we just needed the built in JSR-303 annotations. What was your finding exactly that showed a need for the glassfish dependency?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great question. It turns out that the documentation states that we need these dependencies since we are not running in an environment which includes them. Indeed, it won't run without this.