Back to Repositories

Testing Problem Report Hourly Graph Creation in CAT

This test suite validates the hourly graph creation functionality for problem reports in the CAT monitoring system. It ensures accurate merging and XML processing of problem report data for visualization purposes.

Test Coverage Overview

The test coverage focuses on the ProblemReportHourlyGraphCreator’s ability to process and merge problem reports.

  • Validates XML parsing and processing
  • Tests report merging functionality
  • Verifies graph creation with multiple data inputs
  • Ensures correct XML output generation

Implementation Analysis

The testing approach uses JUnit to validate the graph creation process through XML file comparisons.

Key implementation patterns include:
  • SAX parsing for XML processing
  • File I/O operations for test data loading
  • Report merging with time-based aggregation
  • XML builder pattern for output generation

Technical Details

Testing tools and configuration:

  • JUnit 4 test framework
  • DefaultSaxParser for XML parsing
  • DefaultXmlBuilder for XML generation
  • Files.forIO utility for resource handling
  • UTF-8 encoding for file operations

Best Practices Demonstrated

The test demonstrates strong testing practices through systematic validation of complex data processing.

  • Resource-based test data management
  • Explicit encoding specifications
  • Proper test isolation
  • Comprehensive output validation
  • Clear test method naming

dianping/cat

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

public class ProblemReportHourlyGraphCreatorTest {

	@Test
	public void testGraph() throws Exception {
		String newXml = Files.forIO().readFrom(getClass().getResourceAsStream("problemModel.xml"), "utf-8");
		ProblemReport report1 = DefaultSaxParser.parse(newXml);
		ProblemReport report2 = DefaultSaxParser.parse(newXml);
		String expected = Files.forIO()
								.readFrom(getClass().getResourceAsStream("ProblemReportHourlyGraphResult.xml"),	"utf-8");

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

		ProblemReportHourlyGraphCreator creator = new ProblemReportHourlyGraphCreator(result, 10);

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

		String actual = new DefaultXmlBuilder().buildXml(result);
		Assert.assertEquals("Check the merge result!", expected.replace("\r", ""), actual.replace("\r", ""));
	}
}