Skip to content

Commit

Permalink
[#54] temp: vo 객체 기본 생성자 필요성 테스트
Browse files Browse the repository at this point in the history
kwj1270 committed Oct 11, 2021
1 parent cd498fa commit d7acfa9
Showing 6 changed files with 91 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,55 @@
package com.study.realworld.domain.follow.application;

import com.study.realworld.domain.follow.domain.Follow;
import com.study.realworld.domain.follow.domain.FollowQueryDslRepository;
import com.study.realworld.domain.follow.domain.FollowRepository;
import com.study.realworld.domain.follow.dto.FollowableDto;
import com.study.realworld.domain.user.domain.persist.User;
import com.study.realworld.domain.user.domain.persist.UserRepository;
import com.study.realworld.domain.user.error.exception.IdentityNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional(readOnly = true)
@Service
public class FollowService {

private final UserRepository userRepository;
private final FollowRepository followRepository;
private final FollowQueryDslRepository followQueryDslRepository;

public FollowService(final FollowRepository followRepository, final FollowQueryDslRepository followQueryDslRepository) {
public FollowService(final UserRepository userRepository, final FollowRepository followRepository, final FollowQueryDslRepository followQueryDslRepository) {
this.userRepository = userRepository;
this.followRepository = followRepository;
this.followQueryDslRepository = followQueryDslRepository;
}

public Follow following(final Long myId, final Long targetId) {
final User me = findUserById(myId);
final User target = findUserById(targetId);
final Follow follow = follow(target, me);
me.addFollowing(follow);
return followRepository.save(follow);
}

public User unfollowing(final Long myId, final Long targetId) {
final User me = findUserById(myId);
final User target = findUserById(targetId);
final Follow follow = followRepository.findByFollowingAndFollower(target, me).orElseThrow(IllegalArgumentException::new);
followRepository.delete(follow);
return me;
}

private Follow follow(final User following, final User follower) {
return Follow.Builder()
.following(following)
.follower(follower)
.build();
}

private User findUserById(final Long id) {
return userRepository.findById(id).orElseThrow(() -> new IdentityNotFoundException(id));
}


}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.study.realworld.domain.follow.domain;

import com.study.realworld.domain.user.domain.persist.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface FollowRepository extends JpaRepository<Follow, Long> {
Optional<Follow> findByFollowingAndFollower(User following, User follower);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.study.realworld.domain.follow.domain;

import javax.persistence.CascadeType;
import javax.persistence.Embeddable;
import javax.persistence.OneToMany;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

@Embeddable
public class Followings {

@OneToMany(mappedBy = "following", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Follow> followings = new HashSet<>();

public Followings() {
}

public Followings(final Set<Follow> followings) {
this.followings = followings;
}

public void add(final Follow following) {
validateArgumentNull(following);
followings.add(following);
}

private void validateArgumentNull(final Follow following) {
if(Objects.isNull(following)) {
throw new IllegalArgumentException();
}
}

public Set<Follow> getFollowings() {
return followings;
}

}
Original file line number Diff line number Diff line change
@@ -2,13 +2,13 @@

import com.study.realworld.domain.BaseTimeEntity;
import com.study.realworld.domain.follow.domain.Follow;
import com.study.realworld.domain.follow.domain.Followings;
import com.study.realworld.domain.user.domain.vo.*;
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;
@@ -48,8 +48,8 @@ public class User extends BaseTimeEntity {
@Column(name = "image"))
private Image image;

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

protected User() {
}
@@ -91,8 +91,7 @@ public User changeImage(final Image image) {
return this;
}

public User addfollowing(final Follow following) {
validateArgumentNull(following);
public User addFollowing(final Follow following) {
followings.add(following);
following.changeFollower(this);
return this;
@@ -128,7 +127,7 @@ public Password password() {
return password;
}

public Set<Follow> followings() {
public Followings followings() {
return followings;
}

Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

import com.study.realworld.domain.follow.domain.FollowQueryDslRepository;
import com.study.realworld.domain.follow.domain.FollowRepository;
import com.study.realworld.domain.user.domain.persist.UserRepository;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -14,13 +15,14 @@
@ExtendWith(MockitoExtension.class)
class FollowServiceTest {

@Mock private UserRepository userRepository;
@Mock private FollowRepository followRepository;
@Mock private FollowQueryDslRepository followQueryDslRepository;

@DisplayName("FollowService 인스턴스 생성자 테스트")
@Test
void constructor_test() {
final FollowService followService = new FollowService(followRepository, followQueryDslRepository);
final FollowService followService = new FollowService(userRepository, followRepository, followQueryDslRepository);

assertAll(
() -> assertThat(followService).isNotNull(),
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.study.realworld.domain.user.domain.persist;

import com.study.realworld.domain.follow.domain.Follow;
import com.study.realworld.domain.user.domain.persist.User;
import com.study.realworld.domain.user.domain.vo.*;
import com.study.realworld.domain.user.error.exception.PasswordMissMatchException;
import org.junit.jupiter.api.DisplayName;
@@ -106,11 +105,11 @@ 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);
follower.addFollowing(follow);

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

0 comments on commit d7acfa9

Please sign in to comment.