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 01152af757..89f4fb4fc2 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 @@ -19,6 +19,7 @@ package org.apache.dolphinscheduler.alert.runner; import org.apache.dolphinscheduler.alert.plugin.AlertPluginManager; import org.apache.dolphinscheduler.common.enums.AlertStatus; +import org.apache.dolphinscheduler.common.utils.CollectionUtils; import org.apache.dolphinscheduler.dao.AlertDao; import org.apache.dolphinscheduler.dao.PluginDao; import org.apache.dolphinscheduler.dao.entity.Alert; @@ -106,7 +107,7 @@ public class AlertSender { boolean sendResponseStatus = true; List sendResponseResults = new ArrayList<>(); - if (alertInstanceList == null || alertInstanceList.size() == 0) { + if (CollectionUtils.isEmpty(alertInstanceList)) { sendResponseStatus = false; AlertResult alertResult = new AlertResult(); String message = String.format("Alert GroupId %s send error : not found alert instance",alertGroupId); @@ -151,10 +152,10 @@ public class AlertSender { AlertResult alertResult = alertChannel.process(alertInfo); if (alertResult == null) { - String message = String.format("Alert Plugin %s send error : return value is null",pluginInstanceName); + String message = String.format("Alert Plugin %s send error : return alertResult value is null",pluginInstanceName); alertResultExtend.setStatus("false"); alertResultExtend.setMessage(message); - logger.info("Alert Plugin {} send error : return value is null", pluginInstanceName); + logger.info("Alert Plugin {} send error : return alertResult value is null", pluginInstanceName); } else if (!Boolean.parseBoolean(String.valueOf(alertResult.getStatus()))) { alertResultExtend.setStatus("false"); alertResultExtend.setMessage(alertResult.getMessage()); diff --git a/dolphinscheduler-remote/src/test/java/org/apache/dolphinscheduler/remote/command/alert/AlertSendRequestCommandTest.java b/dolphinscheduler-remote/src/test/java/org/apache/dolphinscheduler/remote/command/alert/AlertSendRequestCommandTest.java new file mode 100644 index 0000000000..4d0e59c082 --- /dev/null +++ b/dolphinscheduler-remote/src/test/java/org/apache/dolphinscheduler/remote/command/alert/AlertSendRequestCommandTest.java @@ -0,0 +1,37 @@ +/* + * 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 org.apache.dolphinscheduler.remote.command.Command; +import org.apache.dolphinscheduler.remote.command.CommandType; + +import org.junit.Assert; +import org.junit.Test; + +public class AlertSendRequestCommandTest { + + @Test + public void testConvert2Command() { + int groupId = 1; + String title = "test-title"; + String content = "test-content"; + AlertSendRequestCommand requestCommand = new AlertSendRequestCommand(groupId,title,content); + Command command = requestCommand.convert2Command(); + Assert.assertEquals(CommandType.ALERT_SEND_REQUEST,command.getType()); + } +} 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 new file mode 100644 index 0000000000..d55807f4b6 --- /dev/null +++ b/dolphinscheduler-remote/src/test/java/org/apache/dolphinscheduler/remote/command/alert/AlertSendResponseCommandTest.java @@ -0,0 +1,34 @@ +/* + * 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 org.apache.dolphinscheduler.remote.command.Command; +import org.apache.dolphinscheduler.remote.command.CommandType; + +import org.junit.Assert; +import org.junit.Test; + +public class AlertSendResponseCommandTest { + + @Test + public void testConvert2Command() { + AlertSendResponseCommand alertSendResponseCommand = new AlertSendResponseCommand(); + Command command = alertSendResponseCommand.convert2Command(1); + Assert.assertEquals(CommandType.ALERT_SEND_RESPONSE,command.getType()); + } +} 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 1624c7dbaa..68518f6b7a 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 @@ -17,42 +17,136 @@ package org.apache.dolphinscheduler.service.alert; +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 java.util.ArrayList; +import java.util.List; import java.util.Objects; import org.junit.Assert; +import org.junit.Before; 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; /** * alert client service test */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({AlertClientService.class}) public class AlertClientServiceTest { private static final Logger logger = LoggerFactory.getLogger(AlertClientServiceTest.class); + private NettyRemotingClient client; + + private AlertClientService alertClient; + + @Before + public void before() throws Exception { + client = PowerMockito.mock(NettyRemotingClient.class); + PowerMockito.whenNew(NettyRemotingClient.class).withAnyArguments().thenReturn(client); + alertClient = new AlertClientService(); + } + @Test - public void testSendAlert() { - String host; + public void testSendAlert() throws Exception { + String host = "127.0.0.1"; int port = 50501; int groupId = 1; String title = "test-title"; String content = "test-content"; - AlertClientService alertClient = new AlertClientService(); - // alter server does not exist - host = "128.0.10.1"; + //1.alter server does not exist AlertSendResponseCommand alertSendResponseCommand = alertClient.sendAlert(host, port, groupId, title, content); Assert.assertNull(alertSendResponseCommand); - host = "127.0.0.1"; + AlertSendRequestCommand alertSendRequestCommand = new AlertSendRequestCommand(); + Command reqCommand = alertSendRequestCommand.convert2Command(); + boolean sendResponseStatus; + List sendResponseResults = new ArrayList<>(); + + //2.alter instance does not exist + sendResponseStatus = false; + AlertResult alertResult = new AlertResult(); + String message = String.format("Alert GroupId %s send error : not found alert instance",groupId); + alertResult.setStatus("false"); + alertResult.setMessage(message); + sendResponseResults.add(alertResult); + AlertSendResponseCommand alertSendResponseCommandData = new AlertSendResponseCommand(sendResponseStatus, sendResponseResults); + Command 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 -> + 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.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 -> + 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.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 -> + 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.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 -> + 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.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 -> + logger.info("alert send response result, status:{}, message:{}",result.getStatus(),result.getMessage())); if (Objects.nonNull(alertClient) && alertClient.isRunning()) { alertClient.close(); } } + } diff --git a/pom.xml b/pom.xml index 5b45dbce00..f6075a92bf 100644 --- a/pom.xml +++ b/pom.xml @@ -867,6 +867,8 @@ **/remote/NettyRemotingClientTest.java **/remote/NettyUtilTest.java **/remote/ResponseFutureTest.java + **/remote/command/alert/AlertSendRequestCommandTest.java + **/remote/command/alert/AlertSendResponseCommandTest.java **/server/log/LoggerServerTest.java **/server/entity/SQLTaskExecutionContextTest.java **/server/log/MasterLogFilterTest.java