Back to Repositories

Validating Administrative Service Operations in Apollo Config

This test suite validates the core administrative functionality of the Apollo Configuration Service, focusing on application management operations including creation, validation, and deletion. The tests ensure proper initialization of configuration hierarchies and verify data integrity across related services.

Test Coverage Overview

The test suite provides comprehensive coverage of administrative operations in Apollo Config Service:

  • Application creation with default cluster and namespace setup
  • Duplicate application creation validation
  • Complete application deletion with cascade operations
  • Audit trail verification for administrative actions

Implementation Analysis

The testing approach employs Spring-based integration testing patterns to validate the AdminService functionality. The implementation leverages JUnit 4 framework with dependency injection through @Autowired annotations to access various service components. Each test case isolates specific administrative operations while maintaining data consistency.

Technical Details

Testing Infrastructure:

  • JUnit 4 testing framework
  • Spring Test context for dependency injection
  • AbstractIntegrationTest as base class for integration testing
  • Multiple service components: AdminService, AuditService, ClusterService, NamespaceService
  • Repository layer integration for data persistence verification

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper test isolation and setup
  • Comprehensive validation of both success and failure scenarios
  • Verification of cascade operations and side effects
  • Clear test method naming and organization
  • Effective use of assertions to validate expected outcomes
  • Exception testing for error conditions

apolloconfig/apollo

apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/AdminServiceTest.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.Audit;
import com.ctrip.framework.apollo.biz.entity.Cluster;
import com.ctrip.framework.apollo.biz.entity.Namespace;
import com.ctrip.framework.apollo.biz.repository.AppRepository;
import com.ctrip.framework.apollo.common.entity.App;
import com.ctrip.framework.apollo.common.exception.ServiceException;
import com.ctrip.framework.apollo.core.ConfigConsts;
import java.util.Date;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class AdminServiceTest extends AbstractIntegrationTest {

  @Autowired
  private AdminService adminService;

  @Autowired
  private AuditService auditService;

  @Autowired
  private AppRepository appRepository;

  @Autowired
  private ClusterService clusterService;

  @Autowired
  private NamespaceService namespaceService;

  @Autowired
  private AppNamespaceService appNamespaceService;

  @Test
  public void testCreateNewApp() {
    String appId = "someAppId";
    App app = new App();
    app.setAppId(appId);
    app.setName("someAppName");
    String owner = "someOwnerName";
    app.setOwnerName(owner);
    app.setOwnerEmail("[email protected]");
    app.setDataChangeCreatedBy(owner);
    app.setDataChangeLastModifiedBy(owner);
    app.setDataChangeCreatedTime(new Date());

    app = adminService.createNewApp(app);
    Assert.assertEquals(appId, app.getAppId());

    List<Cluster> clusters = clusterService.findParentClusters(app.getAppId());
    Assert.assertEquals(1, clusters.size());
    Assert.assertEquals(ConfigConsts.CLUSTER_NAME_DEFAULT, clusters.get(0).getName());

    List<Namespace> namespaces = namespaceService.findNamespaces(appId, clusters.get(0).getName());
    Assert.assertEquals(1, namespaces.size());
    Assert.assertEquals(ConfigConsts.NAMESPACE_APPLICATION, namespaces.get(0).getNamespaceName());

    List<Audit> audits = auditService.findByOwner(owner);
    Assert.assertEquals(4, audits.size());
  }

  @Test(expected = ServiceException.class)
  public void testCreateDuplicateApp() {
    String appId = "someAppId";
    App app = new App();
    app.setAppId(appId);
    app.setName("someAppName");
    String owner = "someOwnerName";
    app.setOwnerName(owner);
    app.setOwnerEmail("[email protected]");
    app.setDataChangeCreatedBy(owner);
    app.setDataChangeLastModifiedBy(owner);
    app.setDataChangeCreatedTime(new Date());

    appRepository.save(app);

    adminService.createNewApp(app);
  }

  @Test
  public void testDeleteApp() {
    String appId = "someAppId";
    App app = new App();
    app.setAppId(appId);
    app.setName("someAppName");
    String owner = "someOwnerName";
    app.setOwnerName(owner);
    app.setOwnerEmail("[email protected]");
    app.setDataChangeCreatedBy(owner);
    app.setDataChangeLastModifiedBy(owner);
    app.setDataChangeCreatedTime(new Date());

    app = adminService.createNewApp(app);

    Assert.assertEquals(appId, app.getAppId());

    Assert.assertEquals(1, appNamespaceService.findByAppId(appId).size());

    Assert.assertEquals(1, clusterService.findClusters(appId).size());

    Assert.assertEquals(1, namespaceService.findNamespaces(appId, ConfigConsts.CLUSTER_NAME_DEFAULT).size());

    adminService.deleteApp(app, owner);

    Assert.assertEquals(0, appNamespaceService.findByAppId(appId).size());

    Assert.assertEquals(0, clusterService.findClusters(appId).size());

    Assert
        .assertEquals(0, namespaceService.findByAppIdAndNamespaceName(appId, ConfigConsts.CLUSTER_NAME_DEFAULT).size());
  }

}