Back to Repositories

Testing Heartbeat Report Merger Implementation in CAT Monitoring System

A comprehensive test suite for validating the heartbeat report merging functionality in the CAT monitoring system. This test ensures accurate combination of heartbeat data from multiple sources while maintaining data integrity and proper XML formatting.

Test Coverage Overview

The test suite provides thorough coverage of the HeartbeatReport merging functionality.

Key areas tested include:
  • XML parsing and transformation
  • Report data merging accuracy
  • Domain-specific heartbeat data handling
  • String comparison with whitespace normalization

Implementation Analysis

The implementation utilizes JUnit for test execution and follows a clear pattern for merger validation. The test loads sample XML files, parses them into HeartbeatReport objects, performs the merge operation, and validates the results against expected output.

Technical implementation features:
  • SAX parsing for XML processing
  • File I/O operations with UTF-8 encoding
  • Visitor pattern for report merging

Technical Details

Testing tools and configuration:
  • JUnit 4 testing framework
  • Unidal Helper library for file operations
  • DefaultSaxParser for XML parsing
  • Custom HeartbeatReportMerger implementation
  • Resource-based test data files

Best Practices Demonstrated

The test exemplifies several testing best practices including proper resource management, clear test method naming, and explicit assertion messages.

Notable practices:
  • Separation of test data from test logic
  • Consistent error messaging
  • Proper exception handling
  • Platform-independent line ending handling

dianping/cat

cat-consumer/src/test/java/com/dianping/cat/consumer/heartbeat/HeartbeatReportMergerTest.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.consumer.heartbeat;

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

import com.dianping.cat.consumer.heartbeat.model.entity.HeartbeatReport;
import com.dianping.cat.consumer.heartbeat.model.transform.DefaultSaxParser;

public class HeartbeatReportMergerTest {
	@Test
	public void testHeartbeatReportMerge() throws Exception {
		String oldXml = Files.forIO().readFrom(getClass().getResourceAsStream("heartbeat_analyzer_old.xml"), "utf-8");
		String newXml = Files.forIO().readFrom(getClass().getResourceAsStream("heartbeat_analyzer_old.xml"), "utf-8");
		HeartbeatReport reportOld = DefaultSaxParser.parse(oldXml);
		HeartbeatReport reportNew = DefaultSaxParser.parse(newXml);
		String expected = Files.forIO().readFrom(getClass().getResourceAsStream("heartbeat_analyzer_merge.xml"), "utf-8");
		HeartbeatReportMerger merger = new HeartbeatReportMerger(new HeartbeatReport(reportOld.getDomain()));

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

		Assert.assertEquals("Check the merge result!", expected.replace("\r", ""),
								merger.getHeartbeatReport().toString().replace("\r", ""));

	}
}