Skip to content

Commit

Permalink
[TEST] Farm 테스트 코드 작성 (#112)
Browse files Browse the repository at this point in the history
* test: 농장이 존재하지 않는 경우
#93

* chore: Member Builder 수정
- ID 추가 (테스트 용)
#93

* test: FarmFarmerServiceTest
- 농장 등록 테스트
- 농장 정보 업데이트 테스트
- 농장 삭제 테스트
#93

* test: FarmPlatformServiceTest
- ID로 농장 가져오기
- 농장 리스트 가져오기
#93
  • Loading branch information
stopmin authored Nov 9, 2024
1 parent 9872fe3 commit d12704e
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/poomasi/domain/member/entity/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
141 changes: 141 additions & 0 deletions src/test/java/poomasi/domain/farm/service/FarmFarmerServiceTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
105 changes: 105 additions & 0 deletions src/test/java/poomasi/domain/farm/service/FarmPlatformServiceTest.java
Original file line number Diff line number Diff line change
@@ -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<Farm> farmList = List.of(farm1, farm2);

given(farmService.getFarmList(pageable)).willReturn(farmList);

// when
List<FarmResponse> 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 호출 확인
}
}
}
16 changes: 16 additions & 0 deletions src/test/java/poomasi/domain/farm/service/FarmServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
}

}

0 comments on commit d12704e

Please sign in to comment.