Skip to content

Commit

Permalink
真偽値のアサートができるようになった (e2eはまだ)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoshiaki-Harada committed Oct 23, 2021
1 parent e695dc1 commit b1d83e2
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 15 deletions.
4 changes: 4 additions & 0 deletions e2e/specs/json/json_path.spec
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
## JsonPathで文字列をAssertする
tags: unimplemented
* "$.key1.key2"が文字列の"test"であることをアサー卜できる

## JsonPathで真偽値をAssertする
tags: unimplmented
* "$.key1.key2"が真偽値の"true"であることをアサー卜できる
11 changes: 9 additions & 2 deletions e2e/src/test/kotlin/com/uzabase/JsonPath.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@ package com.uzabase

import com.thoughtworks.gauge.Step
import org.amshove.kluent.`should not throw`
import org.amshove.kluent.fail
import org.amshove.kluent.invoking

class JsonPath: JsonAssert {
class JsonPath : JsonAssert {
@Step("<jsonPath>が文字列の<value>であることをアサー卜できる")
fun assertJsonPathString(jsonPath: String, value: String) {
val json = """"
{"key1": {"key2": "test"}}
"""".trimMargin()
invoking { json.assertByJsonPath(value, jsonPath) } `should not throw` AssertionError::class
}

@Step("<jsonPath>が真偽値の<value>であることをアサー卜できる")
fun assertJsonPathBoolean(jsonPath: String, value: Boolean) {
val json = """"
{"key1": {"key2": $value}}
"""".trimMargin()
invoking { json.assertByJsonPath(true, jsonPath) } `should not throw` AssertionError::class
}
}
7 changes: 6 additions & 1 deletion json-assert/src/main/kotlin/com/uzabase/JsonAssert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ interface JsonAssert {
}

fun String.assertByJsonPath(expected: String, jsonPath: String) {
val value = reader.getByJsonPath(this, jsonPath)
val value = reader.getStringByJsonPath(this, jsonPath)
expected shouldBeEqualTo value
}

fun String.assertByJsonPath(expected: Boolean, jsonPath: String) {
val value = reader.getBooleanByJsonPath(this, jsonPath)
expected shouldBeEqualTo value
}
}
12 changes: 2 additions & 10 deletions json-assert/src/main/kotlin/com/uzabase/JsonReader.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
package com.uzabase

import com.nfeld.jsonpathkt.JsonPath
import com.nfeld.jsonpathkt.extension.read

interface JsonReader {
fun getByJsonPath(json: String, path: String): String
fun getStringByJsonPath(json: String, path: String): String
fun getBooleanByJsonPath(json: String, path: String): Boolean
}

class JsonReaderImpl : JsonReader {
override fun getByJsonPath(json: String, path: String): String {
return JsonPath.parse(json)?.read(path)!!
}
}
19 changes: 19 additions & 0 deletions json-assert/src/main/kotlin/com/uzabase/JsonReaderImpl.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.uzabase

import com.nfeld.jsonpathkt.JsonPath
import com.nfeld.jsonpathkt.extension.read


class JsonReaderImpl : JsonReader {
override fun getStringByJsonPath(json: String, path: String): String {
return getValueByJsonPath(json, path)
}

override fun getBooleanByJsonPath(json: String, path: String): Boolean {
return getValueByJsonPath(json, path)
}

inline fun <reified T : Any> getValueByJsonPath(json: String, path: String): T {
return JsonPath.parse(json)?.read(path) ?: throw NotFoundJsonValueException(json, path)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.uzabase

data class NotFoundJsonValueException(
val json: String,
private val jsonPath: String
): Exception("Not found value in $json by: $jsonPath")
23 changes: 22 additions & 1 deletion json-assert/src/test/kotlin/com/uzabase/JsonAssertTest.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
package com.uzabase

import org.amshove.kluent.AnyException
import org.amshove.kluent.internal.ComparisonFailedException
import org.amshove.kluent.invoking
import org.amshove.kluent.shouldNotThrow
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows

internal class JsonAssertTest : JsonAssert {
@Test
fun JSONPathを指定して文字列をアサーションできる() {
fun 指定したJSONPathの値が期待した文字列の場合アサートの例外をスローしない() {
val json = """{ "test": "test" }"""
invoking { json.assertByJsonPath("test", "$.test") } shouldNotThrow AnyException
}

@Test
fun 指定したJSONPathの値が期待した文字列の場合アサートの例外をスローする() {
val json = """{ "test": "test" }"""
assertThrows<ComparisonFailedException> { json.assertByJsonPath("xxxx", "$.test") }
}

@Test
fun 指定したJSONPathの値が期待した真偽値の場合アサートの例外をスローしない() {
val json = """{ "test": true }"""
invoking { json.assertByJsonPath(true, "$.test") } shouldNotThrow AnyException
}

@Test
fun 指定したJSONPathの値が期待していない真偽値の場合アサートの例外をスローする() {
val json = """{ "test": false }"""
assertThrows<ComparisonFailedException> { json.assertByJsonPath(true, "$.test") }
}
}
15 changes: 14 additions & 1 deletion json-assert/src/test/kotlin/com/uzabase/JsonReaderImplTest.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
package com.uzabase

import org.amshove.kluent.invoking
import org.amshove.kluent.shouldBeEqualTo
import org.amshove.kluent.shouldThrow
import org.junit.jupiter.api.Test

internal class JsonReaderImplTest {

@Test
fun JSONPathで指定したKeyの値が存在しないとき例外をスローする() {
invoking { JsonReaderImpl().getValueByJsonPath<String>("""{"test": "test"}""", "$.xxx") } shouldThrow NotFoundJsonValueException::class
}

@Test
fun JSONPathで指定したKeyの文字列を取得する() {
val actual = JsonReaderImpl().getByJsonPath("""{"test": "test"}""", "$.test")
val actual = JsonReaderImpl().getStringByJsonPath("""{"test": "test"}""", "$.test")
"test" shouldBeEqualTo actual
}

@Test
fun JSONPathで指定したKeyの真偽値を取得する() {
val actual = JsonReaderImpl().getBooleanByJsonPath("""{"test": true}""", "$.test")
true shouldBeEqualTo actual
}
}

0 comments on commit b1d83e2

Please sign in to comment.