Skip to content

Commit

Permalink
[ISSUES alibaba#11499] add some unit tests for HealthController
Browse files Browse the repository at this point in the history
  • Loading branch information
onewe committed Dec 15, 2023
1 parent c74cc02 commit 6fee798
Showing 1 changed file with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,90 @@ public void testGetHealth() throws Exception {
Assert.assertEquals("UP", actualValue);

}

@Test
public void testGetHealthWhenTheLookUpIsNull() throws Exception {
when(dataSourceService.getHealth()).thenReturn("UP");
when(memberManager.getLookup()).thenReturn(null);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.HEALTH_CONTROLLER_PATH);
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
Assert.assertEquals("DOWN:address server down. ", actualValue);
}

@Test
public void testGetHealthWhenTheLoopUpNotUseAddressServer() throws Exception {
when(dataSourceService.getHealth()).thenReturn("UP");
when(memberManager.getLookup()).thenReturn(memberLookup);
when(memberLookup.useAddressServer()).thenReturn(false);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.HEALTH_CONTROLLER_PATH);
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
Assert.assertEquals("UP", actualValue);
}

@Test
public void testGetHealthWhenTheLoopUpInfoIsNull() throws Exception {
when(dataSourceService.getHealth()).thenReturn("UP");
when(memberManager.getLookup()).thenReturn(memberLookup);
when(memberLookup.useAddressServer()).thenReturn(true);
when(memberLookup.info()).thenReturn(null);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.HEALTH_CONTROLLER_PATH);
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
Assert.assertEquals("DOWN:address server down. ", actualValue);
}

@Test
public void testGetHealthWhenTheLoopUpInfoIsEmpty() throws Exception {
when(dataSourceService.getHealth()).thenReturn("UP");
when(memberManager.getLookup()).thenReturn(memberLookup);
when(memberLookup.useAddressServer()).thenReturn(true);
when(memberLookup.info()).thenReturn(new HashMap<>());
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.HEALTH_CONTROLLER_PATH);
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
Assert.assertEquals("DOWN:address server down. ", actualValue);
}

@Test
public void testGetHealthWhenTheLoopUpInfoIsDown() throws Exception {
when(dataSourceService.getHealth()).thenReturn("UP");
when(memberManager.getLookup()).thenReturn(memberLookup);
when(memberLookup.useAddressServer()).thenReturn(true);

final HashMap<String, Object> info = new HashMap<>();
info.put("addressServerHealth", "false");
when(memberLookup.info()).thenReturn(info);

MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.HEALTH_CONTROLLER_PATH);
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
Assert.assertEquals("DOWN:address server down. ", actualValue);
}

@Test
public void testGetHealthWhenTheLoopUpInfoIsUP() throws Exception {
when(dataSourceService.getHealth()).thenReturn("UP");
when(memberManager.getLookup()).thenReturn(memberLookup);
when(memberLookup.useAddressServer()).thenReturn(true);

final HashMap<String, Object> info = new HashMap<>();
info.put("addressServerHealth", "true");
when(memberLookup.info()).thenReturn(info);

MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.HEALTH_CONTROLLER_PATH);
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
Assert.assertEquals("UP", actualValue);
}

@Test
public void testGetHealthWhenTheLoopUpInfoParseError() throws Exception {
when(dataSourceService.getHealth()).thenReturn("UP");
when(memberManager.getLookup()).thenReturn(memberLookup);
when(memberLookup.useAddressServer()).thenReturn(true);

final HashMap<String, Object> info = new HashMap<>();
info.put("addressServerHealth", "not boolean value");
when(memberLookup.info()).thenReturn(info);

MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.HEALTH_CONTROLLER_PATH);
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
Assert.assertEquals("DOWN:address server down. ", actualValue);
}
}

0 comments on commit 6fee798

Please sign in to comment.