Skip to content

Commit

Permalink
[KAN-91] 페이지네이션 안되는 현상 수정 + 테스트코드
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkyoungdeok committed May 25, 2024
1 parent 802a494 commit 1901319
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/main/kotlin/com/restaurant/be/common/config/SwaggerConfig.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.restaurant.be.common.config

import com.fasterxml.classmate.TypeResolver
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.data.domain.Pageable
import org.springframework.http.HttpMethod
import springfox.documentation.builders.ApiInfoBuilder
import springfox.documentation.builders.PathSelectors
import springfox.documentation.builders.RequestHandlerSelectors
import springfox.documentation.builders.ResponseBuilder
import springfox.documentation.schema.AlternateTypeRule
import springfox.documentation.service.ApiInfo
import springfox.documentation.service.ApiKey
import springfox.documentation.service.AuthorizationScope
Expand All @@ -22,7 +25,9 @@ import java.security.Principal

@Configuration
@EnableSwagger2
class SwaggerConfig {
class SwaggerConfig(
private val typeResolver: TypeResolver
) {
@Bean
fun api(): Docket {
val commonResponse = setCommonResponse()
Expand All @@ -32,6 +37,13 @@ class SwaggerConfig {
.globalResponses(HttpMethod.PUT, commonResponse)
.globalResponses(HttpMethod.PATCH, commonResponse)
.globalResponses(HttpMethod.DELETE, commonResponse)
.alternateTypeRules(
AlternateTypeRule(
typeResolver.resolve(Pageable::class.java),
typeResolver.resolve(PageModel::class.java)
)
)
.consumes(getConsumeContentTypes()).produces(getProduceContentTypes())
.apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.restaurant.be"))
.paths(PathSelectors.ant("/**"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1455,7 +1455,7 @@ class GetRestaurantControllerTest(
}
}

describe("#get restaurants compiste filter test") {
describe("#get restaurants composite filter test") {
it("when all filter should return restaurant") {
// given
val user = userRepository.findByEmail("test@gmail.com")
Expand Down Expand Up @@ -1623,5 +1623,168 @@ class GetRestaurantControllerTest(
actualResult.data!!.restaurants.content.size shouldBe 0
}
}

describe("#get restaurants pagination test") {
it("when no data and set size 1 should return empty") {
// given
// when
val result = mockMvc.perform(
get(restaurantUrl)
.param("size", "1")
)
.also {
println(it.andReturn().response.contentAsString)
}
.andExpect(status().isOk)
.andExpect(jsonPath("$.result").value("SUCCESS"))
.andReturn()

val responseContent = result.response.getContentAsString(Charset.forName("UTF-8"))
val responseType =
object : TypeReference<CommonResponse<GetRestaurantsResponse>>() {}
val actualResult: CommonResponse<GetRestaurantsResponse> = objectMapper.readValue(
responseContent,
responseType
)

// then
actualResult.data!!.restaurants.content.size shouldBe 0
}

it("when 1 data and set size 1 should return 1") {
// given
val restaurantEntity = RestaurantUtil.generateRestaurantEntity(
name = "목구멍 율전점"
)
restaurantRepository.save(restaurantEntity)
val restaurantDocument = RestaurantUtil.generateRestaurantDocument(
id = restaurantEntity.id,
name = "목구멍 율전점"
)
elasticsearchTemplate.save(restaurantDocument)
elasticsearchTemplate.indexOps(RestaurantDocument::class.java).refresh()

// when
val result = mockMvc.perform(
get(restaurantUrl)
.param("size", "1")
)
.also {
println(it.andReturn().response.contentAsString)
}
.andExpect(status().isOk)
.andExpect(jsonPath("$.result").value("SUCCESS"))
.andReturn()

val responseContent = result.response.getContentAsString(Charset.forName("UTF-8"))
val responseType =
object : TypeReference<CommonResponse<GetRestaurantsResponse>>() {}
val actualResult: CommonResponse<GetRestaurantsResponse> = objectMapper.readValue(
responseContent,
responseType
)

// then
actualResult.data!!.restaurants.content.size shouldBe 1
actualResult.data!!.restaurants.content[0].name shouldBe "목구멍 율전점"
}

it("when 2 data and set size 1 page 0 should return 1's restaurant") {
// given
val restaurantEntity1 = RestaurantUtil.generateRestaurantEntity(
name = "목구멍 율전점1"
)
restaurantRepository.save(restaurantEntity1)
val restaurantDocument1 = RestaurantUtil.generateRestaurantDocument(
id = restaurantEntity1.id,
name = "목구멍 율전점1"
)
elasticsearchTemplate.save(restaurantDocument1)

val restaurantEntity2 = RestaurantUtil.generateRestaurantEntity(
name = "목구멍 율전점2"
)
restaurantRepository.save(restaurantEntity2)
val restaurantDocument2 = RestaurantUtil.generateRestaurantDocument(
id = restaurantEntity2.id,
name = "목구멍 율전점2"
)
elasticsearchTemplate.save(restaurantDocument2)
elasticsearchTemplate.indexOps(RestaurantDocument::class.java).refresh()

// when
val result = mockMvc.perform(
get(restaurantUrl)
.param("size", "1")
)
.also {
println(it.andReturn().response.contentAsString)
}
.andExpect(status().isOk)
.andExpect(jsonPath("$.result").value("SUCCESS"))
.andReturn()

val responseContent = result.response.getContentAsString(Charset.forName("UTF-8"))
val responseType =
object : TypeReference<CommonResponse<GetRestaurantsResponse>>() {}
val actualResult: CommonResponse<GetRestaurantsResponse> = objectMapper.readValue(
responseContent,
responseType
)

// then
actualResult.data!!.restaurants.content.size shouldBe 1
actualResult.data!!.restaurants.content[0].name shouldBe "목구멍 율전점1"
}

it("when 2 data and set size 1 page 2 should return 2's restaurant") {
// given
val restaurantEntity1 = RestaurantUtil.generateRestaurantEntity(
name = "목구멍 율전점1"
)
restaurantRepository.save(restaurantEntity1)
val restaurantDocument1 = RestaurantUtil.generateRestaurantDocument(
id = restaurantEntity1.id,
name = "목구멍 율전점1"
)
elasticsearchTemplate.save(restaurantDocument1)

val restaurantEntity2 = RestaurantUtil.generateRestaurantEntity(
name = "목구멍 율전점2"
)
restaurantRepository.save(restaurantEntity2)
val restaurantDocument2 = RestaurantUtil.generateRestaurantDocument(
id = restaurantEntity2.id,
name = "목구멍 율전점2"
)
elasticsearchTemplate.save(restaurantDocument2)
elasticsearchTemplate.indexOps(RestaurantDocument::class.java).refresh()

// when
val result = mockMvc.perform(
get(restaurantUrl)
.param("size", "1")
.param("page", "1")
)
.also {
println(it.andReturn().response.contentAsString)
}
.andExpect(status().isOk)
.andExpect(jsonPath("$.result").value("SUCCESS"))
.andReturn()

val responseContent = result.response.getContentAsString(Charset.forName("UTF-8"))
val responseType =
object : TypeReference<CommonResponse<GetRestaurantsResponse>>() {}
val actualResult: CommonResponse<GetRestaurantsResponse> = objectMapper.readValue(
responseContent,
responseType
)

// then
actualResult.data!!.restaurants.content.size shouldBe 1
actualResult.data!!.restaurants.content[0].name shouldBe "목구멍 율전점2"
}
}
}
}

0 comments on commit 1901319

Please sign in to comment.