diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigProperties.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigProperties.java index c4fdc592..b8ef5bce 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigProperties.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigProperties.java @@ -175,28 +175,8 @@ public class ConfigMapConfigProperties extends AbstractConfigProperties { public NormalizedSource normalize(String defaultName, String defaultNamespace, boolean defaultUseNameAsPrefix) { String normalizedName = StringUtils.hasLength(this.name) ? this.name : defaultName; String normalizedNamespace = StringUtils.hasLength(this.namespace) ? this.namespace : defaultNamespace; - - // if explicitPrefix is set, it takes priority over useNameAsPrefix - // (either the one from 'spring.cloud.kubernetes.config' or - // 'spring.cloud.kubernetes.config.sources') - if (StringUtils.hasText(this.explicitPrefix)) { - return new NormalizedSource(normalizedName, normalizedNamespace, this.explicitPrefix); - } - - // useNameAsPrefix is a java.lang.Boolean and if it's != null, users have - // specified it explicitly - if (this.useNameAsPrefix != null) { - if (useNameAsPrefix) { - return new NormalizedSource(normalizedName, normalizedNamespace, normalizedName); - } - return new NormalizedSource(normalizedName, normalizedNamespace, ""); - } - - if (defaultUseNameAsPrefix) { - return new NormalizedSource(normalizedName, normalizedNamespace, normalizedName); - } - - return new NormalizedSource(normalizedName, normalizedNamespace, ""); + String prefix = ConfigUtils.findPrefix(this.explicitPrefix, useNameAsPrefix, defaultUseNameAsPrefix, normalizedName); + return new NormalizedSource(normalizedName, normalizedNamespace, prefix); } @Override diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java index 86d2059b..aa663abf 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java @@ -47,4 +47,39 @@ public final class ConfigUtils { return configName; } + /** + * + * @param explicitPrefix value of 'spring.cloud.kubernetes.config.sources.explicitPrefix' + * @param useNameAsPrefix value of 'spring.cloud.kubernetes.config.sources.useNameAsPrefix' + * @param defaultUseNameAsPrefix value of 'spring.cloud.kubernetes.config.defaultUseNameAsPrefix' + * @param normalizedName either the name of 'spring.cloud.kubernetes.config.sources.name' or + * 'spring.cloud.kubernetes.config.name' + * + * @return prefix to use in normalized sources, never null + */ + public static String findPrefix(String explicitPrefix, Boolean useNameAsPrefix, + boolean defaultUseNameAsPrefix, String normalizedName) { + // if explicitPrefix is set, it takes priority over useNameAsPrefix + // (either the one from 'spring.cloud.kubernetes.config' or + // 'spring.cloud.kubernetes.config.sources') + if (StringUtils.hasText(explicitPrefix)) { + return explicitPrefix; + } + + // useNameAsPrefix is a java.lang.Boolean and if it's != null, users have + // specified it explicitly + if (useNameAsPrefix != null) { + if (useNameAsPrefix) { + return normalizedName; + } + return ""; + } + + if (defaultUseNameAsPrefix) { + return normalizedName; + } + + return ""; + } + } diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java new file mode 100644 index 00000000..d513737d --- /dev/null +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java @@ -0,0 +1,57 @@ +/* + * 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 org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * @author wind57 + */ +public class ConfigUtilsTests { + + @Test + public void testExplicitPrefixSet() { + String result = ConfigUtils.findPrefix("explicitPrefix", null, false, "irrelevant"); + Assertions.assertEquals(result, "explicitPrefix"); + } + + @Test + public void testUseNameAsPrefixTrue() { + String result = ConfigUtils.findPrefix("", Boolean.TRUE, false, "name-to-use"); + Assertions.assertEquals(result, "name-to-use"); + } + + @Test + public void testUseNameAsPrefixFalse() { + String result = ConfigUtils.findPrefix("", Boolean.FALSE, false, "name-not-to-use"); + Assertions.assertEquals(result, ""); + } + + @Test + public void testDefaultUseNameAsPrefixTrue() { + String result = ConfigUtils.findPrefix("", null, true, "name-to-use"); + Assertions.assertEquals(result, "name-to-use"); + } + + @Test + public void testNoMatch() { + String result = ConfigUtils.findPrefix("", null, false, "name-not-to-use"); + Assertions.assertEquals(result, ""); + } + +}