-
Notifications
You must be signed in to change notification settings - Fork 62
[FEATURE] Refactor ExtensionsRunner test code out of production source tree #171
Description
Is your feature request related to a problem?
We shouldn't have exclusively test-related code in the main source tree, e.g.,
opensearch-sdk-java/src/main/java/org/opensearch/sdk/ExtensionsRunner.java
Lines 117 to 123 in 8fbcc6e
| /** | |
| * Instantiates a new Extensions Runner using test settings. | |
| * | |
| * @throws IOException if the runner failed to read settings or API. | |
| */ | |
| public ExtensionsRunner() throws IOException { | |
| ExtensionSettings extensionSettings = readExtensionSettings(); |
which calls here, only by tests:
opensearch-sdk-java/src/main/java/org/opensearch/sdk/ExtensionsRunner.java
Lines 159 to 163 in 8fbcc6e
| private static ExtensionSettings readExtensionSettings() throws IOException { | |
| File file = new File(ExtensionSettings.EXTENSION_DESCRIPTOR); | |
| ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); | |
| return objectMapper.readValue(file, ExtensionSettings.class); | |
| } |
which uses this constant pointing to the test source tree:
opensearch-sdk-java/src/main/java/org/opensearch/sdk/ExtensionSettings.java
Lines 19 to 22 in 8fbcc6e
| /** | |
| * Path to extension.yml file for testing. Internal use only. | |
| */ | |
| static final String EXTENSION_DESCRIPTOR = "src/test/resources/extension.yml"; |
What solution would you like?
- Remove the no-arg constructor above from
ExtensionsRunner - Make the constructor with an
Extensionargument either package-private (probably) or protected (maybe) so it's accessible by tests - Create a new subclass of
ExtensionsRunnerin thesrc/test/javatree to be used as anExtensionsRunnerobject when needed for tests, such as the linked comment in the context section below. Move the above code snippets there with modifications as noted below. - Update all tests which use the existing no-arg constructor to use the new class.
One would think TestExtensionsRunner would be a good name but it's already in use for our test class. Picking a new name will probably be the hardest part. It can extend ExtensionsRunner like so:
public class ExtensionsRunnerForTesting extends ExtensionsRunner {
public ExtensionsRunnerForTesting() throws IOException {
super(new Extension () {
@Override
public ExtensionSettings getExtensionSettings() {
// Move the result of current readExtensionSettings() method of ExtensionsRunner here
// except get rid of yaml file reading, and just instantiate the object with the values in the yaml
return new ExtensionSettings( ... );
}
@Override
public List<ExtensionRestHandler> getExtensionRestHandlers() {
return Collections.emptyList();
}
});
}
}What alternatives have you considered?
We could use the "sample" extensions for this purpose, or even create a test extension in the sample branch. This seems reasonable for an IT-based approach, but I think this approach is better for unit testing requiring an object with minimal settings.
Do you have any additional context?
I wonder if there's value in creating a "Test" version of the ExtensionsRunner. The no-arg constructor here is used exclusively for testing and I'd like to see it removed and replaced with some TestExtensionsRunner class in the test suite.
Originally posted by @dbwiddis in #170 (comment)