added some unit tests for the merging case (#1175)
This commit is contained in:
@@ -24,5 +24,5 @@ import java.util.Map;
|
||||
* Container for some of the source's fields, it holds its labels (nullable), name and
|
||||
* data.
|
||||
*/
|
||||
public final record StrippedSourceContainer(Map<String, String> labels, String name, Map<String, String> data) {
|
||||
public record StrippedSourceContainer(Map<String, String> labels, String name, Map<String, String> data) {
|
||||
}
|
||||
|
||||
@@ -16,12 +16,18 @@
|
||||
|
||||
package org.springframework.cloud.kubernetes.commons.config;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
/**
|
||||
* @author wind57
|
||||
*/
|
||||
@@ -159,4 +165,35 @@ class ConfigUtilsTests {
|
||||
Assertions.assertEquals(result.sourceData().get("prefix.c"), "d");
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
*
|
||||
* - we have configmap-one with an application.yaml with two properties propA = A, prop = B
|
||||
* - we have configmap-one-kubernetes with an application.yaml with two properties propA = AA, probC = C
|
||||
*
|
||||
* As a result we should get three properties as output.
|
||||
*
|
||||
* </pre>
|
||||
*/
|
||||
@Test
|
||||
void testMerge() {
|
||||
|
||||
StrippedSourceContainer configMapOne = new StrippedSourceContainer(Map.of(), "configmap-one",
|
||||
Map.of("application.yaml", "propA: A\npropB: B"));
|
||||
|
||||
StrippedSourceContainer configMapOneK8s = new StrippedSourceContainer(Map.of(), "configmap-one-kubernetes",
|
||||
Map.of("application.yaml", "propA: AA\npropC: C"));
|
||||
|
||||
LinkedHashSet<String> sourceNames = Stream.of("configmap-one", "configmap-one-kubernetes")
|
||||
.collect(Collectors.toCollection(LinkedHashSet::new));
|
||||
|
||||
MultipleSourcesContainer result = ConfigUtils.processNamedData(List.of(configMapOne, configMapOneK8s),
|
||||
new MockEnvironment(), sourceNames, "default", false);
|
||||
|
||||
Assertions.assertEquals(result.data().size(), 3);
|
||||
Assertions.assertEquals(result.data().get("propA"), "AA");
|
||||
Assertions.assertEquals(result.data().get("propB"), "B");
|
||||
Assertions.assertEquals(result.data().get("propC"), "C");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,18 +40,27 @@ public class PropertySourceUtilsTest {
|
||||
|
||||
@Test
|
||||
void yamlParserGenerator_noProfile() {
|
||||
final Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
|
||||
final Properties properties = function.apply("spring:\n application:\n name: myTestApp\n");
|
||||
Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
|
||||
Properties properties = function.apply("spring:\n application:\n name: myTestApp\n");
|
||||
assertThat(properties.getProperty("spring.application.name")).isEqualTo("myTestApp");
|
||||
assertThat(properties.getProperty("spring.profiles")).isNull();
|
||||
assertThat(properties.getProperty("spring.config.activate.on-profile")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void yamlParserGenerator_simpleProperties() {
|
||||
Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
|
||||
Properties properties = function.apply("propA: A\npropB: B");
|
||||
assertThat(properties.getProperty("propA")).isEqualTo("A");
|
||||
assertThat(properties.getProperty("propB")).isEqualTo("B");
|
||||
assertThat(properties.getProperty("spring.config.activate.on-profile")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void yamlParserGenerator_springProfiles_matchProfile() {
|
||||
willReturn(Boolean.TRUE).given(environment).acceptsProfiles(any(Profiles.class));
|
||||
final Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
|
||||
final Properties properties = function.apply(
|
||||
Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
|
||||
Properties properties = function.apply(
|
||||
"spring:\n application:\n name: myTestApp\n---\nspring:\n profiles: dummy\n application:\n name: myDummyApp");
|
||||
assertThat(properties.getProperty("spring.application.name")).isEqualTo("myDummyApp");
|
||||
assertThat(properties.getProperty("spring.profiles")).isEqualTo("dummy");
|
||||
@@ -61,8 +70,8 @@ public class PropertySourceUtilsTest {
|
||||
@Test
|
||||
void yamlParserGenerator_springProfiles_mismatchProfile() {
|
||||
willReturn(Boolean.FALSE).given(environment).acceptsProfiles(any(Profiles.class));
|
||||
final Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
|
||||
final Properties properties = function.apply(
|
||||
Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
|
||||
Properties properties = function.apply(
|
||||
"spring:\n application:\n name: myTestApp\n---\nspring:\n profiles: dummy\n application:\n name: myDummyApp");
|
||||
assertThat(properties.getProperty("spring.application.name")).isEqualTo("myTestApp");
|
||||
assertThat(properties.getProperty("spring.profiles")).isNull();
|
||||
@@ -72,8 +81,8 @@ public class PropertySourceUtilsTest {
|
||||
@Test
|
||||
void yamlParserGenerator_springConfigActivateOnProfile_matchProfile() {
|
||||
willReturn(Boolean.TRUE).given(environment).acceptsProfiles(any(Profiles.class));
|
||||
final Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
|
||||
final Properties properties = function.apply(
|
||||
Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
|
||||
Properties properties = function.apply(
|
||||
"spring:\n application:\n name: myTestApp\n---\nspring:\n config:\n activate:\n on-profile: dummy\n application:\n name: myDummyApp");
|
||||
assertThat(properties.getProperty("spring.application.name")).isEqualTo("myDummyApp");
|
||||
assertThat(properties.getProperty("spring.profiles")).isNull();
|
||||
@@ -83,8 +92,8 @@ public class PropertySourceUtilsTest {
|
||||
@Test
|
||||
void yamlParserGenerator_springConfigActivateOnProfile_mismatchProfile() {
|
||||
willReturn(Boolean.FALSE).given(environment).acceptsProfiles(any(Profiles.class));
|
||||
final Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
|
||||
final Properties properties = function.apply(
|
||||
Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
|
||||
Properties properties = function.apply(
|
||||
"spring:\n application:\n name: myTestApp\n---\nspring:\n config:\n activate:\n on-profile: dummy\n application:\n name: myDummyApp");
|
||||
assertThat(properties.getProperty("spring.application.name")).isEqualTo("myTestApp");
|
||||
assertThat(properties.getProperty("spring.profiles")).isNull();
|
||||
|
||||
Reference in New Issue
Block a user