Home / Class/ CloudPlatformTests Class — spring-boot Architecture

CloudPlatformTests Class — spring-boot Architecture

Architecture documentation for the CloudPlatformTests class in CloudPlatformTests.java from the spring-boot codebase.

Entity Profile

Relationship Graph

Source Code

core/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java lines 44–292

class CloudPlatformTests {

	@Test
	void getActiveWhenEnvironmentIsNullShouldReturnNull() {
		CloudPlatform platform = CloudPlatform.getActive(null);
		assertThat(platform).isNull();
	}

	@Test
	void getActiveWhenNotInCloudShouldReturnNull() {
		Environment environment = new MockEnvironment();
		CloudPlatform platform = CloudPlatform.getActive(environment);
		assertThat(platform).isNull();
	}

	@Test
	void getActiveWhenHasVcapApplicationShouldReturnCloudFoundry() {
		Environment environment = new MockEnvironment().withProperty("VCAP_APPLICATION", "---");
		CloudPlatform platform = CloudPlatform.getActive(environment);
		assertThat(platform).isNotNull();
		assertThat(platform).isEqualTo(CloudPlatform.CLOUD_FOUNDRY);
		assertThat(platform.isActive(environment)).isTrue();
	}

	@Test
	void getActiveWhenHasVcapServicesShouldReturnCloudFoundry() {
		Environment environment = new MockEnvironment().withProperty("VCAP_SERVICES", "---");
		CloudPlatform platform = CloudPlatform.getActive(environment);
		assertThat(platform).isNotNull();
		assertThat(platform).isEqualTo(CloudPlatform.CLOUD_FOUNDRY);
		assertThat(platform.isActive(environment)).isTrue();
	}

	@Test
	void getActiveWhenHasDynoShouldReturnHeroku() {
		Environment environment = new MockEnvironment().withProperty("DYNO", "---");
		CloudPlatform platform = CloudPlatform.getActive(environment);
		assertThat(platform).isNotNull();
		assertThat(platform).isEqualTo(CloudPlatform.HEROKU);
		assertThat(platform.isActive(environment)).isTrue();
	}

	@Test
	void getActiveWhenHasHcLandscapeShouldReturnSap() {
		Environment environment = new MockEnvironment().withProperty("HC_LANDSCAPE", "---");
		CloudPlatform platform = CloudPlatform.getActive(environment);
		assertThat(platform).isNotNull();
		assertThat(platform).isEqualTo(CloudPlatform.SAP);
		assertThat(platform.isActive(environment)).isTrue();
	}

	@Test
	void getActiveWhenHasNomadAllocIdShouldReturnNomad() {
		Environment environment = new MockEnvironment().withProperty("NOMAD_ALLOC_ID", "---");
		CloudPlatform platform = CloudPlatform.getActive(environment);
		assertThat(platform).isNotNull();
		assertThat(platform).isEqualTo(CloudPlatform.NOMAD);
		assertThat(platform.isActive(environment)).isTrue();
	}

	@Test
	void getActiveWhenHasKubernetesServiceHostAndPortShouldReturnKubernetes() {
		Map<String, Object> envVars = new HashMap<>();
		envVars.put("KUBERNETES_SERVICE_HOST", "---");
		envVars.put("KUBERNETES_SERVICE_PORT", "8080");
		Environment environment = getEnvironmentWithEnvVariables(envVars);
		CloudPlatform platform = CloudPlatform.getActive(environment);
		assertThat(platform).isNotNull();
		assertThat(platform).isEqualTo(CloudPlatform.KUBERNETES);
		assertThat(platform.isActive(environment)).isTrue();
	}

	@Test
	void getActiveWhenHasKubernetesServiceHostAndNoKubernetesServicePortShouldNotReturnKubernetes() {
		Environment environment = getEnvironmentWithEnvVariables(
				Collections.singletonMap("KUBERNETES_SERVICE_HOST", "---"));
		CloudPlatform platform = CloudPlatform.getActive(environment);
		assertThat(platform).isNull();
	}

	@Test
	void getActiveWhenHasKubernetesServicePortAndNoKubernetesServiceHostShouldNotReturnKubernetes() {
		Environment environment = getEnvironmentWithEnvVariables(
				Collections.singletonMap("KUBERNETES_SERVICE_PORT", "8080"));
		CloudPlatform platform = CloudPlatform.getActive(environment);
		assertThat(platform).isNull();
	}

	@Test
	void getActiveWhenHasServiceHostAndServicePortShouldReturnKubernetes() {
		Map<String, Object> envVars = new HashMap<>();
		envVars.put("EXAMPLE_SERVICE_HOST", "---");
		envVars.put("EXAMPLE_SERVICE_PORT", "8080");
		Environment environment = getEnvironmentWithEnvVariables(envVars);
		CloudPlatform platform = CloudPlatform.getActive(environment);
		assertThat(platform).isNotNull();
		assertThat(platform).isEqualTo(CloudPlatform.KUBERNETES);
		assertThat(platform.isActive(environment)).isTrue();
	}

	@Test
	void getActiveWhenHasServiceHostAndNoServicePortShouldNotReturnKubernetes() {
		Environment environment = getEnvironmentWithEnvVariables(
				Collections.singletonMap("EXAMPLE_SERVICE_HOST", "---"));
		CloudPlatform platform = CloudPlatform.getActive(environment);
		assertThat(platform).isNull();
	}

	@Test
	void getActiveWhenHasAllAzureEnvVariablesShouldReturnAzureAppService() {
		Map<String, Object> envVars = new HashMap<>();
		envVars.put("WEBSITE_SITE_NAME", "---");
		envVars.put("WEBSITE_INSTANCE_ID", "1234");
		envVars.put("WEBSITE_RESOURCE_GROUP", "test");
		envVars.put("WEBSITE_SKU", "1234");
		Environment environment = getEnvironmentWithEnvVariables(envVars);
		CloudPlatform platform = CloudPlatform.getActive(environment);
		assertThat(platform).isNotNull();
		assertThat(platform).isEqualTo(CloudPlatform.AZURE_APP_SERVICE);
		assertThat(platform.isActive(environment)).isTrue();
	}

	@Test
	void getActiveWhenHasMissingWebsiteSiteNameShouldNotReturnAzureAppService() {
		Map<String, Object> envVars = new HashMap<>();
		envVars.put("WEBSITE_INSTANCE_ID", "1234");
		envVars.put("WEBSITE_RESOURCE_GROUP", "test");
		envVars.put("WEBSITE_SKU", "1234");
		Environment environment = getEnvironmentWithEnvVariables(envVars);
		CloudPlatform platform = CloudPlatform.getActive(environment);
		assertThat(platform).isNull();
	}

	@Test
	void getActiveWhenHasMissingWebsiteInstanceIdShouldNotReturnAzureAppService() {
		Map<String, Object> envVars = new HashMap<>();
		envVars.put("WEBSITE_SITE_NAME", "---");
		envVars.put("WEBSITE_RESOURCE_GROUP", "test");
		envVars.put("WEBSITE_SKU", "1234");
		Environment environment = getEnvironmentWithEnvVariables(envVars);
		CloudPlatform platform = CloudPlatform.getActive(environment);
		assertThat(platform).isNull();
	}

	@Test
	void getActiveWhenHasMissingWebsiteResourceGroupShouldNotReturnAzureAppService() {
		Map<String, Object> envVars = new HashMap<>();
		envVars.put("WEBSITE_SITE_NAME", "---");
		envVars.put("WEBSITE_INSTANCE_ID", "1234");
		envVars.put("WEBSITE_SKU", "1234");
		Environment environment = getEnvironmentWithEnvVariables(envVars);
		CloudPlatform platform = CloudPlatform.getActive(environment);
		assertThat(platform).isNull();
	}

	@Test
	void getActiveWhenHasMissingWebsiteSkuShouldNotReturnAzureAppService() {
		Map<String, Object> envVars = new HashMap<>();
		envVars.put("WEBSITE_SITE_NAME", "---");
		envVars.put("WEBSITE_INSTANCE_ID", "1234");
		envVars.put("WEBSITE_RESOURCE_GROUP", "test");
		Environment environment = getEnvironmentWithEnvVariables(envVars);
		CloudPlatform platform = CloudPlatform.getActive(environment);
		assertThat(platform).isNull();
	}

	@ParameterizedTest
	@ValueSource(strings = { "AWS_ECS_FARGATE", "AWS_ECS_EC2" })
	void getActiveWhenHasAwsExecutionEnvEcsShouldReturnAwsEcs(String awsExecutionEnv) {
		Map<String, Object> envVars = Map.of("AWS_EXECUTION_ENV", awsExecutionEnv);
		Environment environment = getEnvironmentWithEnvVariables(envVars);
		CloudPlatform platform = CloudPlatform.getActive(environment);
		assertThat(platform).isNotNull().isEqualTo(CloudPlatform.AWS_ECS);
		assertThat(platform.isActive(environment)).isTrue();
	}

	@Test
	void getActiveWhenHasAwsExecutionEnvLambdaShouldNotReturnAwsEcs() {
		Map<String, Object> envVars = Map.of("AWS_EXECUTION_ENV", "AWS_Lambda_java8");
		Environment environment = getEnvironmentWithEnvVariables(envVars);
		CloudPlatform platform = CloudPlatform.getActive(environment);
		assertThat(platform).isNull();
	}

	@Test
	void getActiveWhenHasEnforcedCloudPlatform() {
		Environment environment = getEnvironmentWithEnvVariables(
				Collections.singletonMap("spring.main.cloud-platform", "kubernetes"));
		CloudPlatform platform = CloudPlatform.getActive(environment);
		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 isEnforcedWhenEnvironmentPropertyIsMissingReturnsFalse() {
		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 isEnforcedWhenBinderPropertyIsMissingReturnsFalse() {
		Binder binder = new Binder(new MockConfigurationPropertySource());
		assertThat(CloudPlatform.KUBERNETES.isEnforced(binder)).isFalse();
	}

	void isActiveWhenNoCloudPlatformIsEnforcedAndHasKubernetesServiceHostAndKubernetesServicePort() {
		Map<String, Object> envVars = new HashMap<>();
		envVars.put("EXAMPLE_SERVICE_HOST", "---");
		envVars.put("EXAMPLE_SERVICE_PORT", "8080");
		Environment environment = getEnvironmentWithEnvVariables(envVars);
		((MockEnvironment) environment).setProperty("spring.main.cloud-platform", "none");
		assertThat(Stream.of(CloudPlatform.values()).filter((platform) -> platform.isActive(environment)))
			.containsExactly(CloudPlatform.NONE);
	}

	private Environment getEnvironmentWithEnvVariables(Map<String, Object> environmentVariables) {
		MockEnvironment environment = new MockEnvironment();
		PropertySource<?> propertySource = new SystemEnvironmentPropertySource(
				StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, environmentVariables);
		environment.getPropertySources().addFirst(propertySource);
		return environment;
	}

}

Domain

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free