Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test resources properties factory #667

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private Map<String, String> extractAnnotationProperties(TestResourcesProperties
var testResourcesConfig = properties.entrySet()
.stream()
.filter(e -> e.getKey().startsWith(TEST_RESOURCES_PROPERTY_PREFIX))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
.collect(Collectors.toMap(e -> e.getKey().substring(TEST_RESOURCES_PROPERTY_PREFIX.length()), Map.Entry::getValue));
Map<String, String> resolvedProperties = Stream.of(requestedProperties)
.map(v -> new Object() {
private final String key = v;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ public Optional<String> resolve(String name, Map<String, Object> properties, Map
value += ": " + properties.get("required-property");
}
}
testResourcesConfig.
keySet()
.forEach(k -> {
if (k.startsWith("test-resources.")) {
throw new AssertionError("Test resources properties must be passed without the prefix \"test-resources.\"");
}
});
return Optional.ofNullable(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* The main test resources controller, which will answer requests performed by the
Expand All @@ -49,6 +50,7 @@
public class TestResourcesController implements TestResourcesResolver {
private static final Logger LOGGER = LoggerFactory.getLogger(TestResourcesController.class);
private static final int MAX_STOP_TIMEOUT = 5000;
private static final String TEST_RESOURCES_PREFIX = "test-resources.";

private final ResolverLoader loader;

Expand Down Expand Up @@ -87,15 +89,16 @@ public List<String> getResolvableProperties() {
@Post("/list")
public List<String> getResolvableProperties(Map<String, Collection<String>> propertyEntries,
Map<String, Object> testResourcesConfig) {
var sanitizedTestResourcesConfig = sanitizeTestResourcesConfig(testResourcesConfig);
return loader.getResolvers()
.stream()
.filter(testResourcesResolver -> isEnabled(testResourcesResolver, testResourcesConfig))
.map(r -> r.getResolvableProperties(propertyEntries, testResourcesConfig))
.filter(testResourcesResolver -> isEnabled(testResourcesResolver, sanitizedTestResourcesConfig))
.map(r -> r.getResolvableProperties(propertyEntries, sanitizedTestResourcesConfig))
.flatMap(Collection::stream)
.distinct()
.peek(p -> LOGGER.debug(
"For configuration {} and property entries {} , resolvable property: {}",
testResourcesConfig, propertyEntries, p))
sanitizedTestResourcesConfig, propertyEntries, p))
.toList();
}

Expand Down Expand Up @@ -144,17 +147,18 @@ public List<String> getRequiredPropertyEntries() {
public Optional<String> resolve(String name,
Map<String, Object> properties,
Map<String, Object> testResourcesConfig) {
var sanitizedTestResourcesConfig = sanitizeTestResourcesConfig(testResourcesConfig);
Optional<String> result = Optional.empty();
for (TestResourcesResolver resolver : loader.getResolvers()) {
if (resolver instanceof ToggableTestResourcesResolver toggable &&
!toggable.isEnabled(testResourcesConfig)) {
!toggable.isEnabled(sanitizedTestResourcesConfig)) {
continue;
}
try {
result = resolver.resolve(name, properties, testResourcesConfig);
result = resolver.resolve(name, properties, sanitizedTestResourcesConfig);
LOGGER.debug(
"Attempt to resolve {} with resolver {}, properties {} and test resources configuration {} : {}",
name, resolver.getClass(), properties, testResourcesConfig,
name, resolver.getClass(), properties, sanitizedTestResourcesConfig,
result.orElse("\uD83D\uDEAB"));
} catch (Exception ex) {
for (PropertyResolutionListener listener : propertyResolutionListeners) {
Expand All @@ -165,7 +169,7 @@ public Optional<String> resolve(String name,
if (result.isPresent()) {
for (PropertyResolutionListener listener : propertyResolutionListeners) {
listener.resolved(name, result.get(), resolver, properties,
testResourcesConfig);
sanitizedTestResourcesConfig);
}
return result;
}
Expand Down Expand Up @@ -274,4 +278,33 @@ private static boolean isEnabled(TestResourcesResolver resolver,
}
return true;
}

/**
* This method is responsible of making sure that the test resources configuration
* map doesn't contain any entry which starts with `test-resources.` since the prefix
* is supposed to be removed. However, depending on how the client is used, or even
* if the service is called directly, it may be possible to pass in such configuration.
* This is therefore a "best effort" to cleanup the configuration in case this happens.
*
* @param testResourcesConfig the configuration map
* @return a cleanup map
*/
private static Map<String, Object> sanitizeTestResourcesConfig(Map<String, Object> testResourcesConfig) {
if (testResourcesConfig.keySet().stream().noneMatch(k -> k.startsWith(TEST_RESOURCES_PREFIX))) {
return testResourcesConfig;
}
return testResourcesConfig.entrySet()
.stream()
.collect(Collectors.toMap(
e -> {
var key = e.getKey();
if (key.startsWith(TEST_RESOURCES_PREFIX)) {
LOGGER.warn("Test resources configuration key '{}' should be passed without the '{}' prefix", key, TEST_RESOURCES_PREFIX);
return key.substring(TEST_RESOURCES_PREFIX.length());
}
return key;
},
Map.Entry::getValue
));
}
}
Loading