Back to Repositories

Testing Service Report Merger Implementation in CAT Monitoring System

This test suite validates the merging functionality of Service Reports in the CAT monitoring system. It ensures proper XML parsing and report combination while maintaining data integrity and original report states.

Test Coverage Overview

The test suite provides comprehensive coverage of the ServiceReportMerger functionality.

Key areas tested include:
  • XML parsing of old and new service reports
  • Merging of report data while preserving original content
  • Validation of merged output against expected results
  • Character encoding handling (UTF-8)
  • Verification of source report immutability

Implementation Analysis

The testing approach utilizes JUnit framework with XML file-based test data. The implementation follows a clear pattern of loading test files, parsing XML content, performing merge operations, and validating results through string comparison.

Key technical aspects include:
  • SAX parsing implementation for XML processing
  • Visitor pattern usage for report merging
  • Resource file handling for test data
  • Carriage return normalization for cross-platform compatibility

Technical Details

Testing tools and configuration:
  • JUnit 4 testing framework
  • DefaultSaxParser for XML processing
  • Files.forIO utility for resource handling
  • UTF-8 encoding for file operations
  • Custom ServiceReportMerger implementation
  • Assert statements for validation

Best Practices Demonstrated

The test implementation showcases several testing best practices and quality approaches.

Notable practices include:
  • Separate test data files for input and expected output
  • Explicit encoding specification
  • Platform-independent comparison logic
  • Source data immutability verification
  • Clear test method naming and organization
  • Proper exception handling

dianping/cat

cat-home/src/test/java/com/dianping/cat/report/task/service/ServiceReportMergerTest.java

            
/*
 * Copyright (c) 2011-2018, Meituan Dianping. All Rights Reserved.
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.dianping.cat.report.task.service;

import org.junit.Assert;
import org.junit.Test;
import org.unidal.helper.Files;

import com.dianping.cat.home.service.entity.ServiceReport;
import com.dianping.cat.home.service.transform.DefaultSaxParser;
import com.dianping.cat.report.page.statistics.task.service.ServiceReportMerger;

public class ServiceReportMergerTest {
	@Test
	public void testServiceReportMerge() throws Exception {
		String oldXml = Files.forIO().readFrom(getClass().getResourceAsStream("ServiceReportOld.xml"), "utf-8");
		String newXml = Files.forIO().readFrom(getClass().getResourceAsStream("ServiceReportNew.xml"), "utf-8");
		ServiceReport reportOld = DefaultSaxParser.parse(oldXml);
		ServiceReport reportNew = DefaultSaxParser.parse(newXml);
		String expected = Files.forIO().readFrom(getClass().getResourceAsStream("ServiceReportResult.xml"),	"utf-8");
		ServiceReportMerger merger = new ServiceReportMerger(new ServiceReport(reportOld.getDomain()));

		reportOld.accept(merger);
		reportNew.accept(merger);

		Assert.assertEquals("Check the merge result!", expected.replace("\r", ""),
								merger.getServiceReport().toString().replace("\r", ""));
		Assert.assertEquals("Source report is changed!", newXml.replace("\r", ""), reportNew.toString().replace("\r", ""));
	}
}