|
| 1 | +package com.somemore.center.controller; |
| 2 | + |
| 3 | +import com.somemore.ControllerTestSupport; |
| 4 | +import com.somemore.center.dto.response.CenterProfileResponseDto; |
| 5 | +import com.somemore.center.usecase.query.CenterQueryUseCase; |
| 6 | +import com.somemore.global.exception.BadRequestException; |
| 7 | +import org.junit.jupiter.api.BeforeEach; |
| 8 | +import org.junit.jupiter.api.DisplayName; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 11 | +import org.springframework.http.MediaType; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | +import java.util.UUID; |
| 15 | + |
| 16 | +import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_CENTER; |
| 17 | +import static org.mockito.Mockito.*; |
| 18 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 19 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 20 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 21 | +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; |
| 22 | + |
| 23 | +class CenterQueryApiControllerTest extends ControllerTestSupport { |
| 24 | + |
| 25 | + @MockBean |
| 26 | + protected CenterQueryUseCase centerQueryUseCase; |
| 27 | + |
| 28 | + private UUID centerId; |
| 29 | + private CenterProfileResponseDto responseDto; |
| 30 | + |
| 31 | + @BeforeEach |
| 32 | + void setUp() { |
| 33 | + centerId = UUID.randomUUID(); |
| 34 | + responseDto = CenterProfileResponseDto.builder() |
| 35 | + .centerId(centerId) |
| 36 | + .name("Test Center") |
| 37 | + .contactNumber("010-1234-5678") |
| 38 | + .imgUrl("http://example.com/image.jpg") |
| 39 | + .introduce("This is a test center.") |
| 40 | + .homepageLink("http://example.com") |
| 41 | + .preferItems(List.of()) |
| 42 | + .build(); |
| 43 | + } |
| 44 | + |
| 45 | + @DisplayName("기관 ID로 기관 프로필을 조회할 수 있다. (controller)") |
| 46 | + @Test |
| 47 | + void getCenterProfile() throws Exception { |
| 48 | + // given |
| 49 | + when(centerQueryUseCase.getCenterProfileByCenterId(centerId)).thenReturn(responseDto); |
| 50 | + |
| 51 | + // when // then |
| 52 | + mockMvc.perform( |
| 53 | + get("/api/center/profile/{centerId}", centerId) |
| 54 | + .contentType(MediaType.APPLICATION_JSON) |
| 55 | + ) |
| 56 | + .andDo(print()) |
| 57 | + .andExpect(status().isOk()) |
| 58 | + .andExpect(jsonPath("$.code").value("200")) |
| 59 | + .andExpect(jsonPath("$.message").value("기관 프로필 조회 성공")) |
| 60 | + .andExpect(jsonPath("$.data.center_id").value(centerId.toString())) // center_id로 수정 |
| 61 | + .andExpect(jsonPath("$.data.name").value("Test Center")) |
| 62 | + .andExpect(jsonPath("$.data.contact_number").value("010-1234-5678")) // contact_number로 수정 |
| 63 | + .andExpect(jsonPath("$.data.img_url").value("http://example.com/image.jpg")) // img_url로 수정 |
| 64 | + .andExpect(jsonPath("$.data.introduce").value("This is a test center.")) |
| 65 | + .andExpect(jsonPath("$.data.homepage_link").value("http://example.com")) // homepage_link로 수정 |
| 66 | + .andExpect(jsonPath("$.data.prefer_items").isArray()); // prefer_items로 수정 |
| 67 | + |
| 68 | + verify(centerQueryUseCase, times(1)).getCenterProfileByCenterId(centerId); |
| 69 | + } |
| 70 | + |
| 71 | + @DisplayName("존재하지 않는 기관 ID로 조회 시 예외가 발생한다. (controller)") |
| 72 | + @Test |
| 73 | + void getCenterProfile_NotFound() throws Exception { |
| 74 | + // given |
| 75 | + UUID nonExistentCenterId = UUID.randomUUID(); |
| 76 | + when(centerQueryUseCase.getCenterProfileByCenterId(nonExistentCenterId)) |
| 77 | + .thenThrow(new BadRequestException(NOT_EXISTS_CENTER.getMessage())); |
| 78 | + |
| 79 | + // when // then |
| 80 | + mockMvc.perform( |
| 81 | + get("/api/center/profile/{centerId}", nonExistentCenterId) |
| 82 | + .contentType(MediaType.APPLICATION_JSON) |
| 83 | + ) |
| 84 | + .andDo(print()) |
| 85 | + .andExpect(status().isBadRequest()) |
| 86 | + .andExpect(jsonPath("$.status").value("400")) |
| 87 | + .andExpect(jsonPath("$.detail").value("존재하지 않는 기관 ID 입니다.")); |
| 88 | + |
| 89 | + verify(centerQueryUseCase, times(1)).getCenterProfileByCenterId(nonExistentCenterId); |
| 90 | + } |
| 91 | + |
| 92 | +} |
| 93 | + |
0 commit comments