Commit c7751365 authored by wenmo's avatar wenmo

alert 代码美化

parent eabb360b
package com.dlink.alert;
import com.dlink.assertion.Asserts;
import com.dlink.exception.MetaDataException;
import sun.misc.Service;
import java.util.Iterator;
import java.util.Optional;
import java.util.ServiceLoader;
/**
* Alert
......@@ -44,8 +42,8 @@ public interface Alert {
Alert setConfig(AlertConfig config);
default boolean canHandle(String type){
return Asserts.isEqualsIgnoreCase(getType(),type);
default boolean canHandle(String type) {
return Asserts.isEqualsIgnoreCase(getType(), type);
}
String getType();
......
......@@ -23,7 +23,7 @@ public class AlertResult {
}
public Integer getSuccessCode() {
return success?1:0;
return success ? 1 : 0;
}
public void setSuccess(boolean success) {
......
......@@ -6,7 +6,7 @@ package com.dlink.alert;
* @author wenmo
* @since 2022/2/23 21:32
**/
public enum ShowType {
public enum ShowType {
TABLE(0, "markdown"),
TEXT(1, "text");
......
......@@ -86,7 +86,7 @@ public class DingTalkSender {
private String sendMsg(String title, String content) throws IOException {
String msg = generateMsgJson(title, content);
String httpUrl = url;
if(Asserts.isNotNullString(secret)){
if (Asserts.isNotNullString(secret)) {
httpUrl = generateSignedUrl();
}
HttpPost httpPost = new HttpPost(httpUrl);
......@@ -174,8 +174,9 @@ public class DingTalkSender {
}
/**
* 公共生成 markdown 和 text 消息
* @param title 标题
* 公共生成 markdown 和 text 消息
*
* @param title 标题
* @param content 内容
* @param builder 拼接字符串
* @return
......@@ -213,7 +214,7 @@ public class DingTalkSender {
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8");
sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
} catch (Exception e) {
logger.error("generate sign error, message:{}", e);
}
......
......@@ -19,7 +19,7 @@ import java.util.Map;
**/
public class DingTalkTest {
private static Map<String, String> config = new HashMap<>();
private static Map<String, String> config = new HashMap<>();
private String contentTest = "[{\"id\":\"70\","
+
"\"name\":\"UserBehavior-0--1193959466\","
......@@ -39,6 +39,7 @@ public class DingTalkTest {
"\"Host\": \"192.168.xx.xx\","
+
"\"Notify group\" :\"4\"}]";
@Before
public void initDingTalkConfig() {
......@@ -53,7 +54,7 @@ public class DingTalkTest {
}
@Test
public void sendMarkDownMsgTest(){
public void sendMarkDownMsgTest() {
AlertConfig config = AlertConfig.build("MarkDownTest", "DingTalk", DingTalkTest.config);
Alert alert = Alert.build(config);
AlertResult result = alert.send("Dlinky钉钉告警测试", contentTest);
......@@ -61,7 +62,7 @@ public class DingTalkTest {
}
@Test
public void sendTextMsgTest(){
public void sendTextMsgTest() {
config.put(DingTalkConstants.MSG_TYPE, ShowType.TEXT.getValue());
AlertConfig config = AlertConfig.build("TextMsgTest", "DingTalk", DingTalkTest.config);
Alert alert = Alert.build(config);
......
......@@ -72,7 +72,7 @@ public class WeChatSender {
String data = markdownByAlert(title, content);
String msg = weChatUserSendMsg.replace(USER_REG_EXP, mkString(userList))
.replace(AGENT_ID_REG_EXP, weChatAgentId).replace(MSG_REG_EXP, data)
.replace(SHOW_TYPE_REGEX,showType);
.replace(SHOW_TYPE_REGEX, showType);
if (Asserts.isNullString(weChatToken)) {
alertResult.setMessage("send we chat alert fail,get weChat token error");
alertResult.setSuccess(false);
......
......@@ -84,7 +84,7 @@ public class WeChatSenderTest {
@Test
public void testSendWeChatTextMsg() {
weChatConfig.put(WeChatConstants.SHOW_TYPE,ShowType.TEXT.getValue());
weChatConfig.put(WeChatConstants.SHOW_TYPE, ShowType.TEXT.getValue());
WeChatSender weChatSender = new WeChatSender(weChatConfig);
AlertResult alertResult = weChatSender.send("TEXT-TEST", contentTest);
Assert.assertEquals(true, alertResult.getSuccess());
......
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