Back to Repositories

Testing KeyValue Utilities Map Operations in Apollo Config

This test suite validates the KeyValueUtils class functionality in Apollo Portal, focusing on key filtering and suffix manipulation operations. The tests ensure reliable handling of map entries with specific key patterns and suffix removal capabilities.

Test Coverage Overview

The test suite provides comprehensive coverage of key-value manipulation utilities:
  • Key filtering based on case-insensitive suffix matching
  • Suffix removal from map keys
  • Edge case handling for various key patterns and suffixes
  • Verification of map transformation operations

Implementation Analysis

The testing approach employs JUnit to validate map manipulation methods:
  • Uses HashMap for test data preparation
  • Implements assertion-based verification
  • Tests both positive and negative scenarios
  • Validates string manipulation operations on map keys

Technical Details

Testing infrastructure includes:
  • JUnit 4 testing framework
  • Java Collections Framework (HashMap)
  • Assert methods for validation
  • Static utility method testing patterns

Best Practices Demonstrated

The test suite exemplifies quality testing practices:
  • Clear test method naming conventions
  • Isolated test cases with specific assertions
  • Comprehensive validation of transformation results
  • Effective use of test data setup

apolloconfig/apollo

apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/util/KeyValueUtilsTest.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.portal.util;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class KeyValueUtilsTest {

    @Test
    public void testFilterWithKeyEndswith() {
        // map
        Map<String, String> map = new HashMap<>();
        map.put("abc.met", "none");
        map.put("abc_meta", "none");
        map.put("2bc.meta", "none");
        map.put("abc?met", "none");
        Map<String, String> afterFilter = KeyValueUtils.filterWithKeyIgnoreCaseEndsWith(map, "_meta");
        for(Map.Entry<String, String> entry : afterFilter.entrySet()) {
            String key = entry.getKey();
            assertTrue(key.endsWith("_meta"));
        }
    }

    @Test
    public void testRemoveKeySuffix() {
        Map<String, String> map = new HashMap<>();
        map.put("abc_meta", "none");
        map.put("234_meta", "none");
        map.put("888_meta", "none");
        Map<String, String> afterFilter = KeyValueUtils.removeKeySuffix(map, "_meta".length());
        for(Map.Entry<String, String> entry : afterFilter.entrySet()) {
            String key = entry.getKey();
            assertFalse(key.endsWith("_meta"));
            assertFalse(key.contains("_meta"));
        }
    }

}