Back to Repositories

Testing Database Discovery Client Self-Instance Management in Apollo Config

This test suite validates the behavior of a database discovery client decorator that ensures self-instance inclusion in service discovery results. It verifies the functionality of DatabaseDiscoveryClientAlwaysAddSelfInstanceDecoratorImpl across different service instance scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of service discovery scenarios:
  • Testing other service name instances retrieval
  • Verifying self-service instance inclusion
  • Validating automatic self-instance addition when missing
Key edge cases include handling duplicate instances and different service names with identical endpoints.

Implementation Analysis

The testing approach uses Mockito for service client simulation and JUnit Jupiter for test execution. The implementation follows a decorator pattern to enhance the DatabaseDiscoveryClient with self-instance management capabilities. Each test case validates specific aspects of service instance handling and list manipulation.

Technical Details

Testing tools and configuration:
  • JUnit Jupiter for test framework
  • Mockito for mock object creation and verification
  • ServiceInstanceFactory for test instance generation
  • Custom assertions for instance list validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Clear test method naming reflecting test scenarios
  • Proper mock setup and verification
  • Isolated test cases with specific assertions
  • Comprehensive edge case coverage
  • Well-documented test intentions

apolloconfig/apollo

apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/registry/DatabaseDiscoveryClientAlwaysAddSelfInstanceDecoratorImplTest.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.registry;

import static com.ctrip.framework.apollo.biz.registry.ServiceInstanceFactory.newServiceInstance;
import static org.junit.jupiter.api.Assertions.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

class DatabaseDiscoveryClientAlwaysAddSelfInstanceDecoratorImplTest {

  @Test
  void getInstances_other_service_name() {
    final String otherServiceName = "other-service";
    DatabaseDiscoveryClient client = Mockito.mock(DatabaseDiscoveryClient.class);
    Mockito.when(client.getInstances(otherServiceName))
        .thenReturn(
            Collections.singletonList(
                newServiceInstance(otherServiceName, "http://10.240.34.56:8081/", "beijing")
            )
        );

    final String selfServiceName = "self-service";
    ServiceInstance selfInstance = newServiceInstance(
        selfServiceName, "http://10.240.34.56:8081/", "beijing"
    );

    DatabaseDiscoveryClient decorator = new DatabaseDiscoveryClientAlwaysAddSelfInstanceDecoratorImpl(
        client, selfInstance
    );

    List<ServiceInstance> serviceInstances = decorator.getInstances(otherServiceName);
    assertEquals(1, serviceInstances.size());
    ServiceInstance otherServiceNameInstance = serviceInstances.get(0);
    assertEquals(otherServiceName, otherServiceNameInstance.getServiceName());

    Mockito.verify(client, Mockito.times(1))
        .getInstances(Mockito.eq(otherServiceName));

    Mockito.verify(client, Mockito.never())
        .getInstances(Mockito.eq(selfServiceName));
  }

  @Test
  void getInstances_contain_self() {
    final String otherServiceName = "other-service";
    DatabaseDiscoveryClient client = Mockito.mock(DatabaseDiscoveryClient.class);
    Mockito.when(client.getInstances(otherServiceName))
        .thenReturn(
            Collections.singletonList(
                newServiceInstance(otherServiceName, "http://10.240.34.56:8081/", "beijing")
            )
        );

    final String selfServiceName = "self-service";
    ServiceInstance selfInstance = newServiceInstance(
        selfServiceName, "http://10.240.34.56:8081/", "beijing"
    );
    Mockito.when(client.getInstances(selfServiceName))
        .thenReturn(
            Arrays.asList(
                selfInstance,
                // same service name but different service instance
                newServiceInstance(selfServiceName, "http://10.240.34.56:8082/", "beijing"),
                newServiceInstance(selfServiceName, "http://10.240.34.56:8083/", "beijing")
            )
        );

    DatabaseDiscoveryClient decorator = new DatabaseDiscoveryClientAlwaysAddSelfInstanceDecoratorImpl(
        client, selfInstance
    );

    List<ServiceInstance> serviceInstances = decorator.getInstances(selfServiceName);
    assertEquals(3, serviceInstances.size());

    Mockito.verify(client, Mockito.times(1))
        .getInstances(Mockito.eq(selfServiceName));

    Mockito.verify(client, Mockito.never())
        .getInstances(Mockito.eq(otherServiceName));
  }

  /**
   * will add self
   */
  @Test
  void getInstances_same_service_name_without_self() {
    final String otherServiceName = "other-service";
    DatabaseDiscoveryClient client = Mockito.mock(DatabaseDiscoveryClient.class);
    Mockito.when(client.getInstances(otherServiceName))
        .thenReturn(
            Collections.singletonList(
                newServiceInstance(otherServiceName, "http://10.240.34.56:8081/", "beijing")
            )
        );

    final String selfServiceName = "self-service";
    ServiceInstance selfInstance = newServiceInstance(
        selfServiceName, "http://10.240.34.56:8081/", "beijing"
    );
    Mockito.when(client.getInstances(selfServiceName))
        .thenReturn(
            Arrays.asList(
                // same service name but different service instance
                newServiceInstance(selfServiceName, "http://10.240.34.56:8082/", "beijing"),
                newServiceInstance(selfServiceName, "http://10.240.34.56:8083/", "beijing")
            )
        );

    DatabaseDiscoveryClient decorator = new DatabaseDiscoveryClientAlwaysAddSelfInstanceDecoratorImpl(
        client, selfInstance
    );

    List<ServiceInstance> serviceInstances = decorator.getInstances(selfServiceName);
    // because mocked data don't contain self instance
    // after add self instance, there are 3 instances now
    assertEquals(3, serviceInstances.size());

    Mockito.verify(client, Mockito.times(1))
        .getInstances(Mockito.eq(selfServiceName));

    Mockito.verify(client, Mockito.never())
        .getInstances(Mockito.eq(otherServiceName));
  }
}