Skip to content
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

Add alternate name for Plugin #4968

Merged
merged 3 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
public @interface DataPrepperPlugin {
String DEFAULT_DEPRECATED_NAME = "";

String DEFAULT_ALTERNATE_NAME = "";

/**
*
* @return Name of the plugin which should be unique for the type
Expand All @@ -46,6 +48,12 @@
*/
String deprecatedName() default DEFAULT_DEPRECATED_NAME;

/**
*
* @return Alternate name of the plugin which should be unique for the type
*/
String alternateName() default DEFAULT_ALTERNATE_NAME;
dinujoh marked this conversation as resolved.
Show resolved Hide resolved

/**
* The class type for this plugin.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Set;
import java.util.stream.Collectors;

import static org.opensearch.dataprepper.model.annotations.DataPrepperPlugin.DEFAULT_ALTERNATE_NAME;
import static org.opensearch.dataprepper.model.annotations.DataPrepperPlugin.DEFAULT_DEPRECATED_NAME;

/**
Expand Down Expand Up @@ -78,6 +79,7 @@ private Map<String, Map<Class<?>, Class<?>>> scanForPlugins() {
supportTypeToPluginTypeMap.put(supportedType, concretePluginClass);

addOptionalDeprecatedPluginName(pluginsMap, concretePluginClass);
addOptionalAlternatePluginName(pluginsMap, concretePluginClass);
}

return pluginsMap;
Expand All @@ -96,4 +98,18 @@ private void addOptionalDeprecatedPluginName(
supportTypeToPluginTypeMap.put(supportedType, concretePluginClass);
}
}

private void addOptionalAlternatePluginName(
dinujoh marked this conversation as resolved.
Show resolved Hide resolved
final Map<String, Map<Class<?>, Class<?>>> pluginsMap,
final Class<?> concretePluginClass) {
final DataPrepperPlugin dataPrepperPluginAnnotation = concretePluginClass.getAnnotation(DataPrepperPlugin.class);
final String alternatePluginName = dataPrepperPluginAnnotation.alternateName();
final Class<?> supportedType = dataPrepperPluginAnnotation.pluginType();

if (!alternatePluginName.equals(DEFAULT_ALTERNATE_NAME)) {
final Map<Class<?>, Class<?>> supportTypeToPluginTypeMap =
pluginsMap.computeIfAbsent(alternatePluginName, k -> new HashMap<>());
supportTypeToPluginTypeMap.put(supportedType, concretePluginClass);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
import static org.opensearch.dataprepper.model.annotations.DataPrepperPlugin.DEFAULT_ALTERNATE_NAME;
import static org.opensearch.dataprepper.model.annotations.DataPrepperPlugin.DEFAULT_DEPRECATED_NAME;

class ClasspathPluginProviderTest {
Expand Down Expand Up @@ -105,6 +106,27 @@ void findPlugin_should_return_plugin_if_found_for_deprecated_name_and_type_using
assertThat(optionalPlugin.get(), equalTo(TestSource.class));
}

@Test
void findPlugin_should_return_empty_for_default_alternate_name() {
given(reflections.getTypesAnnotatedWith(DataPrepperPlugin.class))
.willReturn(new HashSet<>(List.of(TestSource.class)));

final Optional<Class<? extends Source>> optionalPlugin = createObjectUnderTest().findPluginClass(Source.class, DEFAULT_ALTERNATE_NAME);
assertThat(optionalPlugin, notNullValue());
assertThat(optionalPlugin.isPresent(), equalTo(false));
}

@Test
void findPlugin_should_return_plugin_if_found_for_alternate_name_and_type_using_pluginType() {
given(reflections.getTypesAnnotatedWith(DataPrepperPlugin.class))
.willReturn(new HashSet<>(List.of(TestSource.class)));

final Optional<Class<? extends Source>> optionalPlugin = createObjectUnderTest().findPluginClass(Source.class, "test_source_alternate_name");
assertThat(optionalPlugin, notNullValue());
assertThat(optionalPlugin.isPresent(), equalTo(true));
assertThat(optionalPlugin.get(), equalTo(TestSource.class));
}

@Nested
class WithPredefinedPlugins {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,33 @@ void loadPlugin_should_create_a_new_instance_of_the_first_plugin_found_with_corr
verify(beanFactoryProvider).get();
}
}

@Nested
class WithAlternatePluginName {
private static final String TEST_SINK_ALTERNATE_NAME = "test_sink_alternate_name";
private Class expectedPluginClass;

@BeforeEach
void setUp() {
expectedPluginClass = TestSink.class;
given(pluginSetting.getName()).willReturn(TEST_SINK_ALTERNATE_NAME);

given(firstPluginProvider.findPluginClass(baseClass, TEST_SINK_ALTERNATE_NAME))
.willReturn(Optional.of(expectedPluginClass));
}

@Test
void loadPlugin_should_create_a_new_instance_of_the_first_plugin_found_with_correct_name_and_alternate_name() {
final TestSink expectedInstance = mock(TestSink.class);
final Object convertedConfiguration = mock(Object.class);
given(pluginConfigurationConverter.convert(PluginSetting.class, pluginSetting))
.willReturn(convertedConfiguration);
given(pluginCreator.newPluginInstance(eq(expectedPluginClass), any(ComponentPluginArgumentsContext.class), eq(TEST_SINK_ALTERNATE_NAME)))
.willReturn(expectedInstance);

assertThat(createObjectUnderTest().loadPlugin(baseClass, pluginSetting), equalTo(expectedInstance));
MatcherAssert.assertThat(expectedInstance.getClass().getAnnotation(DataPrepperPlugin.class).alternateName(), equalTo(TEST_SINK_ALTERNATE_NAME));
verify(beanFactoryProvider).get();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.util.stream.Collectors;
import java.time.Instant;

@DataPrepperPlugin(name = "test_sink", deprecatedName = "test_sink_deprecated_name", pluginType = Sink.class)
@DataPrepperPlugin(name = "test_sink", alternateName = "test_sink_alternate_name", deprecatedName = "test_sink_deprecated_name", pluginType = Sink.class)
public class TestSink implements Sink<Record<String>> {
public boolean isShutdown = false;
private final List<Record<String>> collectedRecords;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

@DataPrepperPlugin(name = "test_source", deprecatedName = "test_source_deprecated_name", pluginType = Source.class)
@DataPrepperPlugin(name = "test_source", alternateName = "test_source_alternate_name", deprecatedName = "test_source_deprecated_name", pluginType = Source.class)
public class TestSource implements Source<Record<String>> {
public static final List<Record<String>> TEST_DATA = Stream.of("TEST")
.map(Record::new).collect(Collectors.toList());
Expand Down
Loading