diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/YamlImExportServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/YamlImExportServiceImpl.java index 5b537514992..7ff529a787d 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/YamlImExportServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/YamlImExportServiceImpl.java @@ -60,7 +60,7 @@ public String getFileName() { * @return form */ @Override - List parseImport(InputStream is) { + public List 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(); @@ -73,7 +73,7 @@ List parseImport(InputStream is) { * @param os output stream */ @Override - void writeOs(List monitorList, OutputStream os) { + public void writeOs(List monitorList, OutputStream os) { DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); options.setIndent(2); diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/service/ExcelImExportServiceTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/service/ExcelImExportServiceTest.java index 97fa7c11c97..5e125c0d123 100644 --- a/manager/src/test/java/org/apache/hertzbeat/manager/service/ExcelImExportServiceTest.java +++ b/manager/src/test/java/org/apache/hertzbeat/manager/service/ExcelImExportServiceTest.java @@ -38,7 +38,7 @@ * Test case for {@link ExcelImExportServiceImpl} */ -class ExcelImExportServiceImplTest { +class ExcelImExportServiceTest { @InjectMocks private ExcelImExportServiceImpl excelImExportService; diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/service/YamlImExportServiceTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/service/YamlImExportServiceTest.java new file mode 100644 index 00000000000..a8bb3e09e98 --- /dev/null +++ b/manager/src/test/java/org/apache/hertzbeat/manager/service/YamlImExportServiceTest.java @@ -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 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 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 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")); + } + +}