Back to Repositories

Testing Server Configuration Management Implementation in Apollo Config

This test suite validates the ServerConfigService functionality in Apollo, focusing on server configuration management operations. It verifies both the retrieval of configurations and the creation/update mechanisms of server configs.

Test Coverage Overview

The test suite provides comprehensive coverage of ServerConfigService operations.

Key areas tested include:
  • Retrieval of all server configurations
  • Creation of new server configurations
  • Update of existing server configurations
  • Validation of configuration properties including key, value, and cluster settings

Implementation Analysis

The testing approach utilizes JUnit with Spring’s dependency injection for service testing. It extends AbstractIntegrationTest for setup consistency and employs AssertJ for fluent assertions.

The tests follow a clear pattern of arrangement, action, and assertion, particularly evident in the createOrUpdateConfig test which verifies both creation and update scenarios.

Technical Details

Testing tools and configuration:
  • JUnit 4 testing framework
  • Spring Framework for dependency injection
  • AssertJ for assertions
  • AbstractIntegrationTest base class for common setup
  • Autowired ServerConfigService component

Best Practices Demonstrated

The test suite exemplifies several testing best practices including:

  • Clear test method naming that describes the functionality being tested
  • Comprehensive assertion chains to verify multiple aspects of the results
  • Proper separation of concerns with focused test methods
  • Integration test inheritance for consistent test environment setup

apolloconfig/apollo

apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/ServerConfigServiceTest.java

            
/*
 * Copyright 2024 Apollo 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
 *
 * http://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.ctrip.framework.apollo.biz.service;

import com.ctrip.framework.apollo.biz.AbstractIntegrationTest;
import com.ctrip.framework.apollo.biz.entity.ServerConfig;
import java.util.List;

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

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author kl (http://kailing.pub)
 * @since 2022/12/14
 */
public class ServerConfigServiceTest extends AbstractIntegrationTest {

  @Autowired
  private ServerConfigService serverConfigService;

  @Test
  public void findAll() {
    List<ServerConfig> serverConfigs = serverConfigService.findAll();
    assertThat(serverConfigs).isNotNull();
    assertThat(serverConfigs.size()).isGreaterThanOrEqualTo(0);
  }

  @Test
  public void createOrUpdateConfig() {
    ServerConfig serverConfig = new ServerConfig();
    serverConfig.setKey("name");
    serverConfig.setValue("kl");
    serverConfigService.createOrUpdateConfig(serverConfig);

    List<ServerConfig> serverConfigs = serverConfigService.findAll();
    assertThat(serverConfigs).isNotNull();
    assertThat(serverConfigs.get(0).getValue()).isEqualTo("kl");
    assertThat(serverConfigs.get(0).getCluster()).isEqualTo("default");
    assertThat(serverConfigs.get(0).getKey()).isEqualTo("name");


    serverConfig.setValue("kl2");
    serverConfigService.createOrUpdateConfig(serverConfig);

    serverConfigs = serverConfigService.findAll();
    assertThat(serverConfigs).isNotNull();
    assertThat(serverConfigs.size()).isEqualTo(1);
    assertThat(serverConfigs.get(0).getValue()).isEqualTo("kl2");
    assertThat(serverConfigs.get(0).getKey()).isEqualTo("name");

    serverConfig = new ServerConfig();
    serverConfig.setKey("name2");
    serverConfig.setValue("kl2");
    serverConfigService.createOrUpdateConfig(serverConfig);

    serverConfigs = serverConfigService.findAll();
    assertThat(serverConfigs).isNotNull();
    assertThat(serverConfigs.size()).isEqualTo(2);
  }
}