|
| 1 | +package devkor.ontime_back.controller; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import devkor.ontime_back.ControllerTestSupport; |
| 5 | +import devkor.ontime_back.TestSecurityConfig; |
| 6 | +import devkor.ontime_back.dto.PreparationDto; |
| 7 | +import jakarta.servlet.http.HttpServletRequest; |
| 8 | +import org.junit.jupiter.api.DisplayName; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.springframework.context.annotation.Import; |
| 11 | +import org.springframework.http.MediaType; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | +import java.util.UUID; |
| 15 | + |
| 16 | +import static org.mockito.ArgumentMatchers.any; |
| 17 | +import static org.mockito.ArgumentMatchers.argThat; |
| 18 | +import static org.mockito.ArgumentMatchers.eq; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; |
| 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 | +@Import(TestSecurityConfig.class) |
| 26 | +public class PreparationScheduleControllerTest extends ControllerTestSupport { |
| 27 | + |
| 28 | + @Test |
| 29 | + @DisplayName("스케줄별 준비과정 생성에 성공한다.") |
| 30 | + void createPreparationSchedule_success() throws Exception { |
| 31 | + // given |
| 32 | + UUID scheduleId = UUID.randomUUID(); |
| 33 | + Long userId = 1L; |
| 34 | + |
| 35 | + List<PreparationDto> preparationDtoList = List.of( |
| 36 | + new PreparationDto(UUID.randomUUID(), "세면", 10, UUID.randomUUID()), |
| 37 | + new PreparationDto(UUID.randomUUID(), "옷입기", 15, null) |
| 38 | + ); |
| 39 | + |
| 40 | + when(userAuthService.getUserIdFromToken(any(HttpServletRequest.class))).thenReturn(userId); |
| 41 | + doNothing().when(preparationScheduleService).makePreparationSchedules(eq(userId), eq(scheduleId), any()); |
| 42 | + |
| 43 | + // when & then |
| 44 | + mockMvc.perform(post("/preparationschedule/create/{scheduleId}", scheduleId) |
| 45 | + .contentType(MediaType.APPLICATION_JSON) |
| 46 | + .content(new ObjectMapper().writeValueAsString(preparationDtoList)) |
| 47 | + .with(csrf())) |
| 48 | + .andExpect(status().isOk()) |
| 49 | + .andExpect(jsonPath("$.code").value("200")) |
| 50 | + .andExpect(jsonPath("$.status").value("success")) |
| 51 | + .andExpect(jsonPath("$.message").value("OK")); |
| 52 | + |
| 53 | + verify(userAuthService, times(1)).getUserIdFromToken(any(HttpServletRequest.class)); |
| 54 | + verify(preparationScheduleService, times(1)) |
| 55 | + .makePreparationSchedules(eq(userId), eq(scheduleId), argThat(list -> list.size() == preparationDtoList.size())); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + @DisplayName("스케줄별 준비과정 수정에 성공한다.") |
| 60 | + void modifyPreparationSchedule_success() throws Exception { |
| 61 | + // given |
| 62 | + UUID scheduleId = UUID.randomUUID(); |
| 63 | + Long userId = 1L; |
| 64 | + |
| 65 | + List<PreparationDto> preparationDtoList = List.of( |
| 66 | + new PreparationDto(UUID.randomUUID(), "세면", 10, UUID.randomUUID()), |
| 67 | + new PreparationDto(UUID.randomUUID(), "옷입기", 15, null) |
| 68 | + ); |
| 69 | + |
| 70 | + when(userAuthService.getUserIdFromToken(any(HttpServletRequest.class))).thenReturn(userId); |
| 71 | + doNothing().when(preparationScheduleService).updatePreparationSchedules(eq(userId), eq(scheduleId), any()); |
| 72 | + |
| 73 | + // when & then |
| 74 | + mockMvc.perform(post("/preparationschedule/modify/{scheduleId}", scheduleId) |
| 75 | + .contentType(MediaType.APPLICATION_JSON) |
| 76 | + .content(new ObjectMapper().writeValueAsString(preparationDtoList)) |
| 77 | + .with(csrf())) |
| 78 | + .andExpect(status().isOk()) |
| 79 | + .andExpect(jsonPath("$.code").value("200")) |
| 80 | + .andExpect(jsonPath("$.status").value("success")) |
| 81 | + .andExpect(jsonPath("$.message").value("OK")); |
| 82 | + |
| 83 | + verify(userAuthService, times(1)).getUserIdFromToken(any(HttpServletRequest.class)); |
| 84 | + verify(preparationScheduleService, times(1)) |
| 85 | + .updatePreparationSchedules(eq(userId), eq(scheduleId), argThat(list -> list.size() == preparationDtoList.size())); |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments