|
17 | 17 |
|
18 | 18 | package org.apache.dolphinscheduler.alert.runner; |
19 | 19 |
|
| 20 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 21 | +import static org.mockito.ArgumentMatchers.anyString; |
| 22 | +import static org.mockito.ArgumentMatchers.eq; |
20 | 23 | import static org.mockito.Mockito.mock; |
| 24 | +import static org.mockito.Mockito.verify; |
21 | 25 | import static org.mockito.Mockito.when; |
22 | 26 |
|
23 | 27 | import org.apache.dolphinscheduler.alert.api.AlertChannel; |
24 | 28 | import org.apache.dolphinscheduler.alert.api.AlertResult; |
25 | 29 | import org.apache.dolphinscheduler.alert.config.AlertConfig; |
26 | 30 | import org.apache.dolphinscheduler.alert.plugin.AlertPluginManager; |
27 | 31 | import org.apache.dolphinscheduler.alert.service.AlertSender; |
| 32 | +import org.apache.dolphinscheduler.common.enums.AlertStatus; |
| 33 | +import org.apache.dolphinscheduler.common.enums.AlertType; |
28 | 34 | import org.apache.dolphinscheduler.common.enums.WarningType; |
29 | 35 | import org.apache.dolphinscheduler.common.utils.JSONUtils; |
30 | 36 | import org.apache.dolphinscheduler.dao.AlertDao; |
@@ -148,22 +154,46 @@ void testRun() { |
148 | 154 | alert.setTitle(TITLE); |
149 | 155 | alert.setContent(CONTENT); |
150 | 156 | alert.setWarningType(WarningType.FAILURE); |
| 157 | + alert.setAlertType(AlertType.TASK_FAILURE); |
151 | 158 |
|
152 | | - int pluginDefineId = 1; |
153 | | - String pluginInstanceParams = "alert-instance-mail-params"; |
154 | | - String pluginInstanceName = "alert-instance-mail"; |
155 | 159 | List<AlertPluginInstance> alertInstanceList = new ArrayList<>(); |
| 160 | + when(alertDao.listInstanceByAlertGroupId(ALERT_GROUP_ID)).thenReturn(alertInstanceList); |
| 161 | + |
| 162 | + // 1. alert plugin send success |
156 | 163 | AlertPluginInstance alertPluginInstance = new AlertPluginInstance( |
157 | | - pluginDefineId, pluginInstanceParams, pluginInstanceName); |
| 164 | + PLUGIN_DEFINE_ID, PLUGIN_INSTANCE_PARAMS, PLUGIN_INSTANCE_NAME); |
| 165 | + alertPluginInstance.setId(alertPluginInstance.getPluginDefineId()); |
158 | 166 | alertInstanceList.add(alertPluginInstance); |
159 | | - when(alertDao.listInstanceByAlertGroupId(ALERT_GROUP_ID)).thenReturn(alertInstanceList); |
160 | 167 |
|
161 | | - AlertResult alertResult = new AlertResult(); |
162 | | - alertResult.setSuccess(true); |
163 | | - alertResult.setMessage(String.format("Alert Plugin %s send success", pluginInstanceName)); |
164 | | - Assertions.assertTrue(alertResult.isSuccess()); |
165 | | - when(alertDao.listInstanceByAlertGroupId(1)).thenReturn(new ArrayList<>()); |
| 168 | + AlertChannel alertChannelMock = mock(AlertChannel.class); |
| 169 | + when(alertPluginManager.getAlertChannel(PLUGIN_DEFINE_ID)).thenReturn(Optional.of(alertChannelMock)); |
| 170 | + AlertResult alertSuccessResult = AlertResult.success(); |
| 171 | + when(alertChannelMock.process(Mockito.any())).thenReturn(alertSuccessResult); |
| 172 | + alertSender.sendEvent(alert); |
| 173 | + verify(alertDao).updateAlert(eq(AlertStatus.EXECUTION_SUCCESS), anyString(), anyInt()); |
| 174 | + |
| 175 | + // 2. alert plugin send failed |
| 176 | + AlertPluginInstance otherAlertPluginInstance = new AlertPluginInstance( |
| 177 | + PLUGIN_DEFINE_ID + 1, PLUGIN_INSTANCE_PARAMS, PLUGIN_INSTANCE_NAME); |
| 178 | + otherAlertPluginInstance.setId(otherAlertPluginInstance.getPluginDefineId()); |
| 179 | + alertInstanceList.clear(); |
| 180 | + alertInstanceList.add(otherAlertPluginInstance); |
| 181 | + |
| 182 | + AlertChannel otherAlertChannelMock = mock(AlertChannel.class); |
| 183 | + when(alertPluginManager.getAlertChannel(PLUGIN_DEFINE_ID + 1)).thenReturn(Optional.of(otherAlertChannelMock)); |
| 184 | + AlertResult alertFailedResult = |
| 185 | + AlertResult.fail(String.format("Alert Plugin %s send failed", PLUGIN_INSTANCE_NAME)); |
| 186 | + when(otherAlertChannelMock.process(Mockito.any())).thenReturn(alertFailedResult); |
166 | 187 | alertSender.sendEvent(alert); |
| 188 | + verify(alertDao).updateAlert(eq(AlertStatus.EXECUTION_FAILURE), anyString(), anyInt()); |
| 189 | + |
| 190 | + // 3. alert plugin send partial success |
| 191 | + alertInstanceList.clear(); |
| 192 | + alertInstanceList.add(alertPluginInstance); |
| 193 | + alertInstanceList.add(otherAlertPluginInstance); |
| 194 | + alertSender.sendEvent(alert); |
| 195 | + verify(alertDao).updateAlert(eq(AlertStatus.EXECUTION_PARTIAL_SUCCESS), anyString(), anyInt()); |
| 196 | + |
167 | 197 | } |
168 | 198 |
|
169 | 199 | @Test |
|
0 commit comments