Browse Source

add ut

pull/3/MERGE
CalvinKirs 4 years ago
parent
commit
6877073777
  1. 2
      dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/AlertServer.java
  2. 4
      dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/processor/AlertRequestProcessor.java
  3. 7
      dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/runner/AlertSender.java
  4. 7
      dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/AlertServerTest.java
  5. 2
      dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/plugin/EmailAlertPluginTest.java
  6. 34
      dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/runner/AlertSenderTest.java
  7. 109
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertPluginInstanceServiceTest.java

2
dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/AlertServer.java

@ -133,7 +133,7 @@ public class AlertServer {
logger.warn("No Alert Plugin . Can not send alert info. ");
} else {
List<Alert> alerts = alertDao.listWaitExecutionAlert();
alertSender = new AlertSender(alerts, alertDao, alertPluginManager, pluginDao);
alertSender = new AlertSender(alerts, alertDao, alertPluginManager);
alertSender.run();
}
}

4
dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/processor/AlertRequestProcessor.java

@ -35,7 +35,7 @@ import org.slf4j.LoggerFactory;
import io.netty.channel.Channel;
/**
* alert request processor
* alert request processor
*/
public class AlertRequestProcessor implements NettyRequestProcessor {
@ -59,7 +59,7 @@ public class AlertRequestProcessor implements NettyRequestProcessor {
command.getBody(), AlertSendRequestCommand.class);
logger.info("received command : {}", alertSendRequestCommand);
AlertSender alertSender = new AlertSender(alertDao, alertPluginManager, pluginDao);
AlertSender alertSender = new AlertSender(alertDao, alertPluginManager);
AlertSendResponseCommand alertSendResponseCommand = alertSender.syncHandler(alertSendRequestCommand.getGroupId(), alertSendRequestCommand.getTitle(), alertSendRequestCommand.getContent());
channel.writeAndFlush(alertSendResponseCommand.convert2Command(command.getOpaque()));

7
dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/runner/AlertSender.java

@ -48,25 +48,22 @@ public class AlertSender {
private List<Alert> alertList;
private AlertDao alertDao;
private PluginDao pluginDao;
private AlertPluginManager alertPluginManager;
public AlertSender(AlertPluginManager alertPluginManager) {
this.alertPluginManager = alertPluginManager;
}
public AlertSender(AlertDao alertDao, AlertPluginManager alertPluginManager, PluginDao pluginDao) {
public AlertSender(AlertDao alertDao, AlertPluginManager alertPluginManager) {
super();
this.alertDao = alertDao;
this.pluginDao = pluginDao;
this.alertPluginManager = alertPluginManager;
}
public AlertSender(List<Alert> alertList, AlertDao alertDao, AlertPluginManager alertPluginManager, PluginDao pluginDao) {
public AlertSender(List<Alert> alertList, AlertDao alertDao, AlertPluginManager alertPluginManager) {
super();
this.alertList = alertList;
this.alertDao = alertDao;
this.pluginDao = pluginDao;
this.alertPluginManager = alertPluginManager;
}

7
dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/AlertServerTest.java

@ -40,7 +40,7 @@ import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({AlertServer.class,DaoFactory.class})
@PrepareForTest({AlertServer.class, DaoFactory.class})
public class AlertServerTest {
@Before
@ -62,7 +62,7 @@ public class AlertServerTest {
AlertPluginManager alertPluginManager = PowerMockito.mock(AlertPluginManager.class);
PowerMockito.whenNew(AlertPluginManager.class).withNoArguments().thenReturn(alertPluginManager);
ConcurrentHashMap alertChannelMap = new ConcurrentHashMap<>();
alertChannelMap.put("pluginName",alertChannelMock);
alertChannelMap.put("pluginName", alertChannelMock);
PowerMockito.when(alertPluginManager.getPluginNameById(Mockito.anyInt())).thenReturn("pluginName");
PowerMockito.when(alertPluginManager.getAlertChannelMap()).thenReturn(alertChannelMap);
@ -81,7 +81,8 @@ public class AlertServerTest {
Assert.assertNotNull(alertServer);
new Thread(() -> {
alertServer.start(); })
alertServer.start();
})
.start();
Thread.sleep(5 * Constants.ALERT_SCAN_INTERVAL);

2
dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/plugin/EmailAlertPluginTest.java

@ -136,7 +136,7 @@ public class EmailAlertPluginTest {
alertPluginInstance.setPluginInstanceParams(getEmailAlertParams());
alertDao.getAlertPluginInstanceMapper().insert(alertPluginInstance);
AlertSender alertSender = new AlertSender(alertList, alertDao, alertPluginManager, pluginDao);
AlertSender alertSender = new AlertSender(alertList, alertDao, alertPluginManager);
alertSender.run();
Alert alertResult = alertDao.getAlertMapper().selectById(alert1.getId());

34
dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/runner/AlertSenderTest.java

@ -67,7 +67,7 @@ public class AlertSenderTest {
int alertGroupId = 1;
String title = "alert mail test title";
String content = "alert mail test content";
alertSender = new AlertSender(alertDao,alertPluginManager,pluginDao);
alertSender = new AlertSender(alertDao, alertPluginManager);
//1.alert instance does not exist
PowerMockito.when(alertDao.listInstanceByAlertGroupId(alertGroupId)).thenReturn(null);
@ -75,7 +75,7 @@ public class AlertSenderTest {
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()));
logger.info("alert send response result, status:{}, message:{}", result.getStatus(), result.getMessage()));
//2.alert plugin does not exist
int pluginDefineId = 1;
@ -83,31 +83,31 @@ public class AlertSenderTest {
String pluginInstanceName = "alert-instance-mail";
List<AlertPluginInstance> alertInstanceList = new ArrayList<>();
AlertPluginInstance alertPluginInstance = new AlertPluginInstance(
pluginDefineId,pluginInstanceParams,pluginInstanceName);
pluginDefineId, pluginInstanceParams, pluginInstanceName);
alertInstanceList.add(alertPluginInstance);
PowerMockito.when(alertDao.listInstanceByAlertGroupId(1)).thenReturn(alertInstanceList);
String pluginName = "alert-plugin-mail";
PluginDefine pluginDefine = new PluginDefine(pluginName,"1",null);
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()));
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<String, AlertChannel> alertChannelMap = new ConcurrentHashMap<>();
alertChannelMap.put(pluginName,alertChannelMock);
alertChannelMap.put(pluginName, alertChannelMock);
PowerMockito.when(alertPluginManager.getAlertChannelMap()).thenReturn(alertChannelMap);
PowerMockito.when(alertPluginManager.getPluginNameById(Mockito.anyInt())).thenReturn("alert-plugin-mail");
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()));
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();
@ -115,27 +115,27 @@ public class AlertSenderTest {
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);
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()));
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));
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);
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()));
logger.info("alert send response result, status:{}, message:{}", result.getStatus(), result.getMessage()));
}
@ -151,29 +151,29 @@ public class AlertSenderTest {
alert.setContent(content);
alertList.add(alert);
alertSender = new AlertSender(alertList,alertDao,alertPluginManager,pluginDao);
alertSender = new AlertSender(alertList, alertDao, alertPluginManager);
int pluginDefineId = 1;
String pluginInstanceParams = "alert-instance-mail-params";
String pluginInstanceName = "alert-instance-mail";
List<AlertPluginInstance> alertInstanceList = new ArrayList<>();
AlertPluginInstance alertPluginInstance = new AlertPluginInstance(
pluginDefineId,pluginInstanceParams,pluginInstanceName);
pluginDefineId, pluginInstanceParams, pluginInstanceName);
alertInstanceList.add(alertPluginInstance);
PowerMockito.when(alertDao.listInstanceByAlertGroupId(alertGroupId)).thenReturn(alertInstanceList);
String pluginName = "alert-plugin-mail";
PluginDefine pluginDefine = new PluginDefine(pluginName,"1",null);
PluginDefine pluginDefine = new PluginDefine(pluginName, "1", null);
PowerMockito.when(pluginDao.getPluginDefineById(pluginDefineId)).thenReturn(pluginDefine);
PowerMockito.when(alertPluginManager.getPluginNameById(1)).thenReturn("alert-instance-mail");
AlertResult alertResult = new AlertResult();
alertResult.setStatus(String.valueOf(true));
alertResult.setMessage(String.format("Alert Plugin %s send success",pluginInstanceName));
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);
alertChannelMap.put(pluginName, alertChannelMock);
PowerMockito.when(alertPluginManager.getAlertChannelMap()).thenReturn(alertChannelMap);
Assert.assertTrue(Boolean.parseBoolean(alertResult.getStatus()));
alertSender.run();

109
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertPluginInstanceServiceTest.java

@ -22,6 +22,7 @@ import org.apache.dolphinscheduler.api.service.impl.AlertPluginInstanceServiceIm
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.enums.UserType;
import org.apache.dolphinscheduler.dao.entity.AlertPluginInstance;
import org.apache.dolphinscheduler.dao.entity.PluginDefine;
import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.mapper.AlertGroupMapper;
import org.apache.dolphinscheduler.dao.mapper.AlertPluginInstanceMapper;
@ -29,6 +30,7 @@ import org.apache.dolphinscheduler.dao.mapper.PluginDefineMapper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ -60,6 +62,82 @@ public class AlertPluginInstanceServiceTest {
private User user;
private String uiParams="[\n"
+ " {\n"
+ " \"field\":\"userParams\",\n"
+ " \"name\":\"user.params\",\n"
+ " \"props\":{\n"
+ " \"placeholder\":\"please enter your custom parameters, which will be passed to you when calling your script\",\n"
+ " \"size\":\"small\"\n"
+ " },\n"
+ " \"type\":\"input\",\n"
+ " \"title\":\"user.params\",\n"
+ " \"value\":\"userParams\",\n"
+ " \"validate\":[\n"
+ " {\n"
+ " \"required\":false,\n"
+ " \"message\":null,\n"
+ " \"type\":\"string\",\n"
+ " \"trigger\":\"blur\",\n"
+ " \"min\":null,\n"
+ " \"max\":null\n"
+ " }\n"
+ " ]\n"
+ " },\n"
+ " {\n"
+ " \"field\":\"path\",\n"
+ " \"name\":\"path\",\n"
+ " \"props\":{\n"
+ " \"placeholder\":\"please upload the file to the disk directory of the alert server, and ensure that the path is absolute and has the corresponding access rights\",\n"
+ " \"size\":\"small\"\n"
+ " },\n"
+ " \"type\":\"input\",\n"
+ " \"title\":\"path\",\n"
+ " \"value\":\"/kris/script/path\",\n"
+ " \"validate\":[\n"
+ " {\n"
+ " \"required\":true,\n"
+ " \"message\":null,\n"
+ " \"type\":\"string\",\n"
+ " \"trigger\":\"blur\",\n"
+ " \"min\":null,\n"
+ " \"max\":null\n"
+ " }\n"
+ " ]\n"
+ " },\n"
+ " {\n"
+ " \"field\":\"type\",\n"
+ " \"name\":\"type\",\n"
+ " \"props\":{\n"
+ " \"placeholder\":null,\n"
+ " \"size\":\"small\"\n"
+ " },\n"
+ " \"type\":\"radio\",\n"
+ " \"title\":\"type\",\n"
+ " \"value\":0,\n"
+ " \"validate\":[\n"
+ " {\n"
+ " \"required\":true,\n"
+ " \"message\":null,\n"
+ " \"type\":\"string\",\n"
+ " \"trigger\":\"blur\",\n"
+ " \"min\":null,\n"
+ " \"max\":null\n"
+ " }\n"
+ " ],\n"
+ " \"options\":[\n"
+ " {\n"
+ " \"label\":\"SHELL\",\n"
+ " \"value\":0,\n"
+ " \"disabled\":false\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ "]\n"
+ "\n";
private String paramsMap="{\"path\":\"/kris/script/path\",\"userParams\":\"userParams\",\"type\":\"0\"}";
@Before
public void before() {
user = new User();
@ -77,10 +155,10 @@ public class AlertPluginInstanceServiceTest {
@Test
public void testCreate() {
Mockito.when(alertPluginInstanceMapper.queryByInstanceName("test")).thenReturn(alertPluginInstances);
Map<String, Object> result = alertPluginInstanceService.create(user, 1, "test", "test params");
Map<String, Object> result = alertPluginInstanceService.create(user, 1, "test", uiParams);
Assert.assertEquals(Status.PLUGIN_INSTANCE_ALREADY_EXIT, result.get(Constants.STATUS));
Mockito.when(alertPluginInstanceMapper.insert(Mockito.any())).thenReturn(1);
result = alertPluginInstanceService.create(user, 1, "test1", "test params");
result = alertPluginInstanceService.create(user, 1, "test1", uiParams);
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
}
@ -96,4 +174,31 @@ public class AlertPluginInstanceServiceTest {
}
@Test
public void testUpdate(){
Mockito.when(alertPluginInstanceMapper.updateById(Mockito.any())).thenReturn(0);
Map<String, Object> result= alertPluginInstanceService.update(user,1,"testUpdate",uiParams);
Assert.assertEquals(Status.SAVE_ERROR, result.get(Constants.STATUS));
Mockito.when(alertPluginInstanceMapper.updateById(Mockito.any())).thenReturn(1);
result= alertPluginInstanceService.update(user,1,"testUpdate",uiParams);
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
}
@Test
public void testQueryAll(){
AlertPluginInstance alertPluginInstance=new AlertPluginInstance();
alertPluginInstance.setId(1);
alertPluginInstance.setPluginDefineId(1);
alertPluginInstance.setPluginInstanceParams(paramsMap);
alertPluginInstance.setInstanceName("test");
PluginDefine pluginDefine=new PluginDefine("script","script",uiParams);
pluginDefine.setId(1);
List<PluginDefine> pluginDefines= Collections.singletonList(pluginDefine);
List<AlertPluginInstance> pluginInstanceList= Collections.singletonList(alertPluginInstance);
Mockito.when(alertPluginInstanceMapper.queryAllAlertPluginInstanceList()).thenReturn(pluginInstanceList);
Mockito.when(pluginDefineMapper.queryAllPluginDefineList()).thenReturn(pluginDefines);
Map<String, Object> result = alertPluginInstanceService.queryAll();
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
}
}

Loading…
Cancel
Save