Skip to content

Commit

Permalink
[add] #27 User 부분 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
kmjenny committed May 20, 2024
1 parent f064969 commit 42448de
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ResponseEntity<Void> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
57 changes: 52 additions & 5 deletions src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 42448de

Please sign in to comment.