Back to Repositories

Testing Apollo DataSource Factory Bean Integration in Spring Cloud Alibaba

This test suite validates the functionality of the ApolloDataSourceFactoryBean implementation in Spring Cloud Alibaba’s Sentinel datasource module. It ensures proper integration between Sentinel and Apollo configuration service for managing flow rules.

Test Coverage Overview

The test coverage focuses on the ApolloDataSourceFactoryBean’s core functionality for managing Sentinel flow rules through Apollo configuration service. Key areas tested include:

  • Factory bean initialization and configuration
  • Flow rule key management
  • Namespace handling
  • Default value processing
  • Data source object creation and access

Implementation Analysis

The testing approach utilizes JUnit 5 with Mockito for dependency isolation. The implementation employs spy and mock patterns to verify the factory bean’s behavior without requiring an actual Apollo connection.

Technical patterns include:
  • Spy pattern for partial mocking of the factory bean
  • Mock objects for JsonConverter and ApolloDataSource
  • Fluent assertions using AssertJ

Technical Details

Testing tools and configuration:

  • JUnit Jupiter for test execution
  • Mockito for mocking and spying
  • AssertJ for assertions
  • Apollo DataSource integration
  • Sentinel flow rule configuration
  • JSON conversion handling

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolation of external dependencies through mocking
  • Comprehensive verification of getter/setter methods
  • Clear test method naming
  • Proper setup and configuration of test objects
  • Verification of both object creation and data access

alibaba/spring-cloud-alibaba

spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/test/java/com/alibaba/cloud/sentinel/datasource/ApolloDataSourceFactoryBeanTests.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.converter.JsonConverter;
import com.alibaba.cloud.sentinel.datasource.factorybean.ApolloDataSourceFactoryBean;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.datasource.apollo.ApolloDataSource;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

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

	private String flowRuleKey = "sentinel";

	private String namespace = "namespace";

	private String defaultFlowValue = "{}";

	@Test
	public void testApolloFactoryBean() throws Exception {
		ApolloDataSourceFactoryBean factoryBean = spy(new ApolloDataSourceFactoryBean());

		Converter converter = mock(JsonConverter.class);

		factoryBean.setDefaultFlowRuleValue(defaultFlowValue);
		factoryBean.setFlowRulesKey(flowRuleKey);
		factoryBean.setNamespaceName(namespace);
		factoryBean.setConverter(converter);

		ApolloDataSource apolloDataSource = mock(ApolloDataSource.class);

		when(apolloDataSource.readSource()).thenReturn("{}");
		doReturn(apolloDataSource).when(factoryBean).getObject();

		assertThat(factoryBean.getObject()).isEqualTo(apolloDataSource);
		assertThat(factoryBean.getObject().readSource()).isEqualTo("{}");
		assertThat(factoryBean.getConverter()).isEqualTo(converter);
		assertThat(factoryBean.getFlowRulesKey()).isEqualTo(flowRuleKey);
		assertThat(factoryBean.getNamespaceName()).isEqualTo(namespace);
		assertThat(factoryBean.getDefaultFlowRuleValue()).isEqualTo(defaultFlowValue);
	}

}