Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
D
dlink
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhaowei
dlink
Commits
803a9266
Commit
803a9266
authored
Mar 01, 2022
by
zhu-mingye
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
钉钉/企微告警测试 & fix bug
parent
d2dbb824
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
100 additions
and
15 deletions
+100
-15
ShowType.java
...nk-alert-base/src/main/java/com/dlink/alert/ShowType.java
+1
-1
WeChatSender.java
...at/src/main/java/com/dlink/alert/wechat/WeChatSender.java
+4
-11
WeChatSenderTest.java
...rc/test/java/com/dlink/alert/wechat/WeChatSenderTest.java
+92
-0
WeChatForm.tsx
dlink-web/src/pages/AlertInstance/components/WeChatForm.tsx
+3
-3
No files found.
dlink-alert/dlink-alert-base/src/main/java/com/dlink/alert/ShowType.java
View file @
803a9266
...
@@ -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
;
...
...
dlink-alert/dlink-alert-wechat/src/main/java/com/dlink/alert/wechat/WeChatSender.java
View file @
803a9266
...
@@ -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
);
...
...
dlink-alert/dlink-alert-wechat/src/test/java/com/dlink/alert/wechat/WeChatSenderTest.java
0 → 100644
View file @
803a9266
/*
* 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
());
}
}
dlink-web/src/pages/AlertInstance/components/WeChatForm.tsx
View file @
803a9266
import
React
,
{
use
Effect
,
use
State
}
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
>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment