Back to Repositories

Testing Problem Report Daily Graph Generation in CAT Monitoring System

This test suite validates the daily graph creation functionality for problem reports in the CAT monitoring system. It ensures accurate merging and processing of problem report data across multiple time periods.

Test Coverage Overview

The test suite provides comprehensive coverage of the ProblemReportDailyGraphCreator functionality.

Key areas tested include:
  • XML parsing and processing of base problem reports
  • Graph creation with specified time periods
  • Merging of multiple problem reports
  • Validation of generated XML output against expected results

Implementation Analysis

The testing approach utilizes JUnit framework for unit testing, with a focus on XML-based data processing.

Key implementation patterns include:
  • File-based test data loading using resource streams
  • SAX parsing for XML processing
  • Date-based report generation with specific timeframes
  • String comparison for XML validation

Technical Details

Testing tools and configuration:
  • JUnit 4 testing framework
  • DefaultSaxParser for XML parsing
  • DefaultXmlBuilder for XML generation
  • SimpleDateFormat for date handling
  • Files.forIO() utility for resource management

Best Practices Demonstrated

The test implementation showcases several testing best practices.

Notable practices include:
  • Clean separation of test data and test logic
  • Explicit date formatting and handling
  • Comprehensive input/output validation
  • Proper resource file management
  • Careful handling of platform-specific line endings

dianping/cat

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

import java.text.SimpleDateFormat;

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

import com.dianping.cat.consumer.problem.model.entity.ProblemReport;
import com.dianping.cat.consumer.problem.model.transform.DefaultSaxParser;
import com.dianping.cat.consumer.problem.model.transform.DefaultXmlBuilder;
import com.dianping.cat.report.page.problem.task.ProblemReportDailyGraphCreator;

public class ProblemReportDailyGraphCreatorTest {

	@Test
	public void test() throws Exception {
		String oldXml1 = Files.forIO().readFrom(getClass().getResourceAsStream("BaseDailyProblemReport1.xml"),	"utf-8");
		String oldXml2 = Files.forIO().readFrom(getClass().getResourceAsStream("BaseDailyProblemReport2.xml"),	"utf-8");

		ProblemReport report1 = DefaultSaxParser.parse(oldXml1);
		ProblemReport report2 = DefaultSaxParser.parse(oldXml2);
		String expected = Files.forIO()
								.readFrom(getClass().getResourceAsStream("ProblemReportDailyGraphResult.xml"), "utf-8");

		ProblemReport result = new ProblemReport(report1.getDomain());

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ProblemReportDailyGraphCreator creator = new ProblemReportDailyGraphCreator(result, 7,
								sdf.parse("2016-01-23 00:00:00"));

		creator.createGraph(report1);
		creator.createGraph(report2);

		String actual = new DefaultXmlBuilder().buildXml(result);

		Assert.assertEquals("Check the merge result!", expected.replace("\r", ""), actual.replace("\r", ""));
	}
}