-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Improve] add AlertDefineExcel unit test and fix bugs (#2375)
Signed-off-by: yuluo-yx <[email protected]> Co-authored-by: tomsun28 <[email protected]>
- Loading branch information
Showing
9 changed files
with
398 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
alerter/src/main/java/org/apache/hertzbeat/alert/dto/AlertDefineDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* 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.dto; | ||
|
||
import cn.afterturn.easypoi.excel.annotation.Excel; | ||
import cn.afterturn.easypoi.excel.annotation.ExcelTarget; | ||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import java.util.List; | ||
import lombok.Data; | ||
import org.apache.hertzbeat.common.entity.manager.TagItem; | ||
|
||
/** | ||
* Data transfer object for alert configuration | ||
*/ | ||
|
||
@Data | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
@ExcelTarget(value = "AlertDefineDTO") | ||
public class AlertDefineDTO { | ||
@Excel(name = "App") | ||
private String app; | ||
@Excel(name = "Metric") | ||
private String metric; | ||
@Excel(name = "Field") | ||
private String field; | ||
@Excel(name = "Preset") | ||
private Boolean preset; | ||
@Excel(name = "Expr") | ||
private String expr; | ||
@Excel(name = "Priority") | ||
private Byte priority; | ||
@Excel(name = "Times") | ||
private Integer times; | ||
@Excel(name = "Tags") | ||
private List<TagItem> tags; | ||
@Excel(name = "Enable") | ||
private Boolean enable; | ||
@Excel(name = "RecoverNotice") | ||
private Boolean recoverNotice; | ||
@Excel(name = "Template") | ||
private String template; | ||
} |
38 changes: 38 additions & 0 deletions
38
alerter/src/main/java/org/apache/hertzbeat/alert/dto/ExportAlertDefineDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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.dto; | ||
|
||
import cn.afterturn.easypoi.excel.annotation.Excel; | ||
import cn.afterturn.easypoi.excel.annotation.ExcelTarget; | ||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import lombok.Data; | ||
|
||
/** | ||
* Export data transfer objects for alert configurations | ||
*/ | ||
|
||
@Data | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
@ExcelTarget(value = "ExportAlertDefineDTO") | ||
public class ExportAlertDefineDTO { | ||
|
||
@Excel(name = "AlertDefine") | ||
private AlertDefineDTO alertDefine; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertConvergeServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* 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.Collections; | ||
import java.util.Set; | ||
import java.util.Optional; | ||
|
||
import org.apache.hertzbeat.alert.dao.AlertConvergeDao; | ||
import org.apache.hertzbeat.alert.service.impl.AlertConvergeServiceImpl; | ||
import org.apache.hertzbeat.common.entity.alerter.AlertConverge; | ||
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 org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageImpl; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.domain.Specification; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* test case for {@link org.apache.hertzbeat.alert.service.impl.AlertConvergeServiceImpl} | ||
*/ | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class AlertConvergeServiceTest { | ||
|
||
@Mock | ||
private AlertConvergeDao alertConvergeDao; | ||
|
||
@InjectMocks | ||
private AlertConvergeServiceImpl alertConvergeService; | ||
|
||
@Test | ||
public void testAddAlertConverge() { | ||
|
||
AlertConverge alertConverge = new AlertConverge(); | ||
alertConvergeService.addAlertConverge(alertConverge); | ||
|
||
verify(alertConvergeDao, times(1)).save(alertConverge); | ||
} | ||
|
||
@Test | ||
public void testModifyAlertConverge() { | ||
|
||
AlertConverge alertConverge = new AlertConverge(); | ||
alertConvergeService.modifyAlertConverge(alertConverge); | ||
|
||
verify(alertConvergeDao, times(1)).save(alertConverge); | ||
} | ||
|
||
@Test | ||
public void testGetAlertConverge() { | ||
|
||
long convergeId = 1L; | ||
AlertConverge alertConverge = new AlertConverge(); | ||
when(alertConvergeDao.findById(convergeId)).thenReturn(Optional.of(alertConverge)); | ||
AlertConverge result = alertConvergeService.getAlertConverge(convergeId); | ||
|
||
verify(alertConvergeDao, times(1)).findById(convergeId); | ||
assertEquals(alertConverge, result); | ||
} | ||
|
||
@Test | ||
public void testDeleteAlertConverges() { | ||
|
||
Set<Long> convergeIds = Set.of(1L, 2L, 3L); | ||
alertConvergeService.deleteAlertConverges(convergeIds); | ||
|
||
verify(alertConvergeDao, times(1)).deleteAlertConvergesByIdIn(convergeIds); | ||
} | ||
|
||
@Test | ||
public void testGetAlertConverges() { | ||
|
||
Specification<AlertConverge> specification = mock(Specification.class); | ||
PageRequest pageRequest = PageRequest.of(0, 10); | ||
Page<AlertConverge> page = new PageImpl<>(Collections.emptyList()); | ||
when(alertConvergeDao.findAll( | ||
any(Specification.class), | ||
any(Pageable.class)) | ||
).thenReturn(page); | ||
|
||
Page<AlertConverge> result = alertConvergeService.getAlertConverges(specification, pageRequest); | ||
|
||
verify(alertConvergeDao, times(1)).findAll(specification, pageRequest); | ||
assertEquals(page, result); | ||
} | ||
|
||
} |
Oops, something went wrong.