-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate plugin configuration objects using JSR-303/380 validation. H…
…ibernate Validator provides this validation, removing the need for bval. Signed-off-by: David Venable <[email protected]>
- Loading branch information
Showing
9 changed files
with
323 additions
and
4 deletions.
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
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.