Skip to content

Commit

Permalink
test: 사용자 서비스 테스트를 작성한다
Browse files Browse the repository at this point in the history
  • Loading branch information
vectorch9 committed Dec 15, 2023
1 parent b36502a commit daee91e
Show file tree
Hide file tree
Showing 6 changed files with 322 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package daybyquest.user.application;

import static daybyquest.global.error.ExceptionCode.DUPLICATED_USERNAME;
import static daybyquest.support.fixture.UserFixtures.ALICE;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import daybyquest.global.error.exception.InvalidDomainException;
import daybyquest.support.test.ServiceTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class CheckUsernameServiceTest extends ServiceTest {

@Autowired
private CheckUsernameService checkUsernameService;

@Test
void 사용자_이름_중복을_검사한다() {
// given & when & then
assertThatCode(() -> checkUsernameService.invoke(ALICE.username))
.doesNotThrowAnyException();
}

@Test
void 사용자_이름이_이미_있다면_예외를_던진다() {
// given
ALICE를_저장한다();

// when & then
assertThatThrownBy(() -> checkUsernameService.invoke(ALICE.username))
.isInstanceOf(InvalidDomainException.class)
.hasMessageContaining(DUPLICATED_USERNAME.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package daybyquest.user.application;

import static daybyquest.support.fixture.UserFixtures.BOB;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

import daybyquest.image.domain.BaseImageProperties;
import daybyquest.support.config.StubImages;
import daybyquest.support.test.ServiceTest;
import daybyquest.user.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class DeleteUserImageServiceTest extends ServiceTest {

@Autowired
private DeleteUserImageService deleteUserImageService;

@Autowired
private BaseImageProperties properties;

@Autowired
private StubImages images;

@Test
void 사용자_사진을_삭제한다() {
// given
final Long id = BOB을_저장한다();
images.upload(BOB.imageIdentifier, null);

// when
deleteUserImageService.invoke(id);

// then
final User user = users.getById(id);
assertAll(() -> {
assertThat(images.hasUploadImage(BOB.imageIdentifier)).isFalse();
assertThat(user.getImageIdentifier()).isEqualTo(properties.getUserIdentifier());
});
}

@Test
void 기본_사진이라면_삭제하지_않는다() {
// given
final Long id = ALICE를_저장한다();
images.upload(properties.getUserIdentifier(), null);

// when
deleteUserImageService.invoke(id);

// then
final User user = users.getById(id);
assertAll(() -> {
assertThat(images.hasUploadImage(properties.getUserIdentifier())).isTrue();
assertThat(user.getImageIdentifier()).isEqualTo(properties.getUserIdentifier());
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package daybyquest.user.application;

import static daybyquest.support.fixture.PostFixtures.POST_1;
import static daybyquest.support.fixture.PostFixtures.POST_2;
import static daybyquest.support.fixture.PostFixtures.POST_3;
import static daybyquest.support.fixture.UserFixtures.ALICE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

import daybyquest.post.domain.Posts;
import daybyquest.relation.domain.Follow;
import daybyquest.relation.domain.Follows;
import daybyquest.support.test.ServiceTest;
import daybyquest.user.dto.response.MyProfileResponse;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class GetMyProfileServiceTest extends ServiceTest {

@Autowired
private GetMyProfileService getMyProfileService;

@Autowired
private Posts posts;

@Autowired
private Follows follows;

@Test
void 사용자_프로필을_조회한다() {
// given
final Long id = ALICE를_저장한다();

// when
final MyProfileResponse response = getMyProfileService.invoke(id);

// then
assertAll(() -> {
assertThat(response.username()).isEqualTo(ALICE.username);
assertThat(response.name()).isEqualTo(ALICE.name);
assertThat(response.imageIdentifier()).isEqualTo(ALICE.imageIdentifier);
});
}

@Test
void 게시물_수가_함께_조회된다() {
// given
final Long id = ALICE를_저장한다();
posts.save(POST_1.생성(id));
posts.save(POST_2.생성(id));
posts.save(POST_3.생성(id));

// when
final MyProfileResponse response = getMyProfileService.invoke(id);

// then
assertThat(response.postCount()).isEqualTo(3);
}

@Test
void 팔로워_수가_함께_조회된다() {
// given
final Long id = ALICE를_저장한다();
follows.save(new Follow(BOB을_저장한다(), id));
follows.save(new Follow(CHARLIE를_저장한다(), id));
follows.save(new Follow(DAVID를_저장한다(), id));

// when
final MyProfileResponse response = getMyProfileService.invoke(id);

// then
assertThat(response.followerCount()).isEqualTo(3);
}

@Test
void 팔로잉_수가_함께_조회된다() {
// given
final Long id = ALICE를_저장한다();
follows.save(new Follow(id, BOB을_저장한다()));
follows.save(new Follow(id, CHARLIE를_저장한다()));
follows.save(new Follow(id, DAVID를_저장한다()));

// when
final MyProfileResponse response = getMyProfileService.invoke(id);

// then
assertThat(response.followingCount()).isEqualTo(3);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package daybyquest.user.application;

import static daybyquest.support.fixture.PostFixtures.POST_1;
import static daybyquest.support.fixture.PostFixtures.POST_2;
import static daybyquest.support.fixture.PostFixtures.POST_3;
import static daybyquest.support.fixture.UserFixtures.ALICE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

import daybyquest.post.domain.Posts;
import daybyquest.relation.domain.Follow;
import daybyquest.relation.domain.Follows;
import daybyquest.support.test.ServiceTest;
import daybyquest.user.dto.response.ProfileResponse;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class GetProfileByUsernameServiceTest extends ServiceTest {

@Autowired
private GetProfileByUsernameService getProfileByUsernameService;

@Autowired
private Follows follows;

@Autowired
private Posts posts;

@Test
void 사용자_프로필을_조회한다() {
// given
final Long id = BOB을_저장한다();
ALICE를_저장한다();

// when
final ProfileResponse response = getProfileByUsernameService.invoke(id, ALICE.username);

// then
assertAll(() -> {
assertThat(response.username()).isEqualTo(ALICE.username);
assertThat(response.name()).isEqualTo(ALICE.name);
assertThat(response.imageIdentifier()).isEqualTo(ALICE.imageIdentifier);
});
}

@Test
void 팔로우_여부가_함께_조회된다() {
// given
final Long id = BOB을_저장한다();
final Long aliceId = ALICE를_저장한다();
follows.save(new Follow(id, aliceId));

// when
final ProfileResponse response = getProfileByUsernameService.invoke(id, ALICE.username);

// then
assertThat(response.following()).isTrue();
}

@Test
void 게시물_수가_함께_조회된다() {
// given
final Long id = BOB을_저장한다();
final Long aliceId = ALICE를_저장한다();
posts.save(POST_1.생성(aliceId));
posts.save(POST_2.생성(aliceId));
posts.save(POST_3.생성(aliceId));

// when
final ProfileResponse response = getProfileByUsernameService.invoke(id, ALICE.username);

// then
assertThat(response.postCount()).isEqualTo(3);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package daybyquest.user.application;

import static daybyquest.user.domain.UserVisibility.PUBLIC;
import static org.assertj.core.api.Assertions.assertThat;

import daybyquest.support.test.ServiceTest;
import daybyquest.user.dto.response.UserVisibilityResponse;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class GetVisibilityServiceTest extends ServiceTest {

@Autowired
private GetVisibilityService getVisibilityService;

@Test
void 사용자_가시성을_조회한다() {
// given
final Long id = ALICE를_저장한다();

// when
final UserVisibilityResponse response = getVisibilityService.invoke(id);

// then
assertThat(response.visibility()).isEqualTo(PUBLIC.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package daybyquest.user.application;

import static daybyquest.support.fixture.UserFixtures.DARTH;
import static daybyquest.support.fixture.UserFixtures.DAVID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

import daybyquest.support.test.ServiceTest;
import daybyquest.user.dto.response.PageProfilesResponse;
import daybyquest.user.dto.response.ProfileResponse;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class SearchUserServiceTest extends ServiceTest {

@Autowired
private SearchUserService searchUserService;

@Test
void 사용자를_검색한다() {
// given
final Long id = DAVID를_저장한다();
DARTH를_저장한다();
ALICE를_저장한다();
final List<String> expected = List.of(DARTH.username, DAVID.username);

// when
final PageProfilesResponse response = searchUserService.invoke(id, "da", 페이지());
final List<String> actual = response.users().stream().map(ProfileResponse::username).toList();

// then
assertAll(() -> {
assertThat(response.users()).hasSize(2);
assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
});
}
}

0 comments on commit daee91e

Please sign in to comment.