Skip to content

Commit

Permalink
test: 관계 도메인 서비스 테스트를 작성한다
Browse files Browse the repository at this point in the history
  • Loading branch information
vectorch9 committed Dec 31, 2023
1 parent 013d3df commit f66a267
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package daybyquest.relation.application;

import static daybyquest.global.error.ExceptionCode.NOT_FOLLOWING_USER;
import static daybyquest.support.fixture.UserFixtures.BOB;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

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

public class DeleteFollowServiceTest extends ServiceTest {

@Autowired
private DeleteFollowService deleteFollowService;

@Test
void 팔로우를_취소한다() {
// given
final Long aliceId = ALICE를_저장한다();
final Long bobId = BOB을_저장한다();
follows.save(new Follow(aliceId, bobId));

// when
deleteFollowService.invoke(aliceId, BOB.username);

// then
assertThatThrownBy(() -> follows.getByUserIdAndTargetId(aliceId, bobId))
.isInstanceOf(InvalidDomainException.class)
.hasMessageContaining(NOT_FOLLOWING_USER.getMessage());
}

@Test
void 팔로우하지_않은_대상은_취소_할_수_없다() {
// given
final Long aliceId = ALICE를_저장한다();
BOB을_저장한다();

// when & then
assertThatThrownBy(() -> deleteFollowService.invoke(aliceId, BOB.username))
.isInstanceOf(InvalidDomainException.class)
.hasMessageContaining(NOT_FOLLOWING_USER.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package daybyquest.relation.application;

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

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

public class DeleteFollowerServiceTest extends ServiceTest {

@Autowired
private DeleteFollowerService deleteFollowerService;

@Test
void 팔로워를_삭제한다() {
// given
final Long aliceId = ALICE를_저장한다();
final Long bobId = BOB을_저장한다();
follows.save(new Follow(aliceId, bobId));

// when
deleteFollowerService.invoke(bobId, ALICE.username);

// then
assertThatThrownBy(() -> follows.getByUserIdAndTargetId(aliceId, bobId))
.isInstanceOf(InvalidDomainException.class)
.hasMessageContaining(NOT_FOLLOWING_USER.getMessage());
}

@Test
void 팔로워가_아닌_대상은_삭제_할_수_없다() {
// given
ALICE를_저장한다();
final Long bobId = BOB을_저장한다();

// when & then
assertThatThrownBy(() -> deleteFollowerService.invoke(bobId, ALICE.username))
.isInstanceOf(InvalidDomainException.class)
.hasMessageContaining(NOT_FOLLOWING_USER.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package daybyquest.relation.application;

import static daybyquest.support.fixture.UserFixtures.BOB;
import static daybyquest.support.fixture.UserFixtures.CHARLIE;
import static org.assertj.core.api.Assertions.assertThat;

import daybyquest.relation.domain.Follow;
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 GetFollowersServiceTest extends ServiceTest {

@Autowired
private GetFollowersService getFollowersService;

@Test
void 팔로워_목록을_조회한다() {
// given
final Long aliceId = ALICE를_저장한다();
final Long bobId = BOB을_저장한다();
final Long charlieId = CHARLIE를_저장한다();
final Long davidId = DAVID를_저장한다();
follows.save(new Follow(bobId, aliceId));
follows.save(new Follow(charlieId, aliceId));
follows.save(new Follow(aliceId, davidId));

final List<String> expected = List.of(BOB.username, CHARLIE.username);

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

// then
assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package daybyquest.relation.application;

import static daybyquest.support.fixture.UserFixtures.BOB;
import static daybyquest.support.fixture.UserFixtures.CHARLIE;
import static org.assertj.core.api.Assertions.assertThat;

import daybyquest.relation.domain.Follow;
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 GetFollowingsServiceTest extends ServiceTest {

@Autowired
private GetFollowingsService getFollowingsService;

@Test
void 팔로잉_목록을_조회한다() {
// given
final Long aliceId = ALICE를_저장한다();
final Long bobId = BOB을_저장한다();
final Long charlieId = CHARLIE를_저장한다();
final Long davidId = DAVID를_저장한다();
follows.save(new Follow(aliceId, bobId));
follows.save(new Follow(aliceId, charlieId));
follows.save(new Follow(davidId, aliceId));

final List<String> expected = List.of(BOB.username, CHARLIE.username);

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

// then
assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package daybyquest.relation.application;

import static daybyquest.global.error.ExceptionCode.ALREADY_FOLLOWING_USER;
import static daybyquest.support.fixture.UserFixtures.BOB;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertAll;

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

public class SaveFollowServiceTest extends ServiceTest {

@Autowired
private SaveFollowService saveFollowService;

@Test
void 팔로우를_저장한다() {
// given
final Long aliceId = ALICE를_저장한다();
final Long bobId = BOB을_저장한다();

// when
saveFollowService.invoke(aliceId, BOB.username);

// then
final Follow follow = follows.getByUserIdAndTargetId(aliceId, bobId);
assertAll(() -> {
assertThat(follow.getUserId()).isEqualTo(aliceId);
assertThat(follow.getTargetId()).isEqualTo(bobId);
});
}

@Test
void 이미_팔로우한_대상은_팔로우_할_수_없다() {
// given
final Long aliceId = ALICE를_저장한다();
final Long bobId = BOB을_저장한다();
follows.save(new Follow(aliceId, bobId));

// when & then
assertThatThrownBy(() -> saveFollowService.invoke(aliceId, BOB.username))
.isInstanceOf(InvalidDomainException.class)
.hasMessageContaining(ALREADY_FOLLOWING_USER.getMessage());
}
}

0 comments on commit f66a267

Please sign in to comment.