Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improve] add DbAlertStoreHandlerImpl unit test #2353

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
}
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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 {
}
Loading