Back to Repositories

Testing AccessKey Service Management in Apollo Configuration System

This test suite validates the AccessKeyService functionality in Apollo, focusing on key creation and management. It ensures proper handling of access key creation with validation checks and error scenarios.

Test Coverage Overview

The test coverage focuses on core access key management operations in Apollo.

  • Validates successful access key creation with valid parameters
  • Tests maximum limit enforcement for access keys per application
  • Verifies exception handling for exceeded key limits

Implementation Analysis

The testing approach utilizes JUnit framework with Spring integration testing capabilities. Tests are structured using AbstractIntegrationTest as the base class, enabling comprehensive service-level validation.

  • Employs dependency injection for service testing
  • Implements proper test isolation and setup
  • Uses expected exception testing pattern

Technical Details

  • JUnit 4 testing framework
  • Spring Test context integration
  • AbstractIntegrationTest base class for setup
  • Autowired service injection
  • BadRequestException validation

Best Practices Demonstrated

The test suite demonstrates several testing best practices for service-layer components.

  • Clear test method naming conventions
  • Proper separation of test cases
  • Helper methods for test data creation
  • Explicit exception testing
  • Integration test inheritance structure

apolloconfig/apollo

apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/AccessKeyServiceTest.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 static org.junit.Assert.assertNotNull;

import com.ctrip.framework.apollo.biz.AbstractIntegrationTest;
import com.ctrip.framework.apollo.biz.entity.AccessKey;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author nisiyong
 */
public class AccessKeyServiceTest extends AbstractIntegrationTest {

  @Autowired
  private AccessKeyService accessKeyService;

  @Test
  public void testCreate() {
    String appId = "someAppId";
    String secret = "someSecret";
    AccessKey entity = assembleAccessKey(appId, secret);

    AccessKey accessKey = accessKeyService.create(appId, entity);

    assertNotNull(accessKey);
  }

  @Test(expected = BadRequestException.class)
  public void testCreateWithException() {
    String appId = "someAppId";
    String secret = "someSecret";
    int maxCount = 5;

    for (int i = 0; i <= maxCount; i++) {
      AccessKey entity = assembleAccessKey(appId, secret);
      accessKeyService.create(appId, entity);
    }
  }

  private AccessKey assembleAccessKey(String appId, String secret) {
    AccessKey accessKey = new AccessKey();
    accessKey.setAppId(appId);
    accessKey.setSecret(secret);
    return accessKey;
  }
}