Skip to content

Commit

Permalink
[#54] test: User 인스턴스 following 테스트 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
kwj1270 committed Oct 3, 2021
1 parent 0694f5f commit 18cf842
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 22 deletions.
62 changes: 40 additions & 22 deletions src/main/java/com/study/realworld/domain/user/domain/User.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.study.realworld.domain.user.domain;

import com.study.realworld.domain.BaseTimeEntity;
import com.study.realworld.domain.follow.domain.Follow;
import com.study.realworld.domain.user.error.exception.PasswordMissMatchException;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.persistence.*;

import java.util.HashSet;
import java.util.Set;

import static java.util.Objects.isNull;

@Entity
Expand Down Expand Up @@ -43,6 +47,9 @@ public class User extends BaseTimeEntity {
@Column(name = "image"))
private Image image;

@OneToMany(mappedBy = "following")
private Set<Follow> followings = new HashSet<>();

protected User() {
}

Expand All @@ -65,6 +72,37 @@ public User encode(final PasswordEncoder passwordEncoder) {
return this;
}

public User changeEmail(final Email email) {
validateArgumentNull(email);
this.email = email;
return this;
}

public User changeBio(final Bio bio) {
validateArgumentNull(bio);
this.bio = bio;
return this;
}

public User changeImage(final Image image) {
validateArgumentNull(image);
this.image = image;
return this;
}

public User addfollowing(final Follow following) {
validateArgumentNull(following);
followings.add(following);
following.changeFollower(this);
return this;
}

private void validateArgumentNull(final Object argument) {
if(isNull(argument)) {
throw new IllegalArgumentException();
}
}

public Long id() {
return id;
}
Expand All @@ -89,28 +127,8 @@ public Password password() {
return password;
}

public User changeEmail(final Email email) {
validateArgumentNull(email);
this.email = email;
return this;
}

public User changeBio(final Bio bio) {
validateArgumentNull(bio);
this.bio = bio;
return this;
}

public User changeImage(final Image image) {
validateArgumentNull(image);
this.image = image;
return this;
}

private void validateArgumentNull(final Object argument) {
if(isNull(argument)) {
throw new IllegalArgumentException();
}
public Set<Follow> followings() {
return followings;
}

public static UserBuilder Builder() {
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/com/study/realworld/domain/user/domain/UserTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.study.realworld.domain.user.domain;

import com.study.realworld.domain.follow.domain.Follow;
import com.study.realworld.domain.user.error.exception.PasswordMissMatchException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.test.util.ReflectionTestUtils;

import static com.study.realworld.domain.follow.domain.FollowTest.followBuilder;
import static com.study.realworld.domain.user.domain.BioTest.BIO;
import static com.study.realworld.domain.user.domain.EmailTest.EMAIL;
import static com.study.realworld.domain.user.domain.ImageTest.IMAGE;
Expand Down Expand Up @@ -95,6 +97,21 @@ void argumentNull_test() {
);
}


@DisplayName("User 인스턴스 following 테스트")
@Test
void following_test() {
final User following = userBuilder(new Email(EMAIL), new Name(USERNAME), new Password(PASSWORD), new Bio(BIO), new Image(IMAGE));
final User follower = userBuilder(new Email("Email2@email.com"), new Name("differentUserName"), new Password("Password2"), new Bio("Bio2"), new Image("Image2"));
final Follow follow = followBuilder(following, follower);
follower.addfollowing(follow);

assertAll(
() -> assertThat(follower.followings().size()).isEqualTo(1),
() -> assertThat(follower.followings().contains(follow)).isTrue()
);
}

public static final User userBuilder(final Email email, final Name username,
final Password password, final Bio bio, final Image image) {
return User.Builder()
Expand Down

0 comments on commit 18cf842

Please sign in to comment.