Skip to content

Commit

Permalink
[improve] add YamlImExportServiceImpl unit test (apache#2470)
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 Aug 7, 2024
1 parent c4fa738 commit e8dc648
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public String getFileName() {
* @return form
*/
@Override
List<ExportMonitorDTO> parseImport(InputStream is) {
public List<ExportMonitorDTO> parseImport(InputStream is) {
// todo now disable this, will enable it in the future.
// upgrade to snakeyaml 2.2 and springboot3.x to fix the issue
Yaml yaml = new Yaml();
Expand All @@ -73,7 +73,7 @@ List<ExportMonitorDTO> parseImport(InputStream is) {
* @param os output stream
*/
@Override
void writeOs(List<ExportMonitorDTO> monitorList, OutputStream os) {
public void writeOs(List<ExportMonitorDTO> monitorList, OutputStream os) {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setIndent(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* Test case for {@link ExcelImExportServiceImpl}
*/

class ExcelImExportServiceImplTest {
class ExcelImExportServiceTest {

@InjectMocks
private ExcelImExportServiceImpl excelImExportService;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* 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.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import org.apache.hertzbeat.manager.service.impl.AbstractImExportServiceImpl;
import org.apache.hertzbeat.manager.service.impl.YamlImExportServiceImpl;
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.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

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

@ExtendWith(MockitoExtension.class)
class YamlImExportServiceTest {

@InjectMocks
private YamlImExportServiceImpl yamlImExportService;

@BeforeEach
void setUp() {

yamlImExportService = new YamlImExportServiceImpl();
}

@Test
void testType() {

assertEquals("YAML", yamlImExportService.type());
}

@Test
void testParseImport() {

String yamlContent = "- id: 1\n name: Monitor1\n- id: 2\n name: Monitor2";
InputStream is = new ByteArrayInputStream(yamlContent.getBytes(StandardCharsets.UTF_8));

List<AbstractImExportServiceImpl.ExportMonitorDTO> result = yamlImExportService.parseImport(is);

assertNotNull(result);
assertEquals(2, result.size());
assertEquals("[{id=1, name=Monitor1}, {id=2, name=Monitor2}]", result.toString());
}

@Test
void testParseImportNull() {

InputStream is = new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));

List<AbstractImExportServiceImpl.ExportMonitorDTO> result = yamlImExportService.parseImport(is);

assertNull(result);
}

@Test
void testWriteOS() {

AbstractImExportServiceImpl.ParamDTO paramDTO = new AbstractImExportServiceImpl.ParamDTO();
paramDTO.setType((byte) 1);
paramDTO.setField("Test");
paramDTO.setValue("Test");
AbstractImExportServiceImpl.MonitorDTO monitorDTO = new AbstractImExportServiceImpl.MonitorDTO();
monitorDTO.setTags(List.of(1L, 2L));
monitorDTO.setIntervals(1);
monitorDTO.setStatus((byte) 1);
AbstractImExportServiceImpl.ExportMonitorDTO exportMonitorDTO1 = new AbstractImExportServiceImpl.ExportMonitorDTO();
exportMonitorDTO1.setParams(List.of(paramDTO));
exportMonitorDTO1.setMonitor(monitorDTO);
exportMonitorDTO1.setMetrics(List.of("Test1", "Test2"));
AbstractImExportServiceImpl.ExportMonitorDTO exportMonitorDTO2 = new AbstractImExportServiceImpl.ExportMonitorDTO();
exportMonitorDTO2.setParams(List.of(paramDTO));
exportMonitorDTO2.setMonitor(monitorDTO);
exportMonitorDTO2.setMetrics(List.of("Test1", "Test2"));

List<AbstractImExportServiceImpl.ExportMonitorDTO> monitorList = Arrays.asList(
exportMonitorDTO1,
exportMonitorDTO2
);
OutputStream os = new ByteArrayOutputStream();

yamlImExportService.writeOs(monitorList, os);

String output = os.toString();
assertTrue(output.contains("metrics:\n - Test1"));
assertTrue(output.contains(" params:\n - &id002\n field: Test"));
}

}

0 comments on commit e8dc648

Please sign in to comment.