Skip to content

Commit

Permalink
Fixed interpolation
Browse files Browse the repository at this point in the history
Signed-off-by: Helber Belmiro <[email protected]>
  • Loading branch information
hbelmiro committed Aug 29, 2023
1 parent 48cdd01 commit 87ba285
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,10 @@
*/
package org.kie.kogito.addons.quarkus.k8s.config;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.kie.kogito.addons.k8s.resource.catalog.KubernetesProtocol;

import io.smallrye.config.ConfigValue;

class ConfigValueExpander {

private static final Pattern placeholderPattern = createPlaceholderPattern();

private static Pattern createPlaceholderPattern() {
String protocols = Arrays.stream(KubernetesProtocol.values())
.map(KubernetesProtocol::getValue)
.collect(Collectors.joining("|"));
return Pattern.compile("\\$\\{(" + protocols + "):.+}");
}

private final KubeDiscoveryConfigCache kubeDiscoveryConfigCache;

ConfigValueExpander(KubeDiscoveryConfigCache kubeDiscoveryConfigCache) {
Expand All @@ -56,14 +40,18 @@ ConfigValue expand(ConfigValue configValue) {
}

public static String interpolate(String input, String replacement) {
return placeholderPattern.matcher(input).replaceAll(replacement);
int startIndex = input.indexOf("${");
int endIndex = input.indexOf("}", startIndex);

return input.substring(0, startIndex) + replacement + input.substring(endIndex + 1);
}

private static String extractServiceCoordinates(String rawValue) {
Matcher matcher = placeholderPattern.matcher(rawValue);
static String extractServiceCoordinates(String rawValue) {
int startIndex = rawValue.indexOf("${");
int endIndex = rawValue.indexOf("}", startIndex);

if (matcher.find()) {
return matcher.group(1);
if (startIndex != -1 && endIndex != -1) {
return rawValue.substring(startIndex + 2, endIndex);
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
package org.kie.kogito.addons.quarkus.k8s.config;

import java.util.Optional;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

import io.smallrye.config.ConfigValue;
Expand All @@ -27,6 +30,20 @@

class ConfigValueExpanderTest {

static Stream<Arguments> extractServiceCoordinatesSource() {
return Stream.of(
Arguments.of("${kubernetes:pods.v1/kie/kogito}/path", "kubernetes:pods.v1/kie/kogito"),
Arguments.of("${knative:services.v1.serving.knative.dev/default/serverless-workflow-greeting-quarkus}/path",
"knative:services.v1.serving.knative.dev/default/serverless-workflow-greeting-quarkus"));
}

@ParameterizedTest
@MethodSource("extractServiceCoordinatesSource")
void extractServiceCoordinates(String expandableValue, String expectedCoordinate) {
assertThat(ConfigValueExpander.extractServiceCoordinates(expandableValue))
.isEqualTo(expectedCoordinate);
}

@ParameterizedTest
@ValueSource(strings = { "${kubernetes:pods.v1/kie/kogito}/path", "${openshift:pods.v1/kie/kogito}/path", "${knative:kie/kogito}/path" })
void expandable(String expandableValues) {
Expand Down

0 comments on commit 87ba285

Please sign in to comment.