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
Implementation Analysis
Technical Details
Best Practices Demonstrated
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");
}
}