Back to Repositories

Validating Nacos DataSource Configuration in Spring Cloud Alibaba

This test suite validates the functionality of Nacos DataSource Properties in Spring Cloud Alibaba’s Sentinel integration. It ensures proper configuration and handling of Nacos properties for both address-based and credential-based setups.

Test Coverage Overview

The test suite provides comprehensive coverage of NacosDataSourceProperties configuration scenarios.

Key areas tested include:
  • Address-based Nacos configuration with server address, context path, and rule types
  • Credential-based configuration with access keys and endpoints
  • Property validation for both configuration approaches
  • Integration with RuleType enumeration and factory bean naming

Implementation Analysis

The testing approach utilizes JUnit 5 framework with AssertJ assertions for validation. Each test method focuses on a specific configuration scenario, implementing a clear separation of concerns between address-based and properties-based configurations.

The implementation leverages property setters and getters verification to ensure proper data handling and persistence.

Technical Details

Testing tools and configuration:
  • JUnit Jupiter for test execution
  • AssertJ for fluent assertions
  • NacosDataSourceProperties for configuration management
  • RuleType enumeration for flow and system rules
  • Integration with NacosDataSourceFactoryBean

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Java unit testing.

Notable practices include:
  • Clear test method naming reflecting test scenarios
  • Isolated test cases for different configuration types
  • Comprehensive property verification
  • Use of modern assertion libraries
  • Proper test organization and separation

alibaba/spring-cloud-alibaba

spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/test/java/com/alibaba/cloud/sentinel/datasource/NacosDataSourcePropertiesTests.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.cloud.sentinel.datasource.config.NacosDataSourceProperties;
import com.alibaba.cloud.sentinel.datasource.factorybean.NacosDataSourceFactoryBean;
import org.junit.jupiter.api.Test;

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

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

	@Test
	public void testNacosWithAddr() {
		NacosDataSourceProperties nacosDataSourceProperties = new NacosDataSourceProperties();
		nacosDataSourceProperties.setServerAddr("127.0.0.1:8848");
		nacosDataSourceProperties.setContextPath("/my-nacos");
		nacosDataSourceProperties.setRuleType(RuleType.FLOW);
		nacosDataSourceProperties.setDataId("sentinel");
		nacosDataSourceProperties.setGroupId("custom-group");
		nacosDataSourceProperties.setDataType("xml");

		assertThat(nacosDataSourceProperties.getContextPath()).isEqualTo("/my-nacos");
		assertThat(nacosDataSourceProperties.getGroupId()).isEqualTo("custom-group");
		assertThat(nacosDataSourceProperties.getDataId()).isEqualTo("sentinel");
		assertThat(nacosDataSourceProperties.getDataType()).isEqualTo("xml");
		assertThat(nacosDataSourceProperties.getRuleType()).isEqualTo(RuleType.FLOW);
		assertThat(nacosDataSourceProperties.getFactoryBeanName())
				.isEqualTo(NacosDataSourceFactoryBean.class.getName());
	}

	@Test
	public void testNacosWithProperties() {
		NacosDataSourceProperties nacosDataSourceProperties = new NacosDataSourceProperties();
		nacosDataSourceProperties.setAccessKey("ak");
		nacosDataSourceProperties.setSecretKey("sk");
		nacosDataSourceProperties.setEndpoint("endpoint");
		nacosDataSourceProperties.setNamespace("namespace");
		nacosDataSourceProperties.setRuleType(RuleType.SYSTEM);

		assertThat(nacosDataSourceProperties.getAccessKey()).isEqualTo("ak");
		assertThat(nacosDataSourceProperties.getSecretKey()).isEqualTo("sk");
		assertThat(nacosDataSourceProperties.getEndpoint()).isEqualTo("endpoint");
		assertThat(nacosDataSourceProperties.getNamespace()).isEqualTo("namespace");
		assertThat(nacosDataSourceProperties.getRuleType()).isEqualTo(RuleType.SYSTEM);
	}

}