|
| 1 | +package com.baeldung.hateoasvsswagger; |
| 2 | + |
| 3 | +import com.baeldung.hateoasvsswagger.model.NewUser; |
| 4 | +import com.baeldung.hateoasvsswagger.model.User; |
| 5 | +import com.baeldung.hateoasvsswagger.repository.UserRepository; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 10 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 11 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 12 | +import org.springframework.http.MediaType; |
| 13 | +import org.springframework.test.web.servlet.MockMvc; |
| 14 | + |
| 15 | +import java.time.LocalDateTime; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import static org.mockito.ArgumentMatchers.any; |
| 19 | +import static org.mockito.Mockito.when; |
| 20 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 21 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 22 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 23 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 24 | + |
| 25 | +@WebMvcTest(UserHateoasController.class) |
| 26 | +@AutoConfigureMockMvc |
| 27 | +class HateoasControllerIntegrationTest { |
| 28 | + |
| 29 | + @Autowired |
| 30 | + private MockMvc mockMvc; |
| 31 | + |
| 32 | + @MockBean |
| 33 | + private UserRepository userService; |
| 34 | + |
| 35 | + @Test |
| 36 | + void whenAllUsersRequested_thenReturnAllUsersWithLinks() throws Exception { |
| 37 | + User user1 = new User( 1, "John Doe", "[email protected]", LocalDateTime. now()); |
| 38 | + User user2 = new User( 2, "Jane Smith", "[email protected]", LocalDateTime. now()); |
| 39 | + |
| 40 | + when(userService.getAllUsers()).thenReturn(List.of(user1, user2)); |
| 41 | + |
| 42 | + mockMvc.perform(get("/api/hateoas/users").accept(MediaType.APPLICATION_JSON)) |
| 43 | + .andExpect(status().isOk()) |
| 44 | + .andExpect(jsonPath("$._embedded.userList[0].id").value(1)) |
| 45 | + .andExpect(jsonPath("$._embedded.userList[0].name").value("John Doe")) |
| 46 | + .andExpect(jsonPath("$._embedded.userList[0]._links.self.href").exists()) |
| 47 | + .andExpect(jsonPath("$._embedded.userList[1].id").value(2)) |
| 48 | + .andExpect(jsonPath("$._embedded.userList[1].name").value("Jane Smith")) |
| 49 | + .andExpect(jsonPath("$._links.self.href").exists()); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void whenUserByIdRequested_thenReturnUserByIdWithLinks() throws Exception { |
| 54 | + User user = new User( 1, "John Doe", "[email protected]", LocalDateTime. now()); |
| 55 | + |
| 56 | + when(userService.getUserById(1)).thenReturn(user); |
| 57 | + |
| 58 | + mockMvc.perform(get("/api/hateoas/users/1").accept(MediaType.APPLICATION_JSON)) |
| 59 | + .andExpect(status().isOk()) |
| 60 | + .andExpect(jsonPath("$.id").value(1)) |
| 61 | + .andExpect(jsonPath("$.name").value("John Doe")) |
| 62 | + . andExpect( jsonPath( "$.email"). value( "[email protected]")) |
| 63 | + .andExpect(jsonPath("$._links.self.href").exists()) |
| 64 | + .andExpect(jsonPath("$._links.all-users.href").exists()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void whenUserCreationRequested_thenReturnUserByIdWithLinks() throws Exception { |
| 69 | + User user = new User( 1, "John Doe", "[email protected]", LocalDateTime. now()); |
| 70 | + when(userService.createUser(any(NewUser.class))).thenReturn(user); |
| 71 | + |
| 72 | + mockMvc.perform(post("/api/hateoas/users").contentType(MediaType.APPLICATION_JSON) |
| 73 | + . content( "{\"name\":\"John Doe\",\"email\":\"[email protected]\"}")) |
| 74 | + .andExpect(status().isCreated()) |
| 75 | + .andExpect(jsonPath("$.id").value(1)) |
| 76 | + .andExpect(jsonPath("$.name").value("John Doe")) |
| 77 | + .andExpect(jsonPath("$._links.self.href").exists()); |
| 78 | + } |
| 79 | +} |
0 commit comments