EnterpriseWeChatUtilsTest.java 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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.
 */
17

Q
qiaozhanwei 已提交
18
package org.apache.dolphinscheduler.alert.utils;
19

20 21
import org.apache.dolphinscheduler.common.enums.AlertType;
import org.apache.dolphinscheduler.common.enums.ShowType;
22
import org.apache.dolphinscheduler.common.utils.JSONUtils;
23
import org.apache.dolphinscheduler.dao.entity.Alert;
24
import org.apache.dolphinscheduler.plugin.model.AlertData;
25 26 27 28 29

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

30
import org.junit.Assert;
31
import org.junit.Before;
32
import org.junit.Test;
33 34 35 36 37
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
38 39 40 41

/**
 * Please manually modify the configuration file before testing.
 * file: alert.properties
42 43 44 45 46 47 48
 * enterprise.wechat.corp.id
 * enterprise.wechat.secret
 * enterprise.wechat.token.url
 * enterprise.wechat.push.url
 * enterprise.wechat.send.msg
 * enterprise.wechat.agent.id
 * enterprise.wechat.users
49
 */
50 51
@PrepareForTest(PropertyUtils.class)
@RunWith(PowerMockRunner.class)
52 53
public class EnterpriseWeChatUtilsTest {

54 55 56
    private static final String toParty = "wwc99134b6fc1edb6";
    private static final String enterpriseWechatSecret = "Uuv2KFrkdf7SeKOsTDCpsTkpawXBMNRhFy6VKX5FV";
    private static final String enterpriseWechatAgentId = "1000004";
57
    private static final String enterpriseWechatUsers = "LiGang,journey";
58 59
    private static final String msg = "hello world";

60 61 62 63 64 65
    private static final String enterpriseWechatTeamSendMsg = "{\\\"toparty\\\":\\\"{toParty}\\\",\\\"agentid\\\":\\\"{agentId}\\\""
            +
            ",\\\"msgtype\\\":\\\"text\\\",\\\"text\\\":{\\\"content\\\":\\\"{msg}\\\"},\\\"safe\\\":\\\"0\\\"}";
    private static final String enterpriseWechatUserSendMsg = "{\\\"touser\\\":\\\"{toUser}\\\",\\\"agentid\\\":\\\"{agentId}\\\""
            +
            ",\\\"msgtype\\\":\\\"markdown\\\",\\\"markdown\\\":{\\\"content\\\":\\\"{msg}\\\"}}";
66

67
    @Before
68
    public void init() {
69 70
        PowerMockito.mockStatic(PropertyUtils.class);
        Mockito.when(PropertyUtils.getBoolean(Constants.ENTERPRISE_WECHAT_ENABLE)).thenReturn(true);
71 72 73 74 75
        Mockito.when(PropertyUtils.getString(Constants.ENTERPRISE_WECHAT_USER_SEND_MSG)).thenReturn(enterpriseWechatUserSendMsg);
        Mockito.when(PropertyUtils.getString(Constants.ENTERPRISE_WECHAT_TEAM_SEND_MSG)).thenReturn(enterpriseWechatTeamSendMsg);
    }

    @Test
76
    public void testIsEnable() {
77 78 79 80 81
        Boolean weChartEnable = EnterpriseWeChatUtils.isEnable();
        Assert.assertTrue(weChartEnable);
    }

    @Test
82
    public void testMakeTeamSendMsg1() {
83 84 85 86 87 88 89
        String sendMsg = EnterpriseWeChatUtils.makeTeamSendMsg(toParty, enterpriseWechatSecret, msg);
        Assert.assertTrue(sendMsg.contains(toParty));
        Assert.assertTrue(sendMsg.contains(enterpriseWechatSecret));
        Assert.assertTrue(sendMsg.contains(msg));

    }

90
    @Test
91
    public void testMakeTeamSendMsg2() {
92 93 94 95 96 97 98 99
        List<String> parties = new ArrayList<>();
        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));
100 101 102
    }

    @Test
103
    public void tesMakeUserSendMsg1() {
104

105 106 107 108
        String sendMsg = EnterpriseWeChatUtils.makeUserSendMsg(enterpriseWechatUsers, enterpriseWechatAgentId, msg);
        Assert.assertTrue(sendMsg.contains(enterpriseWechatUsers));
        Assert.assertTrue(sendMsg.contains(enterpriseWechatAgentId));
        Assert.assertTrue(sendMsg.contains(msg));
109 110 111
    }

    @Test
112
    public void tesMakeUserSendMsg2() {
113 114 115 116 117 118 119 120 121
        List<String> users = new ArrayList<>();
        users.add("user1");
        users.add("user2");

        String sendMsg = EnterpriseWeChatUtils.makeUserSendMsg(users, enterpriseWechatAgentId, msg);
        Assert.assertTrue(sendMsg.contains(users.get(0)));
        Assert.assertTrue(sendMsg.contains(users.get(1)));
        Assert.assertTrue(sendMsg.contains(enterpriseWechatAgentId));
        Assert.assertTrue(sendMsg.contains(msg));
122 123 124
    }

    @Test
125
    public void testMarkdownByAlertForText() {
126
        Alert alertForText = createAlertForText();
127 128 129 130 131
        AlertData alertData = new AlertData();
        alertData.setTitle(alertForText.getTitle())
                .setShowType(alertForText.getShowType().getDescp())
                .setContent(alertForText.getContent());
        String result = EnterpriseWeChatUtils.markdownByAlert(alertData);
132
        Assert.assertNotNull(result);
133 134
    }

135
    @Test
136
    public void testMarkdownByAlertForTable() {
137
        Alert alertForText = createAlertForTable();
138 139 140 141 142
        AlertData alertData = new AlertData();
        alertData.setTitle(alertForText.getTitle())
                .setShowType(alertForText.getShowType().getDescp())
                .setContent(alertForText.getContent());
        String result = EnterpriseWeChatUtils.markdownByAlert(alertData);
143 144 145
        Assert.assertNotNull(result);
    }

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    private Alert createAlertForText() {
        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\"}]";
166 167 168 169 170 171 172 173 174 175 176

        Alert alert = new Alert();
        alert.setTitle("Mysql Exception");
        alert.setShowType(ShowType.TEXT);
        alert.setContent(content);
        alert.setAlertType(AlertType.EMAIL);
        alert.setAlertGroupId(4);

        return alert;
    }

177
    private String list2String() {
178 179

        LinkedHashMap<String, Object> map1 = new LinkedHashMap<>();
180 181 182 183 184
        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");
185 186

        LinkedHashMap<String, Object> map2 = new LinkedHashMap<>();
187 188
        map2.put("mysql service name", "mysql210");
        map2.put("mysql address", "192.168.xx.xx");
189 190 191 192 193 194 195 196 197 198 199
        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;
    }

200
    private Alert createAlertForTable() {
201 202 203
        Alert alert = new Alert();
        alert.setTitle("Mysql Exception");
        alert.setShowType(ShowType.TABLE);
204
        String content = list2String();
205 206 207 208 209 210 211
        alert.setContent(content);
        alert.setAlertType(AlertType.EMAIL);
        alert.setAlertGroupId(1);
        return alert;
    }


212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    //    @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 = JSONUtils.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 = JSONUtils.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 = JSONUtils.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 = JSONUtils.parseObject(resp).getString("errmsg");
    //            Assert.assertEquals("ok",errmsg);
    //        } catch (IOException e) {
    //            e.printStackTrace();
    //        }
    //    }
282

283
}