Back to Repositories

Testing Apollo Audit Operator Authentication in apolloconfig/apollo

This test suite validates the Apollo Audit Operator Supplier functionality, focusing on operator authentication and context management in the Apollo Configuration Center. It ensures proper handling of audit spans and operator identification within the Apollo framework.

Test Coverage Overview

The test suite provides comprehensive coverage of the ApolloAuditOperatorSupplier component, focusing on operator identification scenarios.

  • Tests active span operator retrieval
  • Validates anonymous operator fallback
  • Covers request context handling
  • Verifies tracer integration points

Implementation Analysis

The testing approach utilizes Spring Boot’s testing framework with MockBean and SpyBean annotations for dependency management. The implementation leverages Mockito for mocking dependencies and JUnit Jupiter for test execution.

Key patterns include:
  • Request context simulation
  • Audit span context manipulation
  • Mock service integration

Technical Details

Testing infrastructure includes:

  • Spring Boot Test framework
  • JUnit Jupiter test runner
  • Mockito mocking framework
  • Custom ApolloAuditSpan implementation
  • RequestContextHolder configuration

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Java enterprise applications.

  • Proper test isolation using @BeforeEach
  • Effective use of Spring Boot testing annotations
  • Clear test case naming conventions
  • Comprehensive mock object setup
  • Focused test scenarios with single responsibility

apolloconfig/apollo

apollo-audit/apollo-audit-impl/src/test/java/com/ctrip/framework/apollo/audit/spi/ApolloAuditOperatorSupplierTest.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.audit.spi;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.ctrip.framework.apollo.audit.constants.ApolloAuditConstants;
import com.ctrip.framework.apollo.audit.context.ApolloAuditSpan;
import com.ctrip.framework.apollo.audit.context.ApolloAuditSpanContext;
import com.ctrip.framework.apollo.audit.context.ApolloAuditTracer;
import com.ctrip.framework.apollo.audit.spi.defaultimpl.ApolloAuditOperatorDefaultSupplier;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

@SpringBootTest
@ContextConfiguration(classes = ApolloAuditOperatorSupplier.class)
public class ApolloAuditOperatorSupplierTest {

  @SpyBean
  ApolloAuditOperatorDefaultSupplier defaultSupplier;

  @MockBean
  RequestAttributes requestAttributes;
  @MockBean
  ApolloAuditTracer tracer;

  @BeforeEach
  public void setUp() {
    Mockito.when(requestAttributes.getAttribute(
        Mockito.eq(ApolloAuditConstants.TRACER),
        Mockito.eq(RequestAttributes.SCOPE_REQUEST))
    ).thenReturn(tracer);
    RequestContextHolder.setRequestAttributes(requestAttributes);
  }

  @Test
  public void testGetOperatorCaseActiveSpanExist() {
    final String operator = "test";
    {
      ApolloAuditSpan activeSpan = new ApolloAuditSpan();
      activeSpan.setContext(new ApolloAuditSpanContext(null, null, operator, null, null));
      Mockito.when(tracer.getActiveSpan()).thenReturn(activeSpan);
    }

    assertEquals(operator, defaultSupplier.getOperator());
  }

  @Test
  public void testGetOperatorCaseActiveSpanNotExist() {
    assertEquals("anonymous", defaultSupplier.getOperator());
  }

}