zhuangchong
4 years ago
10 changed files with 449 additions and 43 deletions
@ -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(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -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); |
||||||
|
} |
||||||
|
} |
@ -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<AlertPluginInstance> 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<String, AlertChannel> 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<Alert> 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<AlertPluginInstance> 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(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -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; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue