Skip to content

Commit ca324ce

Browse files
committed
add unit test
1 parent 2472319 commit ca324ce

7 files changed

Lines changed: 201 additions & 5 deletions

File tree

dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/java/org/apache/dolphinscheduler/alert/service/ListenerEventPostService.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ public ListenerEventPostService() {
8989
@Override
9090
public void run() {
9191
log.info("listener event post thread started");
92-
log.error("______________________________________{}________________", QUERY_ALERT_THRESHOLD);
9392
while (!ServerLifeCycleManager.isStopped()) {
9493
try {
9594
List<ListenerEvent> listenerEvents = listenerEventMapper

dolphinscheduler-alert/dolphinscheduler-alert-server/src/test/java/org/apache/dolphinscheduler/alert/runner/ListenerEventPostServiceTest.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void before() {
7474
}
7575

7676
@Test
77-
public void testRun() {
77+
public void testSendServerDownEventSuccess() {
7878
List<ListenerEvent> events = new ArrayList<>();
7979
ServerDownListenerEvent serverDownListenerEvent = new ServerDownListenerEvent();
8080
serverDownListenerEvent.setEventTime(new Date());
@@ -84,7 +84,7 @@ public void testRun() {
8484
successEvent.setId(1);
8585
successEvent.setPostStatus(AlertStatus.WAIT_EXECUTION);
8686
successEvent.setContent(JSONUtils.toJsonString(serverDownListenerEvent));
87-
successEvent.setSign(DigestUtils.sha1Hex(successEvent.getContent()));
87+
successEvent.setSign(DigestUtils.sha256Hex(successEvent.getContent()));
8888
successEvent.setEventType(ListenerEventType.SERVER_DOWN);
8989
successEvent.setCreateTime(new Date());
9090
successEvent.setUpdateTime(new Date());
@@ -112,4 +112,43 @@ public void testRun() {
112112
when(listenerEventMapper.deleteById(1)).thenReturn(1);
113113
listenerEventPostService.send(events);
114114
}
115+
116+
@Test
117+
public void testSendServerDownEventFailed() {
118+
List<ListenerEvent> events = new ArrayList<>();
119+
ServerDownListenerEvent serverDownListenerEvent = new ServerDownListenerEvent();
120+
serverDownListenerEvent.setEventTime(new Date());
121+
serverDownListenerEvent.setType("WORKER");
122+
serverDownListenerEvent.setHost("192.168.*.*");
123+
ListenerEvent successEvent = new ListenerEvent();
124+
successEvent.setId(1);
125+
successEvent.setPostStatus(AlertStatus.WAIT_EXECUTION);
126+
successEvent.setContent(JSONUtils.toJsonString(serverDownListenerEvent));
127+
successEvent.setSign(DigestUtils.sha1Hex(successEvent.getContent()));
128+
successEvent.setEventType(ListenerEventType.SERVER_DOWN);
129+
successEvent.setCreateTime(new Date());
130+
successEvent.setUpdateTime(new Date());
131+
events.add(successEvent);
132+
133+
int pluginDefineId = 1;
134+
String pluginInstanceParams =
135+
"{\"User\":\"xx\",\"receivers\":\"xx\",\"sender\":\"xx\",\"smtpSslTrust\":\"*\",\"enableSmtpAuth\":\"true\",\"receiverCcs\":null,\"showType\":\"table\",\"starttlsEnable\":\"false\",\"serverPort\":\"25\",\"serverHost\":\"xx\",\"Password\":\"xx\",\"sslEnable\":\"false\"}";
136+
String pluginInstanceName = "alert-instance-mail";
137+
List<AlertPluginInstance> alertInstanceList = new ArrayList<>();
138+
AlertPluginInstance alertPluginInstance = new AlertPluginInstance(
139+
pluginDefineId, pluginInstanceParams, pluginInstanceName);
140+
alertPluginInstance.setInstanceType(AlertPluginInstanceType.GLOBAL);
141+
alertPluginInstance.setId(1);
142+
alertInstanceList.add(alertPluginInstance);
143+
when(alertPluginInstanceMapper.queryAllGlobalAlertPluginInstanceList()).thenReturn(alertInstanceList);
144+
145+
AlertResult sendResult = new AlertResult();
146+
sendResult.setStatus(String.valueOf(false));
147+
sendResult.setMessage(String.format("Alert Plugin %s send failed", pluginInstanceName));
148+
AlertChannel alertChannelMock = mock(AlertChannel.class);
149+
when(alertChannelMock.process(Mockito.any())).thenReturn(sendResult);
150+
when(alertPluginManager.getAlertChannel(1)).thenReturn(Optional.of(alertChannelMock));
151+
Assertions.assertFalse(Boolean.parseBoolean(sendResult.getStatus()));
152+
listenerEventPostService.send(events);
153+
}
115154
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.dolphinscheduler.common.enums;
19+
20+
import org.junit.jupiter.api.Assertions;
21+
import org.junit.jupiter.api.Test;
22+
23+
public class ListenerEventTypeTest {
24+
25+
@Test
26+
public void testGetCode() {
27+
Assertions.assertEquals(0, ListenerEventType.SERVER_DOWN.getCode());
28+
Assertions.assertEquals(1, ListenerEventType.PROCESS_DEFINITION_CREATED.getCode());
29+
}
30+
31+
@Test
32+
public void testGetDesp() {
33+
Assertions.assertEquals("PROCESS_DEFINITION_UPDATED", ListenerEventType.PROCESS_DEFINITION_UPDATED.getDescp());
34+
Assertions.assertEquals("PROCESS_DEFINITION_DELETED", ListenerEventType.PROCESS_DEFINITION_DELETED.getDescp());
35+
}
36+
37+
@Test
38+
public void testGetListenerEventTypeByCode() {
39+
Assertions.assertEquals(ListenerEventType.PROCESS_START, ListenerEventType.of(4));
40+
Assertions.assertNotEquals(ListenerEventType.PROCESS_END, ListenerEventType.of(6));
41+
Assertions.assertThrows(IllegalArgumentException.class, () -> ListenerEventType.of(-1));
42+
}
43+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.dolphinscheduler.dao.entity;
19+
20+
import org.apache.dolphinscheduler.common.enums.ListenerEventType;
21+
import org.apache.dolphinscheduler.common.enums.ReleaseState;
22+
import org.apache.dolphinscheduler.dao.entity.event.ProcessDefinitionCreatedListenerEvent;
23+
24+
import org.junit.jupiter.api.Assertions;
25+
import org.junit.jupiter.api.Test;
26+
27+
public class ProcessDefinitionCreatedListenerEventTest {
28+
29+
@Test
30+
public void testBuildProcessDefinitionUpdatedListenerEvent() {
31+
int id = 1;
32+
long code = 1L;
33+
String name = "testName";
34+
ReleaseState releaseState = ReleaseState.OFFLINE;
35+
ProcessDefinition processDefinition = new ProcessDefinition();
36+
processDefinition.setId(id);
37+
processDefinition.setCode(code);
38+
processDefinition.setName(name);
39+
processDefinition.setReleaseState(releaseState);
40+
ProcessDefinitionCreatedListenerEvent event = new ProcessDefinitionCreatedListenerEvent(processDefinition);
41+
Assertions.assertEquals(event.getEventType(), ListenerEventType.PROCESS_DEFINITION_CREATED);
42+
Assertions.assertEquals(event.getId(), id);
43+
Assertions.assertEquals(event.getCode(), code);
44+
Assertions.assertEquals(event.getName(), name);
45+
Assertions.assertEquals(event.getReleaseState(), releaseState);
46+
Assertions.assertEquals(String.format("process definition created:%s", name), event.getTitle());
47+
}
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.dolphinscheduler.dao.entity;
19+
20+
import org.apache.dolphinscheduler.common.enums.ListenerEventType;
21+
import org.apache.dolphinscheduler.common.enums.ReleaseState;
22+
import org.apache.dolphinscheduler.dao.entity.event.ProcessDefinitionUpdatedListenerEvent;
23+
24+
import org.junit.jupiter.api.Assertions;
25+
import org.junit.jupiter.api.Test;
26+
27+
public class ProcessDefinitionUpdatedListenerEventTest {
28+
29+
@Test
30+
public void testBuildProcessDefinitionUpdatedListenerEvent() {
31+
int id = 1;
32+
long code = 1L;
33+
String name = "testName";
34+
ReleaseState releaseState = ReleaseState.OFFLINE;
35+
ProcessDefinition processDefinition = new ProcessDefinition();
36+
processDefinition.setId(id);
37+
processDefinition.setCode(code);
38+
processDefinition.setName(name);
39+
processDefinition.setReleaseState(releaseState);
40+
ProcessDefinitionUpdatedListenerEvent event = new ProcessDefinitionUpdatedListenerEvent(processDefinition);
41+
Assertions.assertEquals(event.getEventType(), ListenerEventType.PROCESS_DEFINITION_UPDATED);
42+
Assertions.assertEquals(event.getId(), id);
43+
Assertions.assertEquals(event.getCode(), code);
44+
Assertions.assertEquals(event.getName(), name);
45+
Assertions.assertEquals(event.getReleaseState(), releaseState);
46+
Assertions.assertEquals(String.format("process definition updated:%s", name), event.getTitle());
47+
}
48+
}

dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/alert/ListenerEventAlertManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
public class ListenerEventAlertManager {
6464

6565
@Value("${alert.alarm-suppression.crash:60}")
66-
private Integer crashAlarmSuppression;
66+
private int crashAlarmSuppression;
6767

6868
@Autowired
6969
private ListenerEventMapper listenerEventMapper;

dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/alert/ListenerEventAlertManagerTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
package org.apache.dolphinscheduler.service.alert;
1919

20+
import static org.mockito.ArgumentMatchers.any;
21+
22+
import org.apache.dolphinscheduler.dao.entity.AlertPluginInstance;
2023
import org.apache.dolphinscheduler.dao.entity.ProcessDefinition;
2124
import org.apache.dolphinscheduler.dao.entity.ProcessInstance;
2225
import org.apache.dolphinscheduler.dao.entity.ProcessTaskRelationLog;
@@ -26,6 +29,7 @@
2629
import org.apache.dolphinscheduler.dao.entity.TaskInstance;
2730
import org.apache.dolphinscheduler.dao.entity.User;
2831
import org.apache.dolphinscheduler.dao.mapper.AlertPluginInstanceMapper;
32+
import org.apache.dolphinscheduler.dao.mapper.ListenerEventMapper;
2933

3034
import java.util.ArrayList;
3135
import java.util.List;
@@ -48,15 +52,24 @@ public class ListenerEventAlertManagerTest {
4852
private static final Logger logger = LoggerFactory.getLogger(ListenerEventAlertManagerTest.class);
4953

5054
@InjectMocks
51-
ListenerEventAlertManager listenerEventAlertManager = new ListenerEventAlertManager();
55+
ListenerEventAlertManager listenerEventAlertManager;
5256

5357
@Mock
5458
AlertPluginInstanceMapper alertPluginInstanceMapper;
5559

60+
@Mock
61+
ListenerEventMapper listenerEventMapper;
62+
5663
@Test
5764
public void sendServerDownListenerEventTest() {
5865
String host = "127.0.0.1";
5966
String type = "WORKER";
67+
List<AlertPluginInstance> globalPluginInstanceList = new ArrayList<>();
68+
AlertPluginInstance instance = new AlertPluginInstance(1, "instanceParams", "instanceName");
69+
globalPluginInstanceList.add(instance);
70+
Mockito.when(alertPluginInstanceMapper.queryAllGlobalAlertPluginInstanceList())
71+
.thenReturn(globalPluginInstanceList);
72+
Mockito.doNothing().when(listenerEventMapper).insertServerDownEvent(any(), any());
6073
listenerEventAlertManager.publishServerDownListenerEvent(host, type);
6174
}
6275

@@ -66,6 +79,12 @@ public void sendProcessDefinitionCreatedListenerEvent() {
6679
ProcessDefinition processDefinition = Mockito.mock(ProcessDefinition.class);
6780
List<TaskDefinitionLog> taskDefinitionLogs = new ArrayList<>();
6881
List<ProcessTaskRelationLog> processTaskRelationLogs = new ArrayList<>();
82+
AlertPluginInstance instance = new AlertPluginInstance(1, "instanceParams", "instanceName");
83+
List<AlertPluginInstance> globalPluginInstanceList = new ArrayList<>();
84+
globalPluginInstanceList.add(instance);
85+
Mockito.when(alertPluginInstanceMapper.queryAllGlobalAlertPluginInstanceList())
86+
.thenReturn(globalPluginInstanceList);
87+
Mockito.when(listenerEventMapper.insert(any())).thenReturn(1);
6988
listenerEventAlertManager.publishProcessDefinitionCreatedListenerEvent(user, processDefinition,
7089
taskDefinitionLogs, processTaskRelationLogs);
7190
}

0 commit comments

Comments
 (0)