Back to Repositories

Testing AppNamespace Controller Operations in Apollo Config

This test suite validates the functionality of Apollo’s AppNamespace controller, focusing on namespace creation and persistence operations. The tests ensure proper handling of application namespace management within the Apollo configuration system.

Test Coverage Overview

The test suite provides comprehensive coverage of AppNamespace creation operations.

Key areas tested include:
  • Namespace creation with valid application ID
  • Data persistence verification
  • Automatic timestamp and audit field population
  • Response validation for created namespace objects

Implementation Analysis

The testing approach utilizes Spring’s REST template for HTTP endpoint validation and JUnit for assertions. The implementation leverages SQL scripts for test cleanup and employs Spring’s dependency injection for repository access.

Notable patterns include:
  • Controller-level integration testing
  • Repository autowiring for data verification
  • POST endpoint testing with DTO objects

Technical Details

Testing tools and configuration:
  • JUnit test framework
  • Spring Test Context framework
  • SQL execution scripts for cleanup
  • REST template for HTTP operations
  • Repository integration for data verification

Best Practices Demonstrated

The test suite demonstrates several testing best practices for Spring-based applications.

Notable practices include:
  • Proper test cleanup using SQL scripts
  • Comprehensive assertion checking
  • Clear test method naming
  • Separation of test data setup and verification
  • Effective use of Spring testing annotations

apolloconfig/apollo

apollo-adminservice/src/test/java/com/ctrip/framework/apollo/adminservice/controller/AppNamespaceControllerTest.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 com.ctrip.framework.apollo.biz.repository.AppNamespaceRepository;
import com.ctrip.framework.apollo.common.dto.AppNamespaceDTO;
import com.ctrip.framework.apollo.common.entity.AppNamespace;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.jdbc.Sql;

public class AppNamespaceControllerTest extends AbstractControllerTest{

  @Autowired
  private AppNamespaceRepository namespaceRepository;

  @Test
  @Sql(scripts = "/controller/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
  public void testCreate(){
    String appId = "6666";
    String name = "testnamespace";
    String comment = "comment";
    AppNamespaceDTO dto = new AppNamespaceDTO();
    dto.setAppId(appId);
    dto.setName(name);
    dto.setComment(comment);
    dto.setDataChangeCreatedBy("apollo");

    AppNamespaceDTO resultDto = restTemplate.postForEntity(url("/apps/{appId}/appnamespaces"), dto, AppNamespaceDTO.class, appId).getBody();

    Assert.assertNotNull(resultDto);
    Assert.assertEquals(appId, resultDto.getAppId());
    Assert.assertTrue(resultDto.getId() > 0);

    AppNamespace savedAppNs = namespaceRepository.findByAppIdAndName(appId, name);
    Assert.assertNotNull(savedAppNs);
    Assert.assertNotNull(savedAppNs.getDataChangeCreatedTime());
    Assert.assertNotNull(savedAppNs.getDataChangeLastModifiedTime());
    Assert.assertNotNull(savedAppNs.getDataChangeLastModifiedBy());
    Assert.assertNotNull(savedAppNs.getDataChangeCreatedBy());
  }
}