Browse Source

add test.

pull/3/MERGE
zhuangchong 4 years ago
parent
commit
500d481e72
  1. 7
      dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/runner/AlertSender.java
  2. 37
      dolphinscheduler-remote/src/test/java/org/apache/dolphinscheduler/remote/command/alert/AlertSendRequestCommandTest.java
  3. 34
      dolphinscheduler-remote/src/test/java/org/apache/dolphinscheduler/remote/command/alert/AlertSendResponseCommandTest.java
  4. 106
      dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/alert/AlertClientServiceTest.java
  5. 2
      pom.xml

7
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.alert.plugin.AlertPluginManager;
import org.apache.dolphinscheduler.common.enums.AlertStatus; 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.AlertDao;
import org.apache.dolphinscheduler.dao.PluginDao; import org.apache.dolphinscheduler.dao.PluginDao;
import org.apache.dolphinscheduler.dao.entity.Alert; import org.apache.dolphinscheduler.dao.entity.Alert;
@ -106,7 +107,7 @@ public class AlertSender {
boolean sendResponseStatus = true; boolean sendResponseStatus = true;
List<AlertResult> sendResponseResults = new ArrayList<>(); List<AlertResult> sendResponseResults = new ArrayList<>();
if (alertInstanceList == null || alertInstanceList.size() == 0) { if (CollectionUtils.isEmpty(alertInstanceList)) {
sendResponseStatus = false; sendResponseStatus = false;
AlertResult alertResult = new AlertResult(); AlertResult alertResult = new AlertResult();
String message = String.format("Alert GroupId %s send error : not found alert instance",alertGroupId); 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); AlertResult alertResult = alertChannel.process(alertInfo);
if (alertResult == null) { 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.setStatus("false");
alertResultExtend.setMessage(message); 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()))) { } else if (!Boolean.parseBoolean(String.valueOf(alertResult.getStatus()))) {
alertResultExtend.setStatus("false"); alertResultExtend.setStatus("false");
alertResultExtend.setMessage(alertResult.getMessage()); alertResultExtend.setMessage(alertResult.getMessage());

37
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());
}
}

34
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());
}
}

106
dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/alert/AlertClientServiceTest.java

@ -17,42 +17,136 @@
package org.apache.dolphinscheduler.service.alert; 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.remote.command.alert.AlertSendResponseCommand;
import org.apache.dolphinscheduler.spi.alert.AlertResult;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects; import java.util.Objects;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before;
import org.junit.Test; 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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* alert client service test * alert client service test
*/ */
@RunWith(PowerMockRunner.class)
@PrepareForTest({AlertClientService.class})
public class AlertClientServiceTest { public class AlertClientServiceTest {
private static final Logger logger = LoggerFactory.getLogger(AlertClientServiceTest.class); 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 @Test
public void testSendAlert() { public void testSendAlert() throws Exception {
String host; String host = "127.0.0.1";
int port = 50501; int port = 50501;
int groupId = 1; int groupId = 1;
String title = "test-title"; String title = "test-title";
String content = "test-content"; String content = "test-content";
AlertClientService alertClient = new AlertClientService();
// alter server does not exist //1.alter server does not exist
host = "128.0.10.1";
AlertSendResponseCommand alertSendResponseCommand = alertClient.sendAlert(host, port, groupId, title, content); AlertSendResponseCommand alertSendResponseCommand = alertClient.sendAlert(host, port, groupId, title, content);
Assert.assertNull(alertSendResponseCommand); Assert.assertNull(alertSendResponseCommand);
host = "127.0.0.1"; AlertSendRequestCommand alertSendRequestCommand = new AlertSendRequestCommand();
Command reqCommand = alertSendRequestCommand.convert2Command();
boolean sendResponseStatus;
List<AlertResult> 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); 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()) { if (Objects.nonNull(alertClient) && alertClient.isRunning()) {
alertClient.close(); alertClient.close();
} }
} }
} }

2
pom.xml

@ -867,6 +867,8 @@
<include>**/remote/NettyRemotingClientTest.java</include> <include>**/remote/NettyRemotingClientTest.java</include>
<include>**/remote/NettyUtilTest.java</include> <include>**/remote/NettyUtilTest.java</include>
<include>**/remote/ResponseFutureTest.java</include> <include>**/remote/ResponseFutureTest.java</include>
<include>**/remote/command/alert/AlertSendRequestCommandTest.java</include>
<include>**/remote/command/alert/AlertSendResponseCommandTest.java</include>
<include>**/server/log/LoggerServerTest.java</include> <include>**/server/log/LoggerServerTest.java</include>
<include>**/server/entity/SQLTaskExecutionContextTest.java</include> <include>**/server/entity/SQLTaskExecutionContextTest.java</include>
<include>**/server/log/MasterLogFilterTest.java</include> <include>**/server/log/MasterLogFilterTest.java</include>

Loading…
Cancel
Save