forked from micronaut-projects/micronaut-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more updated code samples with tests
- Loading branch information
Showing
18 changed files
with
290 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
test-suite-kotlin/src/test/kotlin/io/micronaut/docs/server/sse/Headline.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.micronaut.docs.server.sse | ||
|
||
// tag::class[] | ||
class Headline { | ||
var title: String? = null | ||
var description: String? = null | ||
|
||
constructor() {} | ||
|
||
constructor(title: String, description: String) { | ||
this.title = title | ||
this.description = description | ||
} | ||
} | ||
// end::class[] |
8 changes: 8 additions & 0 deletions
8
test-suite-kotlin/src/test/kotlin/io/micronaut/docs/session/Cart.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.micronaut.docs.session | ||
|
||
import java.util.ArrayList | ||
|
||
class Cart { | ||
|
||
var items: MutableList<String> = ArrayList() | ||
} |
58 changes: 58 additions & 0 deletions
58
test-suite-kotlin/src/test/kotlin/io/micronaut/docs/session/ShoppingController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package io.micronaut.docs.session | ||
|
||
// tag::imports[] | ||
|
||
import io.micronaut.http.annotation.Controller | ||
import io.micronaut.http.annotation.Get | ||
import io.micronaut.http.annotation.Post | ||
import io.micronaut.session.Session | ||
import io.micronaut.session.annotation.SessionValue | ||
|
||
// end::imports[] | ||
|
||
// tag::class[] | ||
@Controller("/shopping") | ||
class ShoppingController { | ||
// end::class[] | ||
|
||
companion object { | ||
private const val ATTR_CART = "cart" // <1> | ||
} | ||
|
||
// tag::view[] | ||
@Get("/cart") | ||
@SessionValue(ATTR_CART) // <1> | ||
internal fun viewCart(@SessionValue cart: Cart?): Cart { // <2> | ||
var cart = cart | ||
if (cart == null) { | ||
cart = Cart() | ||
} | ||
return cart | ||
} | ||
// end::view[] | ||
|
||
// tag::add[] | ||
@Post("/cart/{name}") | ||
internal fun addItem(session: Session, name: String): Cart { // <2> | ||
require(name.isNotBlank()) { "Name cannot be blank" } | ||
val cart = session.get(ATTR_CART, Cart::class.java).orElseGet({ | ||
// <3> | ||
val newCart = Cart() | ||
session.put(ATTR_CART, newCart) // <4> | ||
newCart | ||
}) | ||
cart.items.add(name) | ||
return cart | ||
} | ||
// end::add[] | ||
|
||
// tag::clear[] | ||
@Post("/cart/clear") | ||
internal fun clearCart(session: Session?) { | ||
session?.remove(ATTR_CART) | ||
} | ||
// end::clear[] | ||
|
||
// tag::endclass[] | ||
} | ||
// end::endclass[] |
60 changes: 60 additions & 0 deletions
60
test-suite-kotlin/src/test/kotlin/io/micronaut/docs/session/ShoppingControllerSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package io.micronaut.docs.session | ||
|
||
import io.kotlintest.shouldBe | ||
import io.kotlintest.specs.StringSpec | ||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.http.HttpHeaders | ||
import io.micronaut.http.HttpRequest | ||
import io.micronaut.http.client.RxHttpClient | ||
import io.micronaut.runtime.server.EmbeddedServer | ||
import kotlin.test.assertNotNull | ||
|
||
|
||
class ShoppingControllerSpec: StringSpec() { | ||
|
||
val embeddedServer = autoClose( | ||
ApplicationContext.run(EmbeddedServer::class.java) | ||
) | ||
|
||
val client = autoClose( | ||
embeddedServer.applicationContext.createBean(RxHttpClient::class.java, embeddedServer.url) | ||
) | ||
|
||
init { | ||
"testSessionValueUsedOnReturnValue" { | ||
// tag::view[] | ||
var response = client.exchange(HttpRequest.GET<Cart>("/shopping/cart"), Cart::class.java) // <1> | ||
.blockingFirst() | ||
var cart = response.body() | ||
|
||
assertNotNull(response.header(HttpHeaders.AUTHORIZATION_INFO)) // <2> | ||
assertNotNull(cart) | ||
cart.items.isEmpty() | ||
// end::view[] | ||
|
||
// tag::add[] | ||
val sessionId = response.header(HttpHeaders.AUTHORIZATION_INFO) // <1> | ||
|
||
response = client.exchange( | ||
HttpRequest.POST("/shopping/cart/Apple", "") | ||
.header(HttpHeaders.AUTHORIZATION_INFO, sessionId), Cart::class.java) // <2> | ||
.blockingFirst() | ||
cart = response.body() | ||
// end::add[] | ||
|
||
assertNotNull(cart) | ||
cart.items.size shouldBe 1 | ||
|
||
response = client.exchange(HttpRequest.GET<Any>("/shopping/cart") | ||
.header(HttpHeaders.AUTHORIZATION_INFO, sessionId), Cart::class.java) | ||
.blockingFirst() | ||
cart = response.body() | ||
|
||
response.header(HttpHeaders.AUTHORIZATION_INFO) | ||
assertNotNull(cart) | ||
|
||
cart.items.size shouldBe 1 | ||
cart.items[0] shouldBe "Apple" | ||
} | ||
} | ||
} |
Oops, something went wrong.