Back to Repositories

Validating Sentinel DataSource Properties Configuration in Spring Cloud Alibaba

A comprehensive test suite for validating the DataSourcePropertiesConfiguration in Spring Cloud Alibaba Sentinel. This test suite focuses on verifying the configuration properties and validation of data source fields, particularly for Apollo data source integration.

Test Coverage Overview

The test suite provides focused coverage of the DataSourcePropertiesConfiguration class, specifically targeting the getValidField() method.

Key functionality tested includes:
  • Apollo data source property validation
  • Field validation for active data sources
  • Verification of single data source activation
  • Exclusion of internal fields like $jacocoData

Implementation Analysis

The testing approach utilizes JUnit Jupiter framework with AssertJ assertions for robust verification.

The implementation follows a clear pattern of:
  • Configuration setup with Apollo properties
  • Validation of active data source fields
  • Assertion of expected field counts and contents

Technical Details

Testing tools and configuration:
  • JUnit Jupiter for test execution
  • AssertJ for fluent assertions
  • Mock Apollo configuration with namespace and rule settings
  • RuleType enumeration for flow rule specification

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Clear test method documentation with references to related classes
  • Focused test scenarios with specific assertions
  • Proper separation of configuration and test logic
  • Comprehensive validation of both positive and negative cases

alibaba/spring-cloud-alibaba

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

import com.alibaba.cloud.sentinel.datasource.config.ApolloDataSourceProperties;
import com.alibaba.cloud.sentinel.datasource.config.DataSourcePropertiesConfiguration;
import org.junit.jupiter.api.Test;

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

/**
 * Test cases for {@link DataSourcePropertiesConfiguration}.
 *
 * @author <a href="mailto:[email protected]">hnyyghk</a>
 */
public class DataSourcePropertiesConfigurationTests {

	/**
	 * Test cases for {@link DataSourcePropertiesConfiguration#getValidField()}.
	 *
	 * @see com.alibaba.cloud.sentinel.custom.SentinelDataSourceHandler#afterSingletonsInstantiated()
	 */
	@Test
	public void testGetValidField() {
		DataSourcePropertiesConfiguration configuration = new DataSourcePropertiesConfiguration();
		ApolloDataSourceProperties apollo = new ApolloDataSourceProperties();
		apollo.setNamespaceName("application");
		apollo.setFlowRulesKey("test-flow-rules");
		apollo.setDefaultFlowRuleValue("[]");
		apollo.setDataType("json");
		apollo.setRuleType(RuleType.FLOW);
		configuration.setApollo(apollo);

		//indicate which datasource active
		List<String> validField = configuration.getValidField();

		//not allowed multi datasource active, $jacocoData should not be included
		assertThat(validField.size()).isEqualTo(1);
		assertThat(validField).doesNotContain("$jacocoData");
		assertThat(validField).contains("apollo");
	}

}