From 776c43ec0c1bf1570b9530512d22e0193f1d3dd3 Mon Sep 17 00:00:00 2001 From: chaewss Date: Sat, 9 Sep 2023 15:40:32 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20userId=EB=A1=9C=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=EC=9E=90=20=EC=A0=95=EB=B3=B4=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?#101?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/docs/asciidoc/api-doc.adoc | 2 ++ .../java/com/cvsgo/controller/UserController.java | 6 +++--- src/main/java/com/cvsgo/service/UserService.java | 8 +++++--- .../com/cvsgo/controller/UserControllerTest.java | 7 +++++-- .../java/com/cvsgo/service/UserServiceTest.java | 15 ++++++++++++++- 5 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/docs/asciidoc/api-doc.adoc b/src/docs/asciidoc/api-doc.adoc index 2ae7173b..7af03a72 100644 --- a/src/docs/asciidoc/api-doc.adoc +++ b/src/docs/asciidoc/api-doc.adoc @@ -46,6 +46,8 @@ include::{snippets}/user-controller-test/respond_201_when_sign_up_succeed/respon include::{snippets}/user-controller-test/respond_201_when_sign_up_succeed/http-response.adoc[] === 1-5. 회원 정보 조회 +==== Path Parameters +include::{snippets}/user-controller-test/respond_200_when_read_user_successfully/path-parameters.adoc[] ==== Sample Request include::{snippets}/user-controller-test/respond_200_when_read_user_successfully/http-request.adoc[] ==== Response Fields diff --git a/src/main/java/com/cvsgo/controller/UserController.java b/src/main/java/com/cvsgo/controller/UserController.java index 4de54399..71a98bc4 100644 --- a/src/main/java/com/cvsgo/controller/UserController.java +++ b/src/main/java/com/cvsgo/controller/UserController.java @@ -52,9 +52,9 @@ public SuccessResponse checkNicknameExists(@PathVariable String nicknam return SuccessResponse.from(userService.isDuplicatedNickname(nickname)); } - @GetMapping("/user") - public SuccessResponse readUser(@LoginUser User user) { - return SuccessResponse.from(userService.readUser(user)); + @GetMapping("/users/{userId}") + public SuccessResponse readUser(@PathVariable Long userId) { + return SuccessResponse.from(userService.readUser(userId)); } @PutMapping("/user") diff --git a/src/main/java/com/cvsgo/service/UserService.java b/src/main/java/com/cvsgo/service/UserService.java index 419df19e..d51e6d8b 100644 --- a/src/main/java/com/cvsgo/service/UserService.java +++ b/src/main/java/com/cvsgo/service/UserService.java @@ -102,11 +102,13 @@ public boolean isDuplicatedNickname(String nickname) { /** * 사용자 정보를 조회한다. * - * @param user 로그인한 사용자 - * @return 로그인 한 사용자 상세 정보 + * @param userId 조회할 사용자 ID + * @return 사용자 상세 정보 */ @Transactional(readOnly = true) - public UserResponseDto readUser(User user) { + public UserResponseDto readUser(Long userId) { + User user = userRepository.findById(userId).orElseThrow(() -> NOT_FOUND_USER); + long reviewLikeCount = reviewRepository.findAllByUser(user) .stream().mapToLong(Review::getLikeCount).sum(); diff --git a/src/test/java/com/cvsgo/controller/UserControllerTest.java b/src/test/java/com/cvsgo/controller/UserControllerTest.java index 06981905..193a69b8 100644 --- a/src/test/java/com/cvsgo/controller/UserControllerTest.java +++ b/src/test/java/com/cvsgo/controller/UserControllerTest.java @@ -323,17 +323,20 @@ void respond_false_when_email_exists() throws Exception { } @Test - @DisplayName("로그인한 사용자 정보를 정상적으로 조회하면 HTTP 200을 응답한다") + @DisplayName("사용자 정보를 정상적으로 조회하면 HTTP 200을 응답한다") void respond_200_when_read_user_successfully() throws Exception { given(userService.readUser(any())).willReturn(getUser()); - mockMvc.perform(get("/api/user") + mockMvc.perform(get("/api/users/{userId}", 1L) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andDo(print()) .andDo(document(documentIdentifier, getDocumentRequest(), getDocumentResponse(), + pathParameters( + parameterWithName("userId").description("조회할 회원 ID") + ), relaxedResponseFields( fieldWithPath("data.id").type(JsonFieldType.NUMBER).description("회원 ID").optional(), fieldWithPath("data.email").type(JsonFieldType.STRING).description("이메일"), diff --git a/src/test/java/com/cvsgo/service/UserServiceTest.java b/src/test/java/com/cvsgo/service/UserServiceTest.java index d65c50ba..4f6768cb 100644 --- a/src/test/java/com/cvsgo/service/UserServiceTest.java +++ b/src/test/java/com/cvsgo/service/UserServiceTest.java @@ -168,13 +168,26 @@ void should_return_false_when_nickname_does_not_exist() { @Test @DisplayName("사용자 정보를 조회한다") void succeed_to_read_user() { + given(userRepository.findById(anyLong())).willReturn(Optional.of(user)); given(reviewRepository.findAllByUser(any())).willReturn(List.of(review1)); - userService.readUser(user); + userService.readUser(user.getId()); + then(userRepository).should(times(1)).findById(user.getId()); then(reviewRepository).should(times(1)).findAllByUser(any()); } + @Test + @DisplayName("사용자 정보 조회 시 해당하는 아이디를 가진 사용자가 없으면 NotFoundException이 발생한다") + void should_throw_NotFoundException_when_read_user_but_user_does_not_exist() { + final Long userId = 10000L; + + given(userRepository.findById(anyLong())).willReturn(Optional.empty()); + + assertThrows(NotFoundException.class, () -> userService.readUser(userId)); + then(userRepository).should(times(1)).findById(anyLong()); + } + @Test @DisplayName("회원을 정상적으로 수정한다") void succeed_to_update_user() {