Created
October 11, 2018 03:46
-
-
Save waterlink/55b85cfe8e04ddfe0e0c7bd44a38b66f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| plugins { | |
| id 'org.jetbrains.kotlin.jvm' version '1.2.61' | |
| } | |
| group 'testing-restful' | |
| version '1.0-SNAPSHOT' | |
| repositories { | |
| mavenCentral() | |
| } | |
| dependencies { | |
| compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8" | |
| compile 'com.fasterxml.jackson.core:jackson-databind:2.9.7' | |
| compile 'com.fasterxml.jackson.module:jackson-module-kotlin:2.9.+' | |
| testCompile 'junit:junit:4.12' | |
| testCompile 'org.assertj:assertj-core:3.11.1' | |
| } | |
| compileKotlin { | |
| kotlinOptions.jvmTarget = "1.8" | |
| } | |
| compileTestKotlin { | |
| kotlinOptions.jvmTarget = "1.8" | |
| } |
This file contains hidden or 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
| package myapp | |
| import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | |
| import com.fasterxml.jackson.module.kotlin.readValue | |
| import myapp.ExamplesTest.ExampleResponse.Kitten | |
| import org.assertj.core.api.Assertions.assertThat | |
| import org.intellij.lang.annotations.Language | |
| import org.junit.Test | |
| class ExamplesTest { | |
| private val mapper = jacksonObjectMapper() | |
| @Language("JSON") | |
| private val responseBody = """ | |
| { | |
| "name": "Billie", | |
| "kittens": [ | |
| { "id": 23, "name": "Bob" }, | |
| { "id": 42, "name": "Wild" } | |
| ] | |
| } | |
| """.trimIndent() | |
| data class ExampleResponse(val name: String, | |
| val kittens: List<Kitten>) { | |
| data class Kitten(val id: Long?, | |
| val name: String) | |
| } | |
| private val response = mapper.readValue<ExampleResponse>(responseBody) | |
| // Then match cat.kittens[*].id == [23, 42] | |
| // Then match cat.kittens[*].id contains 23 | |
| // Then match cat.kittens[*].id contains [42, 23] | |
| @Test | |
| fun `has kittens with proper ids`() { | |
| assertThat(response.kittens.map { it.id }).isEqualTo(listOf(23L, 42L)) | |
| assertThat(response.kittens.map { it.id }).contains(23L) | |
| assertThat(response.kittens.map { it.id }).containsExactlyInAnyOrder(42L, 23L) | |
| } | |
| // Then match each cat.kittens contains { id: '#number' } | |
| // For this, JSON parser will fail in the first place, because it is type-safe: | |
| // => InvalidFormatException: Cannot deserialize value of type `long` from String "oops" | |
| // Then match each cat.kittens contains { id: '#notnull', name: '#regex [A-Z][a-z]+' } | |
| @Test | |
| fun `has kittens with non-null id and proper name`() { | |
| response.kittens.forEach { | |
| assertThat(it.id).isNotNull() | |
| assertThat(it.name).matches("[A-Z][a-z]+".toPattern()) | |
| } | |
| } | |
| // * def isLessThanFifty = function(x) { return x < 50 } // > | |
| // Then match each cat.kittens contains { id: '#? isLessThanFifty(_)' } | |
| @Test | |
| fun `has kittens with ids less than fifty`() { | |
| response.kittens.forEach { | |
| assertThat(it.id).isLessThan(50) | |
| } | |
| } | |
| // * def expected = [{ id: 42, name: 'Wild' }, { id: 23, name: 'Bob' }] | |
| // * match cat == { name: 'Billie', kittens: '#(^^expected)' } | |
| @Test | |
| fun `has proper name and expected kittens`() { | |
| val expected = arrayOf(Kitten(id = 42, name = "Wild"), Kitten(id = 23, name = "Bob")) | |
| assertThat(response.name).isEqualTo("Billie") | |
| assertThat(response.kittens).containsExactlyInAnyOrder(*expected) | |
| } | |
| @Test | |
| fun `some "dynamic" stuff`() { | |
| // # find single kitten where id == 23 | |
| // * def bob = get[0] cat.kittens[?(@.id==23)] | |
| // * match bob.name == 'Bob' | |
| val bob = response.kittens.find { it.id == 23L }!! | |
| assertThat(bob.name).isEqualTo("Bob") | |
| // # using the karate object if the expression is dynamic | |
| // * def temp = karate.jsonPath(cat, "$.kittens[?(@.name=='" + bob.name + "')]") | |
| // * match temp[0] == bob | |
| // NOTE: for the example above, jsonPath is not needed | |
| val temp = response.kittens.filter { it.name == bob.name } | |
| assertThat(temp[0]).isEqualTo(bob) | |
| // # getting the first element of the array returned by a json-path expression in one step | |
| // * def temp = karate.jsonPath(cat, "$.kittens[?(@.name=='" + bob.name + "')]")[0] | |
| // * match temp == bob | |
| // NOTE: for the example above, jsonPath is not needed | |
| val temp2 = response.kittens.filter { it.name == bob.name }[0] | |
| assertThat(temp2).isEqualTo(bob) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment