diff --git a/src/main/java/poomasi/domain/member/entity/Member.java b/src/main/java/poomasi/domain/member/entity/Member.java index 3ae6cc9b..4dc7828b 100644 --- a/src/main/java/poomasi/domain/member/entity/Member.java +++ b/src/main/java/poomasi/domain/member/entity/Member.java @@ -79,7 +79,8 @@ public void setMemberProfile(MemberProfile memberProfile) { } @Builder - public Member(String email, Role role, LoginType loginType, String provideId, MemberProfile memberProfile) { + public Member(Long id, String email, Role role, LoginType loginType, String provideId, MemberProfile memberProfile) { + this.id = id; this.email = email; this.role = role; this.loginType = loginType; diff --git a/src/test/java/poomasi/domain/farm/service/FarmFarmerServiceTest.java b/src/test/java/poomasi/domain/farm/service/FarmFarmerServiceTest.java new file mode 100644 index 00000000..c34ca6f8 --- /dev/null +++ b/src/test/java/poomasi/domain/farm/service/FarmFarmerServiceTest.java @@ -0,0 +1,141 @@ +package poomasi.domain.farm.service; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +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; +import poomasi.domain.farm.dto.FarmRegisterRequest; +import poomasi.domain.farm.dto.FarmUpdateRequest; +import poomasi.domain.farm.entity.Farm; +import poomasi.domain.farm.repository.FarmRepository; +import poomasi.domain.member.entity.Member; +import poomasi.global.error.BusinessError; +import poomasi.global.error.BusinessException; + +import java.util.Optional; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.verify; + +@ExtendWith(MockitoExtension.class) +class FarmFarmerServiceTest { + + @InjectMocks + private FarmFarmerService farmFarmerService; + + @Mock + private FarmRepository farmRepository; + + @Nested + @DisplayName("농장 등록") + class RegisterFarm { + @Test + @DisplayName("농장이 이미 존재하는 경우 예외를 발생시킨다") + void should_throwException_when_farmAlreadyExists() { + // given + Member member = Member.builder() + .id(1L) + .build(); + Farm existingFarm = Farm.builder() + .id(1L) + .name("Existing Farm") + .ownerId(1L) + .build(); + + given(farmRepository.getFarmByOwnerIdAndDeletedAtIsNull(member.getId())).willReturn(Optional.of(existingFarm)); + + FarmRegisterRequest request = new FarmRegisterRequest("New Farm", "Address", "Detail", 1.0, 1.0, "010-1234-5678", "Description", 10000L, 10, 5); + + // when & then + assertThatThrownBy(() -> farmFarmerService.registerFarm(member, request)) + .isInstanceOf(BusinessException.class) + .hasFieldOrPropertyWithValue("businessError", BusinessError.FARM_ALREADY_EXISTS); + } + } + + @Nested + @DisplayName("농장 정보 업데이트") + class UpdateFarm { + @Test + @DisplayName("농장이 존재하지 않는 경우 예외를 발생시킨다") + void should_throwException_when_farmNotExist() { + // given + Long farmId = 1L; + FarmUpdateRequest request = new FarmUpdateRequest(farmId, "Updated Farm", "Description", "Address", "Detail", 1.0, 1.0); + given(farmRepository.findByIdAndDeletedAtIsNull(farmId)).willReturn(Optional.empty()); + + // when & then + assertThatThrownBy(() -> farmFarmerService.updateFarm(1L, request)) + .isInstanceOf(BusinessException.class) + .hasFieldOrPropertyWithValue("businessError", BusinessError.FARM_NOT_FOUND); + } + + @Test + @DisplayName("농장 소유자가 아닌 경우 예외를 발생시킨다") + void should_throwException_when_ownerMismatch() { + // given + Long farmId = 1L; + Long farmerId = 2L; + Farm farm = Farm.builder() + .id(farmId) + .name("Farm") + .ownerId(3L) + .build(); + given(farmRepository.findByIdAndDeletedAtIsNull(farmId)).willReturn(Optional.of(farm)); + + FarmUpdateRequest request = new FarmUpdateRequest(farmId, "Updated Farm", "Description", "Address", "Detail", 1.0, 1.0); + + // when & then + assertThatThrownBy(() -> farmFarmerService.updateFarm(farmerId, request)) + .isInstanceOf(BusinessException.class) + .hasFieldOrPropertyWithValue("businessError", BusinessError.FARM_OWNER_MISMATCH); + } + } + + @Nested + @DisplayName("농장 삭제") + class DeleteFarm { + @Test + @DisplayName("소유자가 아닌 경우 예외를 발생시킨다") + void should_throwException_when_ownerMismatchOnDelete() { + // given + Long farmId = 1L; + Long farmerId = 2L; + Farm farm = Farm.builder() + .id(farmId) + .name("Farm") + .ownerId(3L) + .build(); + given(farmRepository.findByIdAndDeletedAtIsNull(farmId)).willReturn(Optional.of(farm)); + + // when & then + assertThatThrownBy(() -> farmFarmerService.deleteFarm(farmerId, farmId)) + .isInstanceOf(BusinessException.class) + .hasFieldOrPropertyWithValue("businessError", BusinessError.FARM_OWNER_MISMATCH); + } + + @Test + @DisplayName("농장 삭제에 성공한다") + void should_deleteFarm_when_ownerMatches() { + // given + Long farmId = 1L; + Long farmerId = 1L; + Farm farm = Farm.builder() + .id(farmId) + .name("Farm") + .ownerId(farmerId) + .build(); + given(farmRepository.findByIdAndDeletedAtIsNull(farmId)).willReturn(Optional.of(farm)); + + // when + farmFarmerService.deleteFarm(farmerId, farmId); + + // then + verify(farmRepository).delete(farm); + } + } +} diff --git a/src/test/java/poomasi/domain/farm/service/FarmPlatformServiceTest.java b/src/test/java/poomasi/domain/farm/service/FarmPlatformServiceTest.java new file mode 100644 index 00000000..60759722 --- /dev/null +++ b/src/test/java/poomasi/domain/farm/service/FarmPlatformServiceTest.java @@ -0,0 +1,105 @@ +package poomasi.domain.farm.service; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +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; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import poomasi.domain.farm.dto.FarmResponse; +import poomasi.domain.farm.entity.Farm; +import poomasi.global.error.BusinessError; +import poomasi.global.error.BusinessException; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.verify; + +@ExtendWith(MockitoExtension.class) +class FarmPlatformServiceTest { + + @InjectMocks + private FarmPlatformService farmPlatformService; + + @Mock + private FarmService farmService; + + @Nested + @DisplayName("ID로 농장 가져오기") + class GetFarmByFarmId { + @Test + @DisplayName("농장 ID로 농장 정보를 성공적으로 가져온다") + void should_returnFarmResponse_when_farmExists() { + // given + Long farmId = 1L; + Farm farm = Farm.builder() + .id(farmId) + .name("Test Farm") + .ownerId(1L) + .build(); + given(farmService.getFarmByFarmId(farmId)).willReturn(farm); + + // when + FarmResponse response = farmPlatformService.getFarmByFarmId(farmId); + + // then + assertThat(response.id()).isEqualTo(farmId); + assertThat(response.name()).isEqualTo("Test Farm"); + verify(farmService).getFarmByFarmId(farmId); // farmService 호출 확인 + } + + @Test + @DisplayName("농장 ID로 농장 정보를 가져오는데 농장이 존재하지 않는 경우 예외를 발생시킨다") + void should_throwException_when_farmNotExist() { + // given + Long farmId = 1L; + given(farmService.getFarmByFarmId(farmId)).willThrow(new BusinessException(BusinessError.FARM_NOT_FOUND)); + + // when & then + assertThatThrownBy(() -> farmPlatformService.getFarmByFarmId(farmId)) + .isInstanceOf(BusinessException.class) + .hasFieldOrPropertyWithValue("businessError", BusinessError.FARM_NOT_FOUND); + + verify(farmService).getFarmByFarmId(farmId); + } + } + + @Nested + @DisplayName("농장 리스트 가져오기") + class GetFarmList { + @Test + @DisplayName("페이징 요청으로 농장 리스트를 성공적으로 가져온다") + void should_returnFarmResponseList_when_farmsExist() { + // given + Pageable pageable = PageRequest.of(0, 10); + Farm farm1 = Farm.builder() + .id(1L) + .name("Farm 1") + .ownerId(1L) + .build(); + Farm farm2 = Farm.builder() + .id(2L) + .name("Farm 2") + .ownerId(2L) + .build(); + List farmList = List.of(farm1, farm2); + + given(farmService.getFarmList(pageable)).willReturn(farmList); + + // when + List responseList = farmPlatformService.getFarmList(pageable); + + // then + assertThat(responseList).hasSize(2); + assertThat(responseList.get(0).name()).isEqualTo("Farm 1"); + assertThat(responseList.get(1).name()).isEqualTo("Farm 2"); + verify(farmService).getFarmList(pageable); // farmService 호출 확인 + } + } +} diff --git a/src/test/java/poomasi/domain/farm/service/FarmServiceTest.java b/src/test/java/poomasi/domain/farm/service/FarmServiceTest.java index cbcb6935..cc9603f0 100644 --- a/src/test/java/poomasi/domain/farm/service/FarmServiceTest.java +++ b/src/test/java/poomasi/domain/farm/service/FarmServiceTest.java @@ -10,10 +10,13 @@ import poomasi.domain.farm.FarmTestHelper; import poomasi.domain.farm.entity.Farm; import poomasi.domain.farm.repository.FarmRepository; +import poomasi.global.error.BusinessError; +import poomasi.global.error.BusinessException; import java.util.Optional; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; import static org.mockito.BDDMockito.given; @ExtendWith(MockitoExtension.class) @@ -42,6 +45,19 @@ void should_successGetFarm_when_farmExist() { assertThat(result.getName()).isEqualTo(farm.getName()); assertThat(result.getStatus()).isEqualTo(farm.getStatus()); } + + @Test + @DisplayName("농장이 존재하지 않는 경우 예외를 발생시킨다") + void should_throwException_when_farmNotExist() { + // given + Long farmId = 1L; + given(farmRepository.findByIdAndDeletedAtIsNull(farmId)).willReturn(Optional.empty()); + + // when & then + assertThatThrownBy(() -> farmService.getFarmByFarmId(farmId)) + .isInstanceOf(BusinessException.class) + .hasFieldOrPropertyWithValue("businessError", BusinessError.FARM_NOT_FOUND); + } } }