From 199cfcd348c56ba0a5ad6315cc42ba302d1b1786 Mon Sep 17 00:00:00 2001 From: zhangjidi2016 <1017543663@qq.com> Date: Fri, 30 Jul 2021 19:51:10 +0800 Subject: [PATCH] [ISSUE #673] Add unit tests for OpsController. (#752) --- .../console/controller/OpsControllerTest.java | 105 ++++++++++++++---- 1 file changed, 84 insertions(+), 21 deletions(-) diff --git a/src/test/java/org/apache/rocketmq/console/controller/OpsControllerTest.java b/src/test/java/org/apache/rocketmq/console/controller/OpsControllerTest.java index fba9d2f..2d6fec9 100644 --- a/src/test/java/org/apache/rocketmq/console/controller/OpsControllerTest.java +++ b/src/test/java/org/apache/rocketmq/console/controller/OpsControllerTest.java @@ -17,41 +17,104 @@ package org.apache.rocketmq.console.controller; -import org.junit.After; +import java.util.ArrayList; +import java.util.List; +import org.apache.rocketmq.console.service.checker.RocketMqChecker; +import org.apache.rocketmq.console.service.checker.impl.ClusterHealthCheckerImpl; +import org.apache.rocketmq.console.service.checker.impl.TopicOnlyOneBrokerCheckerImpl; +import org.apache.rocketmq.console.service.impl.OpsServiceImpl; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.junit4.SpringRunner; +import org.mockito.InjectMocks; +import org.mockito.Spy; +import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import static org.hamcrest.Matchers.hasSize; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doNothing; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +public class OpsControllerTest extends BaseControllerTest { + @InjectMocks + private OpsController opsController; + + private MockHttpServletRequestBuilder requestBuilder = null; + + private ResultActions perform; + + @Spy + private OpsServiceImpl opsService; -@RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@DirtiesContext -public class OpsControllerTest { @Before - public void setUp() throws Exception { - - } - - @After - public void tearDown() throws Exception { - + public void init() { + super.mockRmqConfigure(); } @Test - public void homePage() throws Exception { - + public void testHomePage() throws Exception { + final String url = "/ops/homePage.query"; + requestBuilder = MockMvcRequestBuilders.get(url); + perform = mockMvc.perform(requestBuilder); + perform.andExpect(status().isOk()) + .andExpect(jsonPath("$.data").isMap()) + .andExpect(jsonPath("$.data.useVIPChannel").value(false)) + .andExpect(jsonPath("$.data.namesvrAddrList").isArray()) + .andExpect(jsonPath("$.data.namesvrAddrList", hasSize(1))) + .andExpect(jsonPath("$.data.namesvrAddrList[0]").value("127.0.0.1:9876")); } @Test - public void updateNameSvrAddr() throws Exception { - + public void testUpdateNameSvrAddr() throws Exception { + final String url = "/ops/updateNameSvrAddr.do"; + { + doNothing().when(rMQConfigure).setNamesrvAddr(anyString()); + } + requestBuilder = MockMvcRequestBuilders.post(url); + requestBuilder.param("nameSvrAddrList", "127.0.0.1:9876"); + perform = mockMvc.perform(requestBuilder); + perform.andExpect(status().isOk()) + .andExpect(jsonPath("$.data").value(true)); + Assert.assertEquals(rMQConfigure.getNamesrvAddr(), "127.0.0.1:9876"); } @Test - public void clusterStatus() throws Exception { + public void testUpdateIsVIPChannel() throws Exception { + final String url = "/ops/updateIsVIPChannel.do"; + { + doNothing().when(rMQConfigure).setIsVIPChannel(anyString()); + } + requestBuilder = MockMvcRequestBuilders.post(url); + requestBuilder.param("useVIPChannel", "true"); + perform = mockMvc.perform(requestBuilder); + perform.andExpect(status().isOk()) + .andExpect(jsonPath("$.data").value(true)); + } + @Test + public void testClusterStatus() throws Exception { + final String url = "/ops/rocketMqStatus.query"; + { + List rocketMqCheckerList = new ArrayList<>(); + rocketMqCheckerList.add(new ClusterHealthCheckerImpl()); + rocketMqCheckerList.add(new TopicOnlyOneBrokerCheckerImpl()); + ReflectionTestUtils.setField(opsService, "rocketMqCheckerList", rocketMqCheckerList); + } + requestBuilder = MockMvcRequestBuilders.get(url); + perform = mockMvc.perform(requestBuilder); + perform.andExpect(status().isOk()) + .andExpect(jsonPath("$.data").isMap()) + .andExpect(jsonPath("$.data.CLUSTER_HEALTH_CHECK").isEmpty()) + .andExpect(jsonPath("$.data.TOPIC_ONLY_ONE_BROKER_CHECK").isEmpty()); + } + + @Override + protected Object getTestController() { + return opsController; } } \ No newline at end of file