Skip to content

Commit

Permalink
Merge branch 'master' into bugfix-page
Browse files Browse the repository at this point in the history
  • Loading branch information
yuluo-yx authored Aug 27, 2024
2 parents 6d06d27 + 1799e00 commit 5f7f21e
Show file tree
Hide file tree
Showing 156 changed files with 7,012 additions and 5,029 deletions.
2 changes: 1 addition & 1 deletion .markdownlint-cli2.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"MD036": false,
"MD040": true,
"MD045": false,
"MD046": false,
"MD046": true,
"MD047": true
},
"ignore": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@

package org.apache.hertzbeat.alert.controller;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
import org.apache.hertzbeat.alert.service.AlertConvergeService;
import org.apache.hertzbeat.common.constants.CommonConstants;
import org.apache.hertzbeat.common.entity.alerter.AlertConverge;
Expand All @@ -30,96 +40,85 @@
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;

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

@ExtendWith(MockitoExtension.class)
public class AlertConvergeControllerTest {

private MockMvc mockMvc;
private MockMvc mockMvc;

@Mock
private AlertConvergeService alertConvergeService;
@Mock
private AlertConvergeService alertConvergeService;

private AlertConverge alertConverge;
private AlertConverge alertConverge;

@InjectMocks
private AlertConvergeController alertConvergeController;
@InjectMocks
private AlertConvergeController alertConvergeController;

@BeforeEach
void setUp() {
@BeforeEach
void setUp() {

this.mockMvc = standaloneSetup(alertConvergeController).build();
this.mockMvc = standaloneSetup(alertConvergeController).build();

alertConverge = AlertConverge.builder()
.name("test")
.creator("admin")
.modifier("admin")
.id(1L)
.build();
}
alertConverge = AlertConverge.builder()
.name("test")
.creator("admin")
.modifier("admin")
.id(1L)
.build();
}

@Test
void testAddNewAlertConverge() throws Exception {
@Test
void testAddNewAlertConverge() throws Exception {

doNothing().when(alertConvergeService).validate(any(AlertConverge.class), eq(false));
doNothing().when(alertConvergeService).addAlertConverge(any(AlertConverge.class));
doNothing().when(alertConvergeService).validate(any(AlertConverge.class), eq(false));
doNothing().when(alertConvergeService).addAlertConverge(any(AlertConverge.class));

mockMvc.perform(post("/api/alert/converge")
.contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.toJson(alertConverge))
).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
.andExpect(jsonPath("$.msg").value("Add success"));
}
mockMvc.perform(post("/api/alert/converge")
.contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.toJson(alertConverge))
).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
.andExpect(jsonPath("$.msg").value("Add success"));
}

@Test
void testModifyAlertConverge() throws Exception {
@Test
void testModifyAlertConverge() throws Exception {

doNothing().when(alertConvergeService).validate(any(AlertConverge.class), eq(true));
doNothing().when(alertConvergeService).modifyAlertConverge(any(AlertConverge.class));
doNothing().when(alertConvergeService).validate(any(AlertConverge.class), eq(true));
doNothing().when(alertConvergeService).modifyAlertConverge(any(AlertConverge.class));

mockMvc.perform(put("/api/alert/converge")
.contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.toJson(alertConverge))
).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
.andExpect(jsonPath("$.msg").value("Modify success"));
}
mockMvc.perform(put("/api/alert/converge")
.contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.toJson(alertConverge))
).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
.andExpect(jsonPath("$.msg").value("Modify success"));
}

@Test
void testGetAlertConvergeExists() throws Exception {
@Test
void testGetAlertConvergeExists() throws Exception {

when(alertConvergeService.getAlertConverge(1L)).thenReturn(alertConverge);
when(alertConvergeService.getAlertConverge(1L)).thenReturn(alertConverge);

mockMvc.perform(get("/api/alert/converge/{id}", 1L)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.id").value(alertConverge.getId()));
}
mockMvc.perform(get("/api/alert/converge/{id}", 1L)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.id").value(alertConverge.getId()));
}

@Test
void testGetAlertConvergeNotExists() throws Exception {
@Test
void testGetAlertConvergeNotExists() throws Exception {

when(alertConvergeService.getAlertConverge(1L)).thenReturn(null);
when(alertConvergeService.getAlertConverge(1L)).thenReturn(null);

mockMvc.perform(get("/api/alert/converge/{id}", 1L)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.MONITOR_NOT_EXIST_CODE))
.andExpect(jsonPath("$.msg").value("AlertConverge not exist."));
}
mockMvc.perform(get("/api/alert/converge/{id}", 1L)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.MONITOR_NOT_EXIST_CODE))
.andExpect(jsonPath("$.msg").value("AlertConverge not exist."));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,68 +53,68 @@
@ExtendWith(MockitoExtension.class)
class AlertConvergesControllerTest {

private MockMvc mockMvc;
private MockMvc mockMvc;

@Mock
private AlertConvergeService alertConvergeService;
@Mock
private AlertConvergeService alertConvergeService;

@InjectMocks
private AlertConvergesController alertConvergesController;
@InjectMocks
private AlertConvergesController alertConvergesController;

private List<AlertConverge> alertConvergeList;
private List<AlertConverge> alertConvergeList;

@BeforeEach
void setUp() {
@BeforeEach
void setUp() {

this.mockMvc = standaloneSetup(alertConvergesController).build();
this.mockMvc = standaloneSetup(alertConvergesController).build();

AlertConverge alertConverge1 = AlertConverge.builder()
.name("Converge1")
.id(1L)
.build();
AlertConverge alertConverge1 = AlertConverge.builder()
.name("Converge1")
.id(1L)
.build();

AlertConverge alertConverge2 = AlertConverge.builder()
.name("Converge2")
.id(2L)
.build();
AlertConverge alertConverge2 = AlertConverge.builder()
.name("Converge2")
.id(2L)
.build();

alertConvergeList = Arrays.asList(alertConverge1, alertConverge2);
}
alertConvergeList = Arrays.asList(alertConverge1, alertConverge2);
}

@Test
void testGetAlertConverges() throws Exception {
@Test
void testGetAlertConverges() throws Exception {

Page<AlertConverge> alertConvergePage = new PageImpl<>(
alertConvergeList,
PageRequest.of(0, 8, Sort.by("id").descending()),
alertConvergeList.size()
);
Page<AlertConverge> alertConvergePage = new PageImpl<>(
alertConvergeList,
PageRequest.of(0, 8, Sort.by("id").descending()),
alertConvergeList.size()
);

when(alertConvergeService.getAlertConverges(null, null, "id", "desc", 0, 8)).thenReturn(alertConvergePage);
when(alertConvergeService.getAlertConverges(null, null, "id", "desc", 0, 8)).thenReturn(alertConvergePage);

mockMvc.perform(get("/api/alert/converges")
.param("pageIndex", "0")
.param("pageSize", "8")
.param("sort", "id")
.param("order", "desc")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.content[0].id").value(1))
.andExpect(jsonPath("$.data.content[0].name").value("Converge1"))
.andExpect(jsonPath("$.data.content[1].id").value(2))
.andExpect(jsonPath("$.data.content[1].name").value("Converge2"));
}
mockMvc.perform(get("/api/alert/converges")
.param("pageIndex", "0")
.param("pageSize", "8")
.param("sort", "id")
.param("order", "desc")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.content[0].id").value(1))
.andExpect(jsonPath("$.data.content[0].name").value("Converge1"))
.andExpect(jsonPath("$.data.content[1].id").value(2))
.andExpect(jsonPath("$.data.content[1].name").value("Converge2"));
}

@Test
void testDeleteAlertDefines() throws Exception {
@Test
void testDeleteAlertDefines() throws Exception {

doNothing().when(alertConvergeService).deleteAlertConverges(eq(new HashSet<>(Arrays.asList(1L, 2L))));
doNothing().when(alertConvergeService).deleteAlertConverges(eq(new HashSet<>(Arrays.asList(1L, 2L))));

mockMvc.perform(delete("/api/alert/converges")
.param("ids", "1,2")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE));
}
mockMvc.perform(delete("/api/alert/converges")
.param("ids", "1,2")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ void setUp() {
void addNewAlertDefine() throws Exception {
// Simulate the client sending a request to the server
mockMvc.perform(MockMvcRequestBuilders.post("/api/alert/define")
.contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.toJson(this.alertDefine)))
.contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.toJson(this.alertDefine)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
.andReturn();
Expand All @@ -107,8 +107,8 @@ void addNewAlertDefine() throws Exception {
@Test
void modifyAlertDefine() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.put("/api/alert/define")
.contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.toJson(this.alertDefine)))
.contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.toJson(this.alertDefine)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
.andReturn();
Expand All @@ -121,7 +121,7 @@ void getAlertDefine() throws Exception {
.thenReturn(this.alertDefine);

mockMvc.perform(MockMvcRequestBuilders.get("/api/alert/define/" + this.alertDefine.getId())
.contentType(MediaType.APPLICATION_JSON))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
.andExpect(jsonPath("$.data.id").value(alertDefine.getId()))
Expand All @@ -138,8 +138,8 @@ void getAlertDefine() throws Exception {
@Test
void deleteAlertDefine() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.delete("/api/alert/define/" + this.alertDefine.getId())
.contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.toJson(this.alertDefine)))
.contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.toJson(this.alertDefine)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
.andReturn();
Expand All @@ -148,8 +148,8 @@ void deleteAlertDefine() throws Exception {
@Test
void applyAlertDefineMonitorsBind() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.post("/api/alert/define/" + this.alertDefine.getId() + "/monitors")
.contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.toJson(this.alertDefineMonitorBinds)))
.contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.toJson(this.alertDefineMonitorBinds)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
.andReturn();
Expand All @@ -161,7 +161,7 @@ void getAlertDefineMonitorsBind() throws Exception {
.thenReturn(this.alertDefineMonitorBinds);

mockMvc.perform(MockMvcRequestBuilders.get("/api/alert/define/" + this.alertDefine.getId() + "/monitors")
.contentType(MediaType.APPLICATION_JSON))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
.andExpect(jsonPath("$.data[0].id").value(alertDefineMonitorBinds.get(0).getId()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class AlertDefinesControllerTest {
String order = "asc";
Integer pageIndex = 1;
Integer pageSize = 7;

// Parameter collection
Map<String, Object> content = new HashMap<>();

Expand Down Expand Up @@ -114,13 +114,13 @@ void getAlertDefines() throws Exception {
Mockito.when(alertDefineService.getAlertDefines(null, null, null, "id", "desc", 1, 10)).thenReturn(new PageImpl<>(Collections.singletonList(define)));

mockMvc.perform(MockMvcRequestBuilders.get(
"/api/alert/defines")
.param("ids", ids.toString().substring(1, ids.toString().length() - 1))
.param("priority", priority.toString())
.param("sort", sort)
.param("order", order)
.param("pageIndex", pageIndex.toString())
.param("pageSize", pageSize.toString()))
"/api/alert/defines")
.param("ids", ids.toString().substring(1, ids.toString().length() - 1))
.param("priority", priority.toString())
.param("sort", sort)
.param("order", order)
.param("pageIndex", pageIndex.toString())
.param("pageSize", pageSize.toString()))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
.andExpect(jsonPath("$.data.content").value(new ArrayList<>()))
Expand All @@ -142,8 +142,8 @@ void getAlertDefines() throws Exception {
@Test
void deleteAlertDefines() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.delete("/api/alert/defines")
.contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.toJson(ids)))
.contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.toJson(ids)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
.andReturn();
Expand Down
Loading

0 comments on commit 5f7f21e

Please sign in to comment.