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 ExcelImExportServiceImpl unit test #2460

Merged
merged 5 commits into from
Aug 5, 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
Expand Up @@ -48,7 +48,7 @@
* class AbstractImExportServiceImpl
*/
@Slf4j
abstract class AbstractImExportServiceImpl implements ImExportService {
public abstract class AbstractImExportServiceImpl implements ImExportService {

@Resource
@Lazy
Expand Down Expand Up @@ -173,7 +173,7 @@ protected String fileNamePrefix() {
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@ExcelTarget(value = "ExportMonitorDTO")
protected static class ExportMonitorDTO {
public static class ExportMonitorDTO {
@ExcelEntity(name = "Monitor")
private MonitorDTO monitor;
@ExcelCollection(name = "Params")
Expand All @@ -184,12 +184,11 @@ protected static class ExportMonitorDTO {
private Boolean detected;
}


@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@ExcelTarget(value = "MonitorDTO")
protected static class MonitorDTO {
public static class MonitorDTO {
@Excel(name = "Name")
private String name;
@Excel(name = "App")
Expand All @@ -208,12 +207,11 @@ protected static class MonitorDTO {
private String collector;
}


@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@ExcelTarget(value = "ParamDTO")
protected static class ParamDTO {
public static class ParamDTO {
@Excel(name = "Field")
private String field;
@Excel(name = "Type")
Expand All @@ -222,5 +220,4 @@ protected static class ParamDTO {
private String value;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public String getFileName() {
* @param is input stream
* @return form
*/

@Override
public List<ExportMonitorDTO> parseImport(InputStream is) {
try (Workbook workbook = WorkbookFactory.create(is)) {
Expand Down Expand Up @@ -210,14 +209,13 @@ private Byte getCellValueAsByte(Cell cell) {
return null;
}


/**
* Export Configuration to Output Stream
* @param monitorList config list
* @param os output stream
*/
@Override
void writeOs(List<ExportMonitorDTO> monitorList, OutputStream os) {
public void writeOs(List<ExportMonitorDTO> monitorList, OutputStream os) {
try {
Workbook workbook = WorkbookFactory.create(true);
String sheetName = "Export Monitor";
Expand Down Expand Up @@ -318,5 +316,4 @@ void writeOs(List<ExportMonitorDTO> monitorList, OutputStream os) {
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.apache.hertzbeat.manager.service;


import org.apache.hertzbeat.common.entity.manager.Collector;
import org.apache.hertzbeat.manager.dao.CollectorDao;
import org.apache.hertzbeat.manager.dao.CollectorMonitorBindDao;
Expand All @@ -41,7 +40,6 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;


/**
* Test case for {@link CollectorService}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.apache.hertzbeat.manager.service;


import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.any;
Expand All @@ -40,7 +39,6 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;


/**
* Test case for {@link ConfigService}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* 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.service;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.hertzbeat.manager.service.impl.AbstractImExportServiceImpl;
import org.apache.hertzbeat.manager.service.impl.ExcelImExportServiceImpl;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* Test case for {@link ExcelImExportServiceImpl}
*/

class ExcelImExportServiceImplTest {

@InjectMocks
private ExcelImExportServiceImpl excelImExportService;

@BeforeEach
public void setUp() {

MockitoAnnotations.openMocks(this);
}

@Test
public void testParseImport() throws IOException {

Workbook workbook = new XSSFWorkbook();
var sheet = workbook.createSheet();
var headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue("name");
headerRow.createCell(1).setCellValue("app");
headerRow.createCell(2).setCellValue("host");
var dataRow = sheet.createRow(1);
dataRow.createCell(0).setCellValue("Monitor1");
dataRow.createCell(1).setCellValue("App1");
dataRow.createCell(2).setCellValue("Host1");

ByteArrayOutputStream bos = new ByteArrayOutputStream();
workbook.write(bos);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());

List<AbstractImExportServiceImpl.ExportMonitorDTO> result = excelImExportService.parseImport(bis);
assertNotNull(result);
assertEquals(1, result.size());
assertEquals("Monitor1", result.get(0).getMonitor().getName());
}

@Test
public void testWriteOs() throws IOException {

AbstractImExportServiceImpl.MonitorDTO monitorDTO = new AbstractImExportServiceImpl.MonitorDTO();
monitorDTO.setName("Monitor1");
monitorDTO.setApp("App1");
monitorDTO.setHost("Host1");
monitorDTO.setIntervals(10);
monitorDTO.setStatus((byte) 1);
monitorDTO.setTags(List.of(1L, 2L));

AbstractImExportServiceImpl.ParamDTO paramDTO = new AbstractImExportServiceImpl.ParamDTO();
paramDTO.setField("field1");
paramDTO.setValue("value1");
paramDTO.setType((byte) 1);

AbstractImExportServiceImpl.ExportMonitorDTO exportMonitorDTO = new AbstractImExportServiceImpl.ExportMonitorDTO();
exportMonitorDTO.setMonitor(monitorDTO);
exportMonitorDTO.setParams(List.of(paramDTO));

List<AbstractImExportServiceImpl.ExportMonitorDTO> monitorList = List.of(exportMonitorDTO);

ByteArrayOutputStream bos = new ByteArrayOutputStream();
excelImExportService.writeOs(monitorList, bos);

ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
Workbook workbook = WorkbookFactory.create(bis);
var sheet = workbook.getSheetAt(0);
var dataRow = sheet.getRow(1);

assertEquals("Monitor1", dataRow.getCell(0).getStringCellValue());
assertEquals("App1", dataRow.getCell(1).getStringCellValue());
assertEquals("Host1", dataRow.getCell(2).getStringCellValue());
}

}
Loading