@ -17,13 +17,21 @@
package org.apache.dolphinscheduler.alert.utils ;
package org.apache.dolphinscheduler.alert.utils ;
import com.alibaba.fastjson.JSON ;
import com.alibaba.fastjson.JSON ;
import org.apache.dolphinscheduler.common.enums.AlertType ;
import org.apache.dolphinscheduler.common.enums.ShowType ;
import org.apache.dolphinscheduler.dao.entity.Alert ;
import org.junit.Assert ;
import org.junit.Assert ;
import org.junit.Ignore ;
import org.junit.Ignore ;
import org.junit.Test ;
import org.junit.Test ;
import org.junit.runner.RunWith ;
import org.mockito.Mockito ;
import org.mockito.junit.MockitoJUnitRunner ;
import org.powermock.api.mockito.PowerMockito ;
import org.powermock.core.classloader.annotations.PrepareForTest ;
import org.powermock.modules.junit4.PowerMockRunner ;
import java.io.IOException ;
import java.io.IOException ;
import java.util.Arrays ;
import java.util.* ;
import java.util.Collection ;
/ * *
/ * *
* Please manually modify the configuration file before testing .
* Please manually modify the configuration file before testing .
@ -36,84 +44,211 @@ import java.util.Collection;
* enterprise . wechat . agent . id
* enterprise . wechat . agent . id
* enterprise . wechat . users
* enterprise . wechat . users
* /
* /
@Ignore
@PrepareForTest ( PropertyUtils . class )
@RunWith ( PowerMockRunner . class )
public class EnterpriseWeChatUtilsTest {
public class EnterpriseWeChatUtilsTest {
private String agentId = PropertyUtils . getString ( Constants . ENTERPRISE_WECHAT_AGENT_ID ) ; // app id
private static final String toParty = "wwc99134b6fc1edb6" ;
private Collection < String > listUserId = Arrays . asList ( PropertyUtils . getString ( Constants . ENTERPRISE_WECHAT_USERS ) . split ( "," ) ) ;
private static final String enterpriseWechatSecret = "Uuv2KFrkdf7SeKOsTDCpsTkpawXBMNRhFy6VKX5FV" ;
private static final String enterpriseWechatAgentId = "1000004" ;
private static final String enterpriseWechatUsers = "LiGang,journey" ;
private static final String msg = "hello world" ;
// Please change
private String partyId = "2" ;
private Collection < String > listPartyId = Arrays . asList ( "2" , "4" ) ;
@Test
@Test
public void testSendSingleTeamWeChat ( ) {
public void testIsEnable ( ) {
try {
PowerMockito . mockStatic ( PropertyUtils . class ) ;
String token = EnterpriseWeChatUtils . getToken ( ) ;
Mockito . when ( PropertyUtils . getBoolean ( Constants . ENTERPRISE_WECHAT_ENABLE ) ) . thenReturn ( true ) ;
String msg = EnterpriseWeChatUtils . makeTeamSendMsg ( partyId , agentId , "hello world" ) ;
Boolean weChartEnable = EnterpriseWeChatUtils . isEnable ( ) ;
String resp = EnterpriseWeChatUtils . sendEnterpriseWeChat ( "utf-8" , msg , token ) ;
Assert . assertTrue ( weChartEnable ) ;
String errmsg = JSON . parseObject ( resp ) . getString ( "errmsg" ) ;
Assert . assertEquals ( "ok" , errmsg ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
}
@Test
@Test
public void testSendMultiTeamWeChat ( ) {
public void testMakeTeamSendMsg1 ( ) {
String sendMsg = EnterpriseWeChatUtils . makeTeamSendMsg ( toParty , enterpriseWechatSecret , msg ) ;
Assert . assertTrue ( sendMsg . contains ( toParty ) ) ;
Assert . assertTrue ( sendMsg . contains ( enterpriseWechatSecret ) ) ;
Assert . assertTrue ( sendMsg . contains ( msg ) ) ;
}
try {
String token = EnterpriseWeChatUtils . getToken ( ) ;
String msg = EnterpriseWeChatUtils . makeTeamSendMsg ( listPartyId , agentId , "hello world" ) ;
String resp = EnterpriseWeChatUtils . sendEnterpriseWeChat ( "utf-8" , msg , token ) ;
String errmsg = JSON . parseObject ( resp ) . getString ( "errmsg" ) ;
@Test
Assert . assertEquals ( "ok" , errmsg ) ;
public void testMakeTeamSendMsg2 ( ) {
} catch ( IOException e ) {
List < String > parties = new ArrayList < > ( ) ;
e . printStackTrace ( ) ;
parties . add ( toParty ) ;
parties . add ( "test1" ) ;
String sendMsg = EnterpriseWeChatUtils . makeTeamSendMsg ( parties , enterpriseWechatSecret , msg ) ;
Assert . assertTrue ( sendMsg . contains ( toParty ) ) ;
Assert . assertTrue ( sendMsg . contains ( enterpriseWechatSecret ) ) ;
Assert . assertTrue ( sendMsg . contains ( msg ) ) ;
}
}
@Test
public void tesMakeUserSendMsg1 ( ) {
String sendMsg = EnterpriseWeChatUtils . makeUserSendMsg ( enterpriseWechatUsers , enterpriseWechatAgentId , msg ) ;
Assert . assertTrue ( sendMsg . contains ( enterpriseWechatUsers ) ) ;
Assert . assertTrue ( sendMsg . contains ( enterpriseWechatAgentId ) ) ;
Assert . assertTrue ( sendMsg . contains ( msg ) ) ;
}
}
@Test
@Test
public void testSendSingleUserWeChat ( ) {
public void tesMakeUserSendMsg2 ( ) {
try {
List < String > users = new ArrayList < > ( ) ;
String token = EnterpriseWeChatUtils . getToken ( ) ;
users . add ( "user1" ) ;
String msg = EnterpriseWeChatUtils . makeUserSendMsg ( listUserId . stream ( ) . findFirst ( ) . get ( ) , agentId , "your meeting room has been booked and will be synced to the 'mailbox' later \n" +
users . add ( "user2" ) ;
">**matter details** \n" +
">matter:<font color='info'>meeting</font> <br>" +
String sendMsg = EnterpriseWeChatUtils . makeUserSendMsg ( users , enterpriseWechatAgentId , msg ) ;
">organizer:@miglioguan \n" +
Assert . assertTrue ( sendMsg . contains ( users . get ( 0 ) ) ) ;
">participant:@miglioguan、@kunliu、@jamdeezhou、@kanexiong、@kisonwang \n" +
Assert . assertTrue ( sendMsg . contains ( users . get ( 1 ) ) ) ;
"> \n" +
Assert . assertTrue ( sendMsg . contains ( enterpriseWechatAgentId ) ) ;
">meeting room:<font color='info'>Guangzhou TIT 1st Floor 301</font> \n" +
Assert . assertTrue ( sendMsg . contains ( msg ) ) ;
">date:<font color='warning'>May 18, 2018</font> \n" +
">time:<font color='comment'>9:00-11:00 am</font> \n" +
"> \n" +
">please attend the meeting on time\n" +
"> \n" +
">to modify the meeting information, please click: [Modify Meeting Information](https://work.weixin.qq.com)\"" ) ;
String resp = EnterpriseWeChatUtils . sendEnterpriseWeChat ( "utf-8" , msg , token ) ;
String errmsg = JSON . parseObject ( resp ) . getString ( "errmsg" ) ;
Assert . assertEquals ( "ok" , errmsg ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
@Test
public void testMarkdownByAlertForText ( ) {
Alert alertForText = createAlertForText ( ) ;
String result = EnterpriseWeChatUtils . markdownByAlert ( alertForText ) ;
Assert . assertNotNull ( result ) ;
}
}
@Test
@Test
public void testSendMultiUserWeChat ( ) {
public void testMarkdownByAlertForTable ( ) {
try {
Alert alertForText = createAlertForTable ( ) ;
String token = EnterpriseWeChatUtils . getToken ( ) ;
String result = EnterpriseWeChatUtils . markdownByAlert ( alertForText ) ;
Assert . assertNotNull ( result ) ;
}
String msg = EnterpriseWeChatUtils . makeUserSendMsg ( listUserId , agentId , "hello world" ) ;
private Alert createAlertForText ( ) {
String resp = EnterpriseWeChatUtils . sendEnterpriseWeChat ( "utf-8" , msg , token ) ;
String content = "[\"id:69\"," +
"\"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\"]" ;
Alert alert = new Alert ( ) ;
alert . setTitle ( "Mysql Exception" ) ;
alert . setShowType ( ShowType . TEXT ) ;
alert . setContent ( content ) ;
alert . setAlertType ( AlertType . EMAIL ) ;
alert . setAlertGroupId ( 4 ) ;
return alert ;
}
String errmsg = JSON . parseObject ( resp ) . getString ( "errmsg" ) ;
private String list2String ( ) {
Assert . assertEquals ( "ok" , errmsg ) ;
} catch ( IOException e ) {
LinkedHashMap < String , Object > map1 = new LinkedHashMap < > ( ) ;
e . printStackTrace ( ) ;
map1 . put ( "mysql service name" , "mysql200" ) ;
map1 . put ( "mysql address" , "192.168.xx.xx" ) ;
map1 . put ( "port" , "3306" ) ;
map1 . put ( "no index of number" , "80" ) ;
map1 . put ( "database client connections" , "190" ) ;
LinkedHashMap < String , Object > map2 = new LinkedHashMap < > ( ) ;
map2 . put ( "mysql service name" , "mysql210" ) ;
map2 . put ( "mysql address" , "192.168.xx.xx" ) ;
map2 . put ( "port" , "3306" ) ;
map2 . put ( "no index of number" , "10" ) ;
map2 . put ( "database client connections" , "90" ) ;
List < LinkedHashMap < String , Object > > maps = new ArrayList < > ( ) ;
maps . add ( 0 , map1 ) ;
maps . add ( 1 , map2 ) ;
String mapjson = JSONUtils . toJsonString ( maps ) ;
return mapjson ;
}
}
private Alert createAlertForTable ( ) {
Alert alert = new Alert ( ) ;
alert . setTitle ( "Mysql Exception" ) ;
alert . setShowType ( ShowType . TABLE ) ;
String content = list2String ( ) ;
alert . setContent ( content ) ;
alert . setAlertType ( AlertType . EMAIL ) ;
alert . setAlertGroupId ( 1 ) ;
return alert ;
}
}
// @Test
// public void testSendSingleTeamWeChat() {
// try {
// String token = EnterpriseWeChatUtils.getToken();
// String msg = EnterpriseWeChatUtils.makeTeamSendMsg(partyId, agentId, "hello world");
// String resp = EnterpriseWeChatUtils.sendEnterpriseWeChat("utf-8", msg, token);
//
// String errmsg = JSON.parseObject(resp).getString("errmsg");
// Assert.assertEquals("ok",errmsg);
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
//
// @Test
// public void testSendMultiTeamWeChat() {
//
// try {
// String token = EnterpriseWeChatUtils.getToken();
// String msg = EnterpriseWeChatUtils.makeTeamSendMsg(listPartyId, agentId, "hello world");
// String resp = EnterpriseWeChatUtils.sendEnterpriseWeChat("utf-8", msg, token);
//
// String errmsg = JSON.parseObject(resp).getString("errmsg");
// Assert.assertEquals("ok",errmsg);
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
//
// @Test
// public void testSendSingleUserWeChat() {
// try {
// String token = EnterpriseWeChatUtils.getToken();
// String msg = EnterpriseWeChatUtils.makeUserSendMsg(listUserId.stream().findFirst().get(), agentId, "your meeting room has been booked and will be synced to the 'mailbox' later \n" +
// ">**matter details** \n" +
// ">matter:<font color='info'>meeting</font> <br>" +
// ">organizer:@miglioguan \n" +
// ">participant:@miglioguan、@kunliu、@jamdeezhou、@kanexiong、@kisonwang \n" +
// "> \n" +
// ">meeting room:<font color='info'>Guangzhou TIT 1st Floor 301</font> \n" +
// ">date:<font color='warning'>May 18, 2018</font> \n" +
// ">time:<font color='comment'>9:00-11:00 am</font> \n" +
// "> \n" +
// ">please attend the meeting on time\n" +
// "> \n" +
// ">to modify the meeting information, please click: [Modify Meeting Information](https://work.weixin.qq.com)\"");
//
// String resp = EnterpriseWeChatUtils.sendEnterpriseWeChat("utf-8", msg, token);
//
// String errmsg = JSON.parseObject(resp).getString("errmsg");
// Assert.assertEquals("ok",errmsg);
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
//
// @Test
// public void testSendMultiUserWeChat() {
// try {
// String token = EnterpriseWeChatUtils.getToken();
//
// String msg = EnterpriseWeChatUtils.makeUserSendMsg(listUserId, agentId, "hello world");
// String resp = EnterpriseWeChatUtils.sendEnterpriseWeChat("utf-8", msg, token);
//
// String errmsg = JSON.parseObject(resp).getString("errmsg");
// Assert.assertEquals("ok",errmsg);
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
}
}