Skip to content

Commit

Permalink
[Improve] add AlertConvergesController & AlertConvergeController unit…
Browse files Browse the repository at this point in the history
… test (#2424)

Signed-off-by: yuluo-yx <yuluo08290126@gmail.com>
Co-authored-by: tomsun28 <tomsun28@outlook.com>
  • Loading branch information
yuluo-yx and tomsun28 authored Aug 1, 2024
1 parent 3a7d582 commit 3d99835
Show file tree
Hide file tree
Showing 2 changed files with 247 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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."));
}

}
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit 3d99835

Please sign in to comment.