From 42448deef02503b2b4a3487d298886d68002ff39 Mon Sep 17 00:00:00 2001 From: jenny <88226911+kmjenny@users.noreply.github.com> Date: Mon, 20 May 2024 17:22:53 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[add]=20#27=20User=20=EB=B6=80=EB=B6=84=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/controller/RoomController.java | 2 +- .../server/service/room/RoomService.java | 8 +-- .../server/roomTest/RoomServiceTest.java | 57 +++++++++++++++++-- 3 files changed, 57 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/telepigeon/server/controller/RoomController.java b/src/main/java/com/telepigeon/server/controller/RoomController.java index 42a8128..909f583 100644 --- a/src/main/java/com/telepigeon/server/controller/RoomController.java +++ b/src/main/java/com/telepigeon/server/controller/RoomController.java @@ -25,7 +25,7 @@ public ResponseEntity createRoom( @UserId Long userId, @Valid @RequestBody RoomCreateDto roomCreateDto ) { - Room createdRoom = roomService.createRoom(roomCreateDto); + Room createdRoom = roomService.createRoom(roomCreateDto, userId); URI location = URI.create("/rooms/" + createdRoom.getId()); return ResponseEntity.created(location).build(); } diff --git a/src/main/java/com/telepigeon/server/service/room/RoomService.java b/src/main/java/com/telepigeon/server/service/room/RoomService.java index cca6fec..316846d 100644 --- a/src/main/java/com/telepigeon/server/service/room/RoomService.java +++ b/src/main/java/com/telepigeon/server/service/room/RoomService.java @@ -25,16 +25,16 @@ public class RoomService { private final ProfileSaver profileSaver; @Transactional - public Room createRoom(final RoomCreateDto roomCreateDto){ -// Users user = userRepository.findByIdOrThrow(userId); + public Room createRoom(final RoomCreateDto roomCreateDto, final Long userId){ + Users user = userRepository.findByIdOrThrow(userId); String code = createCode(); Room room = Room.create(roomCreateDto, code); Room savedRoom = roomSaver.save(room); -// Profile profile = Profile.create(user, savedRoom); -// Profile savedProfile = profileSaver.save(profile); + Profile profile = Profile.create(user, savedRoom); + Profile savedProfile = profileSaver.save(profile); return savedRoom; } diff --git a/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java b/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java index 6d69e31..345412d 100644 --- a/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java +++ b/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java @@ -1,29 +1,76 @@ package com.telepigeon.server.roomTest; +import com.telepigeon.server.domain.Profile; import com.telepigeon.server.domain.Room; +import com.telepigeon.server.domain.Users; import com.telepigeon.server.dto.room.request.RoomCreateDto; import com.telepigeon.server.repository.RoomRepository; +import com.telepigeon.server.repository.UserRepository; +import com.telepigeon.server.service.profile.ProfileSaver; import com.telepigeon.server.service.room.RoomRetriever; import com.telepigeon.server.service.room.RoomSaver; +import com.telepigeon.server.service.room.RoomService; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + + +@ExtendWith(MockitoExtension.class) public class RoomServiceTest { @Mock private RoomRepository roomRepository = Mockito.mock(RoomRepository.class); + @Mock + private UserRepository userRepository = Mockito.mock(UserRepository.class); + + @InjectMocks + private RoomService roomService; + + @Mock + private RoomSaver roomSaver; + + @Mock + private ProfileSaver profileSaver; + @Test @DisplayName("Room DB에 저장 확인") public void checkRoomInDB() { - RoomSaver roomSaver = new RoomSaver(roomRepository); - Room room = Room.create(new RoomCreateDto("name"), "code"); - Mockito.doAnswer(invocation -> room).when(roomRepository).save(room); - Room room1 = roomSaver.save(room); - Assertions.assertEquals(room.getName(), room1.getName()); + RoomCreateDto roomCreateDto = new RoomCreateDto("test"); + Long userId = 1L; + Room room = Room.create(roomCreateDto, "code"); + + Users user = Mockito.mock(Users.class); + when(userRepository.findByIdOrThrow(userId)).thenReturn(user); + when(roomRepository.existsByCode(any(String.class))).thenReturn(false); + when(roomSaver.save(any(Room.class))).thenReturn(room); + + Profile savedProfile = Profile.create(user, room); + when(profileSaver.save(any(Profile.class))).thenReturn(savedProfile); + + Room createdRoom = roomService.createRoom(roomCreateDto, userId); + + // 방이 올바르게 생성되었는지 확인 + Assertions.assertEquals(room.getName(), createdRoom.getName()); + + // 프로필이 올바르게 생성되었는지 확인 + Assertions.assertEquals(user, savedProfile.getUser()); + Assertions.assertEquals(room, savedProfile.getRoom()); + + // 정보 출력해 확인 + System.out.println("Room name : " + createdRoom.getName()); + System.out.println("Room code : " + createdRoom.getCode()); + System.out.println("Room created time : " + createdRoom.getCreatedAt()); + System.out.println("Profile room : " + savedProfile.getRoom()); + System.out.println("Profile user : " + savedProfile.getUser()); } @Test From 7557a8a2811874f31eb8b2214e2fd5836d00047e Mon Sep 17 00:00:00 2001 From: jenny <88226911+kmjenny@users.noreply.github.com> Date: Mon, 20 May 2024 17:36:49 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[add]=20#27=20ProfileRetriever=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/code/NotFoundErrorCode.java | 1 + .../server/repository/ProfileRepository.java | 5 ++++ .../service/profile/ProfileRetriever.java | 23 +++++++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java diff --git a/src/main/java/com/telepigeon/server/exception/code/NotFoundErrorCode.java b/src/main/java/com/telepigeon/server/exception/code/NotFoundErrorCode.java index ab0cd1d..e50417b 100644 --- a/src/main/java/com/telepigeon/server/exception/code/NotFoundErrorCode.java +++ b/src/main/java/com/telepigeon/server/exception/code/NotFoundErrorCode.java @@ -11,6 +11,7 @@ public enum NotFoundErrorCode implements DefaultErrorCode{ NOT_FOUND_END_POINT(HttpStatus.NOT_FOUND, "error", "존재하지 않는 API입니다."), ROOM_NOT_FOUND(HttpStatus.NOT_FOUND, "error", "존재하지 않는 방입니다."), USER_NOT_FOUND(HttpStatus.NOT_FOUND, "error", "존재하지 않는 사용자입니다."), + PROFILE_NOT_FOUND(HttpStatus.NOT_FOUND, "error", "존재하지 않는 프로필입니다."), ; @JsonIgnore diff --git a/src/main/java/com/telepigeon/server/repository/ProfileRepository.java b/src/main/java/com/telepigeon/server/repository/ProfileRepository.java index c9a41c8..d55d103 100644 --- a/src/main/java/com/telepigeon/server/repository/ProfileRepository.java +++ b/src/main/java/com/telepigeon/server/repository/ProfileRepository.java @@ -1,7 +1,12 @@ package com.telepigeon.server.repository; import com.telepigeon.server.domain.Profile; +import com.telepigeon.server.domain.Room; +import com.telepigeon.server.domain.Users; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.Optional; + public interface ProfileRepository extends JpaRepository { + Optional findByUserAndRoom(Users user, Room room); } diff --git a/src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java b/src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java new file mode 100644 index 0000000..bb7ef7d --- /dev/null +++ b/src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java @@ -0,0 +1,23 @@ +package com.telepigeon.server.service.profile; + +import com.telepigeon.server.domain.Profile; +import com.telepigeon.server.domain.Room; +import com.telepigeon.server.domain.Users; +import com.telepigeon.server.exception.NotFoundException; +import com.telepigeon.server.exception.code.NotFoundErrorCode; +import com.telepigeon.server.repository.ProfileRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class ProfileRetriever { + + private final ProfileRepository profileRepository; + + public Profile findByUserAndRoom(Users user, Room room) { + return profileRepository.findByUserAndRoom(user, room) + .orElseThrow(() -> new NotFoundException(NotFoundErrorCode.PROFILE_NOT_FOUND)); + } + +} From 2e26e66d711acc666e0dfcc1c427944c5ab4c613 Mon Sep 17 00:00:00 2001 From: jenny <88226911+kmjenny@users.noreply.github.com> Date: Mon, 20 May 2024 17:49:26 +0900 Subject: [PATCH 3/3] =?UTF-8?q?[add]=20#27=20ProfileRetriever=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/telepigeon/server/repository/ProfileRepository.java | 1 + .../telepigeon/server/service/profile/ProfileRetriever.java | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/src/main/java/com/telepigeon/server/repository/ProfileRepository.java b/src/main/java/com/telepigeon/server/repository/ProfileRepository.java index d55d103..7cf8066 100644 --- a/src/main/java/com/telepigeon/server/repository/ProfileRepository.java +++ b/src/main/java/com/telepigeon/server/repository/ProfileRepository.java @@ -9,4 +9,5 @@ public interface ProfileRepository extends JpaRepository { Optional findByUserAndRoom(Users user, Room room); + Optional findByUserNotAndRoom(Users user, Room room); } diff --git a/src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java b/src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java index bb7ef7d..835fc45 100644 --- a/src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java +++ b/src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java @@ -20,4 +20,9 @@ public Profile findByUserAndRoom(Users user, Room room) { .orElseThrow(() -> new NotFoundException(NotFoundErrorCode.PROFILE_NOT_FOUND)); } + public Profile findByUserNotAndRoom(Users user, Room room) { + return profileRepository.findByUserNotAndRoom(user, room) + .orElseThrow(() -> new NotFoundException(NotFoundErrorCode.PROFILE_NOT_FOUND)); + } + }