Skip to content

Commit

Permalink
RHCLOUD-35924 | feature: tests for the configuration override (#3113)
Browse files Browse the repository at this point in the history
* feature: tests for the configuration override

The tests will solidify the implementation.

RHCLOUD-35924

* fix: the Kessel gRPC port is 9000 for both Inventory and Relations

RHCLOUD-35924
  • Loading branch information
MikelAlejoBR authored Nov 12, 2024
1 parent 646b458 commit 503b0f7
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import io.smallrye.config.Priorities;
import io.smallrye.config.SecretKeys;
import jakarta.annotation.Priority;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;

/**
* <p>A configuration interceptor which helps overriding the resolved
Expand Down Expand Up @@ -42,15 +42,6 @@
*/
@Priority(Priorities.APPLICATION)
public class KesselConfigInterceptor implements ConfigSourceInterceptor {
/**
* Holds the association between the Kessel's property names and the gRPC
* ports that need to be used in the URLs.
*/
protected final Map<String, Integer> configPropertyPort = Map.of(
"inventory-api.target-url", 9081,
"relations-api.target-url", 9000
);

/**
* Overrides the {@code inventory-api.target-url}'s and
* {@code relations-api.target-url}'s URL by removing the "http" or "https"
Expand All @@ -66,16 +57,16 @@ public class KesselConfigInterceptor implements ConfigSourceInterceptor {
public ConfigValue getValue(final ConfigSourceInterceptorContext configSourceInterceptorContext, final String name) {
final ConfigValue configValue = SecretKeys.doLocked(() -> configSourceInterceptorContext.proceed(name));

if ((configValue != null) && this.configPropertyPort.containsKey(name)) {
if ((configValue != null) && (name.equals("inventory-api.target-url") || name.equals("relations-api.target-url"))) {
final String kesselUrl = configValue.getValue();
try {
final URL url = new URI(kesselUrl).toURL();
final String newKesselUrl = url.getHost() + ":" + this.configPropertyPort.get(name);
final String newKesselUrl = url.getHost() + ":9000";

Log.debugf("Kessel URL for property \"%s\" changed from \"%s\" to \"%s\"", name, kesselUrl, newKesselUrl);

return configValue.withValue(newKesselUrl);
} catch (final MalformedURLException | URISyntaxException e) {
} catch (final IllegalArgumentException | MalformedURLException | URISyntaxException e) {
Log.debugf(e, "Unable to create a URL from the configuration property \"%s\"'s value \"%s\"", name, kesselUrl);

return configValue;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.redhat.cloud.notifications.config;

import io.smallrye.config.ConfigSourceInterceptorContext;
import io.smallrye.config.ConfigValue;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mockito;

import java.util.stream.Stream;

public class KesselConfigInterceptorTest {
private final KesselConfigInterceptor kesselConfigInterceptor = new KesselConfigInterceptor();

/**
* Test cases which make sure that the configuration interceptor only
* overrides the configuration properties when they're Kessel properties
* and their values are URLs.
* @return the stream of arguments to try.
*/
private static Stream<Arguments> testCases() {
return Stream.of(
Arguments.of("another-property-which-should-not-be-overwritten", "value", "value"),
Arguments.of("another-property-which-should-not-be-overwritten", "http://kessel-url:8000", "http://kessel-url:8000"),
Arguments.of("another-property-which-should-not-be-overwritten", "https://kessel-url:8000", "https://kessel-url:8000"),
Arguments.of("inventory-api.target-url", "invalidUrl", "invalidUrl"),
Arguments.of("inventory-api.target-url", "localhost:8000", "localhost:8000"),
Arguments.of("inventory-api.target-url", "http://kessel-inventory-api.url.test:8000", "kessel-inventory-api.url.test:9000"),
Arguments.of("inventory-api.target-url", "https://kessel-inventory-api-secure.url.test:8000", "kessel-inventory-api-secure.url.test:9000"),
Arguments.of("relations-api.target-url", "invalidUrl", "invalidUrl"),
Arguments.of("relations-api.target-url", "localhost:8000", "localhost:8000"),
Arguments.of("relations-api.target-url", "http://kessel-relations-api.url.test:8000", "kessel-relations-api.url.test:9000"),
Arguments.of("relations-api.target-url", "https://kessel-relations-api-secure.url.test:8000", "kessel-relations-api-secure.url.test:9000")
);
}

/**
* Tests that the Kessel properties are overriden when the property values
* are URLs.
* @param configurationPropertyName the configuration property name that
* might get overriden.
* @param configurationPropertyValue the original configuration property
* value as it would be read from the
* properties files or the environment
* variables.
* @param expectedFinalValue the expected final value of the configuration
* property after it has been overriden or not.
*/
@MethodSource("testCases")
@ParameterizedTest
void testConfigurationOverrides(final String configurationPropertyName, final String configurationPropertyValue, final String expectedFinalValue) {
Assertions.assertEquals(
expectedFinalValue,
this.kesselConfigInterceptor.getValue(this.mockConfigSourceInterceptorContext(configurationPropertyName, configurationPropertyValue), configurationPropertyName).getValue(),
"unexpected configuration property value received"
);
}

/**
* Mocks the configuration source context.
* @param configurationPropertyName the property name that is simulated to
* be processed.
* @param configurationPropertyValue the original property value that is
* simulated to be read from properties
* or environment values.
* @return the built configuration source interceptor context.
*/
private ConfigSourceInterceptorContext mockConfigSourceInterceptorContext(final String configurationPropertyName, final String configurationPropertyValue) {
final ConfigSourceInterceptorContext configSourceInterceptorContext = Mockito.mock(ConfigSourceInterceptorContext.class);

final ConfigValue configValue = ConfigValue.builder().withValue(configurationPropertyValue).build();
Mockito.when(configSourceInterceptorContext.proceed(configurationPropertyName)).thenReturn(configValue);

return configSourceInterceptorContext;
}
}

0 comments on commit 503b0f7

Please sign in to comment.