use of org.apache.kafka.common.config.ConfigDef in project kafka by apache.
the class ConnectorPluginsResourceTest method testValidateConfigWithSingleErrorDueToMissingConnectorClassname.
@Test
public void testValidateConfigWithSingleErrorDueToMissingConnectorClassname() throws Throwable {
Capture<Callback<ConfigInfos>> configInfosCallback = EasyMock.newCapture();
herder.validateConnectorConfig(EasyMock.eq(partialProps), EasyMock.capture(configInfosCallback), EasyMock.anyBoolean());
PowerMock.expectLastCall().andAnswer((IAnswer<Void>) () -> {
ConfigDef connectorConfigDef = ConnectorConfig.configDef();
List<ConfigValue> connectorConfigValues = connectorConfigDef.validate(partialProps);
Connector connector = new ConnectorPluginsResourceTestConnector();
Config config = connector.validate(partialProps);
ConfigDef configDef = connector.config();
Map<String, ConfigDef.ConfigKey> configKeys = configDef.configKeys();
List<ConfigValue> configValues = config.configValues();
Map<String, ConfigDef.ConfigKey> resultConfigKeys = new HashMap<>(configKeys);
resultConfigKeys.putAll(connectorConfigDef.configKeys());
configValues.addAll(connectorConfigValues);
ConfigInfos configInfos = AbstractHerder.generateResult(ConnectorPluginsResourceTestConnector.class.getName(), resultConfigKeys, configValues, Collections.singletonList("Test"));
configInfosCallback.getValue().onCompletion(null, configInfos);
return null;
});
PowerMock.replayAll();
// This call to validateConfigs does not throw a BadRequestException because we've mocked
// validateConnectorConfig.
ConfigInfos configInfos = connectorPluginsResource.validateConfigs(ConnectorPluginsResourceTestConnector.class.getSimpleName(), partialProps);
assertEquals(PARTIAL_CONFIG_INFOS.name(), configInfos.name());
assertEquals(PARTIAL_CONFIG_INFOS.errorCount(), configInfos.errorCount());
assertEquals(PARTIAL_CONFIG_INFOS.groups(), configInfos.groups());
assertEquals(new HashSet<>(PARTIAL_CONFIG_INFOS.values()), new HashSet<>(configInfos.values()));
PowerMock.verifyAll();
}
use of org.apache.kafka.common.config.ConfigDef in project kafka by apache.
the class StandaloneHerderTest method testCreateConnectorFailedValidation.
@Test
public void testCreateConnectorFailedValidation() {
// Basic validation should be performed and return an error, but should still evaluate the connector's config
connector = PowerMock.createMock(BogusSourceConnector.class);
Map<String, String> config = connectorConfig(SourceSink.SOURCE);
config.remove(ConnectorConfig.NAME_CONFIG);
Connector connectorMock = PowerMock.createMock(SourceConnector.class);
EasyMock.expect(worker.configTransformer()).andReturn(transformer).times(2);
final Capture<Map<String, String>> configCapture = EasyMock.newCapture();
EasyMock.expect(transformer.transform(EasyMock.capture(configCapture))).andAnswer(configCapture::getValue);
EasyMock.expect(worker.getPlugins()).andReturn(plugins).times(3);
EasyMock.expect(plugins.compareAndSwapLoaders(connectorMock)).andReturn(delegatingLoader);
EasyMock.expect(plugins.newConnector(EasyMock.anyString())).andReturn(connectorMock);
EasyMock.expect(connectorMock.config()).andStubReturn(new ConfigDef());
ConfigValue validatedValue = new ConfigValue("foo.bar");
EasyMock.expect(connectorMock.validate(config)).andReturn(new Config(singletonList(validatedValue)));
EasyMock.expect(Plugins.compareAndSwapLoaders(delegatingLoader)).andReturn(pluginLoader);
PowerMock.replayAll();
herder.putConnectorConfig(CONNECTOR_NAME, config, false, createCallback);
ExecutionException exception = assertThrows(ExecutionException.class, () -> createCallback.get(1000L, TimeUnit.SECONDS));
assertEquals(BadRequestException.class, exception.getCause().getClass());
PowerMock.verifyAll();
}
use of org.apache.kafka.common.config.ConfigDef in project connect-utils by jcustenborder.
the class PluginLoader method config.
Plugin.Configuration config(Class<?> pluginClass) {
ConfigDef config = configDef(pluginClass);
if (null == config) {
return null;
}
ImmutableConfiguration.Builder configBuilder = ImmutableConfiguration.builder().configDef(config);
Map<String, ImmutableGroup.Builder> groupBuilderCache = new LinkedHashMap<>();
List<ConfigDef.ConfigKey> configKeys = config.configKeys().values().stream().sorted(ConfigKeyComparator.INSTANCE).collect(Collectors.toList());
for (ConfigDef.ConfigKey configKey : configKeys) {
final String group = Strings.isNullOrEmpty(configKey.group) ? GENERAL_GROUP : configKey.group;
Plugin.Item item = ImmutableItem.builder().defaultValue(ConfigDef.NO_DEFAULT_VALUE.equals(configKey.defaultValue) ? null : configKey.defaultValue).doc(configKey.documentation).group(group).importance(configKey.importance).isRequired(!configKey.hasDefault()).name(configKey.name).type(configKey.type).validator(configKey.validator).build();
ImmutableGroup.Builder groupBuilder = groupBuilderCache.computeIfAbsent(group, s -> ImmutableGroup.builder().name(group));
groupBuilder.addItems(item);
if (item.isRequired()) {
configBuilder.addRequiredConfigs(item);
}
}
groupBuilderCache.values().stream().map(ImmutableGroup.Builder::build).forEach(configBuilder::addGroups);
return configBuilder.build();
}
use of org.apache.kafka.common.config.ConfigDef in project connect-utils by jcustenborder.
the class PluginLoader method configDef.
// ConfigDef transformationConfig(Class<? extends Transformation> transformation) {
// try {
// return transformation.newInstance().config();
// } catch (InstantiationException | IllegalAccessException e) {
// throw new IllegalStateException(e);
// }
// }
//
// ConfigDef connectorConfig(Class<? extends Connector> connectorClass) {
// try {
// return connectorClass.newInstance().config();
// } catch (InstantiationException | IllegalAccessException e) {
// throw new IllegalStateException(e);
// }
// }
ConfigDef configDef(Class<?> pluginClass) {
ConfigDef result;
try {
if (Connector.class.isAssignableFrom(pluginClass)) {
Connector connector = (Connector) pluginClass.newInstance();
result = connector.config();
} else if (Transformation.class.isAssignableFrom(pluginClass)) {
Transformation<?> connector = (Transformation<?>) pluginClass.newInstance();
result = connector.config();
} else if (Converter.class.isAssignableFrom(pluginClass) || ConfigProvider.class.isAssignableFrom(pluginClass)) {
Optional<Method> optionalConfigMethod = Stream.of(pluginClass.getMethods()).filter(method -> Modifier.isPublic(method.getModifiers())).filter(method -> ConfigDef.class.equals(method.getReturnType())).findFirst();
if (optionalConfigMethod.isPresent()) {
Method configMethod = optionalConfigMethod.get();
Object instance;
if (Modifier.isStatic(configMethod.getModifiers())) {
instance = null;
} else {
instance = pluginClass.newInstance();
}
result = (ConfigDef) configMethod.invoke(instance);
} else {
result = null;
}
} else {
throw new UnsupportedOperationException(String.format("Type %s is not supported", pluginClass.getName()));
}
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new IllegalStateException(e);
}
return result;
}
use of org.apache.kafka.common.config.ConfigDef in project kafka by apache.
the class AbstractHerder method validateConnectorConfig.
@Override
public ConfigInfos validateConnectorConfig(Map<String, String> connectorConfig) {
String connType = connectorConfig.get(ConnectorConfig.CONNECTOR_CLASS_CONFIG);
if (connType == null)
throw new BadRequestException("Connector config " + connectorConfig + " contains no connector type");
Connector connector = getConnector(connType);
final ConfigDef connectorConfigDef = ConnectorConfig.enrich((connector instanceof SourceConnector) ? SourceConnectorConfig.configDef() : SinkConnectorConfig.configDef(), connectorConfig, false);
List<ConfigValue> configValues = new ArrayList<>();
Map<String, ConfigKey> configKeys = new HashMap<>();
List<String> allGroups = new ArrayList<>();
// do basic connector validation (name, connector type, etc.)
Map<String, ConfigValue> validatedConnectorConfig = validateBasicConnectorConfig(connector, connectorConfigDef, connectorConfig);
configValues.addAll(validatedConnectorConfig.values());
configKeys.putAll(connectorConfigDef.configKeys());
allGroups.addAll(connectorConfigDef.groups());
// do custom connector-specific validation
Config config = connector.validate(connectorConfig);
ConfigDef configDef = connector.config();
configKeys.putAll(configDef.configKeys());
allGroups.addAll(configDef.groups());
configValues.addAll(config.configValues());
return generateResult(connType, configKeys, configValues, allGroups);
}
Aggregations