Back to Repositories

Testing YAML Configuration Validation in Apollo ItemController

This test suite examines the ItemController class in Apollo, focusing on YAML configuration syntax validation and error handling. It verifies the controller’s ability to process and validate YAML configurations while ensuring proper permission checks and namespace management.

Test Coverage Overview

The test suite provides comprehensive coverage of YAML configuration validation scenarios in the ItemController.

Key areas tested include:
  • Valid YAML syntax verification
  • Detection of duplicated values
  • Handling of unsupported YAML types
  • Integration with configuration and namespace services

Implementation Analysis

The testing approach utilizes Mockito for dependency injection and mock object creation. The suite implements a systematic verification of YAML processing capabilities using predefined test cases stored as resource files.

Key patterns include:
  • Mock injection for service dependencies
  • Exception-based validation testing
  • File-based test data management

Technical Details

Testing tools and configuration:
  • JUnit 4 test framework
  • MockitoJUnitRunner for test execution
  • Guava library for file operations
  • YAML test case files in resources directory
  • Mock objects for ItemService, NamespaceService, UserInfoHolder, and PermissionValidator

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Java unit testing.

Notable practices include:
  • Proper separation of test cases
  • Explicit exception testing
  • Clean test setup with @Before annotation
  • External test data management
  • Comprehensive mock object utilization

apolloconfig/apollo

apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/controller/ItemControllerTest.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.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.portal.component.PermissionValidator;
import com.ctrip.framework.apollo.portal.entity.model.NamespaceTextModel;
import com.ctrip.framework.apollo.portal.service.ItemService;
import com.ctrip.framework.apollo.portal.service.NamespaceService;
import com.ctrip.framework.apollo.portal.spi.UserInfoHolder;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class ItemControllerTest {

  @Mock
  private ItemService configService;
  @Mock
  private NamespaceService namespaceService;
  @Mock
  private UserInfoHolder userInfoHolder;
  @Mock
  private PermissionValidator permissionValidator;

  @InjectMocks
  private ItemController itemController;

  @Before
  public void setUp() throws Exception {
    itemController = new ItemController(configService, userInfoHolder, permissionValidator,
        namespaceService);
  }

  @Test
  public void yamlSyntaxCheckOK() throws Exception {
    String yaml = loadYaml("case1.yaml");

    itemController.doSyntaxCheck(assemble(ConfigFileFormat.YAML.getValue(), yaml));
  }

  @Test(expected = BadRequestException.class)
  public void yamlSyntaxCheckWithDuplicatedValue() throws Exception {
    String yaml = loadYaml("case2.yaml");

    itemController.doSyntaxCheck(assemble(ConfigFileFormat.YAML.getValue(), yaml));
  }

  @Test(expected = BadRequestException.class)
  public void yamlSyntaxCheckWithUnsupportedType() throws Exception {
    String yaml = loadYaml("case3.yaml");

    itemController.doSyntaxCheck(assemble(ConfigFileFormat.YAML.getValue(), yaml));
  }

  private NamespaceTextModel assemble(String format, String content) {
    NamespaceTextModel model = new NamespaceTextModel();
    model.setFormat(format);
    model.setConfigText(content);

    return model;
  }

  private String loadYaml(String caseName) throws IOException {
    File file = new File("src/test/resources/yaml/" + caseName);

    return Files.toString(file, Charsets.UTF_8);
  }
}