This is an automated email from the ASF dual-hosted git repository.
gongchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git
The following commit(s) were added to refs/heads/master by this push:
new 3d9983548 [Improve] add AlertConvergesController &
AlertConvergeController unit test (#2424)
3d9983548 is described below
commit 3d99835487a7b9fcd0532b241f35af1463175bfb
Author: YuLuo <[email protected]>
AuthorDate: Thu Aug 1 10:31:18 2024 +0800
[Improve] add AlertConvergesController & AlertConvergeController unit test
(#2424)
Signed-off-by: yuluo-yx <[email protected]>
Co-authored-by: tomsun28 <[email protected]>
---
.../controller/AlertConvergeControllerTest.java | 125 +++++++++++++++++++++
.../controller/AlertConvergesControllerTest.java | 122 ++++++++++++++++++++
2 files changed, 247 insertions(+)
diff --git
a/alerter/src/test/java/org/apache/hertzbeat/alert/controller/AlertConvergeControllerTest.java
b/alerter/src/test/java/org/apache/hertzbeat/alert/controller/AlertConvergeControllerTest.java
new file mode 100644
index 000000000..6ccf34955
--- /dev/null
+++
b/alerter/src/test/java/org/apache/hertzbeat/alert/controller/AlertConvergeControllerTest.java
@@ -0,0 +1,125 @@
+/*
+ * 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.controller;
+
+import org.apache.hertzbeat.alert.service.AlertConvergeService;
+import org.apache.hertzbeat.common.constants.CommonConstants;
+import org.apache.hertzbeat.common.entity.alerter.AlertConverge;
+import org.apache.hertzbeat.common.util.JsonUtil;
+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 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;
+
+ @Mock
+ private AlertConvergeService alertConvergeService;
+
+ private AlertConverge alertConverge;
+
+ @InjectMocks
+ private AlertConvergeController alertConvergeController;
+
+ @BeforeEach
+ void setUp() {
+
+ this.mockMvc = standaloneSetup(alertConvergeController).build();
+
+ alertConverge = AlertConverge.builder()
+ .name("test")
+ .creator("admin")
+ .modifier("admin")
+ .id(1L)
+ .build();
+ }
+
+ @Test
+ void testAddNewAlertConverge() throws Exception {
+
+
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"));
+ }
+
+ @Test
+ void testModifyAlertConverge() throws Exception {
+
+
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"));
+ }
+
+ @Test
+ void testGetAlertConvergeExists() throws Exception {
+
+
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()));
+ }
+
+ @Test
+ void testGetAlertConvergeNotExists() throws Exception {
+
+
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."));
+ }
+
+}
diff --git
a/alerter/src/test/java/org/apache/hertzbeat/alert/controller/AlertConvergesControllerTest.java
b/alerter/src/test/java/org/apache/hertzbeat/alert/controller/AlertConvergesControllerTest.java
new file mode 100644
index 000000000..28088f2d4
--- /dev/null
+++
b/alerter/src/test/java/org/apache/hertzbeat/alert/controller/AlertConvergesControllerTest.java
@@ -0,0 +1,122 @@
+/*
+ * 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.controller;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+
+import org.apache.hertzbeat.alert.service.AlertConvergeService;
+import org.apache.hertzbeat.common.constants.CommonConstants;
+import org.apache.hertzbeat.common.entity.alerter.AlertConverge;
+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 org.springframework.http.MediaType;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageImpl;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Sort;
+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.delete;
+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 static
org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+
+/**
+ * test case for {@link AlertConvergesController}
+ */
+
+@ExtendWith(MockitoExtension.class)
+class AlertConvergesControllerTest {
+
+ private MockMvc mockMvc;
+
+ @Mock
+ private AlertConvergeService alertConvergeService;
+
+ @InjectMocks
+ private AlertConvergesController alertConvergesController;
+
+ private List<AlertConverge> alertConvergeList;
+
+ @BeforeEach
+ void setUp() {
+
+ this.mockMvc =
standaloneSetup(alertConvergesController).build();
+
+ AlertConverge alertConverge1 = AlertConverge.builder()
+ .name("Converge1")
+ .id(1L)
+ .build();
+
+ AlertConverge alertConverge2 = AlertConverge.builder()
+ .name("Converge2")
+ .id(2L)
+ .build();
+
+ alertConvergeList = Arrays.asList(alertConverge1,
alertConverge2);
+ }
+
+ @Test
+ void testGetAlertConverges() throws Exception {
+
+ Page<AlertConverge> alertConvergePage = new PageImpl<>(
+ alertConvergeList,
+ PageRequest.of(0, 8,
Sort.by("id").descending()),
+ alertConvergeList.size()
+ );
+
+ when(alertConvergeService.getAlertConverges(any(),
any(PageRequest.class))).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"));
+ }
+
+ @Test
+ void testDeleteAlertDefines() throws Exception {
+
+
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));
+ }
+}
+
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]