Commit 803a9266 authored by zhu-mingye's avatar zhu-mingye

钉钉/企微告警测试 & fix bug

parent d2dbb824
...@@ -8,7 +8,7 @@ package com.dlink.alert; ...@@ -8,7 +8,7 @@ package com.dlink.alert;
**/ **/
public enum ShowType { public enum ShowType {
TABLE(0, "table"), TABLE(0, "markdown"),
TEXT(1, "text"); TEXT(1, "text");
private int code; private int code;
......
...@@ -18,14 +18,7 @@ import org.slf4j.Logger; ...@@ -18,14 +18,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;
...@@ -44,6 +37,7 @@ public class WeChatSender { ...@@ -44,6 +37,7 @@ public class WeChatSender {
private static final String CORP_ID_REGEX = "{corpId}"; private static final String CORP_ID_REGEX = "{corpId}";
private static final String SECRET_REGEX = "{secret}"; private static final String SECRET_REGEX = "{secret}";
private static final String TOKEN_REGEX = "{token}"; private static final String TOKEN_REGEX = "{token}";
private static final String SHOW_TYPE_REGEX = "{showType}";
private final String weChatAgentId; private final String weChatAgentId;
private final String weChatUsers; private final String weChatUsers;
private final String weChatUserSendMsg; private final String weChatUserSendMsg;
...@@ -71,15 +65,14 @@ public class WeChatSender { ...@@ -71,15 +65,14 @@ public class WeChatSender {
public AlertResult send(String title, String content) { public AlertResult send(String title, String content) {
AlertResult alertResult = new AlertResult(); AlertResult alertResult = new AlertResult();
;
List<String> userList = new ArrayList<>(); List<String> userList = new ArrayList<>();
if (Asserts.isNotNullString(weChatUsers)) { if (Asserts.isNotNullString(weChatUsers)) {
userList = Arrays.asList(weChatUsers.split(",")); userList = Arrays.asList(weChatUsers.split(","));
} }
String data = markdownByAlert(title, content); String data = markdownByAlert(title, content);
String msg = weChatUserSendMsg.replace(USER_REG_EXP, mkString(userList)) String msg = weChatUserSendMsg.replace(USER_REG_EXP, mkString(userList))
.replace(AGENT_ID_REG_EXP, weChatAgentId) .replace(AGENT_ID_REG_EXP, weChatAgentId).replace(MSG_REG_EXP, data)
.replace(MSG_REG_EXP, data); .replace(SHOW_TYPE_REGEX,showType);
if (Asserts.isNullString(weChatToken)) { if (Asserts.isNullString(weChatToken)) {
alertResult.setMessage("send we chat alert fail,get weChat token error"); alertResult.setMessage("send we chat alert fail,get weChat token error");
alertResult.setStatus(ALERT_STATUS); alertResult.setStatus(ALERT_STATUS);
......
/*
* 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.dlink.alert.wechat;
import com.dlink.alert.AlertResult;
import com.dlink.alert.ShowType;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
/**
* WeChatSenderTest
*/
public class WeChatSenderTest {
private static Map<String, String> weChatConfig = new HashMap<>();
private String contentTest = "[{\"id\":\"70\","
+
"\"name\":\"UserBehavior-0--1193959466\","
+
"\"Job name\":\"Start workflow\","
+
"\"State\":\"SUCCESS\","
+
"\"Recovery\":\"NO\","
+
"\"Run time\":\"1\","
+
"\"Start time\": \"2018-08-06 10:31:34.0\","
+
"\"End time\": \"2018-08-06 10:31:49.0\","
+
"\"Host\": \"192.168.xx.xx\","
+
"\"Notify group\" :\"4\"}]";
/**
* init WeChatConfig
*/
@Before
public void initWeChatConfig() {
// Just for this test, I will delete these configurations before this PR is merged
weChatConfig.put(WeChatConstants.AGENT_ID, "AGENT_ID");
weChatConfig.put(WeChatConstants.SECRET, "SECRET");
weChatConfig.put(WeChatConstants.CORP_ID, "CORP_ID");
weChatConfig.put(WeChatConstants.CHARSET, "UTF-8");
weChatConfig.put(WeChatConstants.USER_SEND_MSG, "{\"touser\":\"{toUser}\",\"agentid\":{agentId}"
+
",\"msgtype\":\"{showType}\",\"{showType}\":{\"content\":\"{msg}\"}}"
);
weChatConfig.put(WeChatConstants.USERS, "USERS");
weChatConfig.put(WeChatConstants.TEAM_SEND_MSG, "msg");
weChatConfig.put(WeChatConstants.SHOW_TYPE, ShowType.TABLE.getValue());// default is "table"
weChatConfig.put(WeChatConstants.SEND_TYPE, WeChatType.APP.getValue());
}
@Test
public void testSendWeChatTableMsg() {
WeChatSender weChatSender = new WeChatSender(weChatConfig);
AlertResult alertResult = weChatSender.send("TABLE-TEST", contentTest);
Assert.assertEquals("true", alertResult.getStatus());
}
@Test
public void testSendWeChatTextMsg() {
weChatConfig.put(WeChatConstants.SHOW_TYPE,ShowType.TEXT.getValue());
WeChatSender weChatSender = new WeChatSender(weChatConfig);
AlertResult alertResult = weChatSender.send("TEXT-TEST", contentTest);
Assert.assertEquals("true", alertResult.getStatus());
}
}
import React, {useEffect, useState} from 'react'; import React, {useState} from 'react';
import {Form, Button,Radio, Input, Modal, Divider,Switch} from 'antd'; import {Button, Divider, Form, Input, Modal, Radio, Switch} from 'antd';
import {AlertInstanceTableListItem} from "@/pages/AlertInstance/data"; import {AlertInstanceTableListItem} from "@/pages/AlertInstance/data";
import {buildJSONData, getJSONData} from "@/pages/AlertInstance/function"; import {buildJSONData, getJSONData} from "@/pages/AlertInstance/function";
import {ALERT_TYPE} from "@/pages/AlertInstance/conf"; import {ALERT_TYPE} from "@/pages/AlertInstance/conf";
...@@ -105,7 +105,7 @@ const WeChatForm: React.FC<AlertInstanceFormProps> = (props) => { ...@@ -105,7 +105,7 @@ const WeChatForm: React.FC<AlertInstanceFormProps> = (props) => {
rules={[{required: true, message: '请输入展示方式!'}]} rules={[{required: true, message: '请输入展示方式!'}]}
> >
<Radio.Group > <Radio.Group >
<Radio value='table'>表格</Radio> <Radio value='markdown'>MarkDown</Radio>
<Radio value='text'>文本</Radio> <Radio value='text'>文本</Radio>
</Radio.Group> </Radio.Group>
</Form.Item> </Form.Item>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment