diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/runner/AlertSender.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/runner/AlertSender.java index 89f4fb4fc2..2b8fec2f11 100644 --- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/runner/AlertSender.java +++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/runner/AlertSender.java @@ -25,6 +25,7 @@ import org.apache.dolphinscheduler.dao.PluginDao; import org.apache.dolphinscheduler.dao.entity.Alert; import org.apache.dolphinscheduler.dao.entity.AlertPluginInstance; import org.apache.dolphinscheduler.remote.command.alert.AlertSendResponseCommand; +import org.apache.dolphinscheduler.remote.command.alert.AlertSendResponseResult; import org.apache.dolphinscheduler.spi.alert.AlertChannel; import org.apache.dolphinscheduler.spi.alert.AlertData; import org.apache.dolphinscheduler.spi.alert.AlertInfo; @@ -81,7 +82,7 @@ public class AlertSender { for (AlertPluginInstance instance : alertInstanceList) { - AlertResult alertResult = getAlertResult(instance, alertData); + AlertResult alertResult = this.alertResultHandler(instance, alertData); AlertStatus alertStatus = Boolean.parseBoolean(String.valueOf(alertResult.getStatus())) ? AlertStatus.EXECUTION_SUCCESS : AlertStatus.EXECUTION_FAILURE; alertDao.updateAlert(alertStatus, alertResult.getMessage(), alert.getId()); @@ -105,42 +106,44 @@ public class AlertSender { .setTitle(content); boolean sendResponseStatus = true; - List sendResponseResults = new ArrayList<>(); + List sendResponseResults = new ArrayList<>(); if (CollectionUtils.isEmpty(alertInstanceList)) { sendResponseStatus = false; - AlertResult alertResult = new AlertResult(); + AlertSendResponseResult alertSendResponseResult = new AlertSendResponseResult(); String message = String.format("Alert GroupId %s send error : not found alert instance",alertGroupId); - alertResult.setStatus("false"); - alertResult.setMessage(message); - sendResponseResults.add(alertResult); + alertSendResponseResult.setStatus(sendResponseStatus); + alertSendResponseResult.setMessage(message); + sendResponseResults.add(alertSendResponseResult); logger.error("Alert GroupId {} send error : not found alert instance", alertGroupId); + return new AlertSendResponseCommand(sendResponseStatus,sendResponseResults); } for (AlertPluginInstance instance : alertInstanceList) { - - AlertResult alertResult = getAlertResult(instance, alertData); - sendResponseStatus = sendResponseStatus && Boolean.parseBoolean(String.valueOf(alertResult.getStatus())); - sendResponseResults.add(alertResult); + AlertResult alertResult = this.alertResultHandler(instance, alertData); + AlertSendResponseResult alertSendResponseResult = new AlertSendResponseResult( + Boolean.parseBoolean(String.valueOf(alertResult.getStatus())),alertResult.getMessage()); + sendResponseStatus = sendResponseStatus && alertSendResponseResult.getStatus(); + sendResponseResults.add(alertSendResponseResult); } return new AlertSendResponseCommand(sendResponseStatus,sendResponseResults); } /** - * alert result expansion + * alert result handler * @param instance instance * @param alertData alertData * @return AlertResult */ - private AlertResult getAlertResult(AlertPluginInstance instance, AlertData alertData) { + private AlertResult alertResultHandler(AlertPluginInstance instance, AlertData alertData) { String pluginName = pluginDao.getPluginDefineById(instance.getPluginDefineId()).getPluginName(); AlertChannel alertChannel = alertPluginManager.getAlertChannelMap().get(pluginName); AlertResult alertResultExtend = new AlertResult(); String pluginInstanceName = instance.getInstanceName(); if (alertChannel == null) { String message = String.format("Alert Plugin %s send error : return value is null",pluginInstanceName); - alertResultExtend.setStatus("false"); + alertResultExtend.setStatus(String.valueOf(false)); alertResultExtend.setMessage(message); logger.error("Alert Plugin {} send error : not found plugin {}", pluginInstanceName, pluginName); return alertResultExtend; @@ -153,16 +156,16 @@ public class AlertSender { if (alertResult == null) { String message = String.format("Alert Plugin %s send error : return alertResult value is null",pluginInstanceName); - alertResultExtend.setStatus("false"); + alertResultExtend.setStatus(String.valueOf(false)); alertResultExtend.setMessage(message); logger.info("Alert Plugin {} send error : return alertResult value is null", pluginInstanceName); } else if (!Boolean.parseBoolean(String.valueOf(alertResult.getStatus()))) { - alertResultExtend.setStatus("false"); + alertResultExtend.setStatus(String.valueOf(false)); alertResultExtend.setMessage(alertResult.getMessage()); logger.info("Alert Plugin {} send error : {}", pluginInstanceName, alertResult.getMessage()); } else { String message = String.format("Alert Plugin %s send success",pluginInstanceName); - alertResultExtend.setStatus("true"); + alertResultExtend.setStatus(String.valueOf(true)); alertResultExtend.setMessage(message); logger.info("Alert Plugin {} send success", pluginInstanceName); } diff --git a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/AlertServerTest.java b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/AlertServerTest.java new file mode 100644 index 0000000000..dd9c5bd472 --- /dev/null +++ b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/AlertServerTest.java @@ -0,0 +1,85 @@ +/* + * 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; + +import org.apache.dolphinscheduler.alert.plugin.AlertPluginManager; +import org.apache.dolphinscheduler.alert.plugin.DolphinPluginManagerConfig; +import org.apache.dolphinscheduler.alert.runner.AlertSender; +import org.apache.dolphinscheduler.alert.utils.Constants; +import org.apache.dolphinscheduler.dao.AlertDao; +import org.apache.dolphinscheduler.dao.DaoFactory; +import org.apache.dolphinscheduler.dao.PluginDao; +import org.apache.dolphinscheduler.remote.NettyRemotingServer; +import org.apache.dolphinscheduler.spi.alert.AlertChannel; + +import java.util.concurrent.ConcurrentHashMap; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({AlertServer.class,DaoFactory.class}) +public class AlertServerTest { + + @Before + public void before() { + + } + + @Test + public void testMain() throws Exception { + AlertDao alertDao = PowerMockito.mock(AlertDao.class); + PowerMockito.mockStatic(DaoFactory.class); + PowerMockito.when(DaoFactory.getDaoInstance(AlertDao.class)).thenReturn(alertDao); + + PluginDao pluginDao = PowerMockito.mock(PluginDao.class); + PowerMockito.when(DaoFactory.getDaoInstance(PluginDao.class)).thenReturn(pluginDao); + + AlertChannel alertChannelMock = PowerMockito.mock(AlertChannel.class); + + AlertPluginManager alertPluginManager = PowerMockito.mock(AlertPluginManager.class); + PowerMockito.whenNew(AlertPluginManager.class).withNoArguments().thenReturn(alertPluginManager); + ConcurrentHashMap alertChannelMap = new ConcurrentHashMap<>(); + alertChannelMap.put("pluginName",alertChannelMock); + PowerMockito.when(alertPluginManager.getAlertChannelMap()).thenReturn(alertChannelMap); + + DolphinPluginManagerConfig alertPluginManagerConfig = PowerMockito.mock(DolphinPluginManagerConfig.class); + PowerMockito.whenNew(DolphinPluginManagerConfig.class).withNoArguments().thenReturn(alertPluginManagerConfig); + + NettyRemotingServer nettyRemotingServer = PowerMockito.mock(NettyRemotingServer.class); + PowerMockito.whenNew(NettyRemotingServer.class).withAnyArguments().thenReturn(nettyRemotingServer); + AlertSender alertSender = PowerMockito.mock(AlertSender.class); + PowerMockito.whenNew(AlertSender.class).withAnyArguments().thenReturn(alertSender); + + AlertServer alertServer = AlertServer.getInstance(); + + new Thread(() -> { + alertServer.start(); }) + .start(); + + Thread.sleep(5 * Constants.ALERT_SCAN_INTERVAL); + + alertServer.stop(); + + } + +} diff --git a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/processor/AlertRequestProcessorTest.java b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/processor/AlertRequestProcessorTest.java new file mode 100644 index 0000000000..8102caec17 --- /dev/null +++ b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/processor/AlertRequestProcessorTest.java @@ -0,0 +1,58 @@ +/* + * 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.processor; + +import org.apache.dolphinscheduler.alert.plugin.AlertPluginManager; +import org.apache.dolphinscheduler.dao.AlertDao; +import org.apache.dolphinscheduler.dao.PluginDao; +import org.apache.dolphinscheduler.remote.command.Command; +import org.apache.dolphinscheduler.remote.command.alert.AlertSendRequestCommand; + +import org.junit.Before; +import org.junit.Test; +import org.powermock.api.mockito.PowerMockito; + +import io.netty.channel.Channel; + +/** + * alert request processor test + */ +public class AlertRequestProcessorTest { + + private AlertDao alertDao; + private PluginDao pluginDao; + private AlertPluginManager alertPluginManager; + + private AlertRequestProcessor alertRequestProcessor; + + @Before + public void before() { + alertDao = PowerMockito.mock(AlertDao.class); + pluginDao = PowerMockito.mock(PluginDao.class); + alertPluginManager = PowerMockito.mock(AlertPluginManager.class); + alertRequestProcessor = new AlertRequestProcessor(alertDao,alertPluginManager,pluginDao); + } + + @Test + public void testProcess() { + Channel channel = PowerMockito.mock(Channel.class); + AlertSendRequestCommand alertSendRequestCommand = new AlertSendRequestCommand(1,"title","content"); + Command reqCommand = alertSendRequestCommand.convert2Command(); + alertRequestProcessor.process(channel,reqCommand); + } +} diff --git a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/runner/AlertSenderTest.java b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/runner/AlertSenderTest.java new file mode 100644 index 0000000000..fbe172c2fb --- /dev/null +++ b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/runner/AlertSenderTest.java @@ -0,0 +1,180 @@ +/* + * 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.runner; + +import org.apache.dolphinscheduler.alert.plugin.AlertPluginManager; +import org.apache.dolphinscheduler.dao.AlertDao; +import org.apache.dolphinscheduler.dao.PluginDao; +import org.apache.dolphinscheduler.dao.entity.Alert; +import org.apache.dolphinscheduler.dao.entity.AlertPluginInstance; +import org.apache.dolphinscheduler.dao.entity.PluginDefine; +import org.apache.dolphinscheduler.remote.command.alert.AlertSendResponseCommand; +import org.apache.dolphinscheduler.spi.alert.AlertChannel; +import org.apache.dolphinscheduler.spi.alert.AlertResult; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * alert sender test + */ +public class AlertSenderTest { + + private static final Logger logger = LoggerFactory.getLogger(AlertSenderTest.class); + + private AlertDao alertDao; + private PluginDao pluginDao; + private AlertPluginManager alertPluginManager; + + private AlertSender alertSender; + + @Before + public void before() { + alertDao = PowerMockito.mock(AlertDao.class); + pluginDao = PowerMockito.mock(PluginDao.class); + alertPluginManager = PowerMockito.mock(AlertPluginManager.class); + + } + + @Test + public void testSyncHandler() { + + int alertGroupId = 1; + String title = "alert mail test title"; + String content = "alert mail test content"; + alertSender = new AlertSender(alertDao,alertPluginManager,pluginDao); + + //1.alert instance does not exist + PowerMockito.when(alertDao.listInstanceByAlertGroupId(alertGroupId)).thenReturn(null); + + AlertSendResponseCommand alertSendResponseCommand = alertSender.syncHandler(alertGroupId, title, content); + Assert.assertFalse(alertSendResponseCommand.getResStatus()); + alertSendResponseCommand.getResResults().forEach(result -> + logger.info("alert send response result, status:{}, message:{}",result.getStatus(),result.getMessage())); + + //2.alert plugin does not exist + int pluginDefineId = 1; + String pluginInstanceParams = "alert-instance-mail-params"; + String pluginInstanceName = "alert-instance-mail"; + List alertInstanceList = new ArrayList<>(); + AlertPluginInstance alertPluginInstance = new AlertPluginInstance( + pluginDefineId,pluginInstanceParams,alertGroupId,pluginInstanceName); + alertInstanceList.add(alertPluginInstance); + PowerMockito.when(alertDao.listInstanceByAlertGroupId(1)).thenReturn(alertInstanceList); + + String pluginName = "alert-plugin-mail"; + PluginDefine pluginDefine = new PluginDefine(pluginName,"1",null); + PowerMockito.when(pluginDao.getPluginDefineById(pluginDefineId)).thenReturn(pluginDefine); + + alertSendResponseCommand = alertSender.syncHandler(alertGroupId, title, content); + Assert.assertFalse(alertSendResponseCommand.getResStatus()); + alertSendResponseCommand.getResResults().forEach(result -> + logger.info("alert send response result, status:{}, message:{}",result.getStatus(),result.getMessage())); + + //3.alert result value is null + AlertChannel alertChannelMock = PowerMockito.mock(AlertChannel.class); + PowerMockito.when(alertChannelMock.process(Mockito.any())).thenReturn(null); + Map alertChannelMap = new ConcurrentHashMap<>(); + alertChannelMap.put(pluginName,alertChannelMock); + PowerMockito.when(alertPluginManager.getAlertChannelMap()).thenReturn(alertChannelMap); + + alertSendResponseCommand = alertSender.syncHandler(alertGroupId, title, content); + Assert.assertFalse(alertSendResponseCommand.getResStatus()); + alertSendResponseCommand.getResResults().forEach(result -> + logger.info("alert send response result, status:{}, message:{}",result.getStatus(),result.getMessage())); + + //4.abnormal information inside the alert plug-in code + AlertResult alertResult = new AlertResult(); + alertResult.setStatus(String.valueOf(false)); + alertResult.setMessage("Abnormal information inside the alert plug-in code"); + PowerMockito.when(alertChannelMock.process(Mockito.any())).thenReturn(alertResult); + alertChannelMap = new ConcurrentHashMap<>(); + alertChannelMap.put(pluginName,alertChannelMock); + PowerMockito.when(alertPluginManager.getAlertChannelMap()).thenReturn(alertChannelMap); + + alertSendResponseCommand = alertSender.syncHandler(alertGroupId, title, content); + Assert.assertFalse(alertSendResponseCommand.getResStatus()); + alertSendResponseCommand.getResResults().forEach(result -> + logger.info("alert send response result, status:{}, message:{}",result.getStatus(),result.getMessage())); + + //5.alert plugin send success + alertResult = new AlertResult(); + alertResult.setStatus(String.valueOf(true)); + alertResult.setMessage(String.format("Alert Plugin %s send success",pluginInstanceName)); + PowerMockito.when(alertChannelMock.process(Mockito.any())).thenReturn(alertResult); + alertChannelMap = new ConcurrentHashMap<>(); + alertChannelMap.put(pluginName,alertChannelMock); + PowerMockito.when(alertPluginManager.getAlertChannelMap()).thenReturn(alertChannelMap); + + alertSendResponseCommand = alertSender.syncHandler(alertGroupId, title, content); + Assert.assertTrue(alertSendResponseCommand.getResStatus()); + alertSendResponseCommand.getResResults().forEach(result -> + logger.info("alert send response result, status:{}, message:{}",result.getStatus(),result.getMessage())); + + } + + @Test + public void testRun() { + int alertGroupId = 1; + String title = "alert mail test title"; + String content = "alert mail test content"; + List alertList = new ArrayList<>(); + Alert alert = new Alert(); + alert.setAlertGroupId(alertGroupId); + alert.setTitle(title); + alert.setContent(content); + alertList.add(alert); + + alertSender = new AlertSender(alertList,alertDao,alertPluginManager,pluginDao); + + int pluginDefineId = 1; + String pluginInstanceParams = "alert-instance-mail-params"; + String pluginInstanceName = "alert-instance-mail"; + List alertInstanceList = new ArrayList<>(); + AlertPluginInstance alertPluginInstance = new AlertPluginInstance( + pluginDefineId,pluginInstanceParams,alertGroupId,pluginInstanceName); + alertInstanceList.add(alertPluginInstance); + PowerMockito.when(alertDao.listInstanceByAlertGroupId(alertGroupId)).thenReturn(alertInstanceList); + + String pluginName = "alert-plugin-mail"; + PluginDefine pluginDefine = new PluginDefine(pluginName,"1",null); + PowerMockito.when(pluginDao.getPluginDefineById(pluginDefineId)).thenReturn(pluginDefine); + + AlertResult alertResult = new AlertResult(); + alertResult.setStatus(String.valueOf(true)); + alertResult.setMessage(String.format("Alert Plugin %s send success",pluginInstanceName)); + AlertChannel alertChannelMock = PowerMockito.mock(AlertChannel.class); + PowerMockito.when(alertChannelMock.process(Mockito.any())).thenReturn(alertResult); + ConcurrentHashMap alertChannelMap = new ConcurrentHashMap<>(); + alertChannelMap.put(pluginName,alertChannelMock); + PowerMockito.when(alertPluginManager.getAlertChannelMap()).thenReturn(alertChannelMap); + + alertSender.run(); + } + +} diff --git a/dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/remote/command/alert/AlertSendResponseCommand.java b/dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/remote/command/alert/AlertSendResponseCommand.java index e3cb1474a5..984cc43c94 100644 --- a/dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/remote/command/alert/AlertSendResponseCommand.java +++ b/dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/remote/command/alert/AlertSendResponseCommand.java @@ -20,7 +20,6 @@ package org.apache.dolphinscheduler.remote.command.alert; import org.apache.dolphinscheduler.remote.command.Command; import org.apache.dolphinscheduler.remote.command.CommandType; import org.apache.dolphinscheduler.remote.utils.JsonSerializer; -import org.apache.dolphinscheduler.spi.alert.AlertResult; import java.io.Serializable; import java.util.List; @@ -33,17 +32,31 @@ public class AlertSendResponseCommand implements Serializable { */ private boolean resStatus; - private List resResults; + private List resResults; + public boolean getResStatus() { + return resStatus; + } + + public void setResStatus(boolean resStatus) { + this.resStatus = resStatus; + } + public List getResResults() { + return resResults; + } + + public void setResResults(List resResults) { + this.resResults = resResults; + } public AlertSendResponseCommand() { } - public AlertSendResponseCommand(boolean alertStatus, List alertResults) { - this.alertStatus = alertStatus; - this.alertResults = alertResults; + public AlertSendResponseCommand(boolean resStatus, List resResults) { + this.resStatus = resStatus; + this.resResults = resResults; } /** diff --git a/dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/remote/command/alert/AlertSendResponseResult.java b/dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/remote/command/alert/AlertSendResponseResult.java new file mode 100644 index 0000000000..1263b83a73 --- /dev/null +++ b/dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/remote/command/alert/AlertSendResponseResult.java @@ -0,0 +1,52 @@ +/* + * 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.remote.command.alert; + +import java.io.Serializable; + +public class AlertSendResponseResult implements Serializable { + + private boolean status; + + private String message; + + public boolean getStatus() { + return status; + } + + public void setStatus(boolean status) { + this.status = status; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public AlertSendResponseResult() { + + } + + public AlertSendResponseResult(boolean status, String message) { + this.status = status; + this.message = message; + } +} diff --git a/dolphinscheduler-remote/src/test/java/org/apache/dolphinscheduler/remote/command/alert/AlertSendResponseCommandTest.java b/dolphinscheduler-remote/src/test/java/org/apache/dolphinscheduler/remote/command/alert/AlertSendResponseCommandTest.java index d55807f4b6..41265a5339 100644 --- a/dolphinscheduler-remote/src/test/java/org/apache/dolphinscheduler/remote/command/alert/AlertSendResponseCommandTest.java +++ b/dolphinscheduler-remote/src/test/java/org/apache/dolphinscheduler/remote/command/alert/AlertSendResponseCommandTest.java @@ -20,6 +20,9 @@ package org.apache.dolphinscheduler.remote.command.alert; import org.apache.dolphinscheduler.remote.command.Command; import org.apache.dolphinscheduler.remote.command.CommandType; +import java.util.ArrayList; +import java.util.List; + import org.junit.Assert; import org.junit.Test; @@ -28,6 +31,17 @@ public class AlertSendResponseCommandTest { @Test public void testConvert2Command() { AlertSendResponseCommand alertSendResponseCommand = new AlertSendResponseCommand(); + alertSendResponseCommand.setResStatus(false); + List responseResults = new ArrayList<>(); + AlertSendResponseResult responseResult1 = new AlertSendResponseResult(); + responseResult1.setStatus(false); + responseResult1.setMessage("fail"); + responseResults.add(responseResult1); + + AlertSendResponseResult responseResult2 = new AlertSendResponseResult(true,"success"); + responseResults.add(responseResult2); + alertSendResponseCommand.setResResults(responseResults); + Command command = alertSendResponseCommand.convert2Command(1); Assert.assertEquals(CommandType.ALERT_SEND_RESPONSE,command.getType()); } diff --git a/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/alert/AlertClientService.java b/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/alert/AlertClientService.java index 9581413c39..7839b4a460 100644 --- a/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/alert/AlertClientService.java +++ b/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/alert/AlertClientService.java @@ -78,9 +78,7 @@ public class AlertClientService { Command command = request.convert2Command(); Command response = this.client.sendSync(address, command, ALERT_REQUEST_TIMEOUT); if (response != null) { - AlertSendResponseCommand sendResponse = JsonSerializer.deserialize( - response.getBody(), AlertSendResponseCommand.class); - return sendResponse; + return JsonSerializer.deserialize(response.getBody(), AlertSendResponseCommand.class); } } catch (Exception e) { logger.error("sync alert send error", e); diff --git a/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/alert/AlertClientServiceTest.java b/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/alert/AlertClientServiceTest.java index 68518f6b7a..ba44191b42 100644 --- a/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/alert/AlertClientServiceTest.java +++ b/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/alert/AlertClientServiceTest.java @@ -21,7 +21,7 @@ import org.apache.dolphinscheduler.remote.NettyRemotingClient; import org.apache.dolphinscheduler.remote.command.Command; import org.apache.dolphinscheduler.remote.command.alert.AlertSendRequestCommand; import org.apache.dolphinscheduler.remote.command.alert.AlertSendResponseCommand; -import org.apache.dolphinscheduler.spi.alert.AlertResult; +import org.apache.dolphinscheduler.remote.command.alert.AlertSendResponseResult; import java.util.ArrayList; import java.util.List; @@ -70,16 +70,16 @@ public class AlertClientServiceTest { AlertSendResponseCommand alertSendResponseCommand = alertClient.sendAlert(host, port, groupId, title, content); Assert.assertNull(alertSendResponseCommand); - AlertSendRequestCommand alertSendRequestCommand = new AlertSendRequestCommand(); + AlertSendRequestCommand alertSendRequestCommand = new AlertSendRequestCommand(groupId,title,content); Command reqCommand = alertSendRequestCommand.convert2Command(); boolean sendResponseStatus; - List sendResponseResults = new ArrayList<>(); + List sendResponseResults = new ArrayList<>(); //2.alter instance does not exist sendResponseStatus = false; - AlertResult alertResult = new AlertResult(); + AlertSendResponseResult alertResult = new AlertSendResponseResult(); String message = String.format("Alert GroupId %s send error : not found alert instance",groupId); - alertResult.setStatus("false"); + alertResult.setStatus(false); alertResult.setMessage(message); sendResponseResults.add(alertResult); AlertSendResponseCommand alertSendResponseCommandData = new AlertSendResponseCommand(sendResponseStatus, sendResponseResults); @@ -87,60 +87,60 @@ public class AlertClientServiceTest { PowerMockito.when(client.sendSync(Mockito.any(), Mockito.any(), Mockito.anyLong())).thenReturn(resCommand); alertSendResponseCommand = alertClient.sendAlert(host, port, groupId, title, content); - Assert.assertFalse(alertSendResponseCommand.getAlertStatus()); - alertSendResponseCommand.getAlertResults().forEach(result -> + Assert.assertFalse(alertSendResponseCommand.getResStatus()); + alertSendResponseCommand.getResResults().forEach(result -> logger.info("alert send response result, status:{}, message:{}",result.getStatus(),result.getMessage())); //3.alter plugin does not exist sendResponseStatus = false; String pluginInstanceName = "alert-mail"; message = String.format("Alert Plugin %s send error : return value is null",pluginInstanceName); - alertResult.setStatus("false"); + alertResult.setStatus(false); alertResult.setMessage(message); alertSendResponseCommandData = new AlertSendResponseCommand(sendResponseStatus, sendResponseResults); resCommand = alertSendResponseCommandData.convert2Command(reqCommand.getOpaque()); PowerMockito.when(client.sendSync(Mockito.any(), Mockito.any(), Mockito.anyLong())).thenReturn(resCommand); alertSendResponseCommand = alertClient.sendAlert(host, port, groupId, title, content); - Assert.assertFalse(alertSendResponseCommand.getAlertStatus()); - alertSendResponseCommand.getAlertResults().forEach(result -> + Assert.assertFalse(alertSendResponseCommand.getResStatus()); + alertSendResponseCommand.getResResults().forEach(result -> logger.info("alert send response result, status:{}, message:{}",result.getStatus(),result.getMessage())); //4.alter result is null sendResponseStatus = false; message = String.format("Alert Plugin %s send error : return result value is null",pluginInstanceName); - alertResult.setStatus("false"); + alertResult.setStatus(false); alertResult.setMessage(message); alertSendResponseCommandData = new AlertSendResponseCommand(sendResponseStatus, sendResponseResults); resCommand = alertSendResponseCommandData.convert2Command(reqCommand.getOpaque()); PowerMockito.when(client.sendSync(Mockito.any(), Mockito.any(), Mockito.anyLong())).thenReturn(resCommand); alertSendResponseCommand = alertClient.sendAlert(host, port, groupId, title, content); - Assert.assertFalse(alertSendResponseCommand.getAlertStatus()); - alertSendResponseCommand.getAlertResults().forEach(result -> + Assert.assertFalse(alertSendResponseCommand.getResStatus()); + alertSendResponseCommand.getResResults().forEach(result -> logger.info("alert send response result, status:{}, message:{}",result.getStatus(),result.getMessage())); //5.abnormal information inside the alert plug-in code sendResponseStatus = false; - alertResult.setStatus("false"); + alertResult.setStatus(false); alertResult.setMessage("Abnormal information inside the alert plug-in code"); alertSendResponseCommandData = new AlertSendResponseCommand(sendResponseStatus, sendResponseResults); resCommand = alertSendResponseCommandData.convert2Command(reqCommand.getOpaque()); PowerMockito.when(client.sendSync(Mockito.any(), Mockito.any(), Mockito.anyLong())).thenReturn(resCommand); alertSendResponseCommand = alertClient.sendAlert(host, port, groupId, title, content); - Assert.assertFalse(alertSendResponseCommand.getAlertStatus()); - alertSendResponseCommand.getAlertResults().forEach(result -> + Assert.assertFalse(alertSendResponseCommand.getResStatus()); + alertSendResponseCommand.getResResults().forEach(result -> logger.info("alert send response result, status:{}, message:{}",result.getStatus(),result.getMessage())); //6.alert plugin send success sendResponseStatus = true; message = String.format("Alert Plugin %s send success",pluginInstanceName); - alertResult.setStatus("true"); + alertResult.setStatus(true); alertResult.setMessage(message); alertSendResponseCommandData = new AlertSendResponseCommand(sendResponseStatus, sendResponseResults); resCommand = alertSendResponseCommandData.convert2Command(reqCommand.getOpaque()); PowerMockito.when(client.sendSync(Mockito.any(), Mockito.any(), Mockito.anyLong())).thenReturn(resCommand); alertSendResponseCommand = alertClient.sendAlert(host, port, groupId, title, content); - Assert.assertTrue(alertSendResponseCommand.getAlertStatus()); - alertSendResponseCommand.getAlertResults().forEach(result -> + Assert.assertTrue(alertSendResponseCommand.getResStatus()); + alertSendResponseCommand.getResResults().forEach(result -> logger.info("alert send response result, status:{}, message:{}",result.getStatus(),result.getMessage())); if (Objects.nonNull(alertClient) && alertClient.isRunning()) { diff --git a/pom.xml b/pom.xml index f6075a92bf..f7ceef32e7 100644 --- a/pom.xml +++ b/pom.xml @@ -960,6 +960,9 @@ **/alert/utils/DingTalkUtilsTest.java **/alert/utils/EnterpriseWeChatUtilsTest.java **/alert/utils/FuncUtilsTest.java + **/alert/processor/AlertRequestProcessorTest.java + **/alert/runner/AlertSenderTest.java + **/alert/AlertServerTest.java