use of com.hazelcast.config.properties.PropertyDefinition in project hazelcast by hazelcast.
the class DefaultDiscoveryService method buildProperties.
private Map<String, Comparable> buildProperties(DiscoveryStrategyFactory factory, DiscoveryStrategyConfig config, String className) {
Collection<PropertyDefinition> propertyDefinitions = factory.getConfigurationProperties();
if (propertyDefinitions == null) {
return Collections.emptyMap();
}
Map<String, Comparable> properties = config.getProperties();
Map<String, Comparable> mappedProperties = new HashMap<String, Comparable>();
for (PropertyDefinition propertyDefinition : propertyDefinitions) {
String propertyKey = propertyDefinition.key();
Comparable value = properties.get(propertyKey);
if (value == null) {
if (!propertyDefinition.optional()) {
throw new HazelcastException("Missing property '" + propertyKey + "' on discovery strategy '" + className + "' configuration");
}
continue;
}
TypeConverter typeConverter = propertyDefinition.typeConverter();
Comparable mappedValue = typeConverter.convert(value);
ValueValidator validator = propertyDefinition.validator();
if (validator != null) {
validator.validate(mappedValue);
}
mappedProperties.put(propertyKey, mappedValue);
}
return mappedProperties;
}
use of com.hazelcast.config.properties.PropertyDefinition in project hazelcast by hazelcast.
the class DiscoverySpiTest method test_AbstractDiscoveryStrategy_getOrNull.
@Test
public void test_AbstractDiscoveryStrategy_getOrNull() throws Exception {
PropertyDefinition first = new SimplePropertyDefinition("first", PropertyTypeConverter.STRING);
PropertyDefinition second = new SimplePropertyDefinition("second", BOOLEAN);
PropertyDefinition third = new SimplePropertyDefinition("third", PropertyTypeConverter.INTEGER);
PropertyDefinition fourth = new SimplePropertyDefinition("fourth", true, PropertyTypeConverter.STRING);
PropertyDefinition onlyInEnv = new SimplePropertyDefinition("only_in_env", true, PropertyTypeConverter.INTEGER);
Map<String, Comparable> properties = new HashMap<>();
properties.put("first", "value-first");
properties.put("second", Boolean.FALSE);
properties.put("third", 100);
Map<String, String> environmentVariables = new HashMap<>();
// system property > system environment > configuration
// property 'first' => "value-first"
// property 'second' => true
environmentVariables.put("test.second", "true");
// property 'third' => 300
environmentVariables.put("test.third", "200");
System.setProperty("test.third", "300");
// property 'fourth' => null
environmentVariables.put("test.only_in_env", "500");
PropertyDiscoveryStrategy strategy = new PropertyDiscoveryStrategy(LOGGER, properties, environmentVariables::get);
// without lookup of environment
assertEquals("value-first", strategy.getOrNull(first));
assertEquals(Boolean.FALSE, strategy.getOrNull(second));
assertEquals(100, ((Integer) strategy.getOrNull(third)).intValue());
assertNull(strategy.getOrNull(fourth));
assertEquals("value-first", strategy.getOrNull("test", first));
assertEquals(Boolean.TRUE, strategy.getOrNull("test", second));
assertEquals(300, ((Integer) strategy.getOrNull("test", third)).intValue());
assertNull(strategy.getOrNull("test", fourth));
assertEquals(500, ((Integer) strategy.getOrNull("test", onlyInEnv)).intValue());
}
use of com.hazelcast.config.properties.PropertyDefinition in project hazelcast by hazelcast.
the class DiscoverySpiTest method test_AbstractDiscoveryStrategy_getOrDefault.
@Test
public void test_AbstractDiscoveryStrategy_getOrDefault() throws Exception {
PropertyDefinition value = new SimplePropertyDefinition("value", PropertyTypeConverter.INTEGER);
Map<String, Comparable> properties = Collections.emptyMap();
PropertyDiscoveryStrategy strategy = new PropertyDiscoveryStrategy(LOGGER, properties);
assertEquals(1111, (long) strategy.getOrDefault(value, 1111));
assertEquals(1111, (long) strategy.getOrDefault("test", value, 1111));
}
use of com.hazelcast.config.properties.PropertyDefinition in project hazelcast by hazelcast.
the class DiscoveryServicePropertiesUtilTest method nullProperty.
@Test
public void nullProperty() {
// given
Map<String, Comparable> properties = new HashMap<>(singletonMap(PROPERTY_KEY_1, null));
TypeConverter typeConverter = new TypeConverter() {
@Override
public Comparable convert(Comparable value) {
return value == null ? "hazel" : "cast";
}
};
Collection<PropertyDefinition> propertyDefinitions = singletonList((PropertyDefinition) new SimplePropertyDefinition(PROPERTY_KEY_1, true, typeConverter));
// when
Map<String, Comparable> result = prepareProperties(properties, propertyDefinitions);
// then
assertEquals("hazel", result.get(PROPERTY_KEY_1));
}
use of com.hazelcast.config.properties.PropertyDefinition in project hazelcast by hazelcast.
the class DiscoveryServicePropertiesUtilTest method correctDashlessPropertyConversion.
@Test
public void correctDashlessPropertyConversion() {
// given
Map<String, Comparable> properties = new HashMap<>(singletonMap("customproperty", PROPERTY_VALUE_1));
Collection<PropertyDefinition> propertyDefinitions = singletonList(new SimplePropertyDefinition("custom-property", STRING));
// when
Map<String, Comparable> result = prepareProperties(properties, propertyDefinitions);
// then
assertEquals(PROPERTY_VALUE_1, result.get("custom-property"));
}
Aggregations