Commit 44f18362 authored by Phillip Webb's avatar Phillip Webb

Add Binder backed CloudPlatform.isEnforced method

Update `CloudPlatform` with a new `isEnforced` method that's backed
by a `Binder` rather than the `Environment`. We'll require this when
we overhaul our external config data processing logic.

Closes gh-22498
parent cd364446
......@@ -16,6 +16,7 @@
package org.springframework.boot.cloud;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.Environment;
......@@ -131,6 +132,8 @@ public enum CloudPlatform {
};
private static final String PROPERTY_NAME = "spring.main.cloud-platform";
/**
* Determines if the platform is active (i.e. the application is running in it).
* @param environment the environment
......@@ -148,7 +151,21 @@ public enum CloudPlatform {
* @since 2.3.0
*/
public boolean isEnforced(Environment environment) {
String platform = environment.getProperty("spring.main.cloud-platform");
return isEnforced(environment.getProperty(PROPERTY_NAME));
}
/**
* Determines if the platform is enforced by looking at the
* {@code "spring.main.cloud-platform"} configuration property.
* @param binder the binder
* @return if the platform is enforced
* @since 2.4.0
*/
public boolean isEnforced(Binder binder) {
return isEnforced(binder.bind(PROPERTY_NAME, String.class).orElse(null));
}
private boolean isEnforced(String platform) {
return name().equalsIgnoreCase(platform);
}
......
......@@ -22,6 +22,8 @@ import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.MockConfigurationPropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
......@@ -137,6 +139,44 @@ class CloudPlatformTests {
assertThat(platform).isEqualTo(CloudPlatform.KUBERNETES);
}
@Test
void isEnforcedWhenEnvironmentPropertyMatchesReturnsTrue() {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("spring.main.cloud-platform", "kubernetes");
assertThat(CloudPlatform.KUBERNETES.isEnforced(environment)).isTrue();
}
@Test
void isEnforcedWhenEnvironmentPropertyDoesNotMatchReturnsFalse() {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("spring.main.cloud-platform", "heroku");
assertThat(CloudPlatform.KUBERNETES.isEnforced(environment)).isFalse();
}
@Test
void isEnforcedWhenEnvironmentPropertyIsMissingatchReturnsFalse() {
MockEnvironment environment = new MockEnvironment();
assertThat(CloudPlatform.KUBERNETES.isEnforced(environment)).isFalse();
}
@Test
void isEnforcedWhenBinderPropertyMatchesReturnsTrue() {
Binder binder = new Binder(new MockConfigurationPropertySource("spring.main.cloud-platform", "kubernetes"));
assertThat(CloudPlatform.KUBERNETES.isEnforced(binder)).isTrue();
}
@Test
void isEnforcedWhenBinderPropertyDoesNotMatchReturnsFalse() {
Binder binder = new Binder(new MockConfigurationPropertySource("spring.main.cloud-platform", "heroku"));
assertThat(CloudPlatform.KUBERNETES.isEnforced(binder)).isFalse();
}
@Test
void isEnforcedWhenBinderPropertyIsMissingatchReturnsFalse() {
Binder binder = new Binder(new MockConfigurationPropertySource());
assertThat(CloudPlatform.KUBERNETES.isEnforced(binder)).isFalse();
}
private Environment getEnvironmentWithEnvVariables(Map<String, Object> environmentVariables) {
MockEnvironment environment = new MockEnvironment();
PropertySource<?> propertySource = new SystemEnvironmentPropertySource(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment