Skip to content

Commit

Permalink
test: 보호 동물 목록 조회 개선 버전의 api를 추가한다.
Browse files Browse the repository at this point in the history
  • Loading branch information
hseong3243 committed Dec 13, 2023
1 parent 9b02cc4 commit 5bc66b0
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ public ResponseEntity<FindAnimalsResponse> findAnimals(
));
}

@GetMapping("/v1/animals")
public ResponseEntity<FindAnimalsResponse> 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<FindAnimalsResponse> findAnimalsV2(
Pageable pageable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -23,4 +29,17 @@ public static Animal toAnimal(Shelter shelter, RegisterAnimalRequest registerAni
registerAnimalRequest.information(),
registerAnimalRequest.imageUrls());
}

public static FindAnimalsResponse resultToResponse(Page<FindAnimalsResult> result) {
List<FindAnimalResponse> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ public Page<FindAnimalsResult> findAnimalsV1_1(

Long count = query.select(animal.count())
.from(animal)
.join(animal.shelter)
.where(
animalTypeContains(type),
animalActiveContains(active),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FindAnimalsResult> result = animalRepository.findAnimalsV1_1(
type,
active,
neuteredFilter,
age,
gender,
size,
pageable
);
return AnimalMapper.resultToResponse(result);
}

@Transactional(readOnly = true)
public FindAnimalsResponse findAnimalsV2(
AnimalType type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,22 +200,22 @@ void findAnimalsByShelter() {
@DisplayName("findAnimals 실행 시")
class FindAnimalsTest {

String mockName = "animalName";
String mockInformation = "animalInformation";
String mockBreed = "animalBreed";
List<String> 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<String> 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(
Expand Down Expand Up @@ -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<FindAnimalsResult> 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
Expand Down

0 comments on commit 5bc66b0

Please sign in to comment.