Back to Repositories

Validating Commit History Pagination Controls in Apollo Config

This test suite validates the CommitController functionality in Apollo’s portal module, focusing on pagination parameter validation for commit history retrieval. It ensures proper error handling and input validation for the commits endpoint.

Test Coverage Overview

The test suite covers input validation for the commit history pagination API endpoint.

Key areas tested include:
  • Negative page number validation
  • Zero size parameter validation
  • Error message content verification
  • REST endpoint parameter handling

Implementation Analysis

The testing approach utilizes Spring’s RestTemplate for HTTP request simulation and JUnit assertions for validation. The test extends AbstractIntegrationTest to leverage common testing infrastructure and employs exception handling patterns to verify error scenarios.

Key implementation aspects:
  • REST endpoint testing with path variables
  • Exception message content validation
  • Integration with Spring’s testing framework

Technical Details

Testing tools and configuration:
  • JUnit 4 testing framework
  • Spring RestTemplate for HTTP requests
  • Hamcrest matchers for assertions
  • AbstractIntegrationTest base class for setup
  • HTTP client error handling

Best Practices Demonstrated

The test demonstrates several testing best practices including proper error scenario validation, clear test method naming, and robust assertion handling.

Notable practices:
  • Explicit failure conditions with fail() assertions
  • Descriptive error message validation
  • Isolated test scenarios
  • Clear test method naming conventions

apolloconfig/apollo

apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/controller/CommitControllerTest.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.portal.AbstractIntegrationTest;
import org.junit.Test;
import org.springframework.web.client.HttpClientErrorException;

import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.fail;

/**
 * Created by kezhenxu at 2019/1/14 12:49.
 *
 * @author kezhenxu (kezhenxu at lizhi dot fm)
 */
public class CommitControllerTest extends AbstractIntegrationTest {

  @Test
  public void shouldFailWhenPageOrSiseIsNegative() {
    try {
      restTemplate.getForEntity(
          url("/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/commits?page=-1"),
          List.class, "1", "env", "cl", "ns"
      );
      fail("should throw");
    } catch (final HttpClientErrorException e) {
     assertThat(
          new String(e.getResponseBodyAsByteArray()), containsString("page should be positive or 0")
      );
    }
    try {
      restTemplate.getForEntity(
          url("/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/commits?size=0"),
          List.class, "1", "env", "cl", "ns"
      );
      fail("should throw");
    } catch (final HttpClientErrorException e) {
      assertThat(
          new String(e.getResponseBodyAsByteArray()), containsString("size should be positive number")
      );
    }
  }
}