Back to Repositories

Testing Alert Notification Sender Components in CAT Monitoring System

This test suite validates the alert sender functionality in the CAT monitoring system, focusing on different notification channels including email, SMS, and WeChat. It ensures proper message delivery and formatting across multiple communication platforms.

Test Coverage Overview

The test suite provides comprehensive coverage of the alert sender system by validating:
  • Email notification functionality through MailSender
  • SMS message delivery via SmsSender
  • WeChat integration using WeixinSender
  • Proper message formatting and content delivery
  • Alert type handling for transaction monitoring

Implementation Analysis

The testing approach utilizes JUnit framework with ComponentTestCase as the base class for dependency injection and component lookup. The implementation validates sender interfaces through practical test scenarios with actual message content and recipient information.

The test demonstrates component isolation and integration testing patterns, focusing on the verification of multiple sender implementations.

Technical Details

Key technical components include:
  • JUnit test framework for test execution
  • ComponentTestCase for dependency management
  • SendMessageEntity for message encapsulation
  • Multiple sender implementations (Mail, SMS, WeChat)
  • AlertType enumeration for categorization

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Clear separation of concerns for different sender types
  • Practical test data usage with realistic message content
  • Proper assertion handling for success verification
  • Component-based testing architecture
  • Comprehensive validation across multiple notification channels

dianping/cat

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

import java.util.Arrays;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;
import org.unidal.lookup.ComponentTestCase;

import com.dianping.cat.alarm.spi.AlertType;
import com.dianping.cat.alarm.spi.sender.MailSender;
import com.dianping.cat.alarm.spi.sender.SendMessageEntity;
import com.dianping.cat.alarm.spi.sender.Sender;
import com.dianping.cat.alarm.spi.sender.SmsSender;
import com.dianping.cat.alarm.spi.sender.WeixinSender;

public class SenderTest extends ComponentTestCase {

	@Test
	public void test() {
		Map<String, Sender> mailSender = lookupMap(Sender.class);
		String content = "[CAT 第三方告警] [项目: ] : [[type=get, details=HTTP URL[1234568888888888.com?] GET访问出现异常]][时间: 2015-01-15 18:20] \n<a href='http://cat/r/p?domain=&date=2015011518'>点击此处查看详情</a>";
		SendMessageEntity entity = new SendMessageEntity("CAT", "[CAT第三方告警] [项目: ]", AlertType.Transaction.getName(),	content,
								Arrays.asList("[email protected]"));
		SendMessageEntity entity2 = new SendMessageEntity("CAT", "[CAT第三方告警] [项目: ]", AlertType.Transaction.getName(),	content,
								Arrays.asList("15201789489"));

		Assert.assertEquals(true, mailSender.get(MailSender.ID).send(entity));
		Assert.assertEquals(true, mailSender.get(WeixinSender.ID).send(entity));
		Assert.assertEquals(true, mailSender.get(SmsSender.ID).send(entity2));
	}

	//    @Test
	//    public void testJson() {
	//        SubItem item = new SubItem();
	//        item.setTest("subTest");
	//
	//        Item i = new Item();
	//        i.setTest("test");
	//        i.setItem(item);
	//
	//        JsonBuilder jsonBuilder = new JsonBuilder();
	//        String json = jsonBuilder.toJson(i);
	//        System.out.println(json);
	//        Item result = (Item) jsonBuilder.parse(json, Item.class);
	//        System.out.println(jsonBuilder.toJson(result));
	//    }

	public static class Item {
		private String test;

		private SubItem item;

		public String getTest() {
			return test;
		}

		public void setTest(String test) {
			this.test = test;
		}

		public SubItem getItem() {
			return item;
		}

		public void setItem(SubItem item) {
			this.item = item;
		}

	}

	public static class SubItem {
		private String test;

		public String getTest() {
			return test;
		}

		public void setTest(String test) {
			this.test = test;
		}
	}
}