Skip to content

Commit

Permalink
[#232] 유저 테스트 코드 리팩토링 (#249)
Browse files Browse the repository at this point in the history
* refactor: 코드 정리 (#232)

* refactor: 사용자 유효성 검증 추가 (#232)

- 게스트인 경우 UnauthorizedException 발생

* refactor: DTO 관련 네이밍 정리 (#232)

* test: 컨트롤러 테스트 리팩토링 (#232)

* test: 서비스 테스트 리팩토링 (#232)

* test: Data JPA 테스트 리팩토링 (#232)

* test: 불필요한 상수 삭제 (#232)

* test: 유저 이름 변경 (#232)

- loginUser/guestUser -> testUser로 통일

* test: 서비스 테스트 추가 (#232)

* test: 서비스 테스트 리팩토링 (#232)

* test: 통합 테스트 리팩토링 (#232)

* test: 인수 테스트 리팩토링 (#232)

* refactor: Builder 사용 (#232)

* refactor: Follow 객체에 책임 위임 (#232)

* test: 접근 제어자 수정 (#232)

* refactor: DTO 리팩토링 (#232)

* test: 불필요한 필드 삭제 (#232)

* test: WebEnvironment 옵션을 RANDOM_PORT -> NONE으로 변경 (#232)

* test: @SpringBootTest -> @DataJpaTest 변경 (#232)

* refactor: API 명세에 맞게 필드명 변경 (#232)

- image -> imageUrl 변경
  • Loading branch information
da-nyee committed Aug 4, 2021
1 parent 5e03e32 commit c13e32d
Show file tree
Hide file tree
Showing 21 changed files with 1,118 additions and 787 deletions.
@@ -1,13 +1,13 @@
package com.woowacourse.pickgit.user.application;

import com.woowacourse.pickgit.authentication.domain.user.AppUser;
import com.woowacourse.pickgit.user.application.dto.AuthUserServiceDto;
import com.woowacourse.pickgit.user.application.dto.FollowServiceDto;
import com.woowacourse.pickgit.user.application.dto.UserProfileServiceDto;
import com.woowacourse.pickgit.user.domain.User;
import com.woowacourse.pickgit.user.domain.UserRepository;
import com.woowacourse.pickgit.exception.user.InvalidUserException;
import com.woowacourse.pickgit.exception.user.SameSourceTargetUserException;
import com.woowacourse.pickgit.user.application.dto.request.AuthUserRequestDto;
import com.woowacourse.pickgit.user.application.dto.response.FollowResponseDto;
import com.woowacourse.pickgit.user.application.dto.response.UserProfileResponseDto;
import com.woowacourse.pickgit.user.domain.User;
import com.woowacourse.pickgit.user.domain.UserRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -22,60 +22,88 @@ public UserService(UserRepository userRepository) {
}

@Transactional(readOnly = true)
public UserProfileServiceDto getMyUserProfile(AuthUserServiceDto authUserServiceDto) {
User user = findUserByName(authUserServiceDto.getGithubName());

return new UserProfileServiceDto(
user.getName(), user.getImage(), user.getDescription(),
user.getFollowerCount(), user.getFollowingCount(), user.getPostCount(),
user.getGithubUrl(), user.getCompany(), user.getLocation(),
user.getWebsite(), user.getTwitter(), null
);
public UserProfileResponseDto getMyUserProfile(AuthUserRequestDto requestDto) {
User user = findUserByName(requestDto.getGithubName());

return UserProfileResponseDto.builder()
.name(user.getName())
.imageUrl(user.getImage())
.description(user.getDescription())
.followerCount(user.getFollowerCount())
.followingCount(user.getFollowingCount())
.postCount(user.getPostCount())
.githubUrl(user.getGithubUrl())
.company(user.getCompany())
.location(user.getLocation())
.website(user.getWebsite())
.twitter(user.getTwitter())
.following(false)
.build();
}

@Transactional(readOnly = true)
public UserProfileServiceDto getUserProfile(AppUser appUser, String targetUsername) {
User targetUser = findUserByName(targetUsername);

if (appUser.isGuest()) {
return new UserProfileServiceDto(
targetUser.getName(), targetUser.getImage(), targetUser.getDescription(),
targetUser.getFollowerCount(), targetUser.getFollowingCount(), targetUser.getPostCount(),
targetUser.getGithubUrl(), targetUser.getCompany(), targetUser.getLocation(),
targetUser.getWebsite(), targetUser.getTwitter(), null
);
public UserProfileResponseDto getUserProfile(AppUser user, String targetName) {
User target = findUserByName(targetName);

if (user.isGuest()) {
return UserProfileResponseDto.builder()
.name(target.getName())
.imageUrl(target.getImage())
.description(target.getDescription())
.followerCount(target.getFollowerCount())
.followingCount(target.getFollowingCount())
.postCount(target.getPostCount())
.githubUrl(target.getGithubUrl())
.company(target.getCompany())
.location(target.getLocation())
.website(target.getWebsite())
.twitter(target.getTwitter())
.following(null)
.build();
}

User sourceUser = findUserByName(appUser.getUsername());

return new UserProfileServiceDto(
targetUser.getName(), targetUser.getImage(), targetUser.getDescription(),
targetUser.getFollowerCount(), targetUser.getFollowingCount(), targetUser.getPostCount(),
targetUser.getGithubUrl(), targetUser.getCompany(), targetUser.getLocation(),
targetUser.getWebsite(), targetUser.getTwitter(), sourceUser.isFollowing(targetUser)
);
User source = findUserByName(user.getUsername());

return UserProfileResponseDto.builder()
.name(target.getName())
.imageUrl(target.getImage())
.description(target.getDescription())
.followerCount(target.getFollowerCount())
.followingCount(target.getFollowingCount())
.postCount(target.getPostCount())
.githubUrl(target.getGithubUrl())
.company(target.getCompany())
.location(target.getLocation())
.website(target.getWebsite())
.twitter(target.getTwitter())
.following(source.isFollowing(target))
.build();
}

public FollowServiceDto followUser(AuthUserServiceDto authUserServiceDto,
String targetUsername) {
User source = findUserByName(authUserServiceDto.getGithubName());
User target = findUserByName(targetUsername);
public FollowResponseDto followUser(AuthUserRequestDto requestDto, String targetName) {
User source = findUserByName(requestDto.getGithubName());
User target = findUserByName(targetName);

validateDifferentSourceTarget(source, target);
source.follow(target);

return new FollowServiceDto(target.getFollowerCount(), true);
return FollowResponseDto.builder()
.followerCount(target.getFollowerCount())
.isFollowing(true)
.build();
}

public FollowServiceDto unfollowUser(AuthUserServiceDto authUserServiceDto,
String targetUsername) {
User source = findUserByName(authUserServiceDto.getGithubName());
User target = findUserByName(targetUsername);
public FollowResponseDto unfollowUser(AuthUserRequestDto requestDto, String targetName) {
User source = findUserByName(requestDto.getGithubName());
User target = findUserByName(targetName);

validateDifferentSourceTarget(source, target);
source.unfollow(target);

return new FollowServiceDto(target.getFollowerCount(), false);
return FollowResponseDto.builder()
.followerCount(target.getFollowerCount())
.isFollowing(false)
.build();
}

private User findUserByName(String githubName) {
Expand All @@ -85,7 +113,7 @@ private User findUserByName(String githubName) {
}

private void validateDifferentSourceTarget(User source, User target) {
if (source.getId() == target.getId()) {
if (source.getId().equals(target.getId())) {
throw new SameSourceTargetUserException();
}
}
Expand Down

This file was deleted.

@@ -0,0 +1,20 @@
package com.woowacourse.pickgit.user.application.dto.request;

import lombok.Builder;

@Builder
public class AuthUserRequestDto {

private String githubName;

private AuthUserRequestDto() {
}

public AuthUserRequestDto(String githubName) {
this.githubName = githubName;
}

public String getGithubName() {
return githubName;
}
}
@@ -1,11 +1,17 @@
package com.woowacourse.pickgit.user.application.dto;
package com.woowacourse.pickgit.user.application.dto.response;

public class FollowServiceDto {
import lombok.Builder;

@Builder
public class FollowResponseDto {

private boolean isFollowing;
private int followerCount;
private boolean isFollowing;

private FollowResponseDto() {
}

public FollowServiceDto(int followerCount, boolean isFollowing) {
public FollowResponseDto(int followerCount, boolean isFollowing) {
this.followerCount = followerCount;
this.isFollowing = isFollowing;
}
Expand Down
@@ -1,31 +1,33 @@
package com.woowacourse.pickgit.user.application.dto;
package com.woowacourse.pickgit.user.application.dto.response;

import lombok.Builder;

@Builder
public class UserProfileServiceDto {

private final String name;
private final String image;
private final String description;

private final int followerCount;
private final int followingCount;
private final int postCount;

private final String githubUrl;
private final String company;
private final String location;
private final String website;
private final String twitter;

private final Boolean following;
public class UserProfileResponseDto {

private String name;
private String imageUrl;
private String description;
private int followerCount;
private int followingCount;
private int postCount;
private String githubUrl;
private String company;
private String location;
private String website;
private String twitter;
private Boolean following;

private UserProfileResponseDto() {
}

public UserProfileServiceDto(String name, String image, String description,
int followerCount, int followingCount, int postCount, String githubUrl, String company,
String location, String website, String twitter, Boolean following) {
public UserProfileResponseDto(
String name, String imageUrl, String description,
int followerCount, int followingCount, int postCount,
String githubUrl, String company, String location, String website, String twitter,
Boolean following) {
this.name = name;
this.image = image;
this.imageUrl = imageUrl;
this.description = description;
this.followerCount = followerCount;
this.followingCount = followingCount;
Expand All @@ -42,8 +44,8 @@ public String getName() {
return name;
}

public String getImage() {
return image;
public String getImageUrl() {
return imageUrl;
}

public String getDescription() {
Expand Down
Expand Up @@ -44,25 +44,19 @@ protected User() {
public User(
Long id,
BasicProfile basicProfile,
GithubProfile githubProfile
) {
GithubProfile githubProfile) {
this.id = id;
this.basicProfile = basicProfile;
this.githubProfile = githubProfile;
}

public User(
BasicProfile basicProfile,
GithubProfile githubProfile
) {
GithubProfile githubProfile) {
this.basicProfile = basicProfile;
this.githubProfile = githubProfile;
}

public void changeBasicProfile(BasicProfile basicProfile) {
this.basicProfile = basicProfile;
}

public void changeGithubProfile(GithubProfile githubProfile) {
this.githubProfile = githubProfile;
}
Expand All @@ -77,18 +71,22 @@ public void follow(User target) {
target.followers.add(follow);
}


public void unfollow(User target) {
Follow follow = new Follow(this, target);

if (!this.followings.existFollow(follow)) {
throw new InvalidFollowException();
}

this.followings.remove(follow);
target.followers.remove(follow);
}

public void addComment(Post post, Comment comment) {
comment.toPost(post)
.writeBy(this);
post.addComment(comment);
}

public Boolean isFollowing(User targetUser) {
return this.followings.isFollowing(targetUser);
}
Expand Down Expand Up @@ -149,12 +147,6 @@ public String getTwitter() {
return githubProfile.getTwitter();
}

public void addComment(Post post, Comment comment) {
comment.toPost(post)
.writeBy(this);
post.addComment(comment);
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Expand Up @@ -39,6 +39,10 @@ public Follow(User source, User target) {
this.target = target;
}

public boolean isFollowing(User targetUser) {
return this.target.equals(targetUser);
}

public Long getId() {
return id;
}
Expand Down
Expand Up @@ -10,7 +10,12 @@
@Embeddable
public class Followers {

@OneToMany(mappedBy = "target", fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, orphanRemoval = true)
@OneToMany(
mappedBy = "target",
fetch = FetchType.LAZY,
cascade = CascadeType.PERSIST,
orphanRemoval = true
)
private List<Follow> followers = new ArrayList<>();

public Followers() {
Expand Down
Expand Up @@ -11,7 +11,12 @@
@Embeddable
public class Followings {

@OneToMany(mappedBy = "source", fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, orphanRemoval = true)
@OneToMany(
mappedBy = "source",
fetch = FetchType.LAZY,
cascade = CascadeType.PERSIST,
orphanRemoval = true
)
private List<Follow> followings = new ArrayList<>();

public Followings() {
Expand All @@ -35,8 +40,6 @@ public void remove(Follow follow) {

public Boolean isFollowing(User targetUser) {
return followings.stream()
.filter(follow -> follow.getTarget().equals(targetUser))
.findAny().isPresent();
.anyMatch(follow -> follow.isFollowing(targetUser));
}
}

0 comments on commit c13e32d

Please sign in to comment.