Back to Repositories

Validating Sentinel Bean Autowiring Workflow in Spring Cloud Alibaba

This test suite validates the autowiring functionality of Sentinel beans in Spring Cloud Alibaba, focusing on component initialization and configuration. It ensures proper integration between Sentinel’s web protection features and Spring’s dependency injection system.

Test Coverage Overview

The test suite provides comprehensive coverage of Sentinel’s core bean autowiring capabilities.

  • Validates initialization of UrlCleaner, BlockExceptionHandler, and RequestOriginParser beans
  • Tests SentinelProperties configuration and URL pattern matching
  • Verifies SentinelWebMvcConfig component integration
  • Covers filter order and URL pattern specifications

Implementation Analysis

The testing approach uses Spring Boot’s testing framework with JUnit4 integration.

Key patterns include:
  • Spring Runner configuration with @RunWith(SpringRunner.class)
  • Test-specific configuration class with @EnableAutoConfiguration
  • Custom property injection testing via @SpringBootTest properties
  • Bean validation through assertion chains

Technical Details

Testing infrastructure includes:

  • JUnit 4 testing framework
  • Spring Boot Test context
  • AssertJ assertions library
  • Sentinel Web MVC adaptation layer
  • Custom TestConfig class for bean definitions
  • Mock implementations of Sentinel interfaces

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Spring Cloud applications.

  • Proper separation of test configuration
  • Comprehensive component validation
  • Clear test method organization
  • Effective use of Spring Boot test annotations
  • Proper dependency injection testing patterns

alibaba/spring-cloud-alibaba

spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/test/java/com/alibaba/cloud/sentinel/SentinelBeanAutowiredTests.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;

import com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration;
import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.callback.DefaultBlockExceptionHandler;
import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.callback.RequestOriginParser;
import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.config.SentinelWebMvcConfig;
import com.alibaba.csp.sentinel.adapter.web.common.UrlCleaner;
import jakarta.servlet.http.HttpServletRequest;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * @author <a href="mailto:[email protected]">Jim</a>
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { SentinelBeanAutowiredTests.TestConfig.class },
		properties = { "spring.cloud.sentinel.filter.order=111" })
public class SentinelBeanAutowiredTests {

	@Autowired
	private UrlCleaner urlCleaner;

	@Autowired
	private BlockExceptionHandler blockExceptionHandler;

	@Autowired
	private RequestOriginParser requestOriginParser;

	@Autowired
	private SentinelProperties sentinelProperties;

	@Autowired
	private SentinelWebMvcConfig sentinelWebMvcConfig;

	@Test
	public void contextLoads() throws Exception {
		assertThat(urlCleaner).isNotNull();
		assertThat(blockExceptionHandler).isNotNull();
		assertThat(requestOriginParser).isNotNull();
		assertThat(sentinelProperties).isNotNull();

		checkUrlPattern();
	}

	private void checkUrlPattern() {
		assertThat(sentinelProperties.getFilter().getOrder()).isEqualTo(111);
		assertThat(sentinelProperties.getFilter().getUrlPatterns().size()).isEqualTo(1);
		assertThat(sentinelProperties.getFilter().getUrlPatterns().get(0))
				.isEqualTo("/**");
	}

	@Test
	public void testBeanAutowired() {
		assertThat(sentinelWebMvcConfig.getUrlCleaner()).isEqualTo(urlCleaner);
		assertThat(sentinelWebMvcConfig.getBlockExceptionHandler())
				.isEqualTo(blockExceptionHandler);
		assertThat(sentinelWebMvcConfig.getOriginParser()).isEqualTo(requestOriginParser);
	}

	@Configuration
	@EnableAutoConfiguration
	@ImportAutoConfiguration({ SentinelAutoConfiguration.class,
			SentinelWebAutoConfiguration.class })
	public static class TestConfig {

		@Bean
		public UrlCleaner urlCleaner() {
			return new UrlCleaner() {
				@Override
				public String clean(String s) {
					return s;
				}
			};
		}

		@Bean
		public RequestOriginParser requestOriginParser() {
			return new RequestOriginParser() {
				@Override
				public String parseOrigin(HttpServletRequest httpServletRequest) {
					return httpServletRequest.getRemoteAddr();
				}
			};
		}

		@Bean
		public BlockExceptionHandler blockExceptionHandler() {
			return new DefaultBlockExceptionHandler();
		}

	}

}