|
1 | 1 | package leetcode.api;
|
2 | 2 |
|
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.mockito.Mockito.when; |
| 6 | +import org.springframework.test.web.servlet.MvcResult; |
| 7 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 8 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 11 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 12 | +import org.springframework.test.web.servlet.MockMvc; |
| 13 | + |
| 14 | +@WebMvcTest(UserController.class) |
3 | 15 | public class UserControllerTests {
|
| 16 | + @Autowired |
| 17 | + private MockMvc mockMvc; |
| 18 | + |
| 19 | + @MockBean |
| 20 | + private StatsService service; |
| 21 | + |
| 22 | + |
| 23 | + @Test |
| 24 | + void validUsername() throws Exception { |
| 25 | + StatsResponse mockResponse = new StatsResponse("success", "retrieved", 1, 2,3, 4, 5, 6, 7, 8, (float) 99.99, 10, 11, 12); |
| 26 | + when(service.getStats("user_exists")).thenReturn(mockResponse); |
| 27 | + |
| 28 | + MvcResult result = mockMvc.perform(get("/user_exists")).andExpect(status().isOk()).andReturn(); |
| 29 | + String resultStr = result.getResponse().getContentAsString(); |
| 30 | + String expected = "{\"status\":\"success\",\"message\":\"retrieved\",\"totalSolved\":1,\"totalQuestions\":2,\"easySolved\":3,\"totalEasy\":4,\"mediumSolved\":5,\"totalMedium\":6,\"hardSolved\":7,\"totalHard\":8,\"acceptanceRate\":99.99,\"ranking\":10,\"contributionPoints\":11,\"reputation\":12}"; |
| 31 | + assertEquals(resultStr, expected); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + void noUsername() throws Exception { |
| 36 | + MvcResult result = mockMvc.perform(get("/")).andExpect(status().isOk()).andReturn(); |
| 37 | + String resultStr = result.getResponse().getContentAsString(); |
| 38 | + String expected = "{\"status\":\"error\",\"message\":\"please enter your username (ex: leetcode-stats-api.herokuapp.com/LeetCodeUsername)\",\"totalSolved\":0,\"totalQuestions\":0,\"easySolved\":0,\"totalEasy\":0,\"mediumSolved\":0,\"totalMedium\":0,\"hardSolved\":0,\"totalHard\":0,\"acceptanceRate\":0.0,\"ranking\":0,\"contributionPoints\":0,\"reputation\":0}"; |
| 39 | + assertEquals(resultStr, expected); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void nonValidUsername() throws Exception { |
| 44 | + StatsResponse mockResponse = new StatsResponse("error", "user does not exist", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 45 | + when(service.getStats("user_does_not_exist")).thenReturn(mockResponse); |
| 46 | + |
| 47 | + MvcResult result = mockMvc.perform(get("/user_does_not_exist")).andExpect(status().isOk()).andReturn(); |
| 48 | + String resultStr = result.getResponse().getContentAsString(); |
| 49 | + String expected = "{\"status\":\"error\",\"message\":\"user does not exist\",\"totalSolved\":0,\"totalQuestions\":0,\"easySolved\":0,\"totalEasy\":0,\"mediumSolved\":0,\"totalMedium\":0,\"hardSolved\":0,\"totalHard\":0,\"acceptanceRate\":0.0,\"ranking\":0,\"contributionPoints\":0,\"reputation\":0}"; |
| 50 | + assertEquals(resultStr, expected); |
| 51 | + } |
4 | 52 | }
|
0 commit comments