diff --git a/build.gradle.kts b/build.gradle.kts index 1590a21..8a13719 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -30,6 +30,7 @@ dependencies { developmentOnly("org.springframework.boot:spring-boot-devtools") runtimeOnly("org.postgresql:postgresql") testImplementation("org.jetbrains.kotlin:kotlin-test-junit5") + testImplementation("org.mockito.kotlin:mockito-kotlin:5.4.0") testImplementation("org.springframework.boot:spring-boot-starter-test") } diff --git a/src/main/kotlin/com/example/demo/controllers/ShopController.kt b/src/main/kotlin/com/example/demo/controllers/ShopController.kt index b598bb0..ebf15ac 100644 --- a/src/main/kotlin/com/example/demo/controllers/ShopController.kt +++ b/src/main/kotlin/com/example/demo/controllers/ShopController.kt @@ -14,11 +14,9 @@ import org.springframework.web.bind.annotation.RestController class ShopController ( @field:Autowired private val shopProvider: ShopProvider, ) { - @GetMapping(value = ["/shop/common-info"], produces = ["application/json"] ) + @GetMapping(value = ["/shop/common-info"], produces = ["application/json"]) @ResponseBody fun commonInfo(response: HttpServletResponse): String { - response.contentType = "application/json" - val shop = shopProvider.getRandomShop() if (shop == null) { diff --git a/src/main/kotlin/com/example/demo/provider/html/HtmlProvider.kt b/src/main/kotlin/com/example/demo/provider/html/HtmlProvider.kt index 9facb99..9c9b792 100644 --- a/src/main/kotlin/com/example/demo/provider/html/HtmlProvider.kt +++ b/src/main/kotlin/com/example/demo/provider/html/HtmlProvider.kt @@ -19,7 +19,7 @@ fun renderProductTable(): String { } } - val products = getProducts() + val products = getInnerProducts() for ((i, product) in products.withIndex()) { tr { td (color = getCellColor(0, i+1)) { diff --git a/src/main/kotlin/com/example/demo/provider/html/InnerProduct.kt b/src/main/kotlin/com/example/demo/provider/html/InnerProduct.kt new file mode 100644 index 0000000..6a4f23d --- /dev/null +++ b/src/main/kotlin/com/example/demo/provider/html/InnerProduct.kt @@ -0,0 +1,11 @@ +package com.example.demo.provider.html + +data class InnerProduct(val description: String, val price: Double, val popularity: Int) + +fun getInnerProducts(): Set { + return setOf( + InnerProduct("one", 12.0, 12), + InnerProduct("two", 13.0, 20), + InnerProduct("three", 14.0, 50) + ) +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/demo/provider/html/Product.kt b/src/main/kotlin/com/example/demo/provider/html/Product.kt deleted file mode 100644 index 34211c4..0000000 --- a/src/main/kotlin/com/example/demo/provider/html/Product.kt +++ /dev/null @@ -1,11 +0,0 @@ -package com.example.demo.provider.html - -data class Product(val description: String, val price: Double, val popularity: Int) - -fun getProducts(): Set { - return setOf( - Product("one", 12.0, 12), - Product("two", 13.0, 20), - Product("three", 14.0, 50) - ) -} \ No newline at end of file diff --git a/src/test/kotlin/com/example/demo/DemoApplicationTests.kt b/src/test/kotlin/com/example/demo/DemoApplicationTests.kt deleted file mode 100644 index 2388354..0000000 --- a/src/test/kotlin/com/example/demo/DemoApplicationTests.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.example.demo - -import org.junit.jupiter.api.Test -import org.springframework.boot.test.context.SpringBootTest - -@SpringBootTest -class DemoApplicationTests { - - @Test - fun contextLoads() { - } - -} diff --git a/src/test/kotlin/com/example/demo/controllers/GreetingControllerTest.kt b/src/test/kotlin/com/example/demo/controllers/GreetingControllerTest.kt index 919267d..a9aacc7 100644 --- a/src/test/kotlin/com/example/demo/controllers/GreetingControllerTest.kt +++ b/src/test/kotlin/com/example/demo/controllers/GreetingControllerTest.kt @@ -11,7 +11,6 @@ import kotlin.test.Test @WebMvcTest(GreetingController::class) class GreetingControllerTest(@Autowired val mockMvc: MockMvc) { - @Test fun greetings_shouldSeeGreetingMessage() { mockMvc.perform(get("/greeting")) diff --git a/src/test/kotlin/com/example/demo/controllers/ShopControllerTest.kt b/src/test/kotlin/com/example/demo/controllers/ShopControllerTest.kt new file mode 100644 index 0000000..b01b8e2 --- /dev/null +++ b/src/test/kotlin/com/example/demo/controllers/ShopControllerTest.kt @@ -0,0 +1,58 @@ +package com.example.demo.controllers + +import com.example.demo.models.* +import com.example.demo.provider.ShopProvider +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.whenever +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest +import org.springframework.boot.test.mock.mockito.MockBean +import org.springframework.test.web.servlet.MockMvc +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get +import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content +import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status +import kotlin.test.Test + +@WebMvcTest(ShopController::class) +class ShopControllerTest(@Autowired val mockMvc: MockMvc) { + @MockBean + private lateinit var shopProvider: ShopProvider + + @Test + fun commonInfo_shouldSeeSuccessResponseJson() { + val shopMock = Shop(name="shop", customers= listOf( + Customer( + name = "cus-one", + city = City(name= "city-one"), + orders = listOf( + Order(products = listOf(Product(name = "one", price = 11.2)), isDelivered = false), + Order(products = listOf(Product(name = "two", price = 13.2)), isDelivered = false), + Order(products = listOf(Product(name = "three", price = 15.2)), isDelivered = true), + ) + ), + Customer( + name = "cus-two", + city = City(name= "city-two"), + orders = listOf( + Order(products = listOf(Product(name = "one", price = 12.2)), isDelivered = false), + Order(products = listOf(Product(name = "two", price = 13.2)), isDelivered = true), + Order(products = listOf(Product(name = "four", price = 14.2)), isDelivered = true), + ) + ), + )) + + val expectedJson: String = """{ + |"customers": {"withMoreUndeliveredOrdersThanDelivered": ["cus-one"]}, + |"products": {"orderedByAllCustomers": ["two"]} + |}""".trimMargin() + + whenever( + shopProvider.getRandomShop() + ) doReturn (shopMock) + + mockMvc.perform(get("/shop/common-info")) + .andExpect(status().isOk) + .andExpect(content().contentType("application/json")) + .andExpect(content().json(expectedJson)) + } +} \ No newline at end of file