-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Follow, FollowCommandService 팔로우 단위 테스트 작성
- Loading branch information
Showing
14 changed files
with
223 additions
and
27 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/java/com/study/realworld/domain/follow/api/FollowApiAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.study.realworld.domain.follow.api; | ||
|
||
import com.study.realworld.domain.follow.error.FollowErrorCode; | ||
import com.study.realworld.domain.follow.error.FollowErrorResponse; | ||
import com.study.realworld.domain.follow.error.exception.FollowBusinessException; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
|
||
@RestControllerAdvice | ||
public class FollowApiAdvice { | ||
|
||
private final Logger log = LogManager.getLogger(getClass()); | ||
|
||
@ExceptionHandler(FollowBusinessException.class) | ||
protected ResponseEntity<FollowErrorResponse> handleUserBusinessException(final FollowBusinessException followBusinessException) { | ||
|
||
log.error("FollowBusinessException: {}", followBusinessException.getMessage()); | ||
|
||
final FollowErrorCode followErrorCode = FollowErrorCode.values(followBusinessException); | ||
final FollowErrorResponse followErrorResponse = FollowErrorResponse.from(followErrorCode); | ||
return followErrorResponse.toResponseEntity(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/com/study/realworld/domain/follow/error/FollowErrorCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.study.realworld.domain.follow.error; | ||
|
||
import com.study.realworld.domain.follow.error.exception.DuplicatedFollowException; | ||
import com.study.realworld.domain.follow.error.exception.FollowBusinessException; | ||
import com.study.realworld.domain.follow.error.exception.FollowNotFoundException; | ||
import com.study.realworld.global.error.ErrorCode; | ||
import org.springframework.http.HttpStatus; | ||
|
||
import java.util.Arrays; | ||
|
||
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"); | ||
|
||
private final Class exceptionClass; | ||
private final HttpStatus httpStatus; | ||
private final String message; | ||
|
||
FollowErrorCode(final Class<?> exceptionClass, final HttpStatus httpStatus, final String message) { | ||
this.exceptionClass = exceptionClass; | ||
this.httpStatus = httpStatus; | ||
this.message = message; | ||
} | ||
|
||
public static FollowErrorCode values(final FollowBusinessException exception) { | ||
final Class<? extends FollowBusinessException> exceptionClass = exception.getClass(); | ||
return Arrays.stream(values()) | ||
.filter(userErrorCode -> userErrorCode.exceptionClass.equals(exceptionClass)) | ||
.findFirst() | ||
.orElseThrow(IllegalArgumentException::new); | ||
} | ||
|
||
@Override | ||
public HttpStatus httpStatus() { | ||
return httpStatus; | ||
} | ||
|
||
@Override | ||
public String message() { | ||
return message; | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/study/realworld/domain/follow/error/FollowErrorResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.study.realworld.domain.follow.error; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import com.fasterxml.jackson.annotation.JsonTypeName; | ||
import com.study.realworld.global.error.ErrorCode; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
import java.util.List; | ||
|
||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
@NoArgsConstructor(access = AccessLevel.PACKAGE) | ||
@JsonTypeName("errors") | ||
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME) | ||
public class FollowErrorResponse { | ||
|
||
@JsonIgnore | ||
private HttpStatus httpStatus; | ||
|
||
@JsonProperty("body") | ||
private List<String> body; | ||
|
||
public static FollowErrorResponse from(final ErrorCode errorCode) { | ||
return new FollowErrorResponse(errorCode.httpStatus(), List.of(errorCode.message())); | ||
} | ||
|
||
public ResponseEntity<FollowErrorResponse> toResponseEntity() { | ||
return ResponseEntity.status(this.httpStatus).body(this); | ||
} | ||
|
||
public List<String> body() { | ||
return body; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...ain/java/com/study/realworld/domain/follow/error/exception/DuplicatedFollowException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.study.realworld.domain.follow.error.exception; | ||
|
||
public class DuplicatedFollowException extends FollowBusinessException { | ||
|
||
private static final String DUPLICATED_FOLLOW_MESSAGE = "팔로우 정보가 이미 있습니다"; | ||
|
||
public DuplicatedFollowException() { | ||
super(DUPLICATED_FOLLOW_MESSAGE); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/study/realworld/domain/follow/error/exception/FollowBusinessException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.study.realworld.domain.follow.error.exception; | ||
|
||
public class FollowBusinessException extends RuntimeException { | ||
|
||
public FollowBusinessException(final String message) { | ||
super(message); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/study/realworld/domain/follow/error/exception/FollowNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.study.realworld.domain.follow.error.exception; | ||
|
||
public class FollowNotFoundException extends FollowBusinessException { | ||
|
||
private static final String FOLLOW_NOT_FOUND_MESSAGE = "팔로우 정보를 찾을 수 없습니다"; | ||
|
||
public FollowNotFoundException() { | ||
super(FOLLOW_NOT_FOUND_MESSAGE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/test/java/com/study/realworld/domain/follow/application/FollowCommandServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.study.realworld.domain.follow.application; | ||
|
||
import com.study.realworld.domain.follow.domain.Follow; | ||
import com.study.realworld.domain.follow.domain.FollowRepository; | ||
import com.study.realworld.domain.follow.dto.FollowResponse; | ||
import com.study.realworld.domain.user.application.UserQueryService; | ||
import com.study.realworld.domain.user.domain.persist.User; | ||
import com.study.realworld.domain.user.domain.vo.UserBio; | ||
import com.study.realworld.domain.user.domain.vo.UserImage; | ||
import com.study.realworld.domain.user.domain.vo.UserName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import static com.study.realworld.domain.follow.domain.FollowTest.testFollower; | ||
import static com.study.realworld.domain.user.domain.persist.UserTest.testUser; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.willReturn; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class FollowCommandServiceTest { | ||
|
||
@Mock | ||
private UserQueryService userQueryService; | ||
|
||
@Mock | ||
private FollowQueryService followQueryService; | ||
|
||
@Mock | ||
private FollowRepository followRepository; | ||
|
||
@InjectMocks | ||
private FollowCommandService followCommandService; | ||
|
||
@Test | ||
void 팔로워_아이덴티티와_팔로위_이름을_입력하면_팔로우_할_수_있다() { | ||
final User follower = testUser("user1@gmail.com", "user1", "password1", "bio1", "image1"); | ||
final User followee = testUser("user2@gmail.com", "user2", "password2", "bio2", "image2"); | ||
final Follow follow = testFollower(followee, follower); | ||
willReturn(follower).given(userQueryService).findById(any()); | ||
willReturn(followee).given(userQueryService).findByUserName(any()); | ||
willReturn(false).given(followQueryService).existsByFolloweeAndFollower(any(), any()); | ||
willReturn(follow).given(followRepository).save(any()); | ||
|
||
final FollowResponse followResponse = followCommandService.follow(1L, UserName.from("user2")); | ||
assertAll( | ||
() -> assertThat(followResponse.userName()).isEqualTo(UserName.from("user2")), | ||
() -> assertThat(followResponse.userBio()).isEqualTo(UserBio.from("bio2")), | ||
() -> assertThat(followResponse.userImage()).isEqualTo(UserImage.from("image2")), | ||
() -> assertThat(followResponse.isFollowing()).isTrue() | ||
); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/test/java/com/study/realworld/domain/follow/application/FollowQueryServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.study.realworld.domain.follow.application; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class FollowQueryServiceTest { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
src/test/java/com/study/realworld/domain/user/api/UserApiAdviceTest.java
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
src/test/java/com/study/realworld/domain/user/api/UserCommandApiTest.java
This file was deleted.
Oops, something went wrong.