Back to Repositories

Testing Server Configuration Management in Apollo Config

This test suite validates the functionality of the ServerConfig controller in Apollo’s admin service, focusing on server configuration management and persistence operations. The tests ensure proper handling of configuration retrieval and updates through REST endpoints.

Test Coverage Overview

The test suite provides comprehensive coverage of ServerConfig operations.

Key areas tested include:
  • Retrieval of all server configurations
  • Creation of new server configurations
  • Updating existing configurations
  • Validation of configuration persistence
Edge cases cover empty configurations and multiple configuration updates.

Implementation Analysis

The testing approach utilizes Spring’s test framework with REST template for HTTP operations testing. The implementation follows a clear pattern of setup-execute-verify using SQL scripts for test data preparation and cleanup.

Notable patterns include:
  • SQL script execution for test data management
  • REST template for API testing
  • Assertion-based verification of responses

Technical Details

Testing tools and configuration:
  • JUnit Jupiter for test execution
  • Spring Test Context for database operations
  • SQL scripts for test data management
  • REST template for HTTP requests
  • AbstractControllerTest base class for common functionality

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Java and Spring applications.

Notable practices include:
  • Proper test isolation using SQL cleanup scripts
  • Clear test method naming conventions
  • Comprehensive assertion checks
  • Modular test data setup
  • Efficient use of Spring testing annotations

apolloconfig/apollo

apollo-adminservice/src/test/java/com/ctrip/framework/apollo/adminservice/controller/ServerConfigControllerTest.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.adminservice.controller;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.ctrip.framework.apollo.biz.entity.ServerConfig;
import org.junit.jupiter.api.Test;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;

/**
 * @author kl (http://kailing.pub)
 * @since 2022/12/14
 */
class ServerConfigControllerTest extends AbstractControllerTest {

  @Test
  @Sql(scripts = "/controller/test-server-config.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
  @Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
  void findAllServerConfig() {
    ServerConfig[] serverConfigs = restTemplate.getForObject(url("/server/config/find-all-config"), ServerConfig[].class);
    assertNotNull(serverConfigs);
    assertEquals(1, serverConfigs.length);
    assertEquals("name", serverConfigs[0].getKey());
    assertEquals("kl", serverConfigs[0].getValue());
  }

  @Test
  @Sql(scripts = "/controller/test-server-config.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
  @Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
  void createOrUpdatePortalDBConfig() {
    ServerConfig serverConfig = new ServerConfig();
    serverConfig.setKey("name");
    serverConfig.setValue("ckl");
    ServerConfig response = restTemplate.postForObject(url("/server/config"), serverConfig, ServerConfig.class);
    assertNotNull(response);

    ServerConfig[] serverConfigs = restTemplate.getForObject(url("/server/config/find-all-config"), ServerConfig[].class);
    assertNotNull(serverConfigs);
    assertEquals(1, serverConfigs.length);
    assertEquals("name", serverConfigs[0].getKey());
    assertEquals("ckl", serverConfigs[0].getValue());

    serverConfig = new ServerConfig();
    serverConfig.setKey("age");
    serverConfig.setValue("30");
    response = restTemplate.postForObject(url("/server/config"), serverConfig, ServerConfig.class);
    assertNotNull(response);

    serverConfigs = restTemplate.getForObject(url("/server/config/find-all-config"), ServerConfig[].class);
    assertNotNull(serverConfigs);
    assertEquals(2, serverConfigs.length);

  }
}