Skip to content

Commit 8828e01

Browse files
authored
Merge pull request #172 from DayByQuest/test/relationService
[Test] 관계 도메인 서비스 테스트를 작성한다
2 parents 013d3df + f66a267 commit 8828e01

File tree

5 files changed

+219
-0
lines changed

5 files changed

+219
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package daybyquest.relation.application;
2+
3+
import static daybyquest.global.error.ExceptionCode.NOT_FOLLOWING_USER;
4+
import static daybyquest.support.fixture.UserFixtures.BOB;
5+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
6+
7+
import daybyquest.global.error.exception.InvalidDomainException;
8+
import daybyquest.relation.domain.Follow;
9+
import daybyquest.support.test.ServiceTest;
10+
import org.junit.jupiter.api.Test;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
13+
public class DeleteFollowServiceTest extends ServiceTest {
14+
15+
@Autowired
16+
private DeleteFollowService deleteFollowService;
17+
18+
@Test
19+
void 팔로우를_취소한다() {
20+
// given
21+
final Long aliceId = ALICE를_저장한다();
22+
final Long bobId = BOB을_저장한다();
23+
follows.save(new Follow(aliceId, bobId));
24+
25+
// when
26+
deleteFollowService.invoke(aliceId, BOB.username);
27+
28+
// then
29+
assertThatThrownBy(() -> follows.getByUserIdAndTargetId(aliceId, bobId))
30+
.isInstanceOf(InvalidDomainException.class)
31+
.hasMessageContaining(NOT_FOLLOWING_USER.getMessage());
32+
}
33+
34+
@Test
35+
void 팔로우하지_않은_대상은_취소_할_수_없다() {
36+
// given
37+
final Long aliceId = ALICE를_저장한다();
38+
BOB을_저장한다();
39+
40+
// when & then
41+
assertThatThrownBy(() -> deleteFollowService.invoke(aliceId, BOB.username))
42+
.isInstanceOf(InvalidDomainException.class)
43+
.hasMessageContaining(NOT_FOLLOWING_USER.getMessage());
44+
}
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package daybyquest.relation.application;
2+
3+
import static daybyquest.global.error.ExceptionCode.NOT_FOLLOWING_USER;
4+
import static daybyquest.support.fixture.UserFixtures.ALICE;
5+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
6+
7+
import daybyquest.global.error.exception.InvalidDomainException;
8+
import daybyquest.relation.domain.Follow;
9+
import daybyquest.support.test.ServiceTest;
10+
import org.junit.jupiter.api.Test;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
13+
public class DeleteFollowerServiceTest extends ServiceTest {
14+
15+
@Autowired
16+
private DeleteFollowerService deleteFollowerService;
17+
18+
@Test
19+
void 팔로워를_삭제한다() {
20+
// given
21+
final Long aliceId = ALICE를_저장한다();
22+
final Long bobId = BOB을_저장한다();
23+
follows.save(new Follow(aliceId, bobId));
24+
25+
// when
26+
deleteFollowerService.invoke(bobId, ALICE.username);
27+
28+
// then
29+
assertThatThrownBy(() -> follows.getByUserIdAndTargetId(aliceId, bobId))
30+
.isInstanceOf(InvalidDomainException.class)
31+
.hasMessageContaining(NOT_FOLLOWING_USER.getMessage());
32+
}
33+
34+
@Test
35+
void 팔로워가_아닌_대상은_삭제_할_수_없다() {
36+
// given
37+
ALICE를_저장한다();
38+
final Long bobId = BOB을_저장한다();
39+
40+
// when & then
41+
assertThatThrownBy(() -> deleteFollowerService.invoke(bobId, ALICE.username))
42+
.isInstanceOf(InvalidDomainException.class)
43+
.hasMessageContaining(NOT_FOLLOWING_USER.getMessage());
44+
}
45+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package daybyquest.relation.application;
2+
3+
import static daybyquest.support.fixture.UserFixtures.BOB;
4+
import static daybyquest.support.fixture.UserFixtures.CHARLIE;
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
import daybyquest.relation.domain.Follow;
8+
import daybyquest.support.test.ServiceTest;
9+
import daybyquest.user.dto.response.PageProfilesResponse;
10+
import daybyquest.user.dto.response.ProfileResponse;
11+
import java.util.List;
12+
import org.junit.jupiter.api.Test;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
15+
public class GetFollowersServiceTest extends ServiceTest {
16+
17+
@Autowired
18+
private GetFollowersService getFollowersService;
19+
20+
@Test
21+
void 팔로워_목록을_조회한다() {
22+
// given
23+
final Long aliceId = ALICE를_저장한다();
24+
final Long bobId = BOB을_저장한다();
25+
final Long charlieId = CHARLIE를_저장한다();
26+
final Long davidId = DAVID를_저장한다();
27+
follows.save(new Follow(bobId, aliceId));
28+
follows.save(new Follow(charlieId, aliceId));
29+
follows.save(new Follow(aliceId, davidId));
30+
31+
final List<String> expected = List.of(BOB.username, CHARLIE.username);
32+
33+
// when
34+
final PageProfilesResponse response = getFollowersService.invoke(aliceId, 페이지());
35+
final List<String> actual = response.users().stream().map(ProfileResponse::username).toList();
36+
37+
// then
38+
assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package daybyquest.relation.application;
2+
3+
import static daybyquest.support.fixture.UserFixtures.BOB;
4+
import static daybyquest.support.fixture.UserFixtures.CHARLIE;
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
import daybyquest.relation.domain.Follow;
8+
import daybyquest.support.test.ServiceTest;
9+
import daybyquest.user.dto.response.PageProfilesResponse;
10+
import daybyquest.user.dto.response.ProfileResponse;
11+
import java.util.List;
12+
import org.junit.jupiter.api.Test;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
15+
public class GetFollowingsServiceTest extends ServiceTest {
16+
17+
@Autowired
18+
private GetFollowingsService getFollowingsService;
19+
20+
@Test
21+
void 팔로잉_목록을_조회한다() {
22+
// given
23+
final Long aliceId = ALICE를_저장한다();
24+
final Long bobId = BOB을_저장한다();
25+
final Long charlieId = CHARLIE를_저장한다();
26+
final Long davidId = DAVID를_저장한다();
27+
follows.save(new Follow(aliceId, bobId));
28+
follows.save(new Follow(aliceId, charlieId));
29+
follows.save(new Follow(davidId, aliceId));
30+
31+
final List<String> expected = List.of(BOB.username, CHARLIE.username);
32+
33+
// when
34+
final PageProfilesResponse response = getFollowingsService.invoke(aliceId, 페이지());
35+
final List<String> actual = response.users().stream().map(ProfileResponse::username).toList();
36+
37+
// then
38+
assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
39+
}
40+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package daybyquest.relation.application;
2+
3+
import static daybyquest.global.error.ExceptionCode.ALREADY_FOLLOWING_USER;
4+
import static daybyquest.support.fixture.UserFixtures.BOB;
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
7+
import static org.junit.jupiter.api.Assertions.assertAll;
8+
9+
import daybyquest.global.error.exception.InvalidDomainException;
10+
import daybyquest.relation.domain.Follow;
11+
import daybyquest.support.test.ServiceTest;
12+
import org.junit.jupiter.api.Test;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
15+
public class SaveFollowServiceTest extends ServiceTest {
16+
17+
@Autowired
18+
private SaveFollowService saveFollowService;
19+
20+
@Test
21+
void 팔로우를_저장한다() {
22+
// given
23+
final Long aliceId = ALICE를_저장한다();
24+
final Long bobId = BOB을_저장한다();
25+
26+
// when
27+
saveFollowService.invoke(aliceId, BOB.username);
28+
29+
// then
30+
final Follow follow = follows.getByUserIdAndTargetId(aliceId, bobId);
31+
assertAll(() -> {
32+
assertThat(follow.getUserId()).isEqualTo(aliceId);
33+
assertThat(follow.getTargetId()).isEqualTo(bobId);
34+
});
35+
}
36+
37+
@Test
38+
void 이미_팔로우한_대상은_팔로우_할_수_없다() {
39+
// given
40+
final Long aliceId = ALICE를_저장한다();
41+
final Long bobId = BOB을_저장한다();
42+
follows.save(new Follow(aliceId, bobId));
43+
44+
// when & then
45+
assertThatThrownBy(() -> saveFollowService.invoke(aliceId, BOB.username))
46+
.isInstanceOf(InvalidDomainException.class)
47+
.hasMessageContaining(ALREADY_FOLLOWING_USER.getMessage());
48+
}
49+
}

0 commit comments

Comments
 (0)