Back to Repositories

Validating Sentinel RuleType Resolution in Spring Cloud Alibaba

This test suite validates the RuleType functionality in Spring Cloud Alibaba Sentinel’s datasource module. It verifies the correct mapping and identification of different Sentinel rule types through name-based and class-based lookups.

Test Coverage Overview

The test suite provides comprehensive coverage of RuleType validation mechanisms.

Key areas tested include:
  • Name-based rule type resolution for valid and invalid inputs
  • Class-based rule type identification
  • Validation of all supported rule types: Flow, Degrade, Param-Flow, System, and Authority
  • Edge case handling for empty strings and invalid rule names

Implementation Analysis

The testing approach utilizes JUnit 5 with AssertJ assertions for clear and expressive test cases.

The implementation follows a systematic pattern of testing both positive and negative scenarios for rule type resolution, ensuring robust validation of the RuleType enum functionality. Test cases verify both the presence of valid rules and the absence of invalid ones using Optional return types.

Technical Details

Testing stack includes:
  • JUnit Jupiter for test execution
  • AssertJ for fluent assertions
  • Sentinel core classes for rule definitions
  • Integration with Spring Cloud Alibaba’s Sentinel datasource module

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Comprehensive positive and negative test cases
  • Clear test method naming
  • Systematic validation of all enum values
  • Proper use of assertion libraries
  • Focused test methods with single responsibility

alibaba/spring-cloud-alibaba

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

import com.alibaba.csp.sentinel.slots.block.AbstractRule;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import org.junit.jupiter.api.Test;

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

/**
 * @author <a href="mailto:[email protected]">Jim</a>
 */
public class RuleTypeTests {

	@Test
	public void testGetByName() {
		assertThat(RuleType.getByName("").isPresent()).isEqualTo(Boolean.FALSE);
		assertThat(RuleType.getByName("test").isPresent()).isEqualTo(Boolean.FALSE);
		assertThat(RuleType.getByName("param_flow").isPresent()).isEqualTo(Boolean.FALSE);
		assertThat(RuleType.getByName("param").isPresent()).isEqualTo(Boolean.FALSE);
		assertThat(RuleType.getByName("FLOW").isPresent()).isEqualTo(Boolean.FALSE);
		assertThat(RuleType.getByName("flow").isPresent()).isEqualTo(Boolean.TRUE);
		assertThat(RuleType.getByName("degrade").isPresent()).isEqualTo(Boolean.TRUE);
		assertThat(RuleType.getByName("param-flow").isPresent()).isEqualTo(Boolean.TRUE);
		assertThat(RuleType.getByName("system").isPresent()).isEqualTo(Boolean.TRUE);
		assertThat(RuleType.getByName("authority").isPresent()).isEqualTo(Boolean.TRUE);
		assertThat(RuleType.getByName("flow").get()).isEqualTo(RuleType.FLOW);
		assertThat(RuleType.getByName("degrade").get()).isEqualTo(RuleType.DEGRADE);
		assertThat(RuleType.getByName("param-flow").get()).isEqualTo(RuleType.PARAM_FLOW);
		assertThat(RuleType.getByName("system").get()).isEqualTo(RuleType.SYSTEM);
		assertThat(RuleType.getByName("authority").get()).isEqualTo(RuleType.AUTHORITY);
	}

	@Test
	public void testGetByClass() {
		assertThat(RuleType.getByClass(Object.class).isPresent())
				.isEqualTo(Boolean.FALSE);
		assertThat(RuleType.getByClass(AbstractRule.class).isPresent())
				.isEqualTo(Boolean.FALSE);
		assertThat(RuleType.getByClass(FlowRule.class).isPresent())
				.isEqualTo(Boolean.TRUE);
		assertThat(RuleType.getByClass(DegradeRule.class).isPresent())
				.isEqualTo(Boolean.TRUE);
		assertThat(RuleType.getByClass(ParamFlowRule.class).isPresent())
				.isEqualTo(Boolean.TRUE);
		assertThat(RuleType.getByClass(SystemRule.class).isPresent())
				.isEqualTo(Boolean.TRUE);
		assertThat(RuleType.getByClass(AuthorityRule.class).isPresent())
				.isEqualTo(Boolean.TRUE);
	}

}