Skip to content

Commit

Permalink
[KAN-104] 음식점 통합테스트 추가 - 카테고리 API 패키지 잘못된부분 수정 + 테스트코드 추가 (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkyoungdeok authored May 28, 2024
1 parent 44be743 commit 36eaa53
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.restaurant.be.restaurant.domain.entity
package com.restaurant.be.category.domain.entity

import javax.persistence.Column
import javax.persistence.Entity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.restaurant.be.restaurant.domain.service
package com.restaurant.be.category.domain.service

import com.restaurant.be.restaurant.presentation.controller.dto.GetCategoryResponse
import com.restaurant.be.restaurant.presentation.controller.dto.common.CategoryDto
import com.restaurant.be.restaurant.repository.CategoryRepository
import com.restaurant.be.category.presentation.controller.dto.GetCategoriesResponse
import com.restaurant.be.category.presentation.controller.dto.common.CategoryDto
import com.restaurant.be.category.repository.CategoryRepository
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

Expand All @@ -11,10 +11,10 @@ class GetCategoryService(
private val categoryRepository: CategoryRepository
) {
@Transactional(readOnly = true)
fun getCategories(): GetCategoryResponse {
fun getCategories(): GetCategoriesResponse {
val categories = categoryRepository.findAll()

return GetCategoryResponse(
return GetCategoriesResponse(
categories = categories.map { CategoryDto(it.id ?: 0, it.name) }
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.restaurant.be.restaurant.presentation.controller
package com.restaurant.be.category.presentation.controller

import com.restaurant.be.category.domain.service.GetCategoryService
import com.restaurant.be.category.presentation.controller.dto.GetCategoriesResponse
import com.restaurant.be.common.response.CommonResponse
import com.restaurant.be.restaurant.domain.service.GetCategoryService
import com.restaurant.be.restaurant.presentation.controller.dto.GetCategoryResponse
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.v3.oas.annotations.media.Content
Expand All @@ -26,9 +26,9 @@ class GetCategoryController(
@ApiResponse(
responseCode = "200",
description = "성공",
content = [Content(schema = Schema(implementation = GetCategoryResponse::class))]
content = [Content(schema = Schema(implementation = GetCategoriesResponse::class))]
)
fun getCategory(): CommonResponse<GetCategoryResponse> {
fun getCategories(): CommonResponse<GetCategoriesResponse> {
val response = getCategoryService.getCategories()
return CommonResponse.success(response)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
@file:Suppress("ktlint", "MatchingDeclarationName")

package com.restaurant.be.restaurant.presentation.controller.dto
package com.restaurant.be.category.presentation.controller.dto

import com.restaurant.be.restaurant.presentation.controller.dto.common.CategoryDto
import com.restaurant.be.category.presentation.controller.dto.common.CategoryDto
import io.swagger.v3.oas.annotations.media.Schema

data class GetCategoryResponse(
data class GetCategoriesResponse(
@Schema(description = "카테고리 리스트")
val categories: List<CategoryDto>
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.restaurant.be.restaurant.presentation.controller.dto.common
package com.restaurant.be.category.presentation.controller.dto.common

import io.swagger.v3.oas.annotations.media.Schema

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.restaurant.be.restaurant.repository
package com.restaurant.be.category.repository

import com.restaurant.be.restaurant.domain.entity.Category
import com.restaurant.be.category.domain.entity.Category
import org.springframework.data.jpa.repository.JpaRepository

interface CategoryRepository : JpaRepository<Category, Long>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.restaurant.be.restaurant.repository

import com.querydsl.core.types.dsl.PathBuilderFactory
import com.querydsl.jpa.impl.JPAQueryFactory
import com.restaurant.be.restaurant.domain.entity.QCategory.category
import com.restaurant.be.category.domain.entity.QCategory.category
import com.restaurant.be.restaurant.domain.entity.QMenu.menu
import com.restaurant.be.restaurant.domain.entity.QRestaurant.restaurant
import com.restaurant.be.restaurant.domain.entity.QRestaurantCategory.restaurantCategory
Expand Down Expand Up @@ -130,12 +130,8 @@ class RestaurantRepositoryCustomImpl(
userId: Long,
pageable: Pageable
): Page<RestaurantProjectionDto> {
val orderSpecifier = if (pageable.sort.isSorted) {
emptyList()
} else {
val restaurantLikePath = PathBuilderFactory().create(RestaurantLike::class.java)
listOf(restaurantLikePath.getNumber("id", Long::class.java).desc())
}
val restaurantLikePath = PathBuilderFactory().create(RestaurantLike::class.java)
val orderSpecifier = listOf(restaurantLikePath.getNumber("id", Long::class.java).desc())

val myLikeQuery = queryFactory
.select(restaurantLike.restaurantId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.restaurant.be.restaurant.repository.dto

import com.restaurant.be.restaurant.domain.entity.Category
import com.restaurant.be.category.domain.entity.Category
import com.restaurant.be.restaurant.domain.entity.Menu
import com.restaurant.be.restaurant.domain.entity.Restaurant
import com.restaurant.be.restaurant.presentation.controller.dto.common.RestaurantDetailDto
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.restaurant.be.category.presentation.controller

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.module.kotlin.KotlinModule
import com.restaurant.be.category.domain.entity.Category
import com.restaurant.be.category.presentation.controller.dto.GetCategoriesResponse
import com.restaurant.be.category.repository.CategoryRepository
import com.restaurant.be.common.CustomDescribeSpec
import com.restaurant.be.common.IntegrationTest
import com.restaurant.be.common.PageDeserializer
import com.restaurant.be.common.response.CommonResponse
import com.restaurant.be.common.util.setUpUser
import com.restaurant.be.restaurant.presentation.controller.dto.common.RestaurantDto
import com.restaurant.be.user.repository.UserRepository
import io.kotest.matchers.shouldBe
import org.springframework.data.domain.Page
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
import org.springframework.transaction.annotation.Transactional
import java.nio.charset.Charset

@IntegrationTest
@Transactional
class GetCategoryControllerTest(
private val mockMvc: MockMvc,
private val userRepository: UserRepository,
private val categoryRepository: CategoryRepository
) : CustomDescribeSpec() {
private val baseUrl = "/v1/restaurants/category"
private val objectMapper: ObjectMapper = ObjectMapper().registerModule(KotlinModule()).apply {
val module = SimpleModule()
module.addDeserializer(Page::class.java, PageDeserializer(RestaurantDto::class.java))
this.registerModule(module)
}

init {
beforeEach {
setUpUser("[email protected]", userRepository)
}

describe("#getCategories basic test") {
it("when data saved should return categories") {
// given
categoryRepository.saveAll(
listOf(
Category(name = "한식"),
Category(name = "중식"),
Category(name = "일식"),
Category(name = "양식"),
Category(name = "분식")
)
)

// when
val result = mockMvc.perform(
get(baseUrl)
).also {
println(it.andReturn().response.contentAsString)
}
.andExpect(MockMvcResultMatchers.status().isOk)
.andExpect(MockMvcResultMatchers.jsonPath("$.result").value("SUCCESS"))
.andReturn()

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

// then
actualResult.data!!.categories.size shouldBe 5
actualResult.data!!.categories[0].name shouldBe "한식"
actualResult.data!!.categories[1].name shouldBe "중식"
actualResult.data!!.categories[2].name shouldBe "일식"
actualResult.data!!.categories[3].name shouldBe "양식"
actualResult.data!!.categories[4].name shouldBe "분식"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.module.kotlin.KotlinModule
import com.restaurant.be.category.domain.entity.Category
import com.restaurant.be.category.repository.CategoryRepository
import com.restaurant.be.common.CustomDescribeSpec
import com.restaurant.be.common.IntegrationTest
import com.restaurant.be.common.PageDeserializer
import com.restaurant.be.common.response.CommonResponse
import com.restaurant.be.common.util.RestaurantDocument
import com.restaurant.be.common.util.RestaurantUtil
import com.restaurant.be.common.util.setUpUser
import com.restaurant.be.restaurant.domain.entity.Category
import com.restaurant.be.restaurant.domain.entity.RestaurantCategory
import com.restaurant.be.restaurant.domain.entity.RestaurantLike
import com.restaurant.be.restaurant.presentation.controller.dto.GetRestaurantResponse
import com.restaurant.be.restaurant.presentation.controller.dto.GetRestaurantsResponse
import com.restaurant.be.restaurant.presentation.controller.dto.common.RestaurantDto
import com.restaurant.be.restaurant.repository.CategoryRepository
import com.restaurant.be.restaurant.repository.RestaurantCategoryRepository
import com.restaurant.be.restaurant.repository.RestaurantLikeRepository
import com.restaurant.be.restaurant.repository.RestaurantRepository
Expand Down

0 comments on commit 36eaa53

Please sign in to comment.