Back to Repositories

Validating Namespace Handling Operations in Apollo Config

This test suite validates the NamespaceUtil class functionality in Apollo Config Service, focusing on namespace name filtering and normalization operations. The tests ensure proper handling of namespace names with different file extensions and cases, as well as verification of both private and public namespace resolution.

Test Coverage Overview

The test suite provides comprehensive coverage of namespace handling functionality:
  • Namespace name filtering for different file extensions (.properties, .xml)
  • Case sensitivity handling in namespace names
  • Multiple extension handling
  • Private and public namespace resolution
  • Edge cases for namespace normalization failures

Implementation Analysis

The testing approach utilizes JUnit and Mockito frameworks to validate the NamespaceUtil implementation:
  • Mock objects for AppNamespaceServiceWithCache
  • Systematic test organization with @Before setup
  • Assertion-based verification using assertEquals
  • Behavior verification using Mockito verify()

Technical Details

Testing infrastructure and setup:
  • JUnit 4 test runner with MockitoJUnitRunner
  • Mock injection using @Mock annotation
  • setUp() method with @Before for test initialization
  • Mockito stubbing for service responses
  • Verification of service method calls and interaction patterns

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Clear test method naming reflecting test scenarios
  • Isolated test cases for different functionality aspects
  • Proper mock object usage and verification
  • Comprehensive edge case coverage
  • Clean setup and teardown patterns

apolloconfig/apollo

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

import com.ctrip.framework.apollo.common.entity.AppNamespace;
import com.ctrip.framework.apollo.configservice.service.AppNamespaceServiceWithCache;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

/**
 * @author Jason Song([email protected])
 */
@RunWith(MockitoJUnitRunner.class)
public class NamespaceUtilTest {
  private NamespaceUtil namespaceUtil;

  @Mock
  private AppNamespaceServiceWithCache appNamespaceServiceWithCache;

  @Before
  public void setUp() throws Exception {
    namespaceUtil = new NamespaceUtil(appNamespaceServiceWithCache);
  }

  @Test
  public void testFilterNamespaceName() throws Exception {
    String someName = "a.properties";

    assertEquals("a", namespaceUtil.filterNamespaceName(someName));
  }

  @Test
  public void testFilterNamespaceNameUnchanged() throws Exception {
    String someName = "a.xml";

    assertEquals(someName, namespaceUtil.filterNamespaceName(someName));
  }

  @Test
  public void testFilterNamespaceNameWithMultiplePropertiesSuffix() throws Exception {
    String someName = "a.properties.properties";

    assertEquals("a.properties", namespaceUtil.filterNamespaceName(someName));
  }

  @Test
  public void testFilterNamespaceNameWithRandomCase() throws Exception {
    String someName = "AbC.ProPErties";

    assertEquals("AbC", namespaceUtil.filterNamespaceName(someName));
  }

  @Test
  public void testFilterNamespaceNameWithRandomCaseUnchanged() throws Exception {
    String someName = "AbCD.xMl";

    assertEquals(someName, namespaceUtil.filterNamespaceName(someName));
  }

  @Test
  public void testNormalizeNamespaceWithPrivateNamespace() throws Exception {
    String someAppId = "someAppId";
    String someNamespaceName = "someNamespaceName";
    String someNormalizedNamespaceName = "someNormalizedNamespaceName";
    AppNamespace someAppNamespace = mock(AppNamespace.class);

    when(someAppNamespace.getName()).thenReturn(someNormalizedNamespaceName);
    when(appNamespaceServiceWithCache.findByAppIdAndNamespace(someAppId, someNamespaceName)).thenReturn
        (someAppNamespace);

    assertEquals(someNormalizedNamespaceName, namespaceUtil.normalizeNamespace(someAppId, someNamespaceName));

    verify(appNamespaceServiceWithCache, times(1)).findByAppIdAndNamespace(someAppId, someNamespaceName);
    verify(appNamespaceServiceWithCache, never()).findPublicNamespaceByName(someNamespaceName);
  }

  @Test
  public void testNormalizeNamespaceWithPublicNamespace() throws Exception {
    String someAppId = "someAppId";
    String someNamespaceName = "someNamespaceName";
    String someNormalizedNamespaceName = "someNormalizedNamespaceName";
    AppNamespace someAppNamespace = mock(AppNamespace.class);

    when(someAppNamespace.getName()).thenReturn(someNormalizedNamespaceName);
    when(appNamespaceServiceWithCache.findByAppIdAndNamespace(someAppId, someNamespaceName)).thenReturn(null);
    when(appNamespaceServiceWithCache.findPublicNamespaceByName(someNamespaceName)).thenReturn(someAppNamespace);

    assertEquals(someNormalizedNamespaceName, namespaceUtil.normalizeNamespace(someAppId, someNamespaceName));

    verify(appNamespaceServiceWithCache, times(1)).findByAppIdAndNamespace(someAppId, someNamespaceName);
    verify(appNamespaceServiceWithCache, times(1)).findPublicNamespaceByName(someNamespaceName);
  }

  @Test
  public void testNormalizeNamespaceFailed() throws Exception {
    String someAppId = "someAppId";
    String someNamespaceName = "someNamespaceName";

    when(appNamespaceServiceWithCache.findByAppIdAndNamespace(someAppId, someNamespaceName)).thenReturn(null);
    when(appNamespaceServiceWithCache.findPublicNamespaceByName(someNamespaceName)).thenReturn(null);

    assertEquals(someNamespaceName, namespaceUtil.normalizeNamespace(someAppId, someNamespaceName));

    verify(appNamespaceServiceWithCache, times(1)).findByAppIdAndNamespace(someAppId, someNamespaceName);
    verify(appNamespaceServiceWithCache, times(1)).findPublicNamespaceByName(someNamespaceName);
  }
}