Skip to content

Commit

Permalink
Merge pull request #202 from scalecube/same_class_many_instances_many…
Browse files Browse the repository at this point in the history
…_prefixes
  • Loading branch information
artem-v authored Feb 3, 2023
2 parents a08b5e0 + a689874 commit 78650d2
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ public <T> ObjectConfigProperty<T> objectProperty(String name, Function<String,

@Override
public <T> ObjectConfigProperty<T> objectProperty(String prefix, Class<T> cfgClass) {
Map<String, String> bindingMap =
return objectProperty(
Arrays.stream(cfgClass.getDeclaredFields())
.collect(Collectors.toMap(Field::getName, field -> prefix + '.' + field.getName()));
return new ObjectConfigPropertyImpl<>(bindingMap, cfgClass, propertyMap, propertyCallbackMap);
.collect(Collectors.toMap(Field::getName, field -> prefix + '.' + field.getName())),
cfgClass);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
class ObjectConfigPropertyImpl<T> extends AbstractConfigProperty<T>
implements ObjectConfigProperty<T> {

@SuppressWarnings("rawtypes")
ObjectConfigPropertyImpl(
Map<String, String> bindingMap,
Class<T> cfgClass,
Expand All @@ -30,8 +31,7 @@ class ObjectConfigPropertyImpl<T> extends AbstractConfigProperty<T>
setPropertyCallback(computePropertyCallback(cfgClass, propertyFields, propertyCallbackMap));

computeValue(
propertyFields
.stream()
propertyFields.stream()
.map(ObjectPropertyField::getPropertyName)
.filter(propertyMap::containsKey)
.map(propertyMap::get)
Expand Down Expand Up @@ -71,8 +71,7 @@ private PropertyCallback<T> computePropertyCallback(
list -> ObjectPropertyParser.parseObject(list, propertyFields, cfgClass));

List<String> propertyNames =
propertyFields
.stream()
propertyFields.stream()
.map(ObjectPropertyField::getPropertyName)
.collect(Collectors.toList());

Expand All @@ -88,9 +87,9 @@ private PropertyCallback<T> computePropertyCallback(
}

// noinspection unchecked
return propertyCallbackMap
.values()
.stream()
return propertyCallbackMap.entrySet().stream()
.filter(e -> propertyNames.contains(e.getKey()))
.map(Map.Entry::getValue)
.filter(callbackMap -> callbackMap.containsKey(propertyClass))
.map(callbackMap -> callbackMap.get(propertyClass))
.collect(Collectors.toSet())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,89 @@ class ObjectConfigPropertyManyInstancesTest {

// Normal scenarios

@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
void testMultipleInstancesWithSamePrefix() {
when(configSource.loadConfig())
.thenReturn(
toConfigProps(
mapBuilder()
.put("com.acme.accessKey", "access")
.put("com.acme.secretKey", "secret")
.put("com.acme.emailFrom", "[email protected]")
.put("com.acme.connectUrl", "protocol://admi@admin?connecthere")
.build()));
ConfigRegistryImpl configRegistry = newConfigRegistry(configSource);

MailSettings mailPropertyFoo =
configRegistry.objectProperty("com.acme", MailSettings.class).value().get();
ConnectorSettings connectorPropertyFoo =
configRegistry.objectProperty("com.acme", ConnectorSettings.class).value().get();

assertEquals("access", mailPropertyFoo.accessKey);
assertEquals("secret", mailPropertyFoo.secretKey);
assertEquals("[email protected]", mailPropertyFoo.emailFrom);

assertEquals("access", connectorPropertyFoo.accessKey);
assertEquals("secret", connectorPropertyFoo.secretKey);
assertEquals("protocol://admi@admin?connecthere", connectorPropertyFoo.connectUrl);

MailSettings mailPropertyBar =
configRegistry.objectProperty("com.acme", MailSettings.class).value().get();
ConnectorSettings connectorPropertyBar =
configRegistry.objectProperty("com.acme", ConnectorSettings.class).value().get();

assertEquals("access", mailPropertyBar.accessKey);
assertEquals("secret", mailPropertyBar.secretKey);
assertEquals("[email protected]", mailPropertyBar.emailFrom);

assertEquals("access", connectorPropertyBar.accessKey);
assertEquals("secret", connectorPropertyBar.secretKey);
assertEquals("protocol://admi@admin?connecthere", connectorPropertyBar.connectUrl);
}

@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
void testMultipleInstancesWithDifferentPrefix() {
when(configSource.loadConfig())
.thenReturn(
toConfigProps(
mapBuilder()
// default prefix
.put("com.acme.accessKey", "access")
.put("com.acme.secretKey", "secret")
.put("com.acme.emailFrom", "[email protected]")
// backup prefix
.put("com.acme.backup.accessKey", "access_backup")
.put("com.acme.backup.secretKey", "secret_backup")
.put("com.acme.backup.emailFrom", "[email protected]_backup")
// primary prefix
.put("com.acme.primary.accessKey", "access_primary")
.put("com.acme.primary.secretKey", "secret_primary")
.put("com.acme.primary.emailFrom", "[email protected]_primary")
.build()));
ConfigRegistryImpl configRegistry = newConfigRegistry(configSource);

MailSettings mailPropertyDefault =
configRegistry.objectProperty("com.acme", MailSettings.class).value().get();
MailSettings mailPropertyBackup =
configRegistry.objectProperty("com.acme.backup", MailSettings.class).value().get();
MailSettings mailPropertyPrimary =
configRegistry.objectProperty("com.acme.primary", MailSettings.class).value().get();

assertEquals("access", mailPropertyDefault.accessKey);
assertEquals("secret", mailPropertyDefault.secretKey);
assertEquals("[email protected]", mailPropertyDefault.emailFrom);

assertEquals("access_backup", mailPropertyBackup.accessKey);
assertEquals("secret_backup", mailPropertyBackup.secretKey);
assertEquals("[email protected]_backup", mailPropertyBackup.emailFrom);

assertEquals("access_primary", mailPropertyPrimary.accessKey);
assertEquals("secret_primary", mailPropertyPrimary.secretKey);
assertEquals("[email protected]_primary", mailPropertyPrimary.emailFrom);
}

@Test
void testValueNullInitially() {
when(configSource.loadConfig()).thenReturn(toConfigProps(mapBuilder().build()));
Expand Down

0 comments on commit 78650d2

Please sign in to comment.