diff --git a/src/main/java/daybyquest/interest/domain/Interests.java b/src/main/java/daybyquest/interest/domain/Interests.java index cf6ab71..c21a018 100644 --- a/src/main/java/daybyquest/interest/domain/Interests.java +++ b/src/main/java/daybyquest/interest/domain/Interests.java @@ -22,7 +22,7 @@ public void save(final Interest interest) { interestRepository.save(interest); } - public void validateNotExistentByName(final String interestName) { + private void validateNotExistentByName(final String interestName) { if (interestRepository.existsByName(interestName)) { throw new InvalidDomainException(ALREADY_EXIST_INTEREST); } diff --git a/src/test/java/daybyquest/interest/domain/InterestTest.java b/src/test/java/daybyquest/interest/domain/InterestTest.java new file mode 100644 index 0000000..5301e6d --- /dev/null +++ b/src/test/java/daybyquest/interest/domain/InterestTest.java @@ -0,0 +1,18 @@ +package daybyquest.interest.domain; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import daybyquest.global.error.exception.InvalidDomainException; +import daybyquest.image.vo.Image; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +public class InterestTest { + + @ParameterizedTest + @ValueSource(strings = {"", "일이삼사오육칠팔구십일이삼사오육칠팔구십일"}) + void 이름이_1에서_20글자가_아니면_예외를_던진다(final String name) { + assertThatThrownBy(() -> new Interest(name, new Image("사진"))) + .isInstanceOf(InvalidDomainException.class); + } +} diff --git a/src/test/java/daybyquest/interest/domain/InterestsTest.java b/src/test/java/daybyquest/interest/domain/InterestsTest.java new file mode 100644 index 0000000..05e5450 --- /dev/null +++ b/src/test/java/daybyquest/interest/domain/InterestsTest.java @@ -0,0 +1,97 @@ +package daybyquest.interest.domain; + +import static daybyquest.support.fixture.InterestFixtures.INTERST_1; +import static daybyquest.support.fixture.InterestFixtures.INTERST_2; +import static daybyquest.support.fixture.InterestFixtures.INTERST_3; +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.InvalidDomainException; +import daybyquest.global.error.exception.NotExistInterestException; +import java.util.List; +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 InterestsTest { + + @Mock + private InterestRepository interestRepository; + + @InjectMocks + private Interests interests; + + @Test + void 이름_중복_검사를_하고_저장한다() { + // given & when + interests.save(INTERST_1.생성()); + + // then + assertAll(() -> { + then(interestRepository).should().existsByName(INTERST_1.name); + then(interestRepository).should().save(any(Interest.class)); + }); + } + + @Test + void 저장_시_이름이_이미_있다면_예외를_던진다() { + // given + given(interestRepository.existsByName(INTERST_1.name)).willReturn(true); + + // then + assertThatThrownBy(() -> interests.save(INTERST_1.생성())) + .isInstanceOf(InvalidDomainException.class); + } + + @Test + void 이름을_통해_존재_여부를_검증한다() { + // given + given(interestRepository.existsByName(INTERST_1.name)).willReturn(true); + + // when + interests.validateInterest(INTERST_1.name); + + // then + then(interestRepository).should().existsByName(INTERST_1.name); + } + + @Test + void 이름을_통해_존재_여부를_검증_시_존재한다면_에외를_던진다() { + // given & when & then + assertThatThrownBy(() -> interests.validateInterest(INTERST_1.name)) + .isInstanceOf(NotExistInterestException.class); + } + + @Test + void 여러_이름을_통해_존재_여부를_검증한다() { + // given + final List names = List.of(INTERST_1.name, INTERST_2.name, INTERST_3.name); + final List expected = List.of(INTERST_1.생성(), INTERST_2.생성(), INTERST_3.생성()); + given(interestRepository.findAllByNameIn(names)).willReturn(expected); + + // when + interests.validateInterests(names); + + // then + then(interestRepository).should().findAllByNameIn(names); + } + + + @Test + void 여러_이름을_통해_존재_여부를_검증_시_하나라도_없다면_예외를_던진다() { + // given + final List names = List.of(INTERST_1.name, INTERST_2.name, INTERST_3.name); + final List expected = List.of(INTERST_1.생성(), INTERST_2.생성()); + given(interestRepository.findAllByNameIn(names)).willReturn(expected); + + // when + assertThatThrownBy(() -> interests.validateInterests(names)) + .isInstanceOf(NotExistInterestException.class); + } +} diff --git a/src/test/java/daybyquest/support/fixture/InterestFixtures.java b/src/test/java/daybyquest/support/fixture/InterestFixtures.java new file mode 100644 index 0000000..7023753 --- /dev/null +++ b/src/test/java/daybyquest/support/fixture/InterestFixtures.java @@ -0,0 +1,29 @@ +package daybyquest.support.fixture; + +import daybyquest.image.vo.Image; +import daybyquest.interest.domain.Interest; + +public enum InterestFixtures { + + INTERST_1("관심사1", "관심사사진1"), + INTERST_2("관심사2", "관심사사진2"), + INTERST_3("관심사3", "관심사사진3"), + INTERST_4("관심사4", "관심사사진4"); + + public final String name; + + public final String imageIdentifier; + + InterestFixtures(final String name, final String imageIdentifier) { + this.name = name; + this.imageIdentifier = imageIdentifier; + } + + public Interest 생성() { + return new Interest(name, 사진()); + } + + public Image 사진() { + return new Image(imageIdentifier); + } +}