From 5bc66b0ac4812e3767cd216134afd3df105d60aa Mon Sep 17 00:00:00 2001 From: hseong3243 Date: Wed, 13 Dec 2023 16:17:18 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=EB=B3=B4=ED=98=B8=20=EB=8F=99=EB=AC=BC?= =?UTF-8?q?=20=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=20=EA=B0=9C=EC=84=A0?= =?UTF-8?q?=20=EB=B2=84=EC=A0=84=EC=9D=98=20api=EB=A5=BC=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../animal/controller/AnimalController.java | 16 ++++++ .../domain/animal/mapper/AnimalMapper.java | 19 +++++++ .../repository/AnimalRepositoryImpl.java | 1 - .../domain/animal/service/AnimalService.java | 21 ++++++++ .../controller/AnimalControllerTest.java | 19 +++++++ .../animal/service/AnimalServiceTest.java | 49 ++++++++++++++----- 6 files changed, 111 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/clova/anifriends/domain/animal/controller/AnimalController.java b/src/main/java/com/clova/anifriends/domain/animal/controller/AnimalController.java index ed8d3281f..2fd07917d 100644 --- a/src/main/java/com/clova/anifriends/domain/animal/controller/AnimalController.java +++ b/src/main/java/com/clova/anifriends/domain/animal/controller/AnimalController.java @@ -88,6 +88,22 @@ public ResponseEntity findAnimals( )); } + @GetMapping("/v1/animals") + public ResponseEntity findAnimalsV1_1( + Pageable pageable, + @ModelAttribute FindAnimalsRequest findAnimalsRequest + ) { + return ResponseEntity.ok(animalService.findAnimalsV1_1( + findAnimalsRequest.type(), + findAnimalsRequest.active(), + findAnimalsRequest.neuteredFilter(), + findAnimalsRequest.age(), + findAnimalsRequest.gender(), + findAnimalsRequest.animalSize(), + pageable + )); + } + @GetMapping("/v2/animals") public ResponseEntity findAnimalsV2( Pageable pageable, diff --git a/src/main/java/com/clova/anifriends/domain/animal/mapper/AnimalMapper.java b/src/main/java/com/clova/anifriends/domain/animal/mapper/AnimalMapper.java index 8dc85bd0b..a0a1ef7e3 100644 --- a/src/main/java/com/clova/anifriends/domain/animal/mapper/AnimalMapper.java +++ b/src/main/java/com/clova/anifriends/domain/animal/mapper/AnimalMapper.java @@ -2,9 +2,15 @@ import com.clova.anifriends.domain.animal.Animal; import com.clova.anifriends.domain.animal.dto.request.RegisterAnimalRequest; +import com.clova.anifriends.domain.animal.dto.response.FindAnimalsResponse; +import com.clova.anifriends.domain.animal.dto.response.FindAnimalsResponse.FindAnimalResponse; +import com.clova.anifriends.domain.animal.repository.response.FindAnimalsResult; +import com.clova.anifriends.domain.common.PageInfo; import com.clova.anifriends.domain.shelter.Shelter; +import java.util.List; import lombok.AccessLevel; import lombok.NoArgsConstructor; +import org.springframework.data.domain.Page; @NoArgsConstructor(access = AccessLevel.PROTECTED) public final class AnimalMapper { @@ -23,4 +29,17 @@ public static Animal toAnimal(Shelter shelter, RegisterAnimalRequest registerAni registerAnimalRequest.information(), registerAnimalRequest.imageUrls()); } + + public static FindAnimalsResponse resultToResponse(Page result) { + List findAnimals = result.map(animal -> new FindAnimalResponse( + animal.getAnimalId(), + animal.getAnimalName(), + animal.getShelterName(), + animal.getShelterAddress(), + animal.getAnimalImageUrl() + )) + .toList(); + PageInfo pageInfo = PageInfo.of(result.getTotalElements(), result.hasNext()); + return new FindAnimalsResponse(pageInfo, findAnimals); + } } diff --git a/src/main/java/com/clova/anifriends/domain/animal/repository/AnimalRepositoryImpl.java b/src/main/java/com/clova/anifriends/domain/animal/repository/AnimalRepositoryImpl.java index 20c68d4db..66460de58 100644 --- a/src/main/java/com/clova/anifriends/domain/animal/repository/AnimalRepositoryImpl.java +++ b/src/main/java/com/clova/anifriends/domain/animal/repository/AnimalRepositoryImpl.java @@ -163,7 +163,6 @@ public Page findAnimalsV1_1( Long count = query.select(animal.count()) .from(animal) - .join(animal.shelter) .where( animalTypeContains(type), animalActiveContains(active), diff --git a/src/main/java/com/clova/anifriends/domain/animal/service/AnimalService.java b/src/main/java/com/clova/anifriends/domain/animal/service/AnimalService.java index 299bd1730..09aba0ad1 100644 --- a/src/main/java/com/clova/anifriends/domain/animal/service/AnimalService.java +++ b/src/main/java/com/clova/anifriends/domain/animal/service/AnimalService.java @@ -106,6 +106,27 @@ public FindAnimalsResponse findAnimals( return FindAnimalsResponse.from(animalsWithPagination); } + @Transactional(readOnly = true) + public FindAnimalsResponse findAnimalsV1_1( + AnimalType type, + AnimalActive active, + AnimalNeuteredFilter neuteredFilter, + AnimalAge age, + AnimalGender gender, + AnimalSize size, + Pageable pageable) { + Page result = animalRepository.findAnimalsV1_1( + type, + active, + neuteredFilter, + age, + gender, + size, + pageable + ); + return AnimalMapper.resultToResponse(result); + } + @Transactional(readOnly = true) public FindAnimalsResponse findAnimalsV2( AnimalType type, diff --git a/src/test/java/com/clova/anifriends/domain/animal/controller/AnimalControllerTest.java b/src/test/java/com/clova/anifriends/domain/animal/controller/AnimalControllerTest.java index 5e9e23c5e..47539bbe3 100644 --- a/src/test/java/com/clova/anifriends/domain/animal/controller/AnimalControllerTest.java +++ b/src/test/java/com/clova/anifriends/domain/animal/controller/AnimalControllerTest.java @@ -39,6 +39,7 @@ import com.clova.anifriends.domain.animal.dto.response.FindAnimalDetail; import com.clova.anifriends.domain.animal.dto.response.FindAnimalsByShelterResponse; import com.clova.anifriends.domain.animal.dto.response.FindAnimalsResponse; +import com.clova.anifriends.domain.animal.dto.response.FindAnimalsResponse.FindAnimalResponse; import com.clova.anifriends.domain.animal.dto.response.RegisterAnimalResponse; import com.clova.anifriends.domain.animal.repository.response.FindAnimalsResult; import com.clova.anifriends.domain.animal.support.fixture.AnimalDtoFixture; @@ -245,6 +246,24 @@ void findAnimalsByShelter() throws Exception { )); } + @Test + @DisplayName("보호 동물 목록 조회 v1 호출 시") + void findAnimalsV1_1() throws Exception { + //given + FindAnimalResponse findAnimalResponse = new FindAnimalResponse(1L, "동물 이름", "보호소 이름", + "보호소 주소", "imageUrl"); + FindAnimalsResponse findAnimalsResponse = new FindAnimalsResponse(PageInfo.of(1, false), + List.of(findAnimalResponse)); + given(animalService.findAnimalsV1_1(any(), any(), any(), any(), any(), any(), any())) + .willReturn(findAnimalsResponse); + + //when + ResultActions resultActions = mockMvc.perform(get("/api/v1/animals")); + + //then + resultActions.andExpect(status().isOk()); + } + @Test @DisplayName("보호 동물 조회 & 검색(봉사자) api 호출 시") void findAnimals() throws Exception { diff --git a/src/test/java/com/clova/anifriends/domain/animal/service/AnimalServiceTest.java b/src/test/java/com/clova/anifriends/domain/animal/service/AnimalServiceTest.java index 5e683ba66..7d314dfe2 100644 --- a/src/test/java/com/clova/anifriends/domain/animal/service/AnimalServiceTest.java +++ b/src/test/java/com/clova/anifriends/domain/animal/service/AnimalServiceTest.java @@ -200,22 +200,22 @@ void findAnimalsByShelter() { @DisplayName("findAnimals 실행 시") class FindAnimalsTest { + String mockName = "animalName"; + String mockInformation = "animalInformation"; + String mockBreed = "animalBreed"; + List mockImageUrls = List.of("www.aws.s3.com/2"); + + AnimalType typeFilter = AnimalType.DOG; + AnimalActive activeFilter = AnimalActive.ACTIVE; + AnimalNeuteredFilter neuteredFilter = AnimalNeuteredFilter.IS_NEUTERED; + AnimalAge ageFilter = AnimalAge.ADULT; + AnimalGender genderFilter = AnimalGender.MALE; + AnimalSize sizeFilter = AnimalSize.MEDIUM; + @Test @DisplayName("성공: 모든 필터 존재") - void findAnimals1() { + void findAnimals() { // given - String mockName = "animalName"; - String mockInformation = "animalInformation"; - String mockBreed = "animalBreed"; - List mockImageUrls = List.of("www.aws.s3.com/2"); - - AnimalType typeFilter = AnimalType.DOG; - AnimalActive activeFilter = AnimalActive.ACTIVE; - AnimalNeuteredFilter neuteredFilter = AnimalNeuteredFilter.IS_NEUTERED; - AnimalAge ageFilter = AnimalAge.ADULT; - AnimalGender genderFilter = AnimalGender.MALE; - AnimalSize sizeFilter = AnimalSize.MEDIUM; - Shelter shelter = ShelterFixture.shelter(); Animal matchAnimal = new Animal( @@ -251,6 +251,29 @@ void findAnimals1() { assertThat(result).usingRecursiveComparison().isEqualTo(expected); } + + @Test + @DisplayName("성공: v1.1 성공") + void findAnimalsV1_1() { + // given + PageRequest pageRequest = PageRequest.of(0, 10); + FindAnimalsResult result = new FindAnimalsResult(1L, mockName, LocalDateTime.now(), + "보호소 이름", "보호소 주소", "imageUrl"); + PageImpl animalPage = new PageImpl<>(List.of(result), pageRequest, + 1); + + given(animalRepository.findAnimalsV1_1(typeFilter, activeFilter, neuteredFilter, + ageFilter, genderFilter, sizeFilter, pageRequest)).willReturn(animalPage); + + // when + FindAnimalsResponse response = animalService.findAnimalsV1_1( + typeFilter, activeFilter, neuteredFilter, + ageFilter, genderFilter, sizeFilter, pageRequest); + + // then + assertThat(response.animals()).hasSize(1); + + } } @Nested