Skip to content

Commit 074b56b

Browse files
authored
Merge branch 'master' into 0804-yuluo/tets-1
2 parents 4c0fbe0 + 6ac092d commit 074b56b

File tree

11 files changed

+254
-44
lines changed

11 files changed

+254
-44
lines changed

alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineExcelImExportServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

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

20+
import static org.apache.hertzbeat.common.constants.ExportFileConstants.ExcelFile.FILE_SUFFIX;
21+
import static org.apache.hertzbeat.common.constants.ExportFileConstants.ExcelFile.TYPE;
2022
import com.fasterxml.jackson.core.type.TypeReference;
2123
import java.io.IOException;
2224
import java.io.InputStream;
@@ -48,8 +50,6 @@
4850
@Slf4j
4951
@Service
5052
public class AlertDefineExcelImExportServiceImpl extends AlertDefineAbstractImExportServiceImpl {
51-
public static final String TYPE = "EXCEL";
52-
public static final String FILE_SUFFIX = ".xlsx";
5353

5454
/**
5555
* Export file type

alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineJsonImExportServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

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

20+
import static org.apache.hertzbeat.common.constants.ExportFileConstants.JsonFile.FILE_SUFFIX;
21+
import static org.apache.hertzbeat.common.constants.ExportFileConstants.JsonFile.TYPE;
2022
import com.fasterxml.jackson.core.type.TypeReference;
2123
import com.fasterxml.jackson.databind.ObjectMapper;
2224
import java.io.IOException;
@@ -35,8 +37,6 @@
3537
@RequiredArgsConstructor
3638
@Service
3739
public class AlertDefineJsonImExportServiceImpl extends AlertDefineAbstractImExportServiceImpl {
38-
public static final String TYPE = "JSON";
39-
public static final String FILE_SUFFIX = ".json";
4040

4141
private final ObjectMapper objectMapper;
4242

alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineServiceImpl.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@
3737
import org.apache.hertzbeat.alert.dao.AlertMonitorDao;
3838
import org.apache.hertzbeat.alert.service.AlertDefineImExportService;
3939
import org.apache.hertzbeat.alert.service.AlertDefineService;
40+
import org.apache.hertzbeat.common.constants.ExportFileConstants;
4041
import org.apache.hertzbeat.common.entity.alerter.AlertDefine;
4142
import org.apache.hertzbeat.common.entity.alerter.AlertDefineMonitorBind;
4243
import org.apache.hertzbeat.common.entity.manager.Monitor;
44+
import org.apache.hertzbeat.common.util.FileUtil;
4345
import org.apache.hertzbeat.common.util.JexlExpressionRunner;
4446
import org.springframework.beans.factory.annotation.Autowired;
4547
import org.springframework.data.domain.Page;
@@ -236,22 +238,11 @@ public void export(List<Long> ids, String type, HttpServletResponse res) throws
236238

237239
@Override
238240
public void importConfig(MultipartFile file) throws Exception {
239-
var fileName = file.getOriginalFilename();
240-
if (!StringUtils.hasText(fileName)) {
241-
return;
242-
}
243-
var type = "";
244-
if (fileName.toLowerCase().endsWith(AlertDefineJsonImExportServiceImpl.FILE_SUFFIX)) {
245-
type = AlertDefineJsonImExportServiceImpl.TYPE;
246-
}
247-
if (fileName.toLowerCase().endsWith(AlertDefineExcelImExportServiceImpl.FILE_SUFFIX)) {
248-
type = AlertDefineExcelImExportServiceImpl.TYPE;
249-
}
250-
if (fileName.toLowerCase().endsWith(AlertDefineYamlImExportServiceImpl.FILE_SUFFIX)) {
251-
type = AlertDefineYamlImExportServiceImpl.TYPE;
252-
}
241+
242+
var type = FileUtil.getFileType(file);
243+
var fileName = FileUtil.getFileName(file);
253244
if (!alertDefineImExportServiceMap.containsKey(type)) {
254-
throw new RuntimeException("file " + fileName + " is not supported.");
245+
throw new RuntimeException(ExportFileConstants.FILE + " " + fileName + " is not supported.");
255246
}
256247
var imExportService = alertDefineImExportServiceMap.get(type);
257248
imExportService.importConfig(file.getInputStream());

alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineYamlImExportServiceImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

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

20+
import static org.apache.hertzbeat.common.constants.ExportFileConstants.YamlFile.FILE_SUFFIX;
21+
import static org.apache.hertzbeat.common.constants.ExportFileConstants.YamlFile.TYPE;
2022
import java.io.InputStream;
2123
import java.io.OutputStream;
2224
import java.io.OutputStreamWriter;
@@ -29,15 +31,13 @@
2931
import org.yaml.snakeyaml.Yaml;
3032

3133
/**
32-
* Configure the import and export Yaml format
34+
* Configure the import and export Yaml format.
3335
*/
36+
3437
@Slf4j
3538
@Service
3639
public class AlertDefineYamlImExportServiceImpl extends AlertDefineAbstractImExportServiceImpl {
3740

38-
public static final String TYPE = "YAML";
39-
public static final String FILE_SUFFIX = ".yaml";
40-
4141
@Override
4242
public String type() {
4343
return TYPE;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.hertzbeat.common.constants;
19+
20+
/**
21+
* Export file type constants
22+
*/
23+
24+
public interface ExportFileConstants {
25+
26+
/**
27+
* Export file name constants.
28+
*/
29+
String FILE = "file";
30+
31+
/**
32+
* Excel export files constants.
33+
*/
34+
interface ExcelFile {
35+
36+
/**
37+
* Export file type.
38+
*/
39+
String TYPE = "EXCEL";
40+
41+
/**
42+
* Export file suffix.
43+
*/
44+
String FILE_SUFFIX = ".xlsx";
45+
}
46+
47+
/**
48+
* Json export file constants.
49+
*/
50+
interface JsonFile {
51+
52+
/**
53+
* Export file type.
54+
*/
55+
String TYPE = "JSON";
56+
57+
/**
58+
* Export file suffix.
59+
*/
60+
String FILE_SUFFIX = ".json";
61+
}
62+
63+
/**
64+
* Yaml export file constants.
65+
*/
66+
interface YamlFile {
67+
68+
/**
69+
* Export file type.
70+
*/
71+
String TYPE = "YAML";
72+
73+
/**
74+
* Export file suffix.
75+
*/
76+
String FILE_SUFFIX = ".yaml";
77+
}
78+
79+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.hertzbeat.common.util;
19+
20+
import java.util.Map;
21+
import org.apache.hertzbeat.common.constants.ExportFileConstants;
22+
import org.springframework.util.StringUtils;
23+
import org.springframework.web.multipart.MultipartFile;
24+
25+
/**
26+
* File utils.
27+
*/
28+
29+
public final class FileUtil {
30+
31+
private FileUtil() {
32+
}
33+
34+
private static final Map<String, String> fileTypes;
35+
36+
static {
37+
fileTypes = Map.of(
38+
ExportFileConstants.JsonFile.FILE_SUFFIX, ExportFileConstants.JsonFile.TYPE,
39+
ExportFileConstants.YamlFile.FILE_SUFFIX, ExportFileConstants.YamlFile.TYPE,
40+
ExportFileConstants.ExcelFile.FILE_SUFFIX, ExportFileConstants.ExcelFile.TYPE
41+
);
42+
}
43+
44+
/**
45+
* Get file name.
46+
* @param file {@link MultipartFile}
47+
* @return file name
48+
*/
49+
public static String getFileName(MultipartFile file) {
50+
51+
var fileName = file.getOriginalFilename();
52+
if (!StringUtils.hasText(fileName)) {
53+
return "";
54+
}
55+
return fileName;
56+
}
57+
58+
/**
59+
* Get file type.
60+
* @param file {@link MultipartFile}
61+
* @return file type
62+
*/
63+
public static String getFileType(MultipartFile file) {
64+
65+
var fileName = getFileName(file);
66+
67+
if (!StringUtils.hasText(fileName)) {
68+
return "";
69+
}
70+
71+
var dotIndex = fileName.lastIndexOf('.');
72+
if (dotIndex == -1 || dotIndex == fileName.length() - 1) {
73+
return "";
74+
}
75+
var fileNameExtension = fileName.substring(dotIndex);
76+
77+
return fileTypes.get(fileNameExtension);
78+
}
79+
80+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.hertzbeat.common.util;
19+
20+
import org.apache.hertzbeat.common.constants.ExportFileConstants;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
import org.springframework.mock.web.MockMultipartFile;
24+
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
27+
/**
28+
* test case for {@link FileUtil}.
29+
*/
30+
31+
class FileUtilTest {
32+
33+
private static final String JSON_TYPE = "application/json";
34+
private static final String EXCEL_TYPE = "application/vnd.ms-excel";
35+
private static final String YAML_TYPE = "application/x-yaml";
36+
37+
private MockMultipartFile jsonFile;
38+
private MockMultipartFile excelFile;
39+
private MockMultipartFile yamlFile;
40+
private MockMultipartFile emptyFile;
41+
42+
@BeforeEach
43+
void setUp() {
44+
45+
jsonFile = new MockMultipartFile("file", "test.json", JSON_TYPE, "test content".getBytes());
46+
excelFile = new MockMultipartFile("file", "test.xlsx", EXCEL_TYPE, "test content".getBytes());
47+
yamlFile = new MockMultipartFile("file", "test.yaml", YAML_TYPE, "test content".getBytes());
48+
emptyFile = new MockMultipartFile("file", "", null, (byte[]) null);
49+
}
50+
51+
@Test
52+
void testGetFileName() {
53+
54+
assertEquals("test.json", FileUtil.getFileName(jsonFile));
55+
assertEquals("test.xlsx", FileUtil.getFileName(excelFile));
56+
assertEquals("test.yaml", FileUtil.getFileName(yamlFile));
57+
assertEquals("", FileUtil.getFileName(emptyFile));
58+
}
59+
60+
@Test
61+
void testGetFileType() {
62+
63+
assertEquals(ExportFileConstants.JsonFile.TYPE, FileUtil.getFileType(jsonFile));
64+
assertEquals(ExportFileConstants.ExcelFile.TYPE, FileUtil.getFileType(excelFile));
65+
assertEquals(ExportFileConstants.YamlFile.TYPE, FileUtil.getFileType(yamlFile));
66+
assertEquals("", FileUtil.getFileType(emptyFile));
67+
}
68+
69+
}

manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ExcelImExportServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.apache.hertzbeat.manager.service.impl;
1919

20+
import static org.apache.hertzbeat.common.constants.ExportFileConstants.ExcelFile.FILE_SUFFIX;
21+
import static org.apache.hertzbeat.common.constants.ExportFileConstants.ExcelFile.TYPE;
2022
import java.io.IOException;
2123
import java.io.InputStream;
2224
import java.io.OutputStream;
@@ -49,8 +51,6 @@
4951
@RequiredArgsConstructor
5052
@Service
5153
public class ExcelImExportServiceImpl extends AbstractImExportServiceImpl{
52-
public static final String TYPE = "EXCEL";
53-
public static final String FILE_SUFFIX = ".xlsx";
5454

5555
/**
5656
* Export file type

manager/src/main/java/org/apache/hertzbeat/manager/service/impl/JsonImExportServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
package org.apache.hertzbeat.manager.service.impl;
1717

18+
import static org.apache.hertzbeat.common.constants.ExportFileConstants.JsonFile.FILE_SUFFIX;
19+
import static org.apache.hertzbeat.common.constants.ExportFileConstants.JsonFile.TYPE;
1820
import com.fasterxml.jackson.core.type.TypeReference;
1921
import com.fasterxml.jackson.databind.ObjectMapper;
2022
import java.io.IOException;
@@ -32,8 +34,6 @@
3234
@RequiredArgsConstructor
3335
@Service
3436
public class JsonImExportServiceImpl extends AbstractImExportServiceImpl {
35-
public static final String TYPE = "JSON";
36-
public static final String FILE_SUFFIX = ".json";
3737

3838
private final ObjectMapper objectMapper;
3939

0 commit comments

Comments
 (0)