From 633ee3028429d800b8753676bf0afbfb5154f1a9 Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Tue, 23 Jul 2024 16:24:43 +0800 Subject: [PATCH] [Improve] add DbAlertStoreHandlerImpl unit test Signed-off-by: yuluo-yx --- .../AliYunAlertNotifyHandlerImplTest.java | 26 ++++ .../impl/DbAlertStoreHandlerImplTest.java | 142 +++++++++++++++++- .../GotifyAlertNotifyHandlerImplTest.java | 26 ++++ .../ServerChanAlertNotifyHandlerImplTest.java | 25 +++ .../impl/SmsAlertNotifyHandlerImplTest.java | 25 +++ .../WeComAppAlertNotifyHandlerImplTest.java | 25 +++ 6 files changed, 263 insertions(+), 6 deletions(-) create mode 100644 manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/AliYunAlertNotifyHandlerImplTest.java create mode 100644 manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/GotifyAlertNotifyHandlerImplTest.java create mode 100644 manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/ServerChanAlertNotifyHandlerImplTest.java create mode 100644 manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/SmsAlertNotifyHandlerImplTest.java create mode 100644 manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/WeComAppAlertNotifyHandlerImplTest.java diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/AliYunAlertNotifyHandlerImplTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/AliYunAlertNotifyHandlerImplTest.java new file mode 100644 index 00000000000..c95776f8c88 --- /dev/null +++ b/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/AliYunAlertNotifyHandlerImplTest.java @@ -0,0 +1,26 @@ +/* + * 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.hertzbeat.manager.component.alerter.impl; + + +/** + * Test case for {@link AliYunAlertNotifyHandlerImpl} + */ + +class AliYunAlertNotifyHandlerImplTest { +} diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/DbAlertStoreHandlerImplTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/DbAlertStoreHandlerImplTest.java index f9828ea80ac..51ea955a455 100644 --- a/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/DbAlertStoreHandlerImplTest.java +++ b/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/DbAlertStoreHandlerImplTest.java @@ -17,19 +17,149 @@ package org.apache.hertzbeat.manager.component.alerter.impl; +import java.util.HashMap; +import org.apache.hertzbeat.alert.service.AlertService; +import org.apache.hertzbeat.common.constants.CommonConstants; +import org.apache.hertzbeat.common.entity.alerter.Alert; +import org.apache.hertzbeat.common.entity.manager.Monitor; +import org.apache.hertzbeat.manager.service.MonitorService; +import org.apache.hertzbeat.manager.support.exception.IgnoreException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; /** * Test case for {@link DbAlertStoreHandlerImpl} */ + +@ExtendWith(MockitoExtension.class) class DbAlertStoreHandlerImplTest { - @BeforeEach - void setUp() { - } + @Mock + private MonitorService monitorService; + + @Mock + private AlertService alertService; + + @InjectMocks + private DbAlertStoreHandlerImpl dbAlertStoreHandler; + + private Alert alert; + + @BeforeEach + public void setUp() { + + alert = new Alert(); + alert.setTags(new HashMap<>()); + alert.setTarget(CommonConstants.AVAILABILITY); + alert.setStatus(CommonConstants.ALERT_STATUS_CODE_PENDING); + } + + @Test + public void testStoreMonitorNotExist() { + + alert.getTags().put(CommonConstants.TAG_MONITOR_ID, "1"); + when(monitorService.getMonitor(1L)).thenReturn(null); + + dbAlertStoreHandler.store(alert); + + verify(monitorService).getMonitor(1L); + verify(alertService, never()).addAlert(any(Alert.class)); + } + + @Test + public void testStoreMonitorPaused() { + + alert.getTags().put(CommonConstants.TAG_MONITOR_ID, "1"); + + Monitor monitor = new Monitor(); + monitor.setStatus(CommonConstants.MONITOR_PAUSED_CODE); + when(monitorService.getMonitor(1L)).thenReturn(monitor); + + dbAlertStoreHandler.store(alert); + + verify(monitorService).getMonitor(1L); + verify(alertService, never()).addAlert(any(Alert.class)); + } + + @Test + public void testStoreAvailabilityPendingAndMonitorUp() { + + alert.getTags().put(CommonConstants.TAG_MONITOR_ID, "1"); + + Monitor monitor = new Monitor(); + monitor.setId(1L); + monitor.setStatus(CommonConstants.MONITOR_UP_CODE); + when(monitorService.getMonitor(1L)).thenReturn(monitor); + + dbAlertStoreHandler.store(alert); + + verify(monitorService).updateMonitorStatus(1L, CommonConstants.MONITOR_DOWN_CODE); + verify(alertService).addAlert(alert); + } + + @Test + public void testStoreAvailabilityRestoredAndMonitorDown() { + + alert.getTags().put(CommonConstants.TAG_MONITOR_ID, "1"); + alert.setStatus(CommonConstants.ALERT_STATUS_CODE_RESTORED); + + Monitor monitor = new Monitor(); + monitor.setId(1L); + monitor.setStatus(CommonConstants.MONITOR_DOWN_CODE); + when(monitorService.getMonitor(1L)).thenReturn(monitor); + + dbAlertStoreHandler.store(alert); + + verify(monitorService).updateMonitorStatus(1L, CommonConstants.MONITOR_UP_CODE); + verify(alertService).addAlert(alert); + } + + @Test + public void testStoreIgnoreTagExists() { + + alert.getTags().put(CommonConstants.IGNORE, "true"); + + assertThrows(IgnoreException.class, () -> dbAlertStoreHandler.store(alert)); + } + + @Test + public void testStoreNoMonitorId() { + + alert.getTags().remove(CommonConstants.TAG_MONITOR_ID); + dbAlertStoreHandler.store(alert); + + verify(alertService).addAlert(alert); + } + + @Test + public void testStoreAddMonitorNameAndHostIfNotPresent() { + + alert.getTags().put(CommonConstants.TAG_MONITOR_ID, "1"); + + Monitor monitor = new Monitor(); + monitor.setId(1L); + monitor.setName("test-monitor"); + monitor.setHost("test-host"); + monitor.setStatus(CommonConstants.MONITOR_UP_CODE); + when(monitorService.getMonitor(1L)).thenReturn(monitor); + + dbAlertStoreHandler.store(alert); + + verify(monitorService).getMonitor(1L); + assertEquals("test-monitor", alert.getTags().get(CommonConstants.TAG_MONITOR_NAME)); + assertEquals("test-host", alert.getTags().get(CommonConstants.TAG_MONITOR_HOST)); + verify(alertService).addAlert(alert); + } - @Test - void store() { - } } diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/GotifyAlertNotifyHandlerImplTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/GotifyAlertNotifyHandlerImplTest.java new file mode 100644 index 00000000000..0f48da28656 --- /dev/null +++ b/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/GotifyAlertNotifyHandlerImplTest.java @@ -0,0 +1,26 @@ +/* + * 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.hertzbeat.manager.component.alerter.impl; + + +/** + * Test case for {@link AliYunAlertNotifyHandlerImpl} + */ + +class GotifyAlertNotifyHandlerImplTest { +} diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/ServerChanAlertNotifyHandlerImplTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/ServerChanAlertNotifyHandlerImplTest.java new file mode 100644 index 00000000000..1998eb7483c --- /dev/null +++ b/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/ServerChanAlertNotifyHandlerImplTest.java @@ -0,0 +1,25 @@ +/* + * 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.hertzbeat.manager.component.alerter.impl; + +/** + * test case for {@link ServerChanAlertNotifyHandlerImpl} + */ + +class ServerChanAlertNotifyHandlerImplTest { +} diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/SmsAlertNotifyHandlerImplTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/SmsAlertNotifyHandlerImplTest.java new file mode 100644 index 00000000000..ae908b8dbd1 --- /dev/null +++ b/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/SmsAlertNotifyHandlerImplTest.java @@ -0,0 +1,25 @@ +/* + * 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.hertzbeat.manager.component.alerter.impl; + +/** + * test case for {@link SmsAlertNotifyHandlerImpl} + */ + +class SmsAlertNotifyHandlerImplTest { +} diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/WeComAppAlertNotifyHandlerImplTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/WeComAppAlertNotifyHandlerImplTest.java new file mode 100644 index 00000000000..ae66e568803 --- /dev/null +++ b/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/WeComAppAlertNotifyHandlerImplTest.java @@ -0,0 +1,25 @@ +/* + * 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.hertzbeat.manager.component.alerter.impl; + +/** + * test case for {@link WeComAppAlertNotifyHandlerImpl} + */ + +class WeComAppAlertNotifyHandlerImplTest { +}