Back to Repositories

Validating Problem Report Merger Functionality in Cat Monitoring System

This test suite validates the merging functionality of Problem Reports in the Cat monitoring system, focusing on XML parsing and report consolidation capabilities. It ensures proper handling of historical problem reports and validates memory optimization for mobile reports.

Test Coverage Overview

The test suite provides comprehensive coverage of Problem Report merging scenarios.

Key areas tested include:
  • Merging of old and new problem reports with XML validation
  • Memory optimization for mobile problem reports
  • Report size constraints and thread handling
  • Data integrity preservation during merges

Implementation Analysis

The testing approach employs JUnit framework with XML-based test data validation. The implementation utilizes the DefaultSaxParser for XML processing and custom merger classes for report consolidation.

Key patterns include:
  • File-based test resource loading
  • XML comparison with whitespace normalization
  • Memory footprint validation
  • Source data immutability checks

Technical Details

Testing tools and configuration:
  • JUnit 4 test framework
  • Custom ProblemReportMerger implementation
  • Unidal Helper Files utility
  • UTF-8 encoding for XML processing
  • XML test data files for different scenarios
  • Assert-based validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices for complex data processing systems.

Notable practices include:
  • Separate test cases for functionality and performance
  • Validation of source data preservation
  • Memory consumption checks
  • Clear test method naming
  • Structured test resource organization

dianping/cat

cat-home/src/test/java/com/dianping/cat/report/page/problem/ProblemReportMergerTest.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.page.problem;

import java.util.List;

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

import com.dianping.cat.consumer.problem.ProblemReportMerger;
import com.dianping.cat.consumer.problem.model.entity.Entry;
import com.dianping.cat.consumer.problem.model.entity.Machine;
import com.dianping.cat.consumer.problem.model.entity.ProblemReport;
import com.dianping.cat.consumer.problem.model.transform.DefaultSaxParser;
import com.dianping.cat.report.page.problem.task.HistoryProblemReportMerger;

public class ProblemReportMergerTest {

	@Test
	public void testProblemReportMergeAll() throws Exception {
		String oldXml = Files.forIO().readFrom(getClass().getResourceAsStream("ProblemReportOld.xml"), "utf-8");
		String newXml = Files.forIO().readFrom(getClass().getResourceAsStream("ProblemReportNew.xml"), "utf-8");
		ProblemReport reportOld = DefaultSaxParser.parse(oldXml);
		ProblemReport reportNew = DefaultSaxParser.parse(newXml);
		String expected = Files.forIO().readFrom(getClass().getResourceAsStream("ProblemReportMergeAllResult.xml"),	"utf-8");
		ProblemReportMerger merger = new HistoryProblemReportMerger(new ProblemReport(reportOld.getDomain()));

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

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

	@Test
	public void testProblemReportMergerSize() throws Exception {
		String oldXml = Files.forIO().readFrom(getClass().getResourceAsStream("ProblemMobile.xml"), "utf-8");
		ProblemReport reportOld = DefaultSaxParser.parse(oldXml);
		ProblemReportMerger merger = new HistoryProblemReportMerger(new ProblemReport(reportOld.getDomain()));

		for (int i = 0; i < 24; i++) {
			reportOld.accept(merger);
		}
		ProblemReport problemReport = merger.getProblemReport();
		for (Machine machine : problemReport.getMachines().values()) {
			List<Entry> entries = machine.getEntries();
			for (Entry entry : entries) {
				int size = entry.getThreads().size();
				Assert.assertEquals(0, size);
			}
		}
		Assert.assertEquals(true, (double) problemReport.toString().length() / 1024 / 1024 < 1);
	}
}