From d8214a39448c27cf703ba335026bc7664f858ab7 Mon Sep 17 00:00:00 2001 From: Bob Sin Date: Wed, 29 May 2024 00:07:37 +0900 Subject: [PATCH] =?UTF-8?q?[KAN-104]=20=EC=9D=8C=EC=8B=9D=EC=A0=90=20?= =?UTF-8?q?=EC=9C=A0=EB=8B=9B=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20(=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BB=A4=EB=B2=84?= =?UTF-8?q?=EB=A6=AC=EC=A7=80=20100%)=20(#78)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/HelloController.kt | 13 --- .../repository/dto/RestaurantEsDocument.kt | 41 +------- .../be/restaurant/domain/entity/MenuTest.kt | 54 +++++++++++ .../domain/entity/RestaurantCategoryTest.kt | 25 +++++ .../domain/entity/RestaurantLikeTest.kt | 24 +++++ .../domain/entity/RestaurantTest.kt | 96 +++++++++++++++++++ .../dto/common/RestaurantEsDocumentTest.kt | 63 ++++++++++++ .../dto/RestaurantEsDocumentTest.kt | 72 ++++++++++++++ 8 files changed, 337 insertions(+), 51 deletions(-) create mode 100644 src/test/kotlin/com/restaurant/be/restaurant/domain/entity/MenuTest.kt create mode 100644 src/test/kotlin/com/restaurant/be/restaurant/domain/entity/RestaurantCategoryTest.kt create mode 100644 src/test/kotlin/com/restaurant/be/restaurant/domain/entity/RestaurantLikeTest.kt create mode 100644 src/test/kotlin/com/restaurant/be/restaurant/domain/entity/RestaurantTest.kt create mode 100644 src/test/kotlin/com/restaurant/be/restaurant/presentation/controller/dto/common/RestaurantEsDocumentTest.kt create mode 100644 src/test/kotlin/com/restaurant/be/restaurant/repository/dto/RestaurantEsDocumentTest.kt diff --git a/src/main/kotlin/com/restaurant/be/hello/presentation/controller/HelloController.kt b/src/main/kotlin/com/restaurant/be/hello/presentation/controller/HelloController.kt index 612d51d..3a71bfe 100644 --- a/src/main/kotlin/com/restaurant/be/hello/presentation/controller/HelloController.kt +++ b/src/main/kotlin/com/restaurant/be/hello/presentation/controller/HelloController.kt @@ -6,7 +6,6 @@ import com.restaurant.be.hello.presentation.dto.HelloRequest import com.restaurant.be.hello.presentation.dto.HelloResponse import io.swagger.annotations.ApiOperation import io.swagger.v3.oas.annotations.responses.ApiResponse -import org.springframework.security.access.prepost.PreAuthorize import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestBody @@ -38,16 +37,4 @@ class HelloController( val response = helloService.saveHello(request) return CommonResponse.success(response) } - - @PreAuthorize("hasRole('USER')") - @GetMapping("/hello/security-test") - @ApiOperation(value = "Hello API") - @ApiResponse( - responseCode = "200", - description = "성공" - ) - fun helloSecurityTest(): CommonResponse { - val response = helloService.findHellos() - return CommonResponse.success(response) - } } diff --git a/src/main/kotlin/com/restaurant/be/restaurant/repository/dto/RestaurantEsDocument.kt b/src/main/kotlin/com/restaurant/be/restaurant/repository/dto/RestaurantEsDocument.kt index 442f86e..535e06a 100644 --- a/src/main/kotlin/com/restaurant/be/restaurant/repository/dto/RestaurantEsDocument.kt +++ b/src/main/kotlin/com/restaurant/be/restaurant/repository/dto/RestaurantEsDocument.kt @@ -1,11 +1,7 @@ package com.restaurant.be.restaurant.repository.dto -import com.restaurant.be.restaurant.presentation.controller.dto.common.MenuDto -import com.restaurant.be.restaurant.presentation.controller.dto.common.RestaurantDetailDto -import com.restaurant.be.restaurant.presentation.controller.dto.common.RestaurantDto import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import org.springframework.data.jpa.domain.AbstractPersistable_.id @Serializable data class RestaurantEsDocument( @@ -14,37 +10,14 @@ data class RestaurantEsDocument( @SerialName("original_category") val originalCategory: String, @SerialName("naver_review_count") val naverReviewCount: Long, @SerialName("address") val address: String, - @SerialName("naver_rating_avg") val naverRating: Double?, + @SerialName("naver_rating_avg") val naverRatingAvg: Double?, @SerialName("image_url") val imageUrl: String, @SerialName("category") val category: String, @SerialName("discount_content") val discountContent: String?, @SerialName("menus") val menus: List, @SerialName("review_count") val reviewCount: Long, @SerialName("rating_avg") val ratingAvg: Double? -) { - fun toDto() = RestaurantDto( - id = id, - representativeImageUrl = imageUrl, - name = name, - ratingAvg = naverRating ?: 0.0, // RDB로 변경 필요 - reviewCount = naverReviewCount, // RDB로 변경 필요 - likeCount = 0, // RDB - categories = category.split(",").map { it.trim() }, - representativeMenu = menus.firstOrNull()?.toDto(), - representativeReviewContent = "", // RDB - isLike = false, // RDB - discountContent = discountContent, - longitude = 0.0, // RDB - latitude = 0.0, // RDB - naverRatingAvg = naverRating ?: 0.0, - naverReviewCount = naverReviewCount.toInt(), - detailInfo = RestaurantDetailDto( - contactNumber = "", - address = address, - menus = menus.map { it.toDto() } - ) - ) -} +) @Serializable data class MenuEsDocument( @@ -53,12 +26,4 @@ data class MenuEsDocument( @SerialName("price") val price: Int, @SerialName("description") val description: String, @SerialName("image_url") val imageUrl: String? = null -) { - fun toDto() = MenuDto( - name = menuName, - price = price, - description = description, - isRepresentative = false, - imageUrl = imageUrl - ) -} +) diff --git a/src/test/kotlin/com/restaurant/be/restaurant/domain/entity/MenuTest.kt b/src/test/kotlin/com/restaurant/be/restaurant/domain/entity/MenuTest.kt new file mode 100644 index 0000000..7ccfbda --- /dev/null +++ b/src/test/kotlin/com/restaurant/be/restaurant/domain/entity/MenuTest.kt @@ -0,0 +1,54 @@ +package com.restaurant.be.restaurant.domain.entity + +import io.kotest.core.spec.style.DescribeSpec +import io.kotest.matchers.shouldBe + +class MenuTest : DescribeSpec({ + + describe("Menu entity") { + it("should create a correct Menu instance") { + // Arrange + val menu = Menu( + id = 1L, + restaurantId = 1L, + name = "Pizza", + price = 1500, + description = "Delicious cheese pizza", + isRepresentative = true, + imageUrl = "http://example.com/pizza.jpg" + ) + + // Assert + menu.id shouldBe 1L + menu.restaurantId shouldBe 1L + menu.name shouldBe "Pizza" + menu.price shouldBe 1500 + menu.description shouldBe "Delicious cheese pizza" + menu.isRepresentative shouldBe true + menu.imageUrl shouldBe "http://example.com/pizza.jpg" + } + + it("should correctly convert to MenuDto") { + // Arrange + val menu = Menu( + id = 1L, + restaurantId = 1L, + name = "Pizza", + price = 1500, + description = "Delicious cheese pizza", + isRepresentative = true, + imageUrl = "http://example.com/pizza.jpg" + ) + + // Act + val menuDto = menu.toDto() + + // Assert + menuDto.name shouldBe "Pizza" + menuDto.price shouldBe 1500 + menuDto.description shouldBe "Delicious cheese pizza" + menuDto.isRepresentative shouldBe true + menuDto.imageUrl shouldBe "http://example.com/pizza.jpg" + } + } +}) diff --git a/src/test/kotlin/com/restaurant/be/restaurant/domain/entity/RestaurantCategoryTest.kt b/src/test/kotlin/com/restaurant/be/restaurant/domain/entity/RestaurantCategoryTest.kt new file mode 100644 index 0000000..628a874 --- /dev/null +++ b/src/test/kotlin/com/restaurant/be/restaurant/domain/entity/RestaurantCategoryTest.kt @@ -0,0 +1,25 @@ +package com.restaurant.be.restaurant.domain.entity + +import io.kotest.core.spec.style.DescribeSpec +import io.kotest.matchers.shouldBe + +class RestaurantCategoryTest : DescribeSpec({ + + describe("RestaurantCategory") { + it("should create a correct RestaurantCategory instance") { + // Given + val restaurantCategory = RestaurantCategory( + id = 1L, + restaurantId = 1, + categoryId = 1 + ) + + // When + + // Then + restaurantCategory.id shouldBe 1L + restaurantCategory.restaurantId shouldBe 1 + restaurantCategory.categoryId shouldBe 1 + } + } +}) diff --git a/src/test/kotlin/com/restaurant/be/restaurant/domain/entity/RestaurantLikeTest.kt b/src/test/kotlin/com/restaurant/be/restaurant/domain/entity/RestaurantLikeTest.kt new file mode 100644 index 0000000..da817b6 --- /dev/null +++ b/src/test/kotlin/com/restaurant/be/restaurant/domain/entity/RestaurantLikeTest.kt @@ -0,0 +1,24 @@ +package com.restaurant.be.restaurant.domain.entity + +import io.kotest.core.spec.style.DescribeSpec +import io.kotest.matchers.shouldBe + +class RestaurantLikeTest : DescribeSpec({ + describe("RestaurantLike") { + it("should create a correct RestaurantLike instance") { + // Given + val restaurantLike = RestaurantLike( + id = 1L, + restaurantId = 1, + userId = 1 + ) + + // When + + // Then + restaurantLike.id shouldBe 1L + restaurantLike.restaurantId shouldBe 1 + restaurantLike.userId shouldBe 1 + } + } +}) diff --git a/src/test/kotlin/com/restaurant/be/restaurant/domain/entity/RestaurantTest.kt b/src/test/kotlin/com/restaurant/be/restaurant/domain/entity/RestaurantTest.kt new file mode 100644 index 0000000..08ee691 --- /dev/null +++ b/src/test/kotlin/com/restaurant/be/restaurant/domain/entity/RestaurantTest.kt @@ -0,0 +1,96 @@ +package com.restaurant.be.restaurant.domain.entity + +import io.kotest.core.spec.style.DescribeSpec +import io.kotest.matchers.doubles.shouldBeExactly +import io.kotest.matchers.shouldBe + +class RestaurantTest : DescribeSpec({ + describe("Restaurant entity") { + lateinit var restaurant: Restaurant + + beforeEach { + restaurant = Restaurant( + id = 1L, + name = "Test Restaurant", + originalCategories = "Category1", + reviewCount = 0, + likeCount = 0, + address = "123 Test St", + contactNumber = "123-456-7890", + ratingAvg = 0.0, + representativeImageUrl = "http://example.com/image.jpg", + viewCount = 0, + discountContent = null, + longitude = 0.0, + latitude = 0.0, + naverRatingAvg = 0.0, + naverReviewCount = 0, + menus = mutableListOf() + ) + } + context("should create a correct Restaurant instance") { + it("should create a correct Restaurant instance") { + restaurant.id shouldBe 1L + restaurant.name shouldBe "Test Restaurant" + restaurant.originalCategories shouldBe "Category1" + restaurant.reviewCount shouldBe 0 + restaurant.likeCount shouldBe 0 + restaurant.address shouldBe "123 Test St" + restaurant.contactNumber shouldBe "123-456-7890" + restaurant.ratingAvg shouldBeExactly 0.0 + restaurant.representativeImageUrl shouldBe "http://example.com/image.jpg" + restaurant.viewCount shouldBe 0 + restaurant.discountContent shouldBe null + restaurant.longitude shouldBeExactly 0.0 + restaurant.latitude shouldBeExactly 0.0 + restaurant.naverRatingAvg shouldBeExactly 0.0 + restaurant.naverReviewCount shouldBe 0 + restaurant.menus shouldBe mutableListOf() + } + } + + context("createReview") { + it("should update ratingAvg and reviewCount when a new review is added") { + restaurant.createReview(4.0) + restaurant.reviewCount shouldBe 1 + restaurant.ratingAvg shouldBeExactly 4.0 + + restaurant.createReview(2.0) + restaurant.reviewCount shouldBe 2 + restaurant.ratingAvg shouldBeExactly 3.0 + } + } + + context("deleteReview") { + it("should update ratingAvg and reviewCount when a review is deleted") { + // Add initial reviews + restaurant.createReview(4.0) + restaurant.createReview(2.0) + + restaurant.deleteReview(4.0) + restaurant.reviewCount shouldBe 1 + restaurant.ratingAvg shouldBeExactly 2.0 + + restaurant.deleteReview(2.0) + restaurant.reviewCount shouldBe 0 + restaurant.ratingAvg shouldBeExactly 0.0 + } + } + + context("updateReview") { + it("should update ratingAvg when a review is updated") { + // Add initial reviews + restaurant.createReview(4.0) + restaurant.createReview(2.0) + + restaurant.updateReview(2.0, 5.0) + restaurant.reviewCount shouldBe 2 + restaurant.ratingAvg shouldBeExactly 4.5 + + restaurant.updateReview(4.0, 3.0) + restaurant.reviewCount shouldBe 2 + restaurant.ratingAvg shouldBeExactly 4.0 + } + } + } +}) diff --git a/src/test/kotlin/com/restaurant/be/restaurant/presentation/controller/dto/common/RestaurantEsDocumentTest.kt b/src/test/kotlin/com/restaurant/be/restaurant/presentation/controller/dto/common/RestaurantEsDocumentTest.kt new file mode 100644 index 0000000..f24824f --- /dev/null +++ b/src/test/kotlin/com/restaurant/be/restaurant/presentation/controller/dto/common/RestaurantEsDocumentTest.kt @@ -0,0 +1,63 @@ +package com.restaurant.be.restaurant.presentation.controller.dto.common + +import com.restaurant.be.restaurant.repository.dto.RestaurantEsDocument +import io.kotest.core.spec.style.DescribeSpec +import io.kotest.matchers.shouldBe + +class RestaurantEsDocumentTest : DescribeSpec({ + describe("MenuDto") { + it("should create a correct RestaurantEsDocument instance") { + // Given + val restaurantEsDocument = RestaurantEsDocument( + id = 1L, + name = "Pizza", + originalCategory = "Italian", + naverReviewCount = 100, + address = "Seoul", + naverRatingAvg = 4.5, + imageUrl = "http://example.com/pizza.jpg", + category = "Italian", + discountContent = "10% off", + menus = mutableListOf(), + reviewCount = 1L, + ratingAvg = 4.5 + ) + + // When + + // Then + restaurantEsDocument.id shouldBe 1L + restaurantEsDocument.name shouldBe "Pizza" + restaurantEsDocument.originalCategory shouldBe "Italian" + restaurantEsDocument.naverReviewCount shouldBe 100 + restaurantEsDocument.address shouldBe "Seoul" + restaurantEsDocument.naverRatingAvg shouldBe 4.5 + restaurantEsDocument.imageUrl shouldBe "http://example.com/pizza.jpg" + restaurantEsDocument.category shouldBe "Italian" + restaurantEsDocument.discountContent shouldBe "10% off" + restaurantEsDocument.menus shouldBe mutableListOf() + restaurantEsDocument.reviewCount shouldBe 1L + restaurantEsDocument.ratingAvg shouldBe 4.5 + } + + it("should create a correct MenuDto instance") { + // Given + val menuEsDocument = MenuDto( + name = "Pasta", + price = 15000, + description = "Delicious pasta", + isRepresentative = true, + imageUrl = "http://example.com/pasta.jpg" + ) + + // When + + // Then + menuEsDocument.name shouldBe "Pasta" + menuEsDocument.price shouldBe 15000 + menuEsDocument.description shouldBe "Delicious pasta" + menuEsDocument.isRepresentative shouldBe true + menuEsDocument.imageUrl shouldBe "http://example.com/pasta.jpg" + } + } +}) diff --git a/src/test/kotlin/com/restaurant/be/restaurant/repository/dto/RestaurantEsDocumentTest.kt b/src/test/kotlin/com/restaurant/be/restaurant/repository/dto/RestaurantEsDocumentTest.kt new file mode 100644 index 0000000..ec00e77 --- /dev/null +++ b/src/test/kotlin/com/restaurant/be/restaurant/repository/dto/RestaurantEsDocumentTest.kt @@ -0,0 +1,72 @@ +package com.restaurant.be.restaurant.repository.dto + +import io.kotest.core.spec.style.DescribeSpec +import io.kotest.matchers.shouldBe + +class RestaurantEsDocumentTest : DescribeSpec({ + describe("RestaurantEsDocument") { + it("should create a correct RestaurantEsDocument instance") { + // Given + val menuEsDocument = MenuEsDocument( + restaurantId = 1, + menuName = "Pasta", + price = 15000, + description = "Delicious pasta", + imageUrl = "http://example.com/pasta.jpg" + ) + val restaurantEsDocument = RestaurantEsDocument( + id = 1L, + name = "Test Restaurant", + originalCategory = "Italian", + naverReviewCount = 100L, + address = "123 Test St", + naverRatingAvg = 4.5, + imageUrl = "http://example.com/restaurant.jpg", + category = "Italian, Pizza", + discountContent = "10% off", + menus = listOf(menuEsDocument), + reviewCount = 200L, + ratingAvg = 4.5 + ) + + // When + + // Then + restaurantEsDocument.id shouldBe 1L + restaurantEsDocument.name shouldBe "Test Restaurant" + restaurantEsDocument.originalCategory shouldBe "Italian" + restaurantEsDocument.naverReviewCount shouldBe 100L + restaurantEsDocument.address shouldBe "123 Test St" + restaurantEsDocument.naverRatingAvg shouldBe 4.5 + restaurantEsDocument.imageUrl shouldBe "http://example.com/restaurant.jpg" + restaurantEsDocument.category shouldBe "Italian, Pizza" + restaurantEsDocument.discountContent shouldBe "10% off" + restaurantEsDocument.menus.size shouldBe 1 + restaurantEsDocument.menus[0].menuName shouldBe "Pasta" + restaurantEsDocument.reviewCount shouldBe 200L + restaurantEsDocument.ratingAvg shouldBe 4.5 + } + } + + describe("MenuEsDocument") { + it("should create a correct MenuEsDocument instance") { + // Given + val menuEsDocument = MenuEsDocument( + restaurantId = 1, + menuName = "Pasta", + price = 15000, + description = "Delicious pasta", + imageUrl = "http://example.com/pasta.jpg" + ) + + // When + + // Then + menuEsDocument.restaurantId shouldBe 1 + menuEsDocument.menuName shouldBe "Pasta" + menuEsDocument.price shouldBe 15000 + menuEsDocument.description shouldBe "Delicious pasta" + menuEsDocument.imageUrl shouldBe "http://example.com/pasta.jpg" + } + } +})