Back to Repositories

Validating Configuration Name Input Rules in Apollo

This test suite validates input handling in the Apollo configuration system, focusing on cluster and namespace name validation. It ensures proper format checking and validation rules for configuration identifiers across the Apollo platform.

Test Coverage Overview

The test suite provides comprehensive coverage for input validation rules in Apollo’s naming conventions. Key areas tested include:

  • Cluster name validation with various character combinations
  • Application namespace naming rules verification
  • Edge cases including null values, empty strings, and invalid characters
  • File extension handling for configuration files

Implementation Analysis

The testing approach utilizes JUnit’s parameterized testing pattern to validate multiple input scenarios efficiently. The implementation employs helper methods for reusable validation logic, ensuring consistent testing across different name formats and rules.

Each test case systematically verifies both valid and invalid input patterns using boolean assertions.

Technical Details

Testing Stack:
  • JUnit 4 testing framework
  • Static assertion methods from JUnit Assert class
  • Custom InputValidator utility class
  • Helper methods for repeated validation scenarios

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear test method naming reflecting purpose
  • Systematic coverage of both positive and negative cases
  • DRY principle through helper method implementation
  • Comprehensive validation of edge cases and boundary conditions
  • Organized test structure with focused test methods

apolloconfig/apollo

apollo-common/src/test/java/com/ctrip/framework/apollo/common/utils/InputValidatorTest.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.common.utils;

import static org.junit.Assert.*;

import org.junit.Test;

public class InputValidatorTest {

  @Test
  public void testValidClusterName() throws Exception {
    checkClusterName("some.cluster-_name.123", true);
    checkClusterName("some.cluster-_name.123.yml", true);
    checkClusterName("some.&.name", false);
    checkClusterName("", false);
    checkClusterName(null, false);
    checkClusterName(".",false);
  }

  @Test
  public void testValidAppNamespaceName() throws Exception {
    checkAppNamespaceName("some.cluster-_name.123", true);
    checkAppNamespaceName("some.&.name", false);
    checkAppNamespaceName("", false);
    checkAppNamespaceName(null, false);
    checkAppNamespaceName("some.name.json", false);
    checkAppNamespaceName("some.name.yml", false);
    checkAppNamespaceName("some.name.yaml", false);
    checkAppNamespaceName("some.name.xml", false);
    checkAppNamespaceName("some.name.properties", false);
    checkAppNamespaceName("..xml", false);
  }

  private void checkClusterName(String name, boolean valid) {
    assertEquals(valid, InputValidator.isValidClusterNamespace(name));
  }

  private void checkAppNamespaceName(String name, boolean valid) {
    assertEquals(valid, InputValidator.isValidAppNamespace(name));
  }
}