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 AlertSilenceServiceImpl unit test #2393

Merged
merged 3 commits into from
Jul 28, 2024
Merged
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,117 @@
/*
* 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.alert.service;

import java.util.Optional;
import java.util.Set;

import org.apache.hertzbeat.alert.dao.AlertSilenceDao;
import org.apache.hertzbeat.alert.service.impl.AlertSilenceServiceImpl;
import org.apache.hertzbeat.common.entity.alerter.AlertSilence;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* test case for {@link AlertSilenceServiceImpl}
*/

class AlertSilenceServiceTest {

@Mock
private AlertSilenceDao alertSilenceDao;

@InjectMocks
private AlertSilenceServiceImpl alertSilenceService;

@BeforeEach
void setUp() {

MockitoAnnotations.openMocks(this);

alertSilenceDao.save(AlertSilence
.builder()
.id(1L)
.type((byte) 1)
.build()
);

assertNotNull(alertSilenceDao.findAll());
}

@Test
void testValidate() {

AlertSilence alertSilence = new AlertSilence();
alertSilence.setType((byte) 1);

alertSilenceService.validate(alertSilence, false);

assertNotNull(alertSilence.getDays());
assertEquals(7, alertSilence.getDays().size());
}

@Test
void testAddAlertSilence() {

AlertSilence alertSilence = new AlertSilence();
when(alertSilenceDao.save(any(AlertSilence.class))).thenReturn(alertSilence);

assertDoesNotThrow(() -> alertSilenceService.addAlertSilence(alertSilence));
verify(alertSilenceDao, times(1)).save(alertSilence);
}

@Test
void testModifyAlertSilence() {
AlertSilence alertSilence = new AlertSilence();
when(alertSilenceDao.save(any(AlertSilence.class))).thenReturn(alertSilence);

assertDoesNotThrow(() -> alertSilenceService.modifyAlertSilence(alertSilence));
verify(alertSilenceDao, times(1)).save(alertSilence);
}

@Test
void testGetAlertSilence() {
AlertSilence alertSilence = new AlertSilence();
when(alertSilenceDao.findById(anyLong())).thenReturn(Optional.of(alertSilence));

AlertSilence result = alertSilenceService.getAlertSilence(1L);
assertNotNull(result);
verify(alertSilenceDao, times(1)).findById(1L);
}

@Test
void testDeleteAlertSilences() {

alertSilenceDao.deleteAlertSilencesByIdIn(Set.of(1L));

verify(alertSilenceDao, times(1)).deleteAlertSilencesByIdIn(Set.of(1L));
}

}
Loading