Skip to content

Commit

Permalink
[#52] feat: FollowingUser 일급컬랙션 분리 및 isFollow 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
jinyoungchoi95 committed Oct 12, 2021
1 parent 403a20f commit 0a6a1e3
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 30 deletions.
77 changes: 77 additions & 0 deletions src/main/java/com/study/realworld/user/domain/FollowingUsers.java
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);
}

}
35 changes: 5 additions & 30 deletions src/main/java/com/study/realworld/user/domain/User.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
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.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import org.springframework.security.crypto.password.PasswordEncoder;

Expand All @@ -34,11 +26,8 @@ public class User {
@Embedded
private Password password;

@JoinTable(name = "follow",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "follower_id"))
@ManyToMany(cascade = CascadeType.ALL)
private Set<User> followingUsers = new HashSet<>();
@Embedded
private FollowingUsers followingUsers = new FollowingUsers();

protected User() {
}
Expand Down Expand Up @@ -107,29 +96,15 @@ public Profile profile() {
}

public boolean isFollow(User user) {
return followingUsers.contains(user);
return followingUsers.isFollow(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);
}
followingUsers.followingUser(user);
}

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);
}
followingUsers.unfollowingUser(user);
}

@Override
Expand Down
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);
}
}

}

0 comments on commit 0a6a1e3

Please sign in to comment.