Skip to content

Commit

Permalink
[#54] test: ArticleSlugConverter 테스트 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
kwj1270 committed Nov 30, 2021
1 parent cc69568 commit 62d7925
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ArticleSlugConverter {
private static final Logger logger = LoggerFactory.getLogger(ArticleSlugConverter.class);

@Component
public class StringToArticleSlugConverter implements Converter<String, ArticleSlug> {
public static class StringToArticleSlugConverter implements Converter<String, ArticleSlug> {
@Override
public ArticleSlug convert(final String source) {
logger.info("StringToArticleSlugConverter : " + source);
Expand All @@ -20,7 +20,7 @@ public ArticleSlug convert(final String source) {
}

@Component
public class ArticleSlugToStringConverter implements Converter<ArticleSlug, String> {
public static class ArticleSlugToStringConverter implements Converter<ArticleSlug, String> {
@Override
public String convert(final ArticleSlug source) {
logger.info("ArticleSlugToStringConverter : " + source.toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.study.realworld.domain.follow.api;

import com.study.realworld.domain.follow.application.FollowCommandService;
import com.study.realworld.domain.follow.application.FollowUserCommandService;
import com.study.realworld.domain.follow.dto.FollowResponse;
import com.study.realworld.domain.follow.dto.UnFollowResponse;
import com.study.realworld.domain.user.domain.vo.UserName;
Expand All @@ -16,19 +16,19 @@
@RestController
public class FollowCommandApi {

private final FollowCommandService followCommandService;
private final FollowUserCommandService followUserCommandService;

@PostMapping("/profiles/{username}/follow")
public ResponseEntity<FollowResponse> profile2(@AuthenticationPrincipal final Long userId,
@PathVariable final String username) {
final FollowResponse followResponse = followCommandService.follow(userId, UserName.from(username));
final FollowResponse followResponse = followUserCommandService.follow(userId, UserName.from(username));
return ResponseEntity.ok().body(followResponse);
}

@DeleteMapping("/profiles/{username}/follow")
public ResponseEntity<UnFollowResponse> profile(@AuthenticationPrincipal final Long userId,
@PathVariable final String username) {
final UnFollowResponse unfollowResponse = followCommandService.unfollow(userId, UserName.from(username));
final UnFollowResponse unfollowResponse = followUserCommandService.unfollow(userId, UserName.from(username));
return ResponseEntity.ok().body(unfollowResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@RequiredArgsConstructor
@Transactional
@Service
public class FollowCommandService {
public class FollowUserCommandService {

private final UserQueryService userQueryService;
private final FollowRepository followRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
package com.study.realworld.domain.article.api.converter;

import com.study.realworld.domain.article.api.converter.ArticleSlugConverter.ArticleSlugToStringConverter;
import com.study.realworld.domain.article.api.converter.ArticleSlugConverter.StringToArticleSlugConverter;
import com.study.realworld.domain.article.domain.vo.ArticleSlug;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.assertj.core.api.Assertions.assertThat;

@DisplayName("게시글 슬러그 컨버터(ArticleSlugConverter)")
class ArticleSlugConverterTest {

@Test
void test() {
@ParameterizedTest(name = "입력값 : {0}")
@ValueSource(strings = {"a", "ab", "abc"})
void 문자열을_객체로_변환할_수_있다(String slugString) {
final StringToArticleSlugConverter converter = new StringToArticleSlugConverter();
final ArticleSlug articleSlug = converter.convert(slugString);

assertThat(articleSlug.articleSlug()).isEqualTo(slugString);
}

@ParameterizedTest(name = "입력값 : {0}")
@ValueSource(strings = {"a", "ab", "abc"})
void 객체를_문자열로_변환할_수_있다(String slugString) {
final ArticleSlug articleSlug = ArticleSlug.from(slugString);
final ArticleSlugToStringConverter converter = new ArticleSlugToStringConverter();
final String actual = converter.convert(articleSlug);

assertThat(actual).isEqualTo(slugString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

@DisplayName("팔로우 커멘드 서비스(FollowCommandService)")
@ExtendWith(MockitoExtension.class)
class FollowCommandServiceTest {
class FollowUserCommandServiceTest {

@Mock
private UserQueryService userQueryService;
Expand All @@ -35,7 +35,7 @@ class FollowCommandServiceTest {
private FollowRepository followRepository;

@InjectMocks
private FollowCommandService followCommandService;
private FollowUserCommandService followUserCommandService;

@Test
void 팔로워_아이덴티티와_팔로위_이름을_입력하면_팔로우_할_수_있다() {
Expand All @@ -48,7 +48,7 @@ class FollowCommandServiceTest {
willReturn(false).given(followRepository).existsByFolloweeAndFollower(any(), any());
willReturn(follow).given(followRepository).save(any());

final FollowResponse followResponse = followCommandService.follow(1L, followee.userName());
final FollowResponse followResponse = followUserCommandService.follow(1L, followee.userName());
assertAll(
() -> assertThat(followResponse.userName()).isEqualTo(followee.userName()),
() -> assertThat(followResponse.userBio()).isEqualTo(followee.userBio()),
Expand All @@ -66,7 +66,7 @@ class FollowCommandServiceTest {
willReturn(followee).given(userQueryService).findByUserName(any());
willReturn(true).given(followRepository).existsByFolloweeAndFollower(any(), any());

assertThatThrownBy(() -> followCommandService.follow(1L, followee.userName()))
assertThatThrownBy(() -> followUserCommandService.follow(1L, followee.userName()))
.isExactlyInstanceOf(DuplicatedFollowException.class)
.hasMessage("팔로우 정보가 이미 있습니다");
}
Expand All @@ -81,7 +81,7 @@ class FollowCommandServiceTest {
willReturn(followee).given(userQueryService).findByUserName(any());
willReturn(Optional.of(follow)).given(followRepository).findByFolloweeAndFollower(any(), any());

final UnFollowResponse unFollowResponse = followCommandService.unfollow(1L, followee.userName());
final UnFollowResponse unFollowResponse = followUserCommandService.unfollow(1L, followee.userName());
assertAll(
() -> assertThat(unFollowResponse.userName()).isEqualTo(followee.userName()),
() -> assertThat(unFollowResponse.userBio()).isEqualTo(followee.userBio()),
Expand Down

0 comments on commit 62d7925

Please sign in to comment.