Back to Repositories

Testing Search Controller Implementation in Apollo Config

This test suite validates the SearchController functionality in Apollo’s portal module, focusing on application and namespace search capabilities. The tests verify search behavior with different query parameters and pagination.

Test Coverage Overview

The test suite provides comprehensive coverage of the SearchController’s search functionality.

  • Tests empty search key scenarios
  • Validates application search by ID and name
  • Verifies item search functionality with environment-specific behavior
  • Tests pagination and result aggregation across environments

Implementation Analysis

The implementation uses Mockito for dependency mocking and JUnit for test execution. The tests follow a structured pattern of setting up test data, configuring mock behaviors, executing the search operation, and verifying results and interactions.

Key patterns include mock injection, behavior verification, and test data generation using helper methods.

Technical Details

  • Testing Framework: JUnit with MockitoJUnitRunner
  • Mocking: Mockito for service and component mocks
  • Pagination: Spring PageRequest implementation
  • Test Data: Helper methods for generating App and NamespaceDTO test data
  • Environment: Multiple environment testing (DEV, FAT)

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper separation of test cases for different scenarios
  • Comprehensive mock verification
  • Helper methods for test data generation
  • Clear test method naming
  • Effective use of assertions to validate results

apolloconfig/apollo

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

import com.google.common.collect.Lists;

import com.ctrip.framework.apollo.common.dto.NamespaceDTO;
import com.ctrip.framework.apollo.common.dto.PageDTO;
import com.ctrip.framework.apollo.common.entity.App;
import com.ctrip.framework.apollo.portal.component.PortalSettings;
import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.service.AppService;
import com.ctrip.framework.apollo.portal.service.NamespaceService;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.util.List;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;


/**
 * @author lepdou 2021-09-13
 */
@RunWith(MockitoJUnitRunner.class)
public class SearchControllerTest {

  @Mock
  private AppService       appService;
  @Mock
  private NamespaceService namespaceService;
  @Mock
  private PortalSettings   portalSettings;
  @Mock
  private PortalConfig     portalConfig;
  @InjectMocks
  private SearchController searchController;

  @Test
  public void testSearchByEmptyKey() {
    PageRequest request = PageRequest.of(0, 20);
    searchController.search("", request);
    verify(appService, times(1)).findAll(request);
  }

  @Test
  public void testSearchApp() {
    String query = "timeout";
    PageRequest request = PageRequest.of(0, 20);

    PageDTO<App> apps = genPageApp(10, request, 100);

    when(appService.searchByAppIdOrAppName(query, request)).thenReturn(apps);

    searchController.search(query, request);

    verify(appService, times(0)).findAll(request);
    verify(appService, times(1)).searchByAppIdOrAppName(query, request);
  }

  @Test
  public void testSearchItemSwitch() {
    String query = "timeout";
    PageRequest request = PageRequest.of(0, 20);

    PageDTO<App> apps = new PageDTO<>(Lists.newLinkedList(), request, 0);

    when(appService.searchByAppIdOrAppName(query, request)).thenReturn(apps);
    when(portalConfig.supportSearchByItem()).thenReturn(false);

    PageDTO<App> result = searchController.search(query, request);

    Assert.assertFalse(result.hasContent());
    verify(appService, times(0)).findAll(request);
    verify(appService, times(1)).searchByAppIdOrAppName(query, request);
  }

  @Test
  public void testSearchItem() {
    String query = "timeout";
    PageRequest request = PageRequest.of(0, 20);

    PageDTO<App> apps = new PageDTO<>(Lists.newLinkedList(), request, 0);
    PageDTO<NamespaceDTO> devNamespaces = genPageNamespace(10, request, 20);
    PageDTO<NamespaceDTO> fatNamespaces = genPageNamespace(15, request, 30);

    when(appService.searchByAppIdOrAppName(query, request)).thenReturn(apps);
    when(portalConfig.supportSearchByItem()).thenReturn(true);
    when(portalSettings.getActiveEnvs()).thenReturn(Lists.newArrayList(Env.DEV, Env.FAT));
    when(namespaceService.findNamespacesByItem(Env.DEV, query, request)).thenReturn(devNamespaces);
    when(namespaceService.findNamespacesByItem(Env.FAT, query, request)).thenReturn(fatNamespaces);

    PageDTO<App> result = searchController.search(query, request);

    Assert.assertTrue(result.hasContent());
    Assert.assertEquals(25, result.getContent().size());
    Assert.assertEquals(30, result.getTotal());
    verify(appService, times(0)).findAll(request);
    verify(appService, times(1)).searchByAppIdOrAppName(query, request);
    verify(namespaceService).findNamespacesByItem(Env.DEV, query, request);
    verify(namespaceService).findNamespacesByItem(Env.FAT, query, request);
  }

  private PageDTO<App> genPageApp(int size, Pageable pageable, int total) {
    List<App> result = Lists.newLinkedList();
    for (int i = 0; i < size; i++) {
      App app = new App();
      result.add(app);
    }
    return new PageDTO<>(result, pageable, total);
  }

  private PageDTO<NamespaceDTO> genPageNamespace(int size, Pageable pageable, int total) {
    List<NamespaceDTO> result = Lists.newLinkedList();
    for (int i = 0; i < size; i++) {
      NamespaceDTO namespaceDTO = new NamespaceDTO();
      result.add(namespaceDTO);
    }
    return new PageDTO<>(result, pageable, total);
  }
}