Back to Repositories

Testing Gateway Environment Post Processing in Spring Cloud Alibaba

This test suite validates the functionality of the Gateway Environment Post Processor in Spring Cloud Alibaba’s Sentinel Gateway module. It ensures proper handling of environment properties and configuration settings during application startup.

Test Coverage Overview

The test suite provides comprehensive coverage of the GatewayEnvironmentPostProcessor functionality.

Key areas tested include:
  • Property source initialization and configuration
  • Integration with Spring’s ConfigurableEnvironment
  • Handling of existing property sources
  • Verification of sentinel filter configuration

Implementation Analysis

The testing approach utilizes JUnit and Mockito frameworks to isolate and verify the environment post-processing behavior. The implementation employs mock objects to simulate the Spring environment context and validates property source manipulation.

Testing patterns include:
  • Mock object initialization for Spring components
  • Property source verification
  • Configuration assertion checks

Technical Details

Testing infrastructure includes:
  • JUnit 4 test framework
  • Mockito mocking framework
  • Spring Boot test utilities
  • ConfigurableEnvironment and MutablePropertySources components
  • Custom property source configuration

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through clear separation of concerns and thorough verification methods.

Notable practices include:
  • Isolated test cases for different scenarios
  • Proper mock object usage
  • Clear test method documentation
  • Comprehensive assertion checks
  • Clean test organization

alibaba/spring-cloud-alibaba

spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-gateway/src/test/java/com/alibaba/cloud/sentinel/gateway/GatewayEnvironmentPostProcessorTest.java

            
/*
 * Copyright 2013-2023 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 com.alibaba.cloud.sentinel.gateway;

import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;

import org.springframework.boot.SpringApplication;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class GatewayEnvironmentPostProcessorTest {
	/**
	 * Tests the custom property source processing logic.
	 * This test case verifies whether the custom property source is correctly added during the environment post-processing.
	 * Specifically, it checks if the configuration property "spring.cloud.sentinel.filter.enabled" is properly added.
	 */
	@Test
	public void testPostProcessEnvironment() {
		ConfigurableEnvironment environment = mock(ConfigurableEnvironment.class);
		MutablePropertySources propertySources = new MutablePropertySources();
		when(environment.getPropertySources()).thenReturn(propertySources);

		GatewayEnvironmentPostProcessor postProcessor = new GatewayEnvironmentPostProcessor();
		postProcessor.postProcessEnvironment(environment, mock(SpringApplication.class));

		PropertySource<?> propertySource = propertySources.get("defaultProperties");
		Assert.assertNotNull(propertySource);
		Assert.assertNotNull(propertySource.getProperty("spring.cloud.sentinel.filter.enabled"));
	}

	/**
	 * Tests the logic of processing an environment that already contains a property source.
	 * This test case simulates an environment with an existing property source and checks if the post-processor can interact correctly with these property sources.
	 */
	@Test
	public void testPostProcessEnvironmentWithExistingPropertySource() {
		ConfigurableEnvironment environment = mock(ConfigurableEnvironment.class);
		MutablePropertySources propertySources = new MutablePropertySources();
		when(environment.getPropertySources()).thenReturn(propertySources);

		Map<String, Object> existingProperties = new HashMap<>();
		existingProperties.put("existing.property", "value");
		MapPropertySource existingPropertySource = new MapPropertySource("defaultProperties", existingProperties);
		propertySources.addFirst(existingPropertySource);

		GatewayEnvironmentPostProcessor postProcessor = new GatewayEnvironmentPostProcessor();
		postProcessor.postProcessEnvironment(environment, mock(SpringApplication.class));

		PropertySource<?> propertySource = propertySources.get("defaultProperties");
		Assert.assertNotNull(propertySource);
		Assert.assertEquals("value", propertySource.getProperty("existing.property"));
		Assert.assertEquals("false", propertySource.getProperty("spring.cloud.sentinel.filter.enabled"));
	}

}