Skip to content

Commit

Permalink
[#54] test: Follow, Article 부족한 단위 테스트 및 인수 테스트 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
kwj1270 committed Nov 26, 2021
1 parent 72ad48e commit 91b57d5
Show file tree
Hide file tree
Showing 16 changed files with 213 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ResponseEntity<ProfileResponse> profile(@AuthenticationPrincipal final Lo
return ResponseEntity.ok().body(profile);
}

@GetMapping("/profiles2/{username}")
@GetMapping("/profiles/querydsl/{username}")
public ResponseEntity<ProfileResponse> profile2(@AuthenticationPrincipal final Long userId,
@PathVariable final String username) {
final ProfileResponse profile = followQueryService.profile2(userId, UserName.from(username));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Follow {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, updatable = false)
private Long id;
private Long followId;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "followee", nullable = false, foreignKey = @ForeignKey(name = "fk_follow_to_followee"))
Expand All @@ -31,8 +31,8 @@ public Follow(final User followee, final User follower) {
this.follower = follower;
}

public Long id() {
return id;
public Long followId() {
return followId;
}

public User followee() {
Expand All @@ -48,11 +48,11 @@ public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Follow follow = (Follow) o;
return Objects.equals(id(), follow.id());
return Objects.equals(followId(), follow.followId());
}

@Override
public int hashCode() {
return Objects.hash(id());
return Objects.hash(followId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@JsonTypeName("profile")
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
public class FollowResponse {
public final class FollowResponse {

@JsonProperty("username")
private UserName userName;
Expand All @@ -30,27 +30,27 @@ public class FollowResponse {
private Boolean followable;


public static FollowResponse from(final User user) {
public static final FollowResponse from(final User user) {
return of(user, true);
}

private static FollowResponse of(final User user, final boolean followable) {
private static final FollowResponse of(final User user, final boolean followable) {
return new FollowResponse(user.userName(), user.userBio(), user.userImage(), followable);
}

public UserName userName() {
public final UserName userName() {
return userName;
}

public UserBio userBio() {
public final UserBio userBio() {
return userBio;
}

public UserImage userImage() {
public final UserImage userImage() {
return userImage;
}

public Boolean isFollowing() {
public final Boolean isFollowing() {
return followable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@JsonTypeName("profile")
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
public class UnFollowResponse {
public final class UnFollowResponse {

@JsonProperty("username")
private UserName userName;
Expand All @@ -29,28 +29,27 @@ public class UnFollowResponse {
@JsonProperty("following")
private Boolean followable;

public static UnFollowResponse from(final User user) {
public static final UnFollowResponse from(final User user) {
return of(user, false);
}

private static UnFollowResponse of(final User user, final boolean followable) {
private static final UnFollowResponse of(final User user, final boolean followable) {
return new UnFollowResponse(user.userName(), user.userBio(), user.userImage(), followable);
}

public UserName userName() {
public final UserName userName() {
return userName;
}

public UserBio userBio() {
public final UserBio userBio() {
return userBio;
}

public UserImage userImage() {
public final UserImage userImage() {
return userImage;
}

public Boolean isFollowing() {
public final Boolean isFollowing() {
return followable;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

public enum FollowErrorCode implements ErrorCode {

FOLLOW_NOT_FOUND(FollowNotFoundException.class, HttpStatus.BAD_REQUEST, "Follow is not exist"),
DUPLICATED_FOLLOW(DuplicatedFollowException.class, HttpStatus.BAD_REQUEST, "Follow is already exist");
DUPLICATED_FOLLOW(DuplicatedFollowException.class, HttpStatus.BAD_REQUEST, "Follow is already exist"),
FOLLOW_NOT_FOUND(FollowNotFoundException.class, HttpStatus.BAD_REQUEST, "Follow is not exist");

private final Class exceptionClass;
private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonValue;
import com.study.realworld.domain.user.error.exception.PasswordMissMatchException;
import com.study.realworld.domain.user.error.exception.PasswordNullOrEmptyException;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
Expand All @@ -27,8 +28,8 @@ public static UserPassword encode(final String rawPassword, final PasswordEncode
}

private static void validateNullOrBlank(final String rawPassword) {
if (rawPassword.isBlank()) {
throw new IllegalArgumentException();
if (Objects.isNull(rawPassword) || rawPassword.isBlank()) {
throw new PasswordNullOrEmptyException();
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/study/realworld/domain/user/dto/UserInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@JsonTypeName("user")
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
public class UserInfo {
public final class UserInfo {

@JsonProperty("username")
private UserName userName;
Expand All @@ -42,23 +42,23 @@ public static final UserInfo fromUserWithToken(final User user, final AccessToke
return new UserInfo(userName, userEmail, userBio, userImage, accessToken);
}

public UserName userName() {
public final UserName userName() {
return userName;
}

public UserEmail userEmail() {
public final UserEmail userEmail() {
return userEmail;
}

public UserBio userBio() {
public final UserBio userBio() {
return userBio;
}

public UserImage userImage() {
public final UserImage userImage() {
return userImage;
}

public AccessToken accessToken() {
public final AccessToken accessToken() {
return accessToken;
}
}
10 changes: 5 additions & 5 deletions src/main/java/com/study/realworld/domain/user/dto/UserJoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,23 @@ public static final Response fromUserWithToken(final User user, final AccessToke
return new Response(userName, userEmail, userBio, userImage, accessToken);
}

public UserEmail userEmail() {
public final UserEmail userEmail() {
return userEmail;
}

public UserName userName() {
public final UserName userName() {
return userName;
}

public UserBio userBio() {
public final UserBio userBio() {
return userBio;
}

public UserImage userImage() {
public final UserImage userImage() {
return userImage;
}

public AccessToken accessToken() {
public final AccessToken accessToken() {
return accessToken;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public static final class Request {
@JsonProperty("image")
private UserImage userImage;

@Builder
public Request(final UserEmail userEmail, final UserBio userBio, final UserImage userImage) {
this.userEmail = userEmail;
this.userBio = userBio;
this.userImage = userImage;
}

public final UserEmail memberEmail() {
return userEmail;
}
Expand All @@ -42,13 +49,6 @@ public final UserBio memberBio() {
public final UserImage memberImage() {
return userImage;
}

@Builder
public Request(final UserEmail userEmail, final UserBio userBio, final UserImage userImage) {
this.userEmail = userEmail;
this.userBio = userBio;
this.userImage = userImage;
}
}

@AllArgsConstructor(access = AccessLevel.PRIVATE)
Expand Down Expand Up @@ -95,7 +95,6 @@ public UserBio userBio() {
public UserImage userImage() {
return userImage;
}

}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.study.realworld.domain.user.error.exception;

public class PasswordNullOrEmptyException extends UserBusinessException {

private static final String MESSAGE = "올바르지 않은 패스워드입니다.";

public PasswordNullOrEmptyException() {
super(MESSAGE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.study.realworld.domain.follow.dto.FollowResponse;
import com.study.realworld.domain.follow.dto.ProfileResponse;
import com.study.realworld.domain.follow.dto.UnFollowResponse;
import com.study.realworld.domain.follow.error.FollowErrorResponse;
import com.study.realworld.domain.user.dto.UserJoin;
import com.study.realworld.global.common.AccessToken;
import io.restassured.RestAssured;
Expand Down Expand Up @@ -53,6 +54,23 @@ public void setUp() {
);
}

@Test
void 특정_사람의_프로필을_조회한다_QueryDsl() {
final Login.Response loginResponse = 로그인_되어있음(user1.userEmail().userEmail());
final ExtractableResponse<Response> response = 프로필_조회_요청_QueryDsl(
loginResponse.accessToken(),
user2.userName().userName());
final ProfileResponse profileResponse = response.as(ProfileResponse.class);

assertAll(
() -> assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()),
() -> assertThat(profileResponse.userName()).isEqualTo(user2.userName()),
() -> assertThat(profileResponse.userBio()).isEqualTo(user2.userBio()),
() -> assertThat(profileResponse.userImage()).isEqualTo(user2.userImage()),
() -> assertThat(profileResponse.isFollowing()).isFalse()
);
}

@Test
void 특정_사람을_팔로우한다() {
final Login.Response loginResponse = 로그인_되어있음(user1.userEmail().userEmail());
Expand All @@ -71,6 +89,18 @@ public void setUp() {
);
}

@Test
void 특정_사람을_팔로우했는데_한번_더_팔로우하면_예외를_발생시킨다() {
final Login.Response loginResponse = 로그인_되어있음(user1.userEmail().userEmail());
팔로우_요청(loginResponse.accessToken(), user2.userName().userName());
final ExtractableResponse<Response> response = 팔로우_요청(
loginResponse.accessToken(),
user2.userName().userName());

final FollowErrorResponse followErrorResponse = response.as(FollowErrorResponse.class);
assertThat(followErrorResponse.body().get(0)).isEqualTo("Follow is already exist");
}

@Test
void 특정_사람을_언팔로우한다() {
final Login.Response loginResponse = 로그인_되어있음(user1.userEmail().userEmail());
Expand All @@ -90,6 +120,18 @@ public void setUp() {
);
}

@Test
void 특정_사람을_언팔로우했는데_한번_더_언팔로우하면_예외를_발생시킨다() {
final Login.Response loginResponse = 로그인_되어있음(user1.userEmail().userEmail());
언팔로우_요청(loginResponse.accessToken(), user2.userName().userName());
final ExtractableResponse<Response> response = 언팔로우_요청(
loginResponse.accessToken(),
user2.userName().userName());

final FollowErrorResponse followErrorResponse = response.as(FollowErrorResponse.class);
assertThat(followErrorResponse.body().get(0)).isEqualTo("Follow is not exist");
}

protected ExtractableResponse<Response> 프로필_조회_요청(final AccessToken accessToken, final String userName) {
return RestAssured.given()
.header(AUTHORIZATION, BEARER + accessToken.accessToken())
Expand All @@ -99,6 +141,15 @@ public void setUp() {
.extract();
}

protected ExtractableResponse<Response> 프로필_조회_요청_QueryDsl(final AccessToken accessToken, final String userName) {
return RestAssured.given()
.header(AUTHORIZATION, BEARER + accessToken.accessToken())
.when()
.get(String.format("/api/profiles/querydsl/%s", userName))
.then()
.extract();
}

protected ExtractableResponse<Response> 팔로우_요청(final AccessToken accessToken, final String userName) {
return RestAssured.given()
.header(AUTHORIZATION, BEARER + accessToken.accessToken())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.study.realworld.domain.article.domain.vo.ArticleTitle;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.test.util.ReflectionTestUtils;

import static com.study.realworld.domain.user.domain.persist.UserTest.testDefaultUser;
import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -24,6 +25,20 @@ public class ArticleTest {
);
}

@Test
void 식별자가_값으면_동일한_객체이다() {
final Article article = testArticle();
final Article other = testArticle();

ReflectionTestUtils.setField(article, "articleId", 1L);
ReflectionTestUtils.setField(other, "articleId", 1L);

assertAll(
() -> assertThat(article).isEqualTo(other),
() -> assertThat(article).hasSameHashCodeAs(other)
);
}

public static Article testArticle() {
return Article.builder()
.articleSlug(ArticleSlug.from("how-to-train-your-dragon"))
Expand Down
Loading

0 comments on commit 91b57d5

Please sign in to comment.