ConfigValue passwordValue = results.get(PostgresConnectorConfig.PASSWORD.name()); if (passwordValue.value() == null || ((String)passwordValue.value()).isEmpty()) { logger.warn("The connection password is empty"); if (hostnameValue.errorMessages().isEmpty() && portValue.errorMessages().isEmpty() && userValue.errorMessages().isEmpty() && passwordValue.errorMessages().isEmpty() && databaseValue.errorMessages().isEmpty()) { logger.info("Failed testing connection for {} with user '{}'", connection.connectionString(), connection.username()); hostnameValue.addErrorMessage("Unable to connect: " + e.getMessage());
@Test public void testCanAddInternalConfig() throws Exception { final String configName = "internal.config"; final ConfigDef configDef = new ConfigDef().defineInternal(configName, Type.STRING, "", Importance.LOW); final HashMap<String, String> properties = new HashMap<>(); properties.put(configName, "value"); final List<ConfigValue> results = configDef.validate(properties); final ConfigValue configValue = results.get(0); assertEquals("value", configValue.value()); assertEquals(configName, configValue.name()); }
public Map<String, ConfigValue> validateAll(Map<String, String> props) { Map<String, ConfigValue> configValues = new HashMap<>(); for (String name: configKeys.keySet()) { configValues.put(name, new ConfigValue(name)); } List<String> undefinedConfigKeys = undefinedDependentConfigs(); for (String undefinedConfigKey: undefinedConfigKeys) { ConfigValue undefinedConfigValue = new ConfigValue(undefinedConfigKey); undefinedConfigValue.addErrorMessage(undefinedConfigKey + " is referred in the dependents, but not defined."); undefinedConfigValue.visible(false); configValues.put(undefinedConfigKey, undefinedConfigValue); } Map<String, Object> parsed = parseForValidate(props, configValues); return validate(parsed, configValues); }
private void validate(String name, Map<String, Object> parsed, Map<String, ConfigValue> configs) { if (!configKeys.containsKey(name)) { return; } ConfigKey key = configKeys.get(name); ConfigValue value = configs.get(name); if (key.recommender != null) { try { List<Object> recommendedValues = key.recommender.validValues(name, parsed); List<Object> originalRecommendedValues = value.recommendedValues(); if (!originalRecommendedValues.isEmpty()) { Set<Object> originalRecommendedValueSet = new HashSet<>(originalRecommendedValues); recommendedValues.removeIf(o -> !originalRecommendedValueSet.contains(o)); } value.recommendedValues(recommendedValues); value.visible(key.recommender.visible(name, parsed)); } catch (ConfigException e) { value.addErrorMessage(e.getMessage()); } } configs.put(name, value); for (String dependent: key.dependents) { validate(dependent, parsed, configs); } }
value = parseType(key.name, props.get(key.name), key.type); } catch (ConfigException e) { config.addErrorMessage(e.getMessage()); config.addErrorMessage("Missing required configuration \"" + key.name + "\" which has no default value."); } else { value = key.defaultValue; key.validator.ensureValid(key.name, value); } catch (ConfigException e) { config.addErrorMessage(e.getMessage()); config.value(value); parsed.put(name, value); for (String dependent: key.dependents) {
ConfigValue value = results.computeIfAbsent(this.name(), n -> new ConfigValue(n)); value.addErrorMessage(problem); }); try { value.visible(recommender.visible(this, config)); List<Object> previousRecommendations = value.recommendedValues(); if (!previousRecommendations.isEmpty()) { value.recommendedValues(newRecommendations); } catch (ConfigException e) { value.addErrorMessage(e.getMessage());
private static ConfigValueInfo convertConfigValue(ConfigValue configValue, Type type) { String value = ConfigDef.convertToString(configValue.value(), type); List<String> recommendedValues = new LinkedList<>(); if (type == Type.LIST) { for (Object object: configValue.recommendedValues()) { recommendedValues.add(ConfigDef.convertToString(object, Type.STRING)); } } else { for (Object object : configValue.recommendedValues()) { recommendedValues.add(ConfigDef.convertToString(object, type)); } } return new ConfigValueInfo(configValue.name(), value, recommendedValues, configValue.errorMessages(), configValue.visible()); }
private <T> void validateField(Config config, Field field, T expectedValue) { assertNoConfigurationErrors(config, field); Object actualValue = configValue(config, field.name()).value(); if (actualValue == null) { actualValue = field.defaultValue(); } if (expectedValue == null) { assertThat(actualValue).isNull(); } else { if (expectedValue instanceof EnumeratedValue) { assertThat(((EnumeratedValue) expectedValue).getValue()).isEqualTo(actualValue.toString()); } else { assertThat(expectedValue).isEqualTo(actualValue); } } }
public static ConfigInfos generateResult(String connType, Map<String, ConfigKey> configKeys, List<ConfigValue> configValues, List<String> groups) { int errorCount = 0; List<ConfigInfo> configInfoList = new LinkedList<>(); Map<String, ConfigValue> configValueMap = new HashMap<>(); for (ConfigValue configValue: configValues) { String configName = configValue.name(); configValueMap.put(configName, configValue); if (!configKeys.containsKey(configName)) { configValue.addErrorMessage("Configuration is not defined: " + configName); configInfoList.add(new ConfigInfo(null, convertConfigValue(configValue, null))); } } for (Map.Entry<String, ConfigKey> entry : configKeys.entrySet()) { String configName = entry.getKey(); ConfigKeyInfo configKeyInfo = convertConfigKey(entry.getValue()); Type type = entry.getValue().type; ConfigValueInfo configValueInfo = null; if (configValueMap.containsKey(configName)) { ConfigValue configValue = configValueMap.get(configName); configValueInfo = convertConfigValue(configValue, type); errorCount += configValue.errorMessages().size(); } configInfoList.add(new ConfigInfo(configKeyInfo, configValueInfo)); } return new ConfigInfos(connType, errorCount, groups, configInfoList); }
@Override public Config validate(Map<String, String> connectorConfigs) { Configuration config = Configuration.from(connectorConfigs); // First, validate all of the individual fields, which is easy since don't make any of the fields invisible ... Map<String, ConfigValue> results = config.validate(MongoDbConnectorConfig.EXPOSED_FIELDS); // Get the config values for each of the connection-related fields ... ConfigValue hostsValue = results.get(MongoDbConnectorConfig.HOSTS.name()); ConfigValue userValue = results.get(MongoDbConnectorConfig.USER.name()); ConfigValue passwordValue = results.get(MongoDbConnectorConfig.PASSWORD.name()); // If there are no errors on any of these ... if (hostsValue.errorMessages().isEmpty() && userValue.errorMessages().isEmpty() && passwordValue.errorMessages().isEmpty()) { // Try to connect to the database ... try (ConnectionContext connContext = new ConnectionContext(config)) { try ( MongoClient client = connContext.clientFor(connContext.hosts()) ) { client.listDatabaseNames(); } } catch (MongoException e) { hostsValue.addErrorMessage("Unable to connect: " + e.getMessage()); } } return new Config(new ArrayList<>(results.values())); } }
protected void assertNoConfigurationErrors(Config config, io.debezium.config.Field... fields) { for (io.debezium.config.Field field : fields) { ConfigValue value = configValue(config, field.name()); if (value != null) { if (!value.errorMessages().isEmpty()) { fail("Error messages on field '" + field.name() + "': " + value.errorMessages()); } } } }
@Test public void shouldValidateMinimalConfiguration() throws Exception { Configuration config = TestHelper.defaultConfig().build(); Config validateConfig = new PostgresConnector().validate(config.asMap()); validateConfig.configValues().forEach(configValue -> assertTrue("Unexpected error for: " + configValue.name(), configValue.errorMessages().isEmpty())); }
@Test public void testValidateMissingConfigKey() { Map<String, ConfigValue> expected = new HashMap<>(); String errorMessageB = "Missing required configuration \"b\" which has no default value."; String errorMessageC = "Missing required configuration \"c\" which has no default value."; String errorMessageD = "d is referred in the dependents, but not defined."; ConfigValue configA = new ConfigValue("a", 1, Arrays.<Object>asList(1, 2, 3), Collections.<String>emptyList()); ConfigValue configB = new ConfigValue("b", null, Arrays.<Object>asList(4, 5), Arrays.asList(errorMessageB)); ConfigValue configC = new ConfigValue("c", null, Arrays.<Object>asList(4, 5), Arrays.asList(errorMessageC)); ConfigValue configD = new ConfigValue("d", null, Collections.emptyList(), Arrays.asList(errorMessageD)); configD.visible(false); expected.put("a", configA); expected.put("b", configB); expected.put("c", configC); expected.put("d", configD); ConfigDef def = new ConfigDef() .define("a", Type.INT, Importance.HIGH, "docs", "group", 1, Width.SHORT, "a", Arrays.asList("b", "c", "d"), new IntegerRecommender(false)) .define("b", Type.INT, Importance.HIGH, "docs", "group", 2, Width.SHORT, "b", new IntegerRecommender(true)) .define("c", Type.INT, Importance.HIGH, "docs", "group", 3, Width.SHORT, "c", new IntegerRecommender(true)); Map<String, String> props = new HashMap<>(); props.put("a", "1"); List<ConfigValue> configs = def.validate(props); for (ConfigValue config: configs) { String name = config.name(); ConfigValue expectedConfig = expected.get(name); assertEquals(expectedConfig, config); } }
validator.accept(config); } catch (Exception ex) { result.get(validator.name).addErrorMessage(ex.getMessage());
@Test public void testValidateCannotParse() { Map<String, ConfigValue> expected = new HashMap<>(); String errorMessageB = "Invalid value non_integer for configuration a: Not a number of type INT"; ConfigValue configA = new ConfigValue("a", null, Collections.emptyList(), Arrays.asList(errorMessageB)); expected.put("a", configA); ConfigDef def = new ConfigDef().define("a", Type.INT, Importance.HIGH, "docs"); Map<String, String> props = new HashMap<>(); props.put("a", "non_integer"); List<ConfigValue> configs = def.validate(props); for (ConfigValue config: configs) { String name = config.name(); ConfigValue expectedConfig = expected.get(name); assertEquals(expectedConfig, config); } }
protected ConfigValue configValue(Config config, String fieldName) { return config.configValues().stream().filter(value -> value.name().equals(fieldName)).findFirst().orElse(null); }
String errorMessageB = "Missing required configuration \"b\" which has no default value."; String errorMessageC = "Missing required configuration \"c\" which has no default value."; ConfigValue configA = new ConfigValue("a", 1, Collections.<Object>emptyList(), Collections.<String>emptyList()); ConfigValue configB = new ConfigValue("b", null, Collections.<Object>emptyList(), Arrays.asList(errorMessageB, errorMessageB)); ConfigValue configC = new ConfigValue("c", null, Collections.<Object>emptyList(), Arrays.asList(errorMessageC)); ConfigValue configD = new ConfigValue("d", 10, Collections.<Object>emptyList(), Collections.<String>emptyList()); expected.put("a", configA); expected.put("b", configB); configValues.put(name, new ConfigValue(name));
ConfigValue value = results.computeIfAbsent(this.name(), n -> new ConfigValue(n)); value.addErrorMessage(problem); }); try { value.visible(recommender.visible(this, config)); List<Object> previousRecommendations = value.recommendedValues(); if (!previousRecommendations.isEmpty()) { value.recommendedValues(newRecommendations); } catch (ConfigException e) { value.addErrorMessage(e.getMessage());
@Override public List<Map<String, String>> taskConfigs(int maxTasks) { Map<String, String> ret = configValues.entrySet().stream() .filter(a -> a.getValue().value() != null) .collect(Collectors.toMap(a -> a.getKey(), a -> a.getValue().value().toString())); ret.put(PROPERTY_TAGS, ((List<String>) configValues.get(PROPERTY_TAGS).value()).stream().collect(Collectors.joining(","))); return Collections.singletonList(ret); }
@Override protected Map<String, ConfigValue> validateBasicConnectorConfig(Connector connector, ConfigDef configDef, Map<String, String> config) { Map<String, ConfigValue> validatedConfig = super.validateBasicConnectorConfig(connector, configDef, config); if (connector instanceof SinkConnector) { ConfigValue validatedName = validatedConfig.get(ConnectorConfig.NAME_CONFIG); String name = (String) validatedName.value(); if (workerGroupId.equals(SinkUtils.consumerGroupId(name))) { validatedName.addErrorMessage("Consumer group for sink connector named " + name + " conflicts with Connect worker group " + workerGroupId); } } return validatedConfig; }