Skip to content

test: add kotlin sample #282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions samples/restdocs-api-spec-sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
buildscript {
ext.kotlin_version = '2.1.20'
ext {
springBootVersion = '3.0.0'
}
Expand All @@ -9,16 +10,19 @@ buildscript {
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("com.epages:restdocs-api-spec-gradle-plugin:0.19.2")
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.epages.restdocs-api-spec'

sourceCompatibility = 17
targetCompatibility = 17
kotlin {
jvmToolchain(17)
}

repositories {
mavenCentral()
Expand All @@ -32,6 +36,7 @@ dependencies {
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
implementation('org.springframework.boot:spring-boot-starter-data-rest')
implementation('org.springframework.boot:spring-boot-starter-validation')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
runtimeOnly('com.h2database:h2')

testImplementation('org.junit.jupiter:junit-jupiter-engine')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@SpringBootTest
@Transactional
@AutoConfigureMockMvc
class BaseIntegrationTest {
public class BaseIntegrationTest {

@Autowired MockMvc mockMvc;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package com.epages.restdocs.apispec.sample

import com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper.document
import com.epages.restdocs.apispec.ResourceDocumentation.parameterWithName
import com.epages.restdocs.apispec.ResourceDocumentation.resource
import com.epages.restdocs.apispec.ResourceSnippetParameters.Companion.builder
import org.hamcrest.Matchers
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs
import org.springframework.data.rest.webmvc.RestMediaTypes
import org.springframework.http.HttpHeaders
import org.springframework.restdocs.hypermedia.HypermediaDocumentation
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders
import org.springframework.restdocs.payload.PayloadDocumentation
import org.springframework.restdocs.snippet.Snippet
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.test.web.servlet.result.MockMvcResultHandlers
import org.springframework.test.web.servlet.result.MockMvcResultMatchers

@AutoConfigureRestDocs
@ExtendWith(SpringExtension::class)
class CartIntegrationKtTest : BaseIntegrationTest() {

private var cartId: String? = null

@Test
fun should_create_cart() {
whenCartIsCreated()

resultActions
.andExpect(MockMvcResultMatchers.status().isCreated())
.andDo(document(identifier = "carts-create", snippets = arrayOf<Snippet>(resource("Create a cart"))))
}

@Test
fun should_add_product_to_cart() {
givenCart()
givenProduct()

whenProductIsAddedToCart()

resultActions
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(
document(
identifier = "cart-add-product",
snippets = arrayOf<Snippet>(resource("Add products to a cart"))
)
)
}

@Test
fun should_get_cartKt() {
givenCartWithProduct()

whenCartIsRetrieved()

resultActions
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("products", Matchers.hasSize<Any>(1)))
.andExpect(MockMvcResultMatchers.jsonPath("products[0].quantity", Matchers.`is`(1)))
.andExpect(MockMvcResultMatchers.jsonPath("products[0].product.name", Matchers.notNullValue()))
.andExpect(MockMvcResultMatchers.jsonPath("total", Matchers.notNullValue()))
.andDo(
document(
identifier = "cart-get",
snippets = arrayOf<Snippet>(
resource(
builder()
.description("Get a cart by id")
.pathParameters(
parameterWithName("id").description("the cart id")
)
.responseFields(
PayloadDocumentation.fieldWithPath("total")
.description("Total amount of the cart."),
PayloadDocumentation.fieldWithPath("products")
.description("The product line item of the cart."),
PayloadDocumentation.subsectionWithPath("products[]._links.product")
.description("Link to the product."),
PayloadDocumentation.fieldWithPath("products[].quantity")
.description("The quantity of the line item."),
PayloadDocumentation.subsectionWithPath("products[].product")
.description("The product the line item relates to."),
PayloadDocumentation.subsectionWithPath("_links").description("Links section.")
)
.links(
HypermediaDocumentation.linkWithRel("self").ignored(),
HypermediaDocumentation.linkWithRel("order").description("Link to order the cart.")
)
.build()
)
)
)
)
}

@Test
fun should_order_cart() {
givenCartWithProduct()

whenCartIsOrdered()

resultActions
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(document(identifier = "cart-order", snippets = arrayOf<Snippet>(resource("Order a cart"))))
}

private fun whenProductIsAddedToCart() {
resultActions = mockMvc.perform(
RestDocumentationRequestBuilders.post("/carts/{id}/products", cartId)
.contentType(RestMediaTypes.TEXT_URI_LIST)
.content(entityLinks.linkForItemResource(Product::class.java, productId).toUri().toString())
)
}

private fun whenCartIsCreated() {
resultActions = mockMvc.perform(RestDocumentationRequestBuilders.post("/carts"))

val location = resultActions.andReturn().response.getHeader(HttpHeaders.LOCATION)
cartId = location!!.substring(location!!.lastIndexOf("/") + 1)
}

private fun whenCartIsRetrieved() {
resultActions = mockMvc.perform(
RestDocumentationRequestBuilders.get("/carts/{id}", cartId)
.accept(RestMediaTypes.HAL_JSON)
)
.andDo(MockMvcResultHandlers.print())
}

private fun whenCartIsOrdered() {
resultActions = mockMvc.perform(RestDocumentationRequestBuilders.post("/carts/{id}/order", cartId))
}

private fun givenCart() {
whenCartIsCreated()
resultActions.andExpect(MockMvcResultMatchers.status().isCreated())
}

private fun givenCartWithProduct() {
givenCart()
givenProduct()
whenProductIsAddedToCart()

resultActions.andExpect(MockMvcResultMatchers.status().isOk())
}
}
Loading