Skip to content

Commit

Permalink
[Test] 뱃지 도메인, 쿼리 테스트를 작성한다 (#84)
Browse files Browse the repository at this point in the history
* test: 뱃지 도메인 테스트 작성

* test: 특정 길이의 문자열 생성 로직 분리

* refactor: eqauls() 메서드 내 instanceof를 도입하여 로직 간단화

* test: Badges 테스트 작성

* test: Owning 도메인 테스트 작성

* test: 뱃지 쿼리 테스트 작성

* test: 쿼리 테스트 시 where 조건에 대한 확인도 고려

* test: 사용하지 않는 변수 제거
  • Loading branch information
vectorch9 authored Nov 3, 2023
1 parent 090a193 commit ff47099
Show file tree
Hide file tree
Showing 14 changed files with 323 additions and 22 deletions.
15 changes: 10 additions & 5 deletions src/main/java/daybyquest/badge/domain/Ownings.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package daybyquest.badge.domain;

import daybyquest.user.domain.Users;
import org.springframework.stereotype.Component;

@Component
Expand All @@ -9,16 +10,20 @@ public class Ownings {

private final Badges badges;

Ownings(final OwningRepository owningRepository, final Badges badges) {
private final Users users;

Ownings(final OwningRepository owningRepository, final Badges badges, final Users users) {
this.owningRepository = owningRepository;
this.badges = badges;
}

public void save(final Owning owning) {
owningRepository.save(owning);
this.users = users;
}

public void saveByUserIdAndBadgeId(final Long userId, final Long badgeId) {
users.validateExistentById(userId);
save(new Owning(userId, badges.getById(badgeId)));
}

private void save(final Owning owning) {
owningRepository.save(owning);
}
}
3 changes: 1 addition & 2 deletions src/main/java/daybyquest/image/vo/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ public Image(String imageIdentifier) {

@Override
public boolean equals(Object o) {
if (!(o instanceof Image)) {
if (!(o instanceof Image image)) {
return false;
}
final Image image = (Image) o;
return this.imageIdentifier.equals(image.imageIdentifier);
}

Expand Down
19 changes: 19 additions & 0 deletions src/test/java/daybyquest/badge/domain/BadgeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package daybyquest.badge.domain;

import static daybyquest.support.fixture.BadgeFixtures.BADGE_1;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import daybyquest.global.error.exception.InvalidDomainException;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class BadgeTest {

@ParameterizedTest
@ValueSource(strings = {"", "일이삼사오육칠팔구십일이삼사오육"})
void 뱃지_이름이_1_에서_15_글자가_아니면_예외를_던진다(final String name) {
// given & when & then
assertThatThrownBy(() -> new Badge(name, BADGE_1.사진()))
.isInstanceOf(InvalidDomainException.class);
}
}
80 changes: 80 additions & 0 deletions src/test/java/daybyquest/badge/domain/BadgesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package daybyquest.badge.domain;

import static daybyquest.support.fixture.BadgeFixtures.BADGE_1;
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 static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;

import daybyquest.global.error.exception.NotExistBadgeException;
import java.util.Optional;
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;

@ExtendWith(MockitoExtension.class)
public class BadgesTest {

@Mock
private BadgeRepository badgeRepository;
@InjectMocks
private Badges badges;

@Test
void 뱃지를_저장한다() {
// given & when
badges.save(BADGE_1.생성());

// then
then(badgeRepository).should().save(any(Badge.class));
}

@Test
void 뱃지_ID_존재_여부를_검증한다() {
// given
final Long badgeId = 1L;
given(badgeRepository.existsById(badgeId)).willReturn(true);

// when
badges.validateExistentById(badgeId);

// then
then(badgeRepository).should().existsById(badgeId);
}

@Test
void 뱃지_ID_존재_여부_검증_시_없다면_예외를_던진다() {
// given & when & then
assertThatThrownBy(() -> badges.validateExistentById(1L))
.isInstanceOf(NotExistBadgeException.class);
}

@Test
void ID를_통해_뱃지를_조회한다() {
// given
final Long badgeId = 1L;
given(badgeRepository.findById(badgeId)).willReturn(Optional.of(BADGE_1.생성(badgeId)));

// when
final Badge actual = badges.getById(badgeId);

// then
assertAll(() -> {
then(badgeRepository).should().findById(badgeId);
assertThat(actual.getId()).isEqualTo(badgeId);
assertThat(actual.getName()).isEqualTo(BADGE_1.name);
assertThat(actual.getImage()).isEqualTo(BADGE_1.사진());
});
}

@Test
void ID를_통한_조회_시_없다면_예외를_던진다() {
// given & when & then
assertThatThrownBy(() -> badges.getById(1L))
.isInstanceOf(NotExistBadgeException.class);
}
}
57 changes: 57 additions & 0 deletions src/test/java/daybyquest/badge/domain/OwningsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package daybyquest.badge.domain;

import static daybyquest.support.fixture.BadgeFixtures.BADGE_1;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;

import daybyquest.user.domain.Users;
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;

@ExtendWith(MockitoExtension.class)
public class OwningsTest {

@Mock
private OwningRepository owningRepository;

@Mock
private Badges badges;

@Mock
private Users users;

@InjectMocks
private Ownings ownings;

@Test
void 사용자_ID와_뱃지_ID를_통해_소유를_저장한다() {
// given
final Long userId = 1L;
final Long badgeId = 2L;
given(badges.getById(badgeId)).willReturn(BADGE_1.생성(badgeId));

// when
ownings.saveByUserIdAndBadgeId(userId, badgeId);

// then
then(owningRepository).should().save(any(Owning.class));
}

@Test
void 저장_시_사용자_ID_존재_여부를_검증한다() {
// given
final Long userId = 1L;
final Long badgeId = 2L;
given(badges.getById(badgeId)).willReturn(BADGE_1.생성(badgeId));

// when
ownings.saveByUserIdAndBadgeId(userId, badgeId);

// then
then(users).should().validateExistentById(userId);
}
}
87 changes: 87 additions & 0 deletions src/test/java/daybyquest/badge/query/BadgeDaoQuerydslImplTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package daybyquest.badge.query;

import static daybyquest.support.fixture.BadgeFixtures.BADGE_1;
import static daybyquest.support.fixture.BadgeFixtures.BADGE_2;
import static daybyquest.support.fixture.BadgeFixtures.BADGE_3;
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.badge.domain.Badge;
import daybyquest.badge.domain.Owning;
import daybyquest.global.query.NoOffsetTimePage;
import daybyquest.support.test.QuerydslTest;
import daybyquest.user.domain.User;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;

@Import(BadgeDaoQuerydslImpl.class)
public class BadgeDaoQuerydslImplTest extends QuerydslTest {

@Autowired
private BadgeDaoQuerydslImpl badgeDao;

@Test
void 소유한_뱃지_목록을_조회한다() {
// given
final User bob = 저장한다(BOB.생성());
final Badge badge1 = 저장한다(BADGE_1.생성());
final Badge badge2 = 저장한다(BADGE_2.생성());
final Badge badge3 = 저장한다(BADGE_3.생성());

저장한다(new Owning(bob.getId(), badge1));
저장한다(new Owning(bob.getId(), badge2));
저장한다(new Owning(bob.getId(), badge3));

final NoOffsetTimePage page = new NoOffsetTimePage(null, 5);

// when
final List<BadgeData> badgeData = badgeDao.getBadgePageByUserIds(bob.getId(), page);

// then
assertThat(badgeData).hasSize(3);
}

@Test
void 소유하지_않은_뱃지는_목록에_포함되지_않는다() {
// given
final User bob = 저장한다(BOB.생성());
final Badge badge1 = 저장한다(BADGE_1.생성());
저장한다(BADGE_2.생성());
저장한다(BADGE_3.생성());

저장한다(new Owning(bob.getId(), badge1));

final NoOffsetTimePage page = new NoOffsetTimePage(null, 5);

// when
final List<BadgeData> badgeData = badgeDao.getBadgePageByUserIds(bob.getId(), page);

// then
assertThat(badgeData).hasSize(1);
}

@Test
void 뱃지_데이터는_뱃지_정보와_일치_해야한다() {
// given
final User bob = 저장한다(BOB.생성());
final Badge badge1 = 저장한다(BADGE_1.생성());

저장한다(new Owning(bob.getId(), badge1));

final NoOffsetTimePage page = new NoOffsetTimePage(null, 5);

// when
final List<BadgeData> badgeData = badgeDao.getBadgePageByUserIds(bob.getId(), page);

// then
assertAll(() -> {
assertThat(badgeData).hasSize(1);
final BadgeData actual = badgeData.get(0);
assertThat(actual.getName()).isEqualTo(BADGE_1.name);
assertThat(actual.getImageIdentifier()).isEqualTo(BADGE_1.imageIdentifier);
});
}
}
5 changes: 2 additions & 3 deletions src/test/java/daybyquest/comment/domain/CommentTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package daybyquest.comment.domain;

import static daybyquest.support.util.StringUtils.문자열을_만든다;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import daybyquest.global.error.exception.InvalidDomainException;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

Expand All @@ -23,7 +22,7 @@ class 생성자는 {
@Test
void 내용이_200_글자_초과면_예외를_던진다() {
// given
final String content = Stream.generate(() -> "x").limit(201).collect(Collectors.joining());
final String content = 문자열을_만든다(201);

// when & then
assertThatThrownBy(() -> new Comment(1L, 1L, content))
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/daybyquest/group/domain/GroupTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package daybyquest.group.domain;

import static daybyquest.support.fixture.GroupFixtures.GROUP_1;
import static daybyquest.support.util.StringUtils.문자열을_만든다;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import daybyquest.global.error.exception.InvalidDomainException;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand All @@ -23,7 +22,7 @@ public class GroupTest {
@Test
void 설명이_200_글자_초과면_예외를_던진다() {
// given
final String description = Stream.generate(() -> "x").limit(201).collect(Collectors.joining());
final String description = 문자열을_만든다(201);

// when & then
assertThatThrownBy(
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/daybyquest/post/domain/PostTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package daybyquest.post.domain;

import static daybyquest.support.fixture.PostFixtures.POST_1;
import static daybyquest.support.util.StringUtils.문자열을_만든다;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import daybyquest.global.error.exception.InvalidDomainException;
import daybyquest.image.vo.Image;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -46,7 +45,7 @@ class 생성자는 {
@Test
void 내용이_500_글자_초과면_예외를_던진다() {
// given
final String content = Stream.generate(() -> "x").limit(501).collect(Collectors.joining());
final String content = 문자열을_만든다(501);

// when & then
assertThatThrownBy(() -> new Post(1L, null, content, POST_1.사진_목록()))
Expand Down
Loading

0 comments on commit ff47099

Please sign in to comment.