From f12dc763853ceddf85aa89c06800778416db19da Mon Sep 17 00:00:00 2001 From: sinkyoungdeok Date: Tue, 28 May 2024 22:05:46 +0900 Subject: [PATCH] =?UTF-8?q?[KAN-104]=20=EC=9D=8C=EC=8B=9D=EC=A0=90=20?= =?UTF-8?q?=ED=86=B5=ED=95=A9=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20-=20=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC=20API=20?= =?UTF-8?q?=ED=8C=A8=ED=82=A4=EC=A7=80=20=EC=9E=98=EB=AA=BB=EB=90=9C?= =?UTF-8?q?=EB=B6=80=EB=B6=84=20=EC=88=98=EC=A0=95=20+=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/entity/Category.kt | 2 +- .../domain/service/GetCategoryService.kt | 12 +-- .../controller/GetCategoryController.kt | 10 +-- .../controller/dto/GetCategoryDto.kt | 6 +- .../controller/dto/common/CategoryDto.kt | 2 +- .../repository/CategoryRepository.kt | 4 +- .../repository/dto/RestaurantProjectionDto.kt | 2 +- .../controller/GetCategoryControllerTest.kt | 85 +++++++++++++++++++ .../controller/GetRestaurantControllerTest.kt | 4 +- 9 files changed, 106 insertions(+), 21 deletions(-) rename src/main/kotlin/com/restaurant/be/{restaurant => category}/domain/entity/Category.kt (90%) rename src/main/kotlin/com/restaurant/be/{restaurant => category}/domain/service/GetCategoryService.kt (51%) rename src/main/kotlin/com/restaurant/be/{restaurant => category}/presentation/controller/GetCategoryController.kt (78%) rename src/main/kotlin/com/restaurant/be/{restaurant => category}/presentation/controller/dto/GetCategoryDto.kt (52%) rename src/main/kotlin/com/restaurant/be/{restaurant => category}/presentation/controller/dto/common/CategoryDto.kt (74%) rename src/main/kotlin/com/restaurant/be/{restaurant => category}/repository/CategoryRepository.kt (53%) create mode 100644 src/test/kotlin/com/restaurant/be/category/presentation/controller/GetCategoryControllerTest.kt diff --git a/src/main/kotlin/com/restaurant/be/restaurant/domain/entity/Category.kt b/src/main/kotlin/com/restaurant/be/category/domain/entity/Category.kt similarity index 90% rename from src/main/kotlin/com/restaurant/be/restaurant/domain/entity/Category.kt rename to src/main/kotlin/com/restaurant/be/category/domain/entity/Category.kt index b84a1e1..963a698 100644 --- a/src/main/kotlin/com/restaurant/be/restaurant/domain/entity/Category.kt +++ b/src/main/kotlin/com/restaurant/be/category/domain/entity/Category.kt @@ -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 diff --git a/src/main/kotlin/com/restaurant/be/restaurant/domain/service/GetCategoryService.kt b/src/main/kotlin/com/restaurant/be/category/domain/service/GetCategoryService.kt similarity index 51% rename from src/main/kotlin/com/restaurant/be/restaurant/domain/service/GetCategoryService.kt rename to src/main/kotlin/com/restaurant/be/category/domain/service/GetCategoryService.kt index 71f1bc9..0ded16b 100644 --- a/src/main/kotlin/com/restaurant/be/restaurant/domain/service/GetCategoryService.kt +++ b/src/main/kotlin/com/restaurant/be/category/domain/service/GetCategoryService.kt @@ -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 @@ -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) } ) } diff --git a/src/main/kotlin/com/restaurant/be/restaurant/presentation/controller/GetCategoryController.kt b/src/main/kotlin/com/restaurant/be/category/presentation/controller/GetCategoryController.kt similarity index 78% rename from src/main/kotlin/com/restaurant/be/restaurant/presentation/controller/GetCategoryController.kt rename to src/main/kotlin/com/restaurant/be/category/presentation/controller/GetCategoryController.kt index d0198e7..bd3b409 100644 --- a/src/main/kotlin/com/restaurant/be/restaurant/presentation/controller/GetCategoryController.kt +++ b/src/main/kotlin/com/restaurant/be/category/presentation/controller/GetCategoryController.kt @@ -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 @@ -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 { + fun getCategories(): CommonResponse { val response = getCategoryService.getCategories() return CommonResponse.success(response) } diff --git a/src/main/kotlin/com/restaurant/be/restaurant/presentation/controller/dto/GetCategoryDto.kt b/src/main/kotlin/com/restaurant/be/category/presentation/controller/dto/GetCategoryDto.kt similarity index 52% rename from src/main/kotlin/com/restaurant/be/restaurant/presentation/controller/dto/GetCategoryDto.kt rename to src/main/kotlin/com/restaurant/be/category/presentation/controller/dto/GetCategoryDto.kt index 5eeb485..522f0c8 100644 --- a/src/main/kotlin/com/restaurant/be/restaurant/presentation/controller/dto/GetCategoryDto.kt +++ b/src/main/kotlin/com/restaurant/be/category/presentation/controller/dto/GetCategoryDto.kt @@ -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 ) diff --git a/src/main/kotlin/com/restaurant/be/restaurant/presentation/controller/dto/common/CategoryDto.kt b/src/main/kotlin/com/restaurant/be/category/presentation/controller/dto/common/CategoryDto.kt similarity index 74% rename from src/main/kotlin/com/restaurant/be/restaurant/presentation/controller/dto/common/CategoryDto.kt rename to src/main/kotlin/com/restaurant/be/category/presentation/controller/dto/common/CategoryDto.kt index bdd1e89..3124416 100644 --- a/src/main/kotlin/com/restaurant/be/restaurant/presentation/controller/dto/common/CategoryDto.kt +++ b/src/main/kotlin/com/restaurant/be/category/presentation/controller/dto/common/CategoryDto.kt @@ -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 diff --git a/src/main/kotlin/com/restaurant/be/restaurant/repository/CategoryRepository.kt b/src/main/kotlin/com/restaurant/be/category/repository/CategoryRepository.kt similarity index 53% rename from src/main/kotlin/com/restaurant/be/restaurant/repository/CategoryRepository.kt rename to src/main/kotlin/com/restaurant/be/category/repository/CategoryRepository.kt index 831b6e7..6564e00 100644 --- a/src/main/kotlin/com/restaurant/be/restaurant/repository/CategoryRepository.kt +++ b/src/main/kotlin/com/restaurant/be/category/repository/CategoryRepository.kt @@ -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 diff --git a/src/main/kotlin/com/restaurant/be/restaurant/repository/dto/RestaurantProjectionDto.kt b/src/main/kotlin/com/restaurant/be/restaurant/repository/dto/RestaurantProjectionDto.kt index 557778b..2f47161 100644 --- a/src/main/kotlin/com/restaurant/be/restaurant/repository/dto/RestaurantProjectionDto.kt +++ b/src/main/kotlin/com/restaurant/be/restaurant/repository/dto/RestaurantProjectionDto.kt @@ -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 diff --git a/src/test/kotlin/com/restaurant/be/category/presentation/controller/GetCategoryControllerTest.kt b/src/test/kotlin/com/restaurant/be/category/presentation/controller/GetCategoryControllerTest.kt new file mode 100644 index 0000000..cfd52d7 --- /dev/null +++ b/src/test/kotlin/com/restaurant/be/category/presentation/controller/GetCategoryControllerTest.kt @@ -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>() {} + val actualResult: CommonResponse = 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 "분식" + } + } + } +} diff --git a/src/test/kotlin/com/restaurant/be/restaurant/presentation/controller/GetRestaurantControllerTest.kt b/src/test/kotlin/com/restaurant/be/restaurant/presentation/controller/GetRestaurantControllerTest.kt index 2910ca9..e06cccf 100644 --- a/src/test/kotlin/com/restaurant/be/restaurant/presentation/controller/GetRestaurantControllerTest.kt +++ b/src/test/kotlin/com/restaurant/be/restaurant/presentation/controller/GetRestaurantControllerTest.kt @@ -4,6 +4,8 @@ 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 @@ -11,13 +13,11 @@ 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