diff --git a/bitgouel-api/src/test/kotlin/team/msg/domain/university/service/UniversityServiceImplTest.kt b/bitgouel-api/src/test/kotlin/team/msg/domain/university/service/UniversityServiceImplTest.kt new file mode 100644 index 000000000..7b874abea --- /dev/null +++ b/bitgouel-api/src/test/kotlin/team/msg/domain/university/service/UniversityServiceImplTest.kt @@ -0,0 +1,132 @@ +package team.msg.domain.university.service + +import com.appmattus.kotlinfixture.kotlinFixture +import io.kotest.assertions.throwables.shouldThrow +import io.kotest.core.spec.IsolationMode +import io.kotest.core.spec.style.BehaviorSpec +import io.kotest.matchers.shouldBe +import io.mockk.Runs +import io.mockk.every +import io.mockk.just +import io.mockk.mockk +import io.mockk.verify +import org.springframework.data.repository.findByIdOrNull +import team.msg.domain.university.exception.AlreadyExistUniversityException +import team.msg.domain.university.exception.UniversityHasProfessorConstraintException +import team.msg.domain.university.model.University +import team.msg.domain.university.presentation.data.request.CreateUniversityRequest +import team.msg.domain.university.presentation.data.response.UniversitiesResponse +import team.msg.domain.university.presentation.data.response.UniversityResponse +import team.msg.domain.university.repository.ProfessorRepository +import team.msg.domain.university.repository.UniversityRepository + +class UniversityServiceImplTest : BehaviorSpec({ + isolationMode = IsolationMode.InstancePerLeaf + + val fixture = kotlinFixture() + + val universityRepository = mockk() + val professorRepository = mockk() + + val universityServiceImpl = UniversityServiceImpl( + universityRepository = universityRepository, + professorRepository = professorRepository + ) + + // createUniversity 테스트 코드 + Given("CreateUniversityRequest 가 주어졌을 때") { + val universityName = "대충 대학교" + + val request = fixture { + property(University::name) { universityName } + } + + val university = fixture { + property(University::name) { universityName } + } + + every { universityRepository.existsByName(any()) } returns false + every { universityRepository.save(any()) } returns university + + When("대학 추가 시") { + universityServiceImpl.createUniversity(request) + + Then("University 가 저장이 되어야 한다") { + verify(exactly = 1) { universityRepository.save(any()) } + } + } + + When("이미 존재하는 대학 이름이라면") { + every { universityRepository.existsByName(any()) } returns true + + Then("AlreadyExistUniversityException 이 발생해야 한다") { + shouldThrow { + universityServiceImpl.createUniversity(request) + } + } + } + } + + // deleteUniversity 테스트 코드 + Given("UniversityId 가 주어졌을 때") { + val universityId = 1L + + val university = fixture() + + every { universityRepository.findByIdOrNull(universityId) } returns university + every { professorRepository.existsByUniversity(university) } returns false + every { universityRepository.delete(university) } just Runs + + When("대학 삭제 시") { + universityServiceImpl.deleteUniversity(universityId) + + Then("University 가 삭제가 되어야 한다") { + verify(exactly = 1) { universityRepository.delete(university) } + } + } + + When("University 와 연관 관계를 가진 Professor 가 존재한다면") { + every { professorRepository.existsByUniversity(university) } returns true + + Then("UniversityHasProfessorConstraintException 이 발생해야 한다") { + shouldThrow { + universityServiceImpl.deleteUniversity(universityId) + } + } + } + } + + // queryUniversities 테스트 코드 + Given("University가 주어질 때") { + val universityId = 1L + val universityName = "대학교" + val department = "컴퓨터 공학과" + val university = fixture { + property(University::id) { universityId } + property(University::name) { universityName } + property(University::department) { department } + } + + val universities = listOf(university) + + val universityResponse = fixture { + property(UniversityResponse::id) { universityId } + property(UniversityResponse::universityName) { universityName } + property(UniversityResponse::department) { department } + } + + val response = fixture { + property(UniversitiesResponse::universities) { listOf(universityResponse) } + } + + every { universityRepository.findAll() } returns universities + + When("대학 조회 시") { + val result = universityServiceImpl.queryUniversities() + + Then("result 가 response 가 같아야 한다.") { + result shouldBe response + } + } + } +}) \ No newline at end of file