Skip to content

Commit

Permalink
[KAN-104] 음식점 통합테스트 추가 - 카테고리 API 패키지 잘못된부분 수정 + 테스트코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkyoungdeok committed May 28, 2024
1 parent 44be743 commit f12dc76
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 21 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
@@ -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("test@gmail.com", 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 f12dc76

Please sign in to comment.