Back to Repositories

Testing User-Defined Rule Execution System in CAT

This test suite validates the functionality of user-defined rules in the CAT monitoring system. It focuses on testing the dynamic rule evaluation mechanism that allows custom monitoring rules to be defined and executed at runtime.

Test Coverage Overview

The test coverage focuses on the UserDefineRule implementation and its rule execution capabilities.

  • Tests empty array handling for rule validation
  • Verifies correct rule execution with populated arrays
  • Validates rule result formatting and return value pairs

Implementation Analysis

The testing approach uses JUnit to verify the RuleType enumeration and dynamic rule execution system. It implements a mock user-defined rule that processes numerical arrays and returns boolean-string pairs indicating alert status.

  • Uses string-based rule definition for dynamic compilation
  • Tests rule execution through RuleType.executeRule method
  • Validates Pair return format

Technical Details

Testing infrastructure leverages:

  • JUnit 4 testing framework
  • Unidal Tuple library for pair operations
  • Dynamic rule compilation and execution system
  • Custom RuleType enumeration for rule management

Best Practices Demonstrated

The test suite demonstrates several testing best practices for dynamic rule systems.

  • Separate test cases for empty and populated inputs
  • Clear assertion messages and expected values
  • Proper exception handling and validation
  • Comprehensive edge case coverage

dianping/cat

cat-home/src/test/java/com/dianping/cat/report/alert/UserDefineRuleTest.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.alert;

import org.junit.Assert;
import org.junit.Test;
import org.unidal.tuple.Pair;

import com.dianping.cat.alarm.spi.rule.RuleType;

public class UserDefineRuleTest {
	@Test
	public void testUserDefineRule() throws Exception {
		String userDefineStr = "import org.unidal.tuple.Pair; import com.dianping.cat.report.alert.RuleType.MonitorRule; public class UserDefinedRule implements MonitorRule{ /* * 请编写checkData()方法, 除了import标准库,其余部分不能改变 * 该方法接受两个参数: values:当前值数组 baselineValue:基线值数组 * 该方法返回一个Pair对象,key是boolean类型,表明是否触发告警; value是String类型,表明告警内容 * 如:没有触发,返回:return new Pair<Boolean, String>(false, \"\"); * 触发报警,返回:return new Pair<Boolean, String>(true, \"alert info\"); */ @Override public Pair<Boolean, String> checkData(double[] values, double[] baselineValues) { if(values.length <= 0){ return new Pair<Boolean, String>(false, \"\"); } else{ return new Pair<Boolean, String>(true, Double.toString(values[0])); } } }";
		RuleType userDefineRuleType = RuleType.getByTypeId(RuleType.UserDefine.getId());
		Pair<Boolean, String> result = userDefineRuleType.executeRule(new double[0], new double[0], userDefineStr);
		Assert.assertFalse(result.getKey());

		double[] array = { 10.0 };
		result = userDefineRuleType.executeRule(array, array, userDefineStr);
		Assert.assertTrue(result.getKey());
		Assert.assertEquals("10.0", result.getValue());
	}
}