Skip to content

Commit

Permalink
test: User 테스트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonise committed Apr 21, 2024
1 parent 22e196d commit 756789f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/main/java/site/youtogether/room/Room.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package site.youtogether.room;

import static site.youtogether.util.AppConstants.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.TimeToLive;

import com.redis.om.spring.annotations.Document;
import com.redis.om.spring.annotations.Indexed;
Expand Down Expand Up @@ -38,6 +41,9 @@ public class Room {
private String password;
private Map<Long, Participant> participants = new HashMap<>(10);

@TimeToLive
private final Long expirationTime = TIME_TO_LIVE;

@Builder
private Room(String code, String title, int capacity, String password, LocalDateTime createdAt, User host) {
this.code = code;
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/site/youtogether/user/User.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package site.youtogether.user;

import static site.youtogether.util.AppConstants.*;

import java.util.HashMap;
import java.util.Map;

import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.TimeToLive;

import com.redis.om.spring.annotations.Document;

Expand All @@ -29,6 +32,9 @@ public class User {
private String currentRoomCode;
private Map<String, Role> history = new HashMap<>();

@TimeToLive
private final Long expirationTime = TIME_TO_LIVE;

@Builder
private User(Long id, String nickname, String currentRoomCode) {
this.id = id;
Expand Down Expand Up @@ -58,6 +64,11 @@ public boolean hasLowerOrEqualRoleThan(Role compareRole) {
return role.isLowerOrEqualThan(compareRole);
}

private boolean hasLowerRoleThan(Role compareRole) {
Role role = getRoleInCurrentRoom();
return role.isLowerThan(compareRole);
}

public void changeNickname(String updateNickname) {
nickname = updateNickname;
}
Expand All @@ -79,7 +90,7 @@ public void changeOtherUserRole(String roomCode, User targetUser, Role newUserRo
throw new HigherOrEqualRoleUserChangeException();
}

if (hasLowerOrEqualRoleThan(newUserRole)) {
if (hasLowerRoleThan(newUserRole)) {
throw new HigherOrEqualRoleChangeException();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/site/youtogether/util/AppConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public final class AppConstants {
public static final String STOMP_ENDPOINT = "/stomp";
public static final String USER_ID = "userId";
public static final String ROOM_CODE = "roomCode";
public static final String BEARER = "Bearer ";
public static final Long TIME_TO_LIVE = 86400L;

}
58 changes: 58 additions & 0 deletions src/test/java/site/youtogether/user/UserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package site.youtogether.user;

import static org.assertj.core.api.Assertions.*;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import site.youtogether.exception.user.HigherOrEqualRoleUserChangeException;

class UserTest {

@Test
@DisplayName("자신보다 낮은 역할의 유저를 자신과 같은 역할까지 올릴 수 있다")
void changeRole() {
// given
String roomCode = "room code";

User host = User.builder()
.id(1L)
.currentRoomCode(roomCode)
.build();
host.getHistory().put(roomCode, Role.HOST);
User targetUser = User.builder()
.id(2L)
.currentRoomCode(roomCode)
.build();
targetUser.getHistory().put(roomCode, Role.VIEWER);

// when
host.changeOtherUserRole(roomCode, targetUser, Role.HOST);

// then
assertThat(targetUser.getRoleInCurrentRoom()).isEqualTo(Role.HOST);
}

@Test
@DisplayName("자신과 동일하거나 높은 유저의 역할은 변경할 수 없다")
void changeRoleFail() {
// given
String roomCode = "room code";

User manager = User.builder()
.id(1L)
.currentRoomCode(roomCode)
.build();
manager.getHistory().put(roomCode, Role.MANAGER);
User targetUser = User.builder()
.id(2L)
.currentRoomCode(roomCode)
.build();
targetUser.getHistory().put(roomCode, Role.MANAGER);

// when / then
assertThatThrownBy(() -> manager.changeOtherUserRole(roomCode, targetUser, Role.GUEST))
.isInstanceOf(HigherOrEqualRoleUserChangeException.class);
}

}

0 comments on commit 756789f

Please sign in to comment.