Skip to content

Commit

Permalink
[Improve] add AlertDefineExcel unit test and fix bugs (#2375)
Browse files Browse the repository at this point in the history
Signed-off-by: yuluo-yx <[email protected]>
Co-authored-by: tomsun28 <[email protected]>
  • Loading branch information
yuluo-yx and tomsun28 authored Jul 26, 2024
1 parent 9e305f7 commit 7367b73
Show file tree
Hide file tree
Showing 9 changed files with 398 additions and 55 deletions.
6 changes: 6 additions & 0 deletions alerter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@
<version>4.1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.1</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
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;
}
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,17 @@

package org.apache.hertzbeat.alert.service.impl;

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 jakarta.annotation.Resource;
import java.io.InputStream;
import java.io.OutputStream;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.alert.dto.AlertDefineDTO;
import org.apache.hertzbeat.alert.dto.ExportAlertDefineDTO;
import org.apache.hertzbeat.alert.service.AlertDefineImExportService;
import org.apache.hertzbeat.alert.service.AlertDefineService;
import org.apache.hertzbeat.common.entity.alerter.AlertDefine;
import org.apache.hertzbeat.common.entity.manager.TagItem;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.CollectionUtils;
Expand All @@ -51,7 +46,7 @@ public void importConfig(InputStream is) {
var formList = parseImport(is)
.stream()
.map(this::convert)
.collect(Collectors.toUnmodifiableList());
.toList();
if (!CollectionUtils.isEmpty(formList)) {
formList.forEach(alertDefine -> {
alertDefineService.validate(alertDefine, false);
Expand All @@ -65,7 +60,7 @@ public void exportConfig(OutputStream os, List<Long> configList) {
var monitorList = configList.stream()
.map(it -> alertDefineService.getAlertDefine(it))
.map(this::convert)
.collect(Collectors.toUnmodifiableList());
.toList();
writeOs(monitorList, os);
}

Expand Down Expand Up @@ -105,47 +100,4 @@ protected String fileNamePrefix() {
return "hertzbeat_alertDefine_" + LocalDate.now();
}

/**
* Export data transfer objects for alert configurations
*/
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@ExcelTarget(value = "ExportAlertDefineDTO")
protected static class ExportAlertDefineDTO {
@Excel(name = "AlertDefine")
private AlertDefineDTO alertDefine;
}

/**
* Data transfer object for alert configuration
*/
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@ExcelTarget(value = "AlertDefineDTO")
protected static 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
import java.util.List;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.alert.dto.AlertDefineDTO;
import org.apache.hertzbeat.alert.dto.ExportAlertDefineDTO;
import org.apache.hertzbeat.common.entity.manager.TagItem;
import org.apache.hertzbeat.common.util.JsonUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
Expand Down Expand Up @@ -73,7 +76,7 @@ public String getFileName() {
* @return form list
*/
@Override
List<ExportAlertDefineDTO> parseImport(InputStream is) {
public List<ExportAlertDefineDTO> parseImport(InputStream is) {
try (Workbook workbook = WorkbookFactory.create(is)) {
Sheet sheet = workbook.getSheetAt(0);
List<ExportAlertDefineDTO> alertDefines = new ArrayList<>();
Expand Down Expand Up @@ -184,9 +187,9 @@ private AlertDefineDTO extractAlertDefineDataFromRow(Row row) {
* @param os output stream
*/
@Override
void writeOs(List<ExportAlertDefineDTO> exportAlertDefineList, OutputStream os) {
public void writeOs(List<ExportAlertDefineDTO> exportAlertDefineList, OutputStream os) {
try {
Workbook workbook = WorkbookFactory.create(true);
Workbook workbook = new HSSFWorkbook();
String sheetName = "Export AlertDefine";
Sheet sheet = workbook.createSheet(sheetName);
sheet.setDefaultColumnWidth(20);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.alert.dto.ExportAlertDefineDTO;
import org.springframework.stereotype.Service;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.nio.charset.StandardCharsets;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.alert.dto.ExportAlertDefineDTO;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
Expand Down
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);
}

}
Loading

0 comments on commit 7367b73

Please sign in to comment.