* #778 Support property 'spring.config.activate.on-profile'. * #778 Extend coverage. Co-authored-by: Matthias Maeller <matthias.maeller@witt-gruppe.eu>
This commit is contained in:
@@ -33,6 +33,16 @@ public final class Constants {
|
||||
*/
|
||||
public static final String PROPERTY_SOURCE_NAME_SEPARATOR = ".";
|
||||
|
||||
/**
|
||||
* Property for legacy profile specific configuration.
|
||||
*/
|
||||
public static final String SPRING_PROFILES = "spring.profiles";
|
||||
|
||||
/**
|
||||
* Property for profile specific configuration.
|
||||
*/
|
||||
public static final String SPRING_CONFIG_ACTIVATE_ON_PROFILE = "spring.config.activate.on-profile";
|
||||
|
||||
private Constants() {
|
||||
}
|
||||
|
||||
|
||||
@@ -33,6 +33,8 @@ import org.springframework.util.StringUtils;
|
||||
import static org.springframework.beans.factory.config.YamlProcessor.MatchStatus.ABSTAIN;
|
||||
import static org.springframework.beans.factory.config.YamlProcessor.MatchStatus.FOUND;
|
||||
import static org.springframework.beans.factory.config.YamlProcessor.MatchStatus.NOT_FOUND;
|
||||
import static org.springframework.cloud.kubernetes.commons.config.Constants.SPRING_CONFIG_ACTIVATE_ON_PROFILE;
|
||||
import static org.springframework.cloud.kubernetes.commons.config.Constants.SPRING_PROFILES;
|
||||
|
||||
/**
|
||||
* Utility class to work with property sources.
|
||||
@@ -76,13 +78,19 @@ public final class PropertySourceUtils {
|
||||
return s -> {
|
||||
YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
|
||||
yamlFactory.setDocumentMatchers(properties -> {
|
||||
String profiles = properties.getProperty("spring.profiles");
|
||||
if (environment != null && StringUtils.hasText(profiles)) {
|
||||
return environment.acceptsProfiles(Profiles.of(profiles)) ? FOUND : NOT_FOUND;
|
||||
}
|
||||
else {
|
||||
return ABSTAIN;
|
||||
if (environment != null) {
|
||||
String profiles = null;
|
||||
if (properties.containsKey(SPRING_CONFIG_ACTIVATE_ON_PROFILE)) {
|
||||
profiles = properties.getProperty(SPRING_CONFIG_ACTIVATE_ON_PROFILE);
|
||||
}
|
||||
else if (properties.containsKey(SPRING_PROFILES)) {
|
||||
profiles = properties.getProperty(SPRING_PROFILES);
|
||||
}
|
||||
if (StringUtils.hasText(profiles)) {
|
||||
return environment.acceptsProfiles(Profiles.of(profiles)) ? FOUND : NOT_FOUND;
|
||||
}
|
||||
}
|
||||
return ABSTAIN;
|
||||
});
|
||||
yamlFactory.setResources(new ByteArrayResource(s.getBytes()));
|
||||
return yamlFactory.getObject();
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright 2013-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.kubernetes.commons.config;
|
||||
|
||||
import java.util.Properties;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.Profiles;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.willReturn;
|
||||
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class PropertySourceUtilsTest {
|
||||
|
||||
@Mock
|
||||
private Environment environment;
|
||||
|
||||
@Test
|
||||
void yamlParserGenerator_noProfile() {
|
||||
final Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
|
||||
final 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_springProfiles_matchProfile() {
|
||||
willReturn(Boolean.TRUE).given(environment).acceptsProfiles(any(Profiles.class));
|
||||
final Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
|
||||
final 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");
|
||||
assertThat(properties.getProperty("spring.config.activate.on-profile")).isNull();
|
||||
}
|
||||
|
||||
@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("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();
|
||||
assertThat(properties.getProperty("spring.config.activate.on-profile")).isNull();
|
||||
}
|
||||
|
||||
@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("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();
|
||||
assertThat(properties.getProperty("spring.config.activate.on-profile")).isEqualTo("dummy");
|
||||
}
|
||||
|
||||
@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("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();
|
||||
assertThat(properties.getProperty("spring.config.activate.on-profile")).isNull();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user