From 9eb823c0aa3d95b268f9f388fa533cfb75de5027 Mon Sep 17 00:00:00 2001 From: "DK.Pino" Date: Tue, 7 Jan 2020 10:20:58 +0800 Subject: [PATCH] Refactor alert template (#1713) * refactor Alert Template * refactor alert template(remove freemarker) * add license * update abstract class to interface * make test into root pom * fix powermock static * fix powermock static * remove custom exception * remove custom exception --- dolphinscheduler-alert/pom.xml | 27 ++- .../alert/template/AlertTemplate.java | 41 +++++ .../alert/template/AlertTemplateFactory.java | 54 ++++++ .../template/impl/DefaultHTMLTemplate.java | 161 ++++++++++++++++++ .../alert/utils/Constants.java | 4 +- .../alert/utils/MailUtils.java | 120 +------------ .../src/main/resources/alert.properties | 3 + .../mail_templates/alert_mail_template.ftl | 17 -- .../template/AlertTemplateFactoryTest.java | 66 +++++++ .../impl/DefaultHTMLTemplateTest.java | 123 +++++++++++++ .../alert/utils/MailUtilsTest.java | 51 +----- pom.xml | 2 + 12 files changed, 484 insertions(+), 185 deletions(-) create mode 100644 dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/AlertTemplate.java create mode 100644 dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/AlertTemplateFactory.java create mode 100644 dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/impl/DefaultHTMLTemplate.java delete mode 100644 dolphinscheduler-alert/src/main/resources/mail_templates/alert_mail_template.ftl create mode 100644 dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/template/AlertTemplateFactoryTest.java create mode 100644 dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/template/impl/DefaultHTMLTemplateTest.java diff --git a/dolphinscheduler-alert/pom.xml b/dolphinscheduler-alert/pom.xml index c02368418..188bfa3d9 100644 --- a/dolphinscheduler-alert/pom.xml +++ b/dolphinscheduler-alert/pom.xml @@ -36,15 +36,34 @@ junit test + + org.mockito + mockito-core + jar + test + - org.apache.commons - commons-email + org.powermock + powermock-module-junit4 + test + + + + org.powermock + powermock-api-mockito2 + test + + + org.mockito + mockito-core + + - org.freemarker - freemarker + org.apache.commons + commons-email diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/AlertTemplate.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/AlertTemplate.java new file mode 100644 index 000000000..cc74ff71e --- /dev/null +++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/AlertTemplate.java @@ -0,0 +1,41 @@ +/* + * 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 org.apache.dolphinscheduler.alert.template; + +import org.apache.dolphinscheduler.common.enums.ShowType; + +/** + * alert message template + */ +public interface AlertTemplate { + + /** + * get a message from a specified alert template + * @param content alert message content + * @param showType show type + * @param showAll whether to show all + * @return a message from a specified alert template + */ + String getMessageFromTemplate(String content, ShowType showType,boolean showAll); + + /** + * default showAll is true + */ + default String getMessageFromTemplate(String content,ShowType showType){ + return getMessageFromTemplate(content,showType,true); + } +} diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/AlertTemplateFactory.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/AlertTemplateFactory.java new file mode 100644 index 000000000..58e380033 --- /dev/null +++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/AlertTemplateFactory.java @@ -0,0 +1,54 @@ +/* + * 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 org.apache.dolphinscheduler.alert.template; + +import org.apache.dolphinscheduler.alert.template.impl.DefaultHTMLTemplate; +import org.apache.dolphinscheduler.alert.utils.Constants; +import org.apache.dolphinscheduler.alert.utils.PropertyUtils; +import org.apache.dolphinscheduler.common.utils.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * the alert template factory + */ +public class AlertTemplateFactory { + + private static final Logger logger = LoggerFactory.getLogger(AlertTemplateFactory.class); + + private static final String alertTemplate = PropertyUtils.getString(Constants.ALERT_TEMPLATE); + + private AlertTemplateFactory(){} + + /** + * get a template from alert.properties conf file + * @return a template, default is DefaultHTMLTemplate + */ + public static AlertTemplate getMessageTemplate() { + + if(StringUtils.isEmpty(alertTemplate)){ + return new DefaultHTMLTemplate(); + } + + switch (alertTemplate){ + case "html": + return new DefaultHTMLTemplate(); + default: + throw new IllegalArgumentException(String.format("not support alert template: %s",alertTemplate)); + } + } +} diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/impl/DefaultHTMLTemplate.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/impl/DefaultHTMLTemplate.java new file mode 100644 index 000000000..428fa4cb6 --- /dev/null +++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/impl/DefaultHTMLTemplate.java @@ -0,0 +1,161 @@ +/* + * 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 org.apache.dolphinscheduler.alert.template.impl; + +import org.apache.dolphinscheduler.alert.template.AlertTemplate; +import org.apache.dolphinscheduler.alert.utils.Constants; +import org.apache.dolphinscheduler.alert.utils.JSONUtils; +import org.apache.dolphinscheduler.alert.utils.MailUtils; +import org.apache.dolphinscheduler.common.enums.ShowType; +import org.apache.dolphinscheduler.common.utils.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.*; + +import static org.apache.dolphinscheduler.common.utils.Preconditions.*; + +/** + * the default html alert message template + */ +public class DefaultHTMLTemplate implements AlertTemplate { + + public static final Logger logger = LoggerFactory.getLogger(DefaultHTMLTemplate.class); + + @Override + public String getMessageFromTemplate(String content, ShowType showType,boolean showAll) { + + switch (showType){ + case TABLE: + return getTableTypeMessage(content,showAll); + case TEXT: + return getTextTypeMessage(content,showAll); + default: + throw new IllegalArgumentException(String.format("not support showType: %s in DefaultHTMLTemplate",showType)); + } + } + + /** + * get alert message which type is TABLE + * @param content message content + * @param showAll weather to show all + * @return alert message + */ + private String getTableTypeMessage(String content,boolean showAll){ + + if (StringUtils.isNotEmpty(content)){ + List mapItemsList = JSONUtils.toList(content, LinkedHashMap.class); + + if(!showAll && mapItemsList.size() > Constants.NUMBER_1000){ + mapItemsList = mapItemsList.subList(0,Constants.NUMBER_1000); + } + + StringBuilder contents = new StringBuilder(200); + + boolean flag = true; + + String title = ""; + for (LinkedHashMap mapItems : mapItemsList){ + + Set> entries = mapItems.entrySet(); + + Iterator> iterator = entries.iterator(); + + StringBuilder t = new StringBuilder(Constants.TR); + StringBuilder cs = new StringBuilder(Constants.TR); + while (iterator.hasNext()){ + + Map.Entry entry = iterator.next(); + t.append(Constants.TH).append(entry.getKey()).append(Constants.TH_END); + cs.append(Constants.TD).append(String.valueOf(entry.getValue())).append(Constants.TD_END); + + } + t.append(Constants.TR_END); + cs.append(Constants.TR_END); + if (flag){ + title = t.toString(); + } + flag = false; + contents.append(cs); + } + + return getMessageFromHtmlTemplate(title,contents.toString()); + } + + return content; + } + + /** + * get alert message which type is TEXT + * @param content message content + * @param showAll weather to show all + * @return alert message + */ + private String getTextTypeMessage(String content,boolean showAll){ + + if (StringUtils.isNotEmpty(content)){ + List list; + try { + list = JSONUtils.toList(content,String.class); + }catch (Exception e){ + logger.error("json format exception",e); + return null; + } + + StringBuilder contents = new StringBuilder(100); + for (String str : list){ + contents.append(Constants.TR); + contents.append(Constants.TD).append(str).append(Constants.TD_END); + contents.append(Constants.TR_END); + } + + return getMessageFromHtmlTemplate(null,contents.toString()); + + } + + return content; + } + + /** + * get alert message from a html template + * @param title message title + * @param content message content + * @return alert message which use html template + */ + private String getMessageFromHtmlTemplate(String title,String content){ + + checkNotNull(content); + String htmlTableThead = StringUtils.isEmpty(title) ? "" : String.format("%s\n",title); + + return "\n" + + " \n" + + " dolphinscheduler\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + htmlTableThead + content + + "
\n" + + " \n" + + ""; + } +} diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/Constants.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/Constants.java index 665aac246..f96873bdd 100644 --- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/Constants.java +++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/Constants.java @@ -75,6 +75,8 @@ public class Constants { public static final int NUMBER_1000 = 1000; + public static final String ALERT_TEMPLATE = "alert.template"; + public static final String SPRING_DATASOURCE_DRIVER_CLASS_NAME = "spring.datasource.driver-class-name"; public static final String SPRING_DATASOURCE_URL = "spring.datasource.url"; @@ -115,8 +117,6 @@ public class Constants { public static final String DEVELOPMENT = "development"; - public static final String CLASSPATH_MAIL_TEMPLATES_ALERT_MAIL_TEMPLATE_FTL = "classpath:mail_templates/alert_mail_template.ftl"; - public static final String TR = ""; public static final String TD = ""; diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/MailUtils.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/MailUtils.java index b7634fe56..8bc8c3517 100644 --- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/MailUtils.java +++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/MailUtils.java @@ -16,15 +16,12 @@ */ package org.apache.dolphinscheduler.alert.utils; +import org.apache.dolphinscheduler.alert.template.AlertTemplate; +import org.apache.dolphinscheduler.alert.template.AlertTemplateFactory; import org.apache.dolphinscheduler.common.enums.ShowType; -import freemarker.cache.StringTemplateLoader; -import freemarker.template.Configuration; -import freemarker.template.Template; -import freemarker.template.TemplateException; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.HtmlEmail; import org.apache.dolphinscheduler.common.utils.CollectionUtils; -import org.apache.dolphinscheduler.common.utils.IOUtils; import org.apache.dolphinscheduler.common.utils.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -68,25 +65,7 @@ public class MailUtils { public static final String sslTrust = PropertyUtils.getString(Constants.MAIL_SMTP_SSL_TRUST); - private static Template MAIL_TEMPLATE; - - static { - Configuration cfg = new Configuration(Configuration.VERSION_2_3_21); - cfg.setDefaultEncoding(Constants.UTF_8); - StringTemplateLoader stringTemplateLoader = new StringTemplateLoader(); - cfg.setTemplateLoader(stringTemplateLoader); - InputStreamReader isr = null; - try { - isr = new InputStreamReader(new FileInputStream(ResourceUtils.getFile(Constants.CLASSPATH_MAIL_TEMPLATES_ALERT_MAIL_TEMPLATE_FTL)), - Constants.UTF_8); - - MAIL_TEMPLATE = new Template("alert_mail_template", isr, cfg); - } catch (Exception e) { - MAIL_TEMPLATE = null; - } finally { - IOUtils.closeQuietly(isr); - } - } + public static final AlertTemplate alertTemplate = AlertTemplateFactory.getMessageTemplate(); /** @@ -173,46 +152,7 @@ public class MailUtils { * @return the html table form */ private static String htmlTable(String content, boolean showAll){ - if (StringUtils.isNotEmpty(content)){ - List mapItemsList = JSONUtils.toList(content, LinkedHashMap.class); - - if(!showAll && mapItemsList.size() > Constants.NUMBER_1000){ - mapItemsList = mapItemsList.subList(0,Constants.NUMBER_1000); - } - - StringBuilder contents = new StringBuilder(200); - - boolean flag = true; - - String title = ""; - for (LinkedHashMap mapItems : mapItemsList){ - - Set> entries = mapItems.entrySet(); - - Iterator> iterator = entries.iterator(); - - StringBuilder t = new StringBuilder(Constants.TR); - StringBuilder cs = new StringBuilder(Constants.TR); - while (iterator.hasNext()){ - - Map.Entry entry = iterator.next(); - t.append(Constants.TH).append(entry.getKey()).append(Constants.TH_END); - cs.append(Constants.TD).append(String.valueOf(entry.getValue())).append(Constants.TD_END); - - } - t.append(Constants.TR_END); - cs.append(Constants.TR_END); - if (flag){ - title = t.toString(); - } - flag = false; - contents.append(cs); - } - - return getTemplateContent(title,contents.toString()); - } - - return null; + return alertTemplate.getMessageFromTemplate(content,ShowType.TABLE,showAll); } /** @@ -230,33 +170,9 @@ public class MailUtils { * @return text in html form */ private static String htmlText(String content){ - - if (StringUtils.isNotEmpty(content)){ - List list; - try { - list = JSONUtils.toList(content,String.class); - }catch (Exception e){ - logger.error("json format exception",e); - return null; - } - - StringBuilder contents = new StringBuilder(100); - for (String str : list){ - contents.append(Constants.TR); - contents.append(Constants.TD).append(str).append(Constants.TD_END); - contents.append(Constants.TR_END); - } - - return getTemplateContent(null,contents.toString()); - - } - - return null; + return alertTemplate.getMessageFromTemplate(content,ShowType.TEXT); } - - - /** * send mail as Excel attachment * @param receivers the receiver list @@ -425,28 +341,4 @@ public class MailUtils { retMap.put(Constants.MESSAGE, "Send email to {" + StringUtils.join(receivers, ",") + "} failed," + e.toString()); } - /** - * get the content of the template - * @param title the title - * @param content the content to retrieve - * @return the content in the template or null if exception occurs - */ - private static String getTemplateContent(String title,String content){ - StringWriter out = new StringWriter(); - Map map = new HashMap<>(); - if(null != title){ - map.put(Constants.TITLE,title); - } - map.put(Constants.CONTENT,content); - try { - MAIL_TEMPLATE.process(map, out); - return out.toString(); - } catch (TemplateException e) { - logger.error(e.getMessage(),e); - } catch (IOException e) { - logger.error(e.getMessage(),e); - } - - return null; - } -} +} \ No newline at end of file diff --git a/dolphinscheduler-alert/src/main/resources/alert.properties b/dolphinscheduler-alert/src/main/resources/alert.properties index 127ab5a91..000d0653b 100644 --- a/dolphinscheduler-alert/src/main/resources/alert.properties +++ b/dolphinscheduler-alert/src/main/resources/alert.properties @@ -18,6 +18,9 @@ #alert type is EMAIL/SMS alert.type=EMAIL +# alter msg template, default is html template +#alert.template=html + # mail server configuration mail.protocol=SMTP mail.server.host=xxx.xxx.com diff --git a/dolphinscheduler-alert/src/main/resources/mail_templates/alert_mail_template.ftl b/dolphinscheduler-alert/src/main/resources/mail_templates/alert_mail_template.ftl deleted file mode 100644 index 1ca9cab17..000000000 --- a/dolphinscheduler-alert/src/main/resources/mail_templates/alert_mail_template.ftl +++ /dev/null @@ -1,17 +0,0 @@ -<#-- - ~ 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. ---> - dolphinscheduler<#if title??> ${title}<#if content??> ${content}
\ No newline at end of file diff --git a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/template/AlertTemplateFactoryTest.java b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/template/AlertTemplateFactoryTest.java new file mode 100644 index 000000000..6865b895e --- /dev/null +++ b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/template/AlertTemplateFactoryTest.java @@ -0,0 +1,66 @@ +/* + * 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 org.apache.dolphinscheduler.alert.template; + +import org.apache.dolphinscheduler.alert.template.impl.DefaultHTMLTemplate; +import org.apache.dolphinscheduler.alert.utils.Constants; +import org.apache.dolphinscheduler.alert.utils.PropertyUtils; +import org.junit.Test; +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; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.mockito.Mockito.*; +import static org.junit.Assert.*; + +/** + * test class for AlertTemplateFactory + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest(PropertyUtils.class) +public class AlertTemplateFactoryTest { + + private static final Logger logger = LoggerFactory.getLogger(AlertTemplateFactoryTest.class); + + /** + * GetMessageTemplate method test + */ + @Test + public void testGetMessageTemplate(){ + + PowerMockito.mockStatic(PropertyUtils.class); + when(PropertyUtils.getString(Constants.ALERT_TEMPLATE)).thenReturn("html"); + + AlertTemplate defaultTemplate = AlertTemplateFactory.getMessageTemplate(); + + assertTrue(defaultTemplate instanceof DefaultHTMLTemplate); + } + + /** + * GetMessageTemplate method throw Exception test + */ + @Test + public void testGetMessageTemplateException(){ + + AlertTemplate defaultTemplate = AlertTemplateFactory.getMessageTemplate(); + assertTrue(defaultTemplate instanceof DefaultHTMLTemplate); + } +} diff --git a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/template/impl/DefaultHTMLTemplateTest.java b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/template/impl/DefaultHTMLTemplateTest.java new file mode 100644 index 000000000..58609c07c --- /dev/null +++ b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/template/impl/DefaultHTMLTemplateTest.java @@ -0,0 +1,123 @@ +/* + * 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 org.apache.dolphinscheduler.alert.template.impl; + +import org.apache.dolphinscheduler.alert.utils.JSONUtils; +import org.apache.dolphinscheduler.common.enums.ShowType; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; + +import static org.junit.Assert.*; + +/** + * test class for DefaultHTMLTemplate + */ +public class DefaultHTMLTemplateTest{ + + private static final Logger logger = LoggerFactory.getLogger(DefaultHTMLTemplateTest.class); + + /** + * only need test method GetMessageFromTemplate + */ + @Test + public void testGetMessageFromTemplate(){ + + DefaultHTMLTemplate template = new DefaultHTMLTemplate(); + + String tableTypeMessage = template.getMessageFromTemplate(list2String(), ShowType.TABLE,true); + + assertEquals(tableTypeMessage,generateMockTableTypeResultByHand()); + + String textTypeMessage = template.getMessageFromTemplate(list2String(), ShowType.TEXT,true); + + assertEquals(textTypeMessage,generateMockTextTypeResultByHand()); + } + + /** + * generate some simulation data + */ + private String list2String(){ + + LinkedHashMap map1 = new LinkedHashMap<>(); + 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 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> maps = new ArrayList<>(); + maps.add(0,map1); + maps.add(1,map2); + String mapjson = JSONUtils.toJsonString(maps); + logger.info(mapjson); + + return mapjson; + } + + private String generateMockTableTypeResultByHand(){ + + return "\n" + + " \n" + + " dolphinscheduler\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "\n" + + "
mysql service namemysql addressportno index of numberdatabase client connections
mysql200192.168.xx.xx330680190
mysql210192.168.xx.xx33061090
\n" + + " \n" + + ""; + } + + private String generateMockTextTypeResultByHand(){ + + return "\n" + + " \n" + + " dolphinscheduler\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
{\"mysql service name\":\"mysql200\",\"mysql address\":\"192.168.xx.xx\",\"database client connections\":\"190\",\"port\":\"3306\",\"no index of number\":\"80\"}
{\"mysql service name\":\"mysql210\",\"mysql address\":\"192.168.xx.xx\",\"database client connections\":\"90\",\"port\":\"3306\",\"no index of number\":\"10\"}
\n" + + " \n" + + ""; + } +} diff --git a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/MailUtilsTest.java b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/MailUtilsTest.java index 96f1d9f21..612de3e31 100644 --- a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/MailUtilsTest.java +++ b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/MailUtilsTest.java @@ -23,21 +23,11 @@ import org.apache.dolphinscheduler.dao.AlertDao; import org.apache.dolphinscheduler.dao.DaoFactory; import org.apache.dolphinscheduler.dao.entity.Alert; import org.apache.dolphinscheduler.dao.entity.User; -import freemarker.cache.StringTemplateLoader; -import freemarker.template.Configuration; -import freemarker.template.Template; -import freemarker.template.TemplateException; -import org.apache.commons.io.IOUtils; import org.junit.Ignore; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.util.ResourceUtils; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.StringWriter; import java.util.*; @@ -48,8 +38,8 @@ public class MailUtilsTest { private static final Logger logger = LoggerFactory.getLogger(MailUtilsTest.class); @Test public void testSendMails() { - String[] receivers = new String[]{"xxx@qq.com"}; - String[] receiversCc = new String[]{"xxx@qq.com"}; + String[] receivers = new String[]{"347801120@qq.com"}; + String[] receiversCc = new String[]{"347801120@qq.com"}; String content ="[\"id:69\"," + "\"name:UserBehavior-0--1193959466\"," + @@ -114,7 +104,7 @@ public class MailUtilsTest { @Test public void testSendTableMail(){ - String[] mails = new String[]{"825193156@qq.com"}; + String[] mails = new String[]{"347801120@qq.com"}; Alert alert = new Alert(); alert.setTitle("Mysql Exception"); alert.setShowType(ShowType.TABLE); @@ -194,39 +184,4 @@ public class MailUtilsTest { MailUtils.sendMails(Arrays.asList(mails),"gaojing",alert.getContent(),ShowType.TABLEATTACHMENT); } - @Test - public void template(){ - Template MAIL_TEMPLATE; - Configuration cfg = new Configuration(Configuration.VERSION_2_3_21); - cfg.setDefaultEncoding(Constants.UTF_8); - StringTemplateLoader stringTemplateLoader = new StringTemplateLoader(); - cfg.setTemplateLoader(stringTemplateLoader); - InputStreamReader isr = null; - try { - isr = new InputStreamReader(new FileInputStream(ResourceUtils.getFile(Constants.CLASSPATH_MAIL_TEMPLATES_ALERT_MAIL_TEMPLATE_FTL)), - Constants.UTF_8); - - MAIL_TEMPLATE = new Template("alert_mail_template", isr, cfg); - } catch (Exception e) { - MAIL_TEMPLATE = null; - } finally { - IOUtils.closeQuietly(isr); - } - - - StringWriter out = new StringWriter(); - Map map = new HashMap<>(); - map.put(Constants.TITLE,"title_test"); - try { - MAIL_TEMPLATE.process(map, out); - logger.info(out.toString()); - - } catch (TemplateException e) { - logger.error(e.getMessage(),e); - } catch (IOException e) { - logger.error(e.getMessage(),e); - } - - } - } diff --git a/pom.xml b/pom.xml index e1e340798..6c1d3aa4a 100644 --- a/pom.xml +++ b/pom.xml @@ -703,6 +703,8 @@ **/dao/mapper/AlertGroupMapperTest.java **/dao/mapper/AlertMapperTest.java **/dao/mapper/CommandMapperTest.java + **/alert/template/AlertTemplateFactoryTest.java + **/alert/template/impl/DefaultHTMLTemplateTest.java -- GitLab