Back to Repositories

Testing HeartbeatDailyMerger Report Aggregation in CAT Monitoring System

This test suite validates the daily merging functionality of heartbeat reports in the CAT monitoring system. It ensures accurate aggregation of heartbeat data across different time periods while maintaining data integrity and timestamp handling.

Test Coverage Overview

The test suite provides comprehensive coverage of the HeartbeatDailyMerger component, focusing on merging multiple heartbeat reports into a consolidated daily report.

Key areas tested include:
  • XML parsing and report generation
  • Timestamp handling and date formatting
  • Report merging logic across different time periods
  • Data consistency validation between input and merged reports

Implementation Analysis

The testing approach utilizes JUnit framework with a focus on validating the merger’s core functionality through XML-based test data. The implementation employs the DefaultSaxParser for XML processing and SimpleDateFormat for precise timestamp handling.

Key patterns include:
  • File-based test data loading
  • Timestamp manipulation for different time periods
  • String comparison for validation with whitespace normalization

Technical Details

Testing tools and configuration:
  • JUnit 4 testing framework
  • DefaultSaxParser for XML processing
  • SimpleDateFormat for date handling
  • Files.forIO utility for resource loading
  • Test resources: heartbeat.xml and dailyReport.xml

Best Practices Demonstrated

The test implementation showcases several testing best practices for complex data processing systems.

Notable practices include:
  • Isolated test data through resource files
  • Precise timestamp control for reproducible tests
  • Normalized string comparison for reliable assertions
  • Clear separation of test setup and validation

dianping/cat

cat-home/src/test/java/com/dianping/cat/report/task/heartbeat/HeartbeatDailyMergerTest.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.heartbeat;

import java.text.SimpleDateFormat;

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;
import com.dianping.cat.report.page.heartbeat.task.HeartbeatDailyMerger;

public class HeartbeatDailyMergerTest {

	private SimpleDateFormat m_sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Test
	public void test() throws Exception {
		String oldXml = Files.forIO().readFrom(getClass().getResourceAsStream("heartbeat.xml"), "utf-8");
		HeartbeatReport report1 = DefaultSaxParser.parse(oldXml);
		report1.setStartTime(m_sdf.parse("2015-02-26 00:00:00"));
		HeartbeatReport report2 = DefaultSaxParser.parse(oldXml);
		report2.setStartTime(m_sdf.parse("2015-02-26 05:00:00"));
		String result = Files.forIO().readFrom(getClass().getResourceAsStream("dailyReport.xml"), "utf-8");

		HeartbeatDailyMerger merger = new HeartbeatDailyMerger(new HeartbeatReport("cat"),
								m_sdf.parse("2015-02-26 00:00:00").getTime());

		merger.visitHeartbeatReport(report1);
		merger.visitHeartbeatReport(report2);
		Assert.assertEquals("Check the merge result!", result.replace("\r", ""),
								merger.getHeartbeatReport().toString().replace("\r", ""));
	}

}