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 AlertDefineJsonImExportServiceImpl unit test #2391

Merged
merged 6 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -51,7 +51,7 @@ public String getFileName() {
}

@Override
List<ExportAlertDefineDTO> parseImport(InputStream is) {
public List<ExportAlertDefineDTO> parseImport(InputStream is) {
try {
return objectMapper.readValue(is, new TypeReference<>() {
});
Expand All @@ -62,7 +62,7 @@ List<ExportAlertDefineDTO> parseImport(InputStream is) {
}

@Override
void writeOs(List<ExportAlertDefineDTO> exportAlertDefineList, OutputStream os) {
public void writeOs(List<ExportAlertDefineDTO> exportAlertDefineList, OutputStream os) {
try {
objectMapper.writeValue(os, exportAlertDefineList);
} catch (IOException ex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* 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.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.hertzbeat.alert.dto.AlertDefineDTO;
import org.apache.hertzbeat.alert.dto.ExportAlertDefineDTO;
import org.apache.hertzbeat.alert.service.impl.AlertDefineJsonImExportServiceImpl;
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.Mock;
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.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* test case for {@link AlertDefineJsonImExportServiceImpl}
*/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest delete this blank line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don`t think so, Keeping a certain amount of blank lines facilitates readability without being too centralized

@ExtendWith(MockitoExtension.class)
class AlertDefineJsonImExportServiceImplTest {

@Mock
private ObjectMapper objectMapper;

@InjectMocks
private AlertDefineJsonImExportServiceImpl service;

private static final String JSON_DATA = "[{\"alertDefine\":{\"app\":\"App1\",\"metric\":\"Metric1\",\"field\":\"Field1\",\"preset\":true,\"expr\":\"Expr1\",\"priority\":1,\"times\":1,\"tags\":[],\"enable\":true,\"recoverNotice\":true,\"template\":\"Template1\"}}]";

private InputStream inputStream;
private List<ExportAlertDefineDTO> alertDefineList;

@BeforeEach
public void setup() {

inputStream = new ByteArrayInputStream(JSON_DATA.getBytes());

AlertDefineDTO alertDefine = new AlertDefineDTO();
alertDefine.setApp("App1");
alertDefine.setMetric("Metric1");
alertDefine.setField("Field1");
alertDefine.setPreset(true);
alertDefine.setExpr("Expr1");
alertDefine.setPriority((byte) 1);
alertDefine.setTimes(1);
alertDefine.setTags(List.of());
alertDefine.setEnable(true);
alertDefine.setRecoverNotice(true);
alertDefine.setTemplate("Template1");

ExportAlertDefineDTO exportAlertDefine = new ExportAlertDefineDTO();
exportAlertDefine.setAlertDefine(alertDefine);

alertDefineList = List.of(exportAlertDefine);
}

@Test
void testParseImport() throws IOException {

when(objectMapper.readValue(
any(InputStream.class),
any(TypeReference.class))
).thenReturn(alertDefineList);

List<ExportAlertDefineDTO> result = service.parseImport(inputStream);

assertNotNull(result);
assertEquals(1, result.size());
assertEquals(alertDefineList, result);
verify(objectMapper, times(1)).readValue(any(InputStream.class), any(TypeReference.class));
}

@Test
void testParseImportFailed() throws IOException {

when(objectMapper.readValue(
any(InputStream.class),
any(TypeReference.class))
).thenThrow(new IOException("Test Exception"));

RuntimeException exception = assertThrows(RuntimeException.class, () -> service.parseImport(inputStream));

assertEquals("import alertDefine failed", exception.getMessage());
verify(objectMapper, times(1)).readValue(any(InputStream.class), any(TypeReference.class));
}

@Test
void testWriteOs() throws IOException {

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

service.writeOs(alertDefineList, outputStream);

verify(
objectMapper,
times(1)
).writeValue(
any(OutputStream.class),
eq(alertDefineList)
);
yuluo-yx marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
void testWriteOsFailed() throws IOException {

doThrow(new IOException("Test Exception")).when(objectMapper).writeValue(any(OutputStream.class), any());

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

RuntimeException exception = assertThrows(
RuntimeException.class,
() -> service.writeOs(alertDefineList, outputStream)
);

assertEquals("export alertDefine failed", exception.getMessage());
verify(objectMapper, times(1)).writeValue(any(OutputStream.class), eq(alertDefineList));
}

}
Loading