This is an automated email from the ASF dual-hosted git repository.

gongchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git


The following commit(s) were added to refs/heads/master by this push:
     new 7367b7315 [Improve] add AlertDefineExcel unit test and fix bugs (#2375)
7367b7315 is described below

commit 7367b7315b458d984911fd44d0c6916d72998ee5
Author: YuLuo <[email protected]>
AuthorDate: Fri Jul 26 11:33:15 2024 +0800

    [Improve] add AlertDefineExcel unit test and fix bugs (#2375)
    
    Signed-off-by: yuluo-yx <[email protected]>
    Co-authored-by: tomsun28 <[email protected]>
---
 alerter/pom.xml                                    |   6 +
 .../apache/hertzbeat/alert/dto/AlertDefineDTO.java |  59 +++++++
 .../hertzbeat/alert/dto/ExportAlertDefineDTO.java  |  38 +++++
 .../AlertDefineAbstractImExportServiceImpl.java    |  56 +------
 .../impl/AlertDefineExcelImExportServiceImpl.java  |   9 +-
 .../impl/AlertDefineJsonImExportServiceImpl.java   |   1 +
 .../impl/AlertDefineYamlImExportServiceImpl.java   |   1 +
 .../alert/service/AlertConvergeServiceTest.java    | 114 ++++++++++++++
 .../AlertDefineExcelImExportServiceTest.java       | 169 +++++++++++++++++++++
 9 files changed, 398 insertions(+), 55 deletions(-)

diff --git a/alerter/pom.xml b/alerter/pom.xml
index dcbce501f..8092c0797 100644
--- a/alerter/pom.xml
+++ b/alerter/pom.xml
@@ -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>
diff --git 
a/alerter/src/main/java/org/apache/hertzbeat/alert/dto/AlertDefineDTO.java 
b/alerter/src/main/java/org/apache/hertzbeat/alert/dto/AlertDefineDTO.java
new file mode 100644
index 000000000..0cf72feda
--- /dev/null
+++ b/alerter/src/main/java/org/apache/hertzbeat/alert/dto/AlertDefineDTO.java
@@ -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;
+}
diff --git 
a/alerter/src/main/java/org/apache/hertzbeat/alert/dto/ExportAlertDefineDTO.java
 
b/alerter/src/main/java/org/apache/hertzbeat/alert/dto/ExportAlertDefineDTO.java
new file mode 100644
index 000000000..fb7f25f33
--- /dev/null
+++ 
b/alerter/src/main/java/org/apache/hertzbeat/alert/dto/ExportAlertDefineDTO.java
@@ -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;
+}
diff --git 
a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineAbstractImExportServiceImpl.java
 
b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineAbstractImExportServiceImpl.java
index 2900b0fe3..121e17a9a 100644
--- 
a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineAbstractImExportServiceImpl.java
+++ 
b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineAbstractImExportServiceImpl.java
@@ -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;
@@ -51,7 +46,7 @@ public abstract class AlertDefineAbstractImExportServiceImpl 
implements AlertDef
         var formList = parseImport(is)
                 .stream()
                 .map(this::convert)
-                .collect(Collectors.toUnmodifiableList());
+                .toList();
         if (!CollectionUtils.isEmpty(formList)) {
             formList.forEach(alertDefine -> {
                 alertDefineService.validate(alertDefine, false);
@@ -65,7 +60,7 @@ public abstract class AlertDefineAbstractImExportServiceImpl 
implements AlertDef
         var monitorList = configList.stream()
                 .map(it -> alertDefineService.getAlertDefine(it))
                 .map(this::convert)
-                .collect(Collectors.toUnmodifiableList());
+                .toList();
         writeOs(monitorList, os);
     }
 
@@ -105,47 +100,4 @@ public abstract class 
AlertDefineAbstractImExportServiceImpl implements AlertDef
         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;
-    }
 }
diff --git 
a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineExcelImExportServiceImpl.java
 
b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineExcelImExportServiceImpl.java
index 88fa777ba..b0cace959 100644
--- 
a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineExcelImExportServiceImpl.java
+++ 
b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineExcelImExportServiceImpl.java
@@ -25,8 +25,11 @@ import java.util.ArrayList;
 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;
@@ -73,7 +76,7 @@ public class AlertDefineExcelImExportServiceImpl extends 
AlertDefineAbstractImEx
      * @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<>();
@@ -184,9 +187,9 @@ public class AlertDefineExcelImExportServiceImpl extends 
AlertDefineAbstractImEx
      * @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);
diff --git 
a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineJsonImExportServiceImpl.java
 
b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineJsonImExportServiceImpl.java
index 75bf8b097..894d1f87c 100644
--- 
a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineJsonImExportServiceImpl.java
+++ 
b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineJsonImExportServiceImpl.java
@@ -25,6 +25,7 @@ import java.io.OutputStream;
 import java.util.List;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.hertzbeat.alert.dto.ExportAlertDefineDTO;
 import org.springframework.stereotype.Service;
 
 /**
diff --git 
a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineYamlImExportServiceImpl.java
 
b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineYamlImExportServiceImpl.java
index 2dacffe4a..11cb6581f 100644
--- 
a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineYamlImExportServiceImpl.java
+++ 
b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineYamlImExportServiceImpl.java
@@ -23,6 +23,7 @@ import java.io.OutputStreamWriter;
 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;
diff --git 
a/alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertConvergeServiceTest.java
 
b/alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertConvergeServiceTest.java
new file mode 100644
index 000000000..e763a160d
--- /dev/null
+++ 
b/alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertConvergeServiceTest.java
@@ -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);
+       }
+
+}
diff --git 
a/alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertDefineExcelImExportServiceTest.java
 
b/alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertDefineExcelImExportServiceTest.java
new file mode 100644
index 000000000..9f20caab3
--- /dev/null
+++ 
b/alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertDefineExcelImExportServiceTest.java
@@ -0,0 +1,169 @@
+/*
+ * 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.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.hertzbeat.alert.dto.AlertDefineDTO;
+import org.apache.hertzbeat.alert.dto.ExportAlertDefineDTO;
+import 
org.apache.hertzbeat.alert.service.impl.AlertDefineExcelImExportServiceImpl;
+import org.apache.hertzbeat.common.entity.manager.TagItem;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.usermodel.WorkbookFactory;
+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.junit.jupiter.MockitoExtension;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * test case for {@link AlertDefineExcelImExportServiceImpl}
+ */
+
+@ExtendWith(MockitoExtension.class)
+public class AlertDefineExcelImExportServiceTest {
+
+       @InjectMocks
+       private AlertDefineExcelImExportServiceImpl 
alertDefineExcelImExportService;
+
+       private Workbook workbook;
+       private Sheet sheet;
+
+       @BeforeEach
+       public void setUp() throws IOException {
+
+               Workbook initialWorkbook = WorkbookFactory.create(true);
+               Sheet initialSheet = initialWorkbook.createSheet();
+               Row headerRow = initialSheet.createRow(0);
+               String[] headers = {"app", "metric", "field", "preset", "expr", 
"priority", "times", "tags", "enable", "recoverNotice", "template"};
+               for (int i = 0; i < headers.length; i++) {
+                       Cell cell = headerRow.createCell(i);
+                       cell.setCellValue(headers[i]);
+               }
+
+               Row row = initialSheet.createRow(1);
+               row.createCell(0).setCellValue("app1");
+               row.createCell(1).setCellValue("metric1");
+               row.createCell(2).setCellValue("field1");
+               row.createCell(3).setCellValue(true);
+               row.createCell(4).setCellValue("expr1");
+               row.createCell(5).setCellValue(1);
+               row.createCell(6).setCellValue(10);
+               
row.createCell(7).setCellValue("[{\"name\":\"tag1\",\"value\":\"value1\"}]");
+               row.createCell(8).setCellValue(true);
+               row.createCell(9).setCellValue(true);
+               row.createCell(10).setCellValue("template1");
+
+               ByteArrayInputStream inputStream = new 
ByteArrayInputStream(toByteArray(initialWorkbook));
+
+               workbook = WorkbookFactory.create(inputStream);
+               sheet = workbook.getSheetAt(0);
+       }
+
+       @Test
+       public void testParseImport() throws IOException {
+
+               try (ByteArrayInputStream inputStream = new 
ByteArrayInputStream(toByteArray(workbook))) {
+                       List<ExportAlertDefineDTO> result = 
alertDefineExcelImExportService.parseImport(inputStream);
+
+                       assertEquals(1, result.size());
+                       AlertDefineDTO alertDefineDTO = 
result.get(0).getAlertDefine();
+                       assertEquals("app1", alertDefineDTO.getApp());
+                       assertEquals("metric1", alertDefineDTO.getMetric());
+                       assertEquals("field1", alertDefineDTO.getField());
+                       assertTrue(alertDefineDTO.getPreset());
+                       assertEquals("expr1", alertDefineDTO.getExpr());
+                       assertEquals(10, alertDefineDTO.getTimes());
+                       assertEquals(1, alertDefineDTO.getTags().size());
+                       assertEquals("tag1", 
alertDefineDTO.getTags().get(0).getName());
+                       assertEquals("value1", 
alertDefineDTO.getTags().get(0).getValue());
+                       assertTrue(alertDefineDTO.getEnable());
+                       assertTrue(alertDefineDTO.getRecoverNotice());
+                       assertEquals("template1", alertDefineDTO.getTemplate());
+               }
+       }
+
+       @Test
+       public void testWriteOs() throws IOException {
+
+               List<ExportAlertDefineDTO> exportAlertDefineList = new 
ArrayList<>();
+               ExportAlertDefineDTO exportAlertDefineDTO = new 
ExportAlertDefineDTO();
+               AlertDefineDTO alertDefineDTO = new AlertDefineDTO();
+               alertDefineDTO.setApp("app1");
+               alertDefineDTO.setMetric("metric1");
+               alertDefineDTO.setField("field1");
+               alertDefineDTO.setPreset(true);
+               alertDefineDTO.setExpr("expr1");
+               alertDefineDTO.setPriority((byte) 1);
+               alertDefineDTO.setTimes(10);
+               List<TagItem> tags = new ArrayList<>();
+               TagItem tagItem = new TagItem();
+               tagItem.setName("tag1");
+               tagItem.setValue("value1");
+               tags.add(tagItem);
+               alertDefineDTO.setTags(tags);
+               alertDefineDTO.setEnable(true);
+               alertDefineDTO.setRecoverNotice(true);
+               alertDefineDTO.setTemplate("template1");
+               exportAlertDefineDTO.setAlertDefine(alertDefineDTO);
+               exportAlertDefineList.add(exportAlertDefineDTO);
+
+               try (ByteArrayOutputStream outputStream = new 
ByteArrayOutputStream()) {
+                       
alertDefineExcelImExportService.writeOs(exportAlertDefineList, outputStream);
+
+                       try (Workbook resultWorkbook = 
WorkbookFactory.create(new ByteArrayInputStream(outputStream.toByteArray()))) {
+                               Sheet resultSheet = 
resultWorkbook.getSheetAt(0);
+                               Row headerRow = resultSheet.getRow(0);
+                               assertEquals("app", 
headerRow.getCell(0).getStringCellValue());
+                               assertEquals("metric", 
headerRow.getCell(1).getStringCellValue());
+
+                               Row dataRow = resultSheet.getRow(1);
+                               assertEquals("app1", 
dataRow.getCell(0).getStringCellValue());
+                               assertEquals("metric1", 
dataRow.getCell(1).getStringCellValue());
+                               assertEquals("field1", 
dataRow.getCell(2).getStringCellValue());
+                               
assertTrue(dataRow.getCell(3).getBooleanCellValue());
+                               assertEquals("expr1", 
dataRow.getCell(4).getStringCellValue());
+                               assertEquals(1, (int) 
dataRow.getCell(5).getNumericCellValue());
+                               assertEquals(10, (int) 
dataRow.getCell(6).getNumericCellValue());
+                               
assertEquals("[{\"name\":\"tag1\",\"value\":\"value1\"}]", 
dataRow.getCell(7).getStringCellValue());
+                               
assertTrue(dataRow.getCell(8).getBooleanCellValue());
+                               
assertTrue(dataRow.getCell(9).getBooleanCellValue());
+                               assertEquals("template1", 
dataRow.getCell(10).getStringCellValue());
+                       }
+               }
+       }
+
+       private byte[] toByteArray(Workbook workbook) throws IOException {
+
+               try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
+                       workbook.write(bos);
+                       return bos.toByteArray();
+               }
+       }
+
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to