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