-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#52] feat: FollowingUser 일급컬랙션 분리 및 isFollow 테스트
- Loading branch information
1 parent
403a20f
commit 0a6a1e3
Showing
3 changed files
with
150 additions
and
30 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/main/java/com/study/realworld/user/domain/FollowingUsers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.study.realworld.user.domain; | ||
|
||
import com.study.realworld.global.exception.BusinessException; | ||
import com.study.realworld.global.exception.ErrorCode; | ||
import java.util.HashSet; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import javax.persistence.CascadeType; | ||
import javax.persistence.Embeddable; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.JoinTable; | ||
import javax.persistence.ManyToMany; | ||
|
||
@Embeddable | ||
public class FollowingUsers { | ||
|
||
@JoinTable(name = "follow", | ||
joinColumns = @JoinColumn(name = "user_id"), | ||
inverseJoinColumns = @JoinColumn(name = "follower_id")) | ||
@ManyToMany(cascade = CascadeType.ALL) | ||
private Set<User> followingUsers = new HashSet<>(); | ||
|
||
protected FollowingUsers() { | ||
} | ||
|
||
private FollowingUsers(Set<User> followingUsers) { | ||
this.followingUsers = followingUsers; | ||
} | ||
|
||
public static FollowingUsers of(Set<User> followingUsers) { | ||
return new FollowingUsers(followingUsers); | ||
} | ||
|
||
public boolean isFollow(User user) { | ||
return followingUsers.contains(user); | ||
} | ||
|
||
public void followingUser(User user) { | ||
checkFollowingUser(user); | ||
followingUsers.add(user); | ||
} | ||
|
||
private void checkFollowingUser(User user) { | ||
if (followingUsers.contains(user)) { | ||
throw new BusinessException(ErrorCode.INVALID_FOLLOW); | ||
} | ||
} | ||
|
||
public void unfollowingUser(User user) { | ||
checkUnfollowingUser(user); | ||
followingUsers.remove(user); | ||
} | ||
|
||
private void checkUnfollowingUser(User user) { | ||
if (!followingUsers.contains(user)) { | ||
throw new BusinessException(ErrorCode.INVALID_UNFOLLOW); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
FollowingUsers that = (FollowingUsers) o; | ||
return Objects.equals(followingUsers, that.followingUsers); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(followingUsers); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/test/java/com/study/realworld/user/domain/FollowingUsersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.study.realworld.user.domain; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class FollowingUsersTest { | ||
|
||
private User user; | ||
private User followingUser; | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
user = User.Builder() | ||
.profile(Username.of("username"), null, null) | ||
.password(Password.of("password")) | ||
.email(Email.of("[email protected]")) | ||
.build(); | ||
followingUser = User.Builder() | ||
.profile(Username.of("followingUser"), null, null) | ||
.password(Password.of("password")) | ||
.email(Email.of("[email protected]")) | ||
.build(); | ||
} | ||
|
||
@Nested | ||
@DisplayName("follow 여부를 확인할 수 있다.") | ||
class isFollowTest { | ||
|
||
@Test | ||
@DisplayName("follow된 상태일 때 true여야 한다.") | ||
void trueTest() { | ||
|
||
// given | ||
Set<User> userSet = new HashSet<>(); | ||
userSet.add(followingUser); | ||
FollowingUsers followingUsers = FollowingUsers.of(userSet); | ||
|
||
// when | ||
boolean result = followingUsers.isFollow(followingUser); | ||
|
||
// then | ||
assertTrue(result); | ||
} | ||
|
||
@Test | ||
@DisplayName("follow안된 상태일 때 false여야 한다.") | ||
void falseTest() { | ||
|
||
// given | ||
Set<User> userSet = new HashSet<>(); | ||
FollowingUsers followingUsers = FollowingUsers.of(userSet); | ||
|
||
// when | ||
boolean result = followingUsers.isFollow(followingUser); | ||
|
||
// then | ||
assertFalse(result); | ||
} | ||
} | ||
|
||
} |