private Map<String, ConfigValue> validate(Map<String, Object> parsed, Map<String, ConfigValue> configValues) { Set<String> configsWithNoParent = getConfigsWithNoParent(); for (String name: configsWithNoParent) { validate(name, parsed, configValues); } return configValues; }
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); } }
@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); } }
@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()); }
@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); } }
@Test public void testValidate() { 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."; 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, errorMessageB)); ConfigValue configC = new ConfigValue("c", null, Arrays.<Object>asList(4, 5), Arrays.asList(errorMessageC)); ConfigValue configD = new ConfigValue("d", 10, Arrays.<Object>asList(1, 2, 3), Collections.<String>emptyList()); 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"), 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)) .define("d", Type.INT, Importance.HIGH, "docs", "group", 4, Width.SHORT, "d", Arrays.asList("b"), new IntegerRecommender(false)); Map<String, String> props = new HashMap<>(); props.put("a", "1"); props.put("d", "10"); List<ConfigValue> configs = def.validate(props); for (ConfigValue config : configs) { String name = config.name(); ConfigValue expectedConfig = expected.get(name); assertEquals(expectedConfig, config); } }
@Override public void start(Map<String, String> props) { //shallow copy configValues = config().validate(props).stream().collect(Collectors.toMap(ConfigValue::name, Function.identity())); logger.info("Starting OPC-DA connector (version {}) on server {} reading tags {}", version(), configValues.get(PROPERTY_HOST).value(), configValues.get(PROPERTY_TAGS).value()); }
@Override public void start(Map<String, String> props) { //shallow copy configValues = config().validate(props).stream().collect(Collectors.toMap(ConfigValue::name, Function.identity())); logger.info("Starting OPC-UA connector (version {}) on server {} reading tags {}", version(), configValues.get(PROPERTY_SERVER_URI).value(), configValues.get(PROPERTY_TAGS).value()); }
/** * Validate the connector configuration values against configuration definitions. * @param connectorConfigs the provided configuration values * @return List of Config, each Config contains the updated configuration information given * the current configuration values. */ public Config validate(Map<String, String> connectorConfigs) { ConfigDef configDef = config(); if (null == configDef) { throw new ConnectException( String.format("%s.config() must return a ConfigDef that is not null.", this.getClass().getName()) ); } List<ConfigValue> configValues = configDef.validate(connectorConfigs); return new Config(configValues); }