-
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
Initial support for plugin configuration classes #478
Merged
dlvenable
merged 1 commit into
opensearch-project:main
from
dlvenable:plugin-configuration-classes
Oct 28, 2021
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
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
25 changes: 25 additions & 0 deletions
25
...repper-core/src/main/java/com/amazon/dataprepper/plugin/PluginConfigurationConverter.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,25 @@ | ||
package com.amazon.dataprepper.plugin; | ||
|
||
import com.amazon.dataprepper.model.configuration.PluginSetting; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
|
||
import java.util.Objects; | ||
|
||
class PluginConfigurationConverter { | ||
private final ObjectMapper objectMapper; | ||
|
||
PluginConfigurationConverter() { | ||
this.objectMapper = new ObjectMapper().setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); | ||
} | ||
|
||
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); | ||
Comment on lines
+20
to
+23
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. A nit on my part, but I'd like to see this as an if / else block. |
||
} | ||
} |
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
81 changes: 81 additions & 0 deletions
81
...er-core/src/test/java/com/amazon/dataprepper/plugin/PluginConfigurationConverterTest.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,81 @@ | ||
package com.amazon.dataprepper.plugin; | ||
|
||
import com.amazon.dataprepper.model.configuration.PluginSetting; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Collections; | ||
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.CoreMatchers.sameInstance; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.BDDMockito.then; | ||
import static org.mockito.Mockito.mock; | ||
|
||
class PluginConfigurationConverterTest { | ||
private PluginSetting pluginSetting; | ||
|
||
static class TestConfiguration { | ||
private String myValue; | ||
|
||
public String getMyValue() { | ||
return myValue; | ||
} | ||
} | ||
|
||
@BeforeEach | ||
void setUp() { | ||
pluginSetting = mock(PluginSetting.class); | ||
} | ||
|
||
private PluginConfigurationConverter createObjectUnderTest() { | ||
return new PluginConfigurationConverter(); | ||
} | ||
|
||
@Test | ||
void convert_with_null_configurationType_should_throw() { | ||
final PluginConfigurationConverter objectUnderTest = createObjectUnderTest(); | ||
|
||
assertThrows(NullPointerException.class, | ||
() -> objectUnderTest.convert(null, pluginSetting)); | ||
} | ||
|
||
@Test | ||
void convert_with_null_pluginSetting_should_throw() { | ||
final PluginConfigurationConverter objectUnderTest = createObjectUnderTest(); | ||
|
||
assertThrows(NullPointerException.class, | ||
() -> objectUnderTest.convert(PluginSetting.class, null)); | ||
} | ||
|
||
@Test | ||
void convert_with_PluginSetting_target_should_return_pluginSetting_object_directly() { | ||
assertThat(createObjectUnderTest().convert(PluginSetting.class, pluginSetting), | ||
sameInstance(pluginSetting)); | ||
|
||
then(pluginSetting).shouldHaveNoInteractions(); | ||
} | ||
|
||
@Test | ||
void convert_with_other_target_should_return_pluginSetting_object_directly() { | ||
|
||
final String value = UUID.randomUUID().toString(); | ||
given(pluginSetting.getSettings()) | ||
.willReturn(Collections.singletonMap("my_value", value)); | ||
|
||
final Object convertedConfiguration = createObjectUnderTest().convert(TestConfiguration.class, pluginSetting); | ||
|
||
assertThat(convertedConfiguration, notNullValue()); | ||
assertThat(convertedConfiguration, instanceOf(TestConfiguration.class)); | ||
|
||
final TestConfiguration convertedTestConfiguration = (TestConfiguration) convertedConfiguration; | ||
|
||
assertThat(convertedTestConfiguration.getMyValue(), equalTo(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
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.
Really looking forward to getting rid of
Object
as a type that holds configuration...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.
I believe in this case we need to stick with
Object
. The type is determined at runtime with this change. There might be an approach to making this work with generics, but I suspect it would be overly complicated without providing any value.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.
Sad 😢 , we can leave it as is for right now, but perhaps the work that Shivani is doing with the configuration models can help this.
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.
We could move away from
Object
by using inheritence and providing a base class with common configuration attirbutes like the plugin metrics. But I think you were thinking of keeping these objects separate have your considered combining?