From 8e553126079cee67949596ac497472315b2f1e1a Mon Sep 17 00:00:00 2001 From: MrAolen Date: Wed, 15 Jan 2020 15:34:21 +0100 Subject: [PATCH] Support single json object in validation --- .../oss/jcv/db/assertj/DbComparatorAssertJ.kt | 10 +++--- .../jcv/db/cassandra/DbComparatorCassandra.kt | 9 ++---- .../ekino/oss/jcv/db/DbComparatorAssert.kt | 9 ++---- .../ekino/oss/jcv/db/util/JsonConverter.kt | 31 +++++++------------ .../oss/jcv/db/util/JsonConverterTest.kt | 16 ++++++---- .../ekino/oss/jcv/db/jdbc/DbComparatorJDBC.kt | 9 ++---- .../ekino/oss/jcv/db/jdbc/MSSQLTypeTest.kt | 15 +++++++++ .../ekino/oss/jcv/db/jdbc/MySQLTypeTest.kt | 15 +++++++++ .../ekino/oss/jcv/db/jdbc/PostgresTypeTest.kt | 15 +++++++++ .../oss/jcv/db/mongo/DbComparatorMongo.kt | 11 +++---- .../ekino/oss/jcv/db/mongo/MongoAssertDB.kt | 10 +++--- 11 files changed, 88 insertions(+), 62 deletions(-) diff --git a/jcv-db-assertj-db/src/main/kotlin/com/ekino/oss/jcv/db/assertj/DbComparatorAssertJ.kt b/jcv-db-assertj-db/src/main/kotlin/com/ekino/oss/jcv/db/assertj/DbComparatorAssertJ.kt index 6144095..77d36b5 100644 --- a/jcv-db-assertj-db/src/main/kotlin/com/ekino/oss/jcv/db/assertj/DbComparatorAssertJ.kt +++ b/jcv-db-assertj-db/src/main/kotlin/com/ekino/oss/jcv/db/assertj/DbComparatorAssertJ.kt @@ -16,14 +16,13 @@ import com.ekino.oss.jcv.db.model.TableModel import com.ekino.oss.jcv.db.util.ConverterUtil import com.ekino.oss.jcv.db.util.JsonConverter import com.ekino.oss.jcv.db.util.JsonConverter.compareJsonAndLogResult -import com.ekino.oss.jcv.db.util.takeIfIsJson +import com.ekino.oss.jcv.db.util.JsonConverter.formatInput import org.assertj.core.api.AbstractAssert import org.assertj.db.type.Source import org.assertj.db.type.Table import org.json.JSONArray import org.json.JSONException import org.skyscreamer.jsonassert.JSONCompareMode -import java.io.IOException import java.io.InputStream import java.sql.DriverManager import java.util.HashMap @@ -65,12 +64,11 @@ class DbComparatorAssertJ( mapper ) - fun isValidAgainst(input: String) = input.takeIfIsJson()?.let { compareActualAndExcepted(it as JSONArray) } ?: throw DbAssertException( - "Unable to parse pg object to json" + fun isValidAgainst(input: String) = formatInput(input)?.let { compareActualAndExcepted(it) } ?: throw DbAssertException( + "Unable to parse expected result from string to json" ) - @Throws(IOException::class) - fun isValidAgainst(inputStream: InputStream) = compareActualAndExcepted(JsonConverter.loadJson(inputStream)) + fun isValidAgainst(inputStream: InputStream) = isValidAgainst(JsonConverter.loadFileAsString(inputStream)) private fun compareActualAndExcepted(expected: JSONArray) { isNotNull diff --git a/jcv-db-cassandra/src/main/kotlin/com/ekino/oss/jcv/db/cassandra/DbComparatorCassandra.kt b/jcv-db-cassandra/src/main/kotlin/com/ekino/oss/jcv/db/cassandra/DbComparatorCassandra.kt index 1e758f9..68c0211 100644 --- a/jcv-db-cassandra/src/main/kotlin/com/ekino/oss/jcv/db/cassandra/DbComparatorCassandra.kt +++ b/jcv-db-cassandra/src/main/kotlin/com/ekino/oss/jcv/db/cassandra/DbComparatorCassandra.kt @@ -9,10 +9,8 @@ import com.ekino.oss.jcv.db.cassandra.util.QueryConverter import com.ekino.oss.jcv.db.exception.DbAssertException import com.ekino.oss.jcv.db.util.JsonConverter import com.ekino.oss.jcv.db.util.JsonConverter.compareJsonAndLogResult -import com.ekino.oss.jcv.db.util.takeIfIsJson import org.json.JSONArray import org.skyscreamer.jsonassert.JSONCompareMode -import java.io.IOException import java.io.InputStream class DbComparatorCassandra( @@ -31,12 +29,11 @@ class DbComparatorCassandra( fun assertThatQuery(select: Select) = DBComparatorBuilder.create().build(select.asCql()) } - fun isValidAgainst(input: String) = input.takeIfIsJson()?.let { compareActualAndExcepted(it as JSONArray) } ?: throw DbAssertException( - "Unable to parse expected result from string to json array" + fun isValidAgainst(input: String) = JsonConverter.formatInput(input)?.let { compareActualAndExcepted(it) } ?: throw DbAssertException( + "Unable to parse expected result from string to json" ) - @Throws(IOException::class) - fun isValidAgainst(inputStream: InputStream) = compareActualAndExcepted(JsonConverter.loadJson(inputStream)) + fun isValidAgainst(inputStream: InputStream) = isValidAgainst(JsonConverter.loadFileAsString(inputStream)) private fun compareActualAndExcepted(expected: JSONArray) { val actualJson = queryConverter.fromQueryToTableModel(query).getTableModelAsJson(mapper) diff --git a/jcv-db-core/src/main/kotlin/com/ekino/oss/jcv/db/DbComparatorAssert.kt b/jcv-db-core/src/main/kotlin/com/ekino/oss/jcv/db/DbComparatorAssert.kt index 1c0f58a..eb1b53f 100644 --- a/jcv-db-core/src/main/kotlin/com/ekino/oss/jcv/db/DbComparatorAssert.kt +++ b/jcv-db-core/src/main/kotlin/com/ekino/oss/jcv/db/DbComparatorAssert.kt @@ -6,10 +6,8 @@ import com.ekino.oss.jcv.db.exception.DbAssertException import com.ekino.oss.jcv.db.model.RowModel import com.ekino.oss.jcv.db.model.TableModel import com.ekino.oss.jcv.db.util.JsonConverter -import com.ekino.oss.jcv.db.util.takeIfIsJson import org.json.JSONArray import org.skyscreamer.jsonassert.JSONCompareMode -import java.io.IOException import java.io.InputStream class DbComparatorAssert(private val actualJson: JSONArray, private val jsonComparator: JsonComparator) { @@ -34,12 +32,11 @@ class DbComparatorAssert(private val actualJson: JSONArray, private val jsonComp } } - fun isValidAgainst(input: String) = input.takeIfIsJson()?.let { compareActualAndExcepted(it as JSONArray) } ?: throw DbAssertException( - "Unable to parse pg object to json" + fun isValidAgainst(input: String) = JsonConverter.formatInput(input)?.let { compareActualAndExcepted(it) } ?: throw DbAssertException( + "Unable to parse expected result from string to json" ) - @Throws(IOException::class) - fun isValidAgainst(inputStream: InputStream) = compareActualAndExcepted(JsonConverter.loadJson(inputStream)) + fun isValidAgainst(inputStream: InputStream) = isValidAgainst(JsonConverter.loadFileAsString(inputStream)) private fun compareActualAndExcepted(expected: JSONArray) { JsonConverter.compareJsonAndLogResult(actualJson, expected, jsonComparator) diff --git a/jcv-db-core/src/main/kotlin/com/ekino/oss/jcv/db/util/JsonConverter.kt b/jcv-db-core/src/main/kotlin/com/ekino/oss/jcv/db/util/JsonConverter.kt index 190f7c1..d0bb2b1 100644 --- a/jcv-db-core/src/main/kotlin/com/ekino/oss/jcv/db/util/JsonConverter.kt +++ b/jcv-db-core/src/main/kotlin/com/ekino/oss/jcv/db/util/JsonConverter.kt @@ -1,7 +1,6 @@ package com.ekino.oss.jcv.db.util import com.ekino.oss.jcv.core.JsonComparator -import com.ekino.oss.jcv.db.exception.DbAssertException import com.fasterxml.jackson.databind.ObjectMapper import org.json.JSONArray import org.json.JSONException @@ -21,31 +20,25 @@ object JsonConverter { ?.also { fail(it.fail("", expected, actual).message) } } - @Throws(IOException::class) - fun loadJson(inputStream: InputStream): JSONArray { - val fileContent = fromInputStreamToString(inputStream).takeIfIsJson() - if (fileContent is JSONArray) { - return fileContent - } else { - throw DbAssertException("Unable to load JSON Array from input stream") - } + fun formatInput(input: String) = when (val json = input.takeIfIsJson()) { + is JSONObject -> JSONArray().put(json) + is JSONArray -> json + else -> null } @Throws(IOException::class) - private fun fromInputStreamToString(inputStream: InputStream) = inputStream.bufferedReader().use(BufferedReader::readText) + fun loadFileAsString(inputStream: InputStream) = inputStream.bufferedReader().use(BufferedReader::readText) } fun Any.toJsonObject() = JSONObject(ObjectMapper().writeValueAsString(this)) fun Any.toJsonArray() = JSONArray(ObjectMapper().writeValueAsString(this)) -fun String.takeIfIsJson(): Any? { - try { - val jsonObject = JSONTokener(this).nextValue() - if (jsonObject is String && jsonObject == this) { - return null - } - return jsonObject - } catch (e: JSONException) { - return null +fun String.takeIfIsJson() = try { + when (val json = JSONTokener(this).nextValue()) { + is String -> null + is JSONObject, is JSONArray -> json + else -> null } +} catch (e: JSONException) { + null } diff --git a/jcv-db-core/src/test/kotlin/com/ekino/oss/jcv/db/util/JsonConverterTest.kt b/jcv-db-core/src/test/kotlin/com/ekino/oss/jcv/db/util/JsonConverterTest.kt index 8aa5244..c8d46cb 100644 --- a/jcv-db-core/src/test/kotlin/com/ekino/oss/jcv/db/util/JsonConverterTest.kt +++ b/jcv-db-core/src/test/kotlin/com/ekino/oss/jcv/db/util/JsonConverterTest.kt @@ -2,15 +2,15 @@ package com.ekino.oss.jcv.db.util import assertk.assertThat import assertk.assertions.isEqualTo -import assertk.assertions.isFailure import assertk.assertions.isNull import com.ekino.oss.jcv.db.mapper.TypeMapper import com.ekino.oss.jcv.db.model.RowModel import com.ekino.oss.jcv.db.model.TableModel -import com.ekino.oss.jcv.db.util.JsonConverter.loadJson +import com.ekino.oss.jcv.db.util.JsonConverter.formatInput import org.json.JSONArray import org.json.JSONObject import org.junit.jupiter.api.Test +import java.io.BufferedReader class JsonConverterTest { @@ -31,18 +31,22 @@ class JsonConverterTest { @Test fun `Should load file and return a json array`() { - val expectedJsonArray = JSONArray("""[{ "id" : 1 }]""") - assertThat(loadJson(this::class.java.classLoader.getResourceAsStream("test-file.json")!!).toString()).isEqualTo(expectedJsonArray.toString()) + val expectedResult = JSONArray("""[{"id":1}]""") + val inputString = this::class.java.classLoader.getResourceAsStream("test-file.json")!!.bufferedReader().use(BufferedReader::readText) + assertThat(formatInput(inputString).toString()).isEqualTo(expectedResult.toString()) } @Test fun `Should get an error when trying to load a json object`() { - assertThat { loadJson(this::class.java.classLoader.getResourceAsStream("test-file-object.json")!!) }.isFailure() + val expectedResult = JSONArray("""[{"id":1}]""") + val inputString = this::class.java.classLoader.getResourceAsStream("test-file-object.json")!!.bufferedReader().use(BufferedReader::readText) + assertThat(formatInput(inputString).toString()).isEqualTo(expectedResult.toString()) } @Test fun `Should get an error when trying to load an invalid json`() { - assertThat { loadJson(this::class.java.classLoader.getResourceAsStream("test-file-invalid.json")!!) }.isFailure() + val inputString = this::class.java.classLoader.getResourceAsStream("test-file-invalid.json")!!.bufferedReader().use(BufferedReader::readText) + assertThat(formatInput(inputString)).isNull() } @Test diff --git a/jcv-db-jdbc/src/main/kotlin/com/ekino/oss/jcv/db/jdbc/DbComparatorJDBC.kt b/jcv-db-jdbc/src/main/kotlin/com/ekino/oss/jcv/db/jdbc/DbComparatorJDBC.kt index cdf2ba2..a322d1b 100644 --- a/jcv-db-jdbc/src/main/kotlin/com/ekino/oss/jcv/db/jdbc/DbComparatorJDBC.kt +++ b/jcv-db-jdbc/src/main/kotlin/com/ekino/oss/jcv/db/jdbc/DbComparatorJDBC.kt @@ -8,10 +8,8 @@ import com.ekino.oss.jcv.db.jdbc.util.DBComparatorBuilder import com.ekino.oss.jcv.db.jdbc.util.QueryConverter import com.ekino.oss.jcv.db.util.JsonConverter import com.ekino.oss.jcv.db.util.JsonConverter.compareJsonAndLogResult -import com.ekino.oss.jcv.db.util.takeIfIsJson import org.json.JSONArray import org.skyscreamer.jsonassert.JSONCompareMode -import java.io.IOException import java.io.InputStream import java.sql.Connection @@ -27,12 +25,11 @@ class DbComparatorJDBC constructor( fun assertThatQuery(query: String): DbComparatorJDBC = DBComparatorBuilder.create().build(query) } - fun isValidAgainst(input: String) = input.takeIfIsJson()?.let { compareActualAndExcepted(it as JSONArray) } ?: throw DbAssertException( - "Unable to parse string to json array" + fun isValidAgainst(input: String) = JsonConverter.formatInput(input)?.let { compareActualAndExcepted(it) } ?: throw DbAssertException( + "Unable to parse expected result from string to json" ) - @Throws(IOException::class) - fun isValidAgainst(inputStream: InputStream) = compareActualAndExcepted(JsonConverter.loadJson(inputStream)) + fun isValidAgainst(inputStream: InputStream) = isValidAgainst(JsonConverter.loadFileAsString(inputStream)) fun using(comparator: JsonComparator) = DbComparatorJDBC(query, queryConverter, comparator, customMapper) diff --git a/jcv-db-jdbc/src/test/kotlin/com/ekino/oss/jcv/db/jdbc/MSSQLTypeTest.kt b/jcv-db-jdbc/src/test/kotlin/com/ekino/oss/jcv/db/jdbc/MSSQLTypeTest.kt index c4e97d2..f01ee1e 100644 --- a/jcv-db-jdbc/src/test/kotlin/com/ekino/oss/jcv/db/jdbc/MSSQLTypeTest.kt +++ b/jcv-db-jdbc/src/test/kotlin/com/ekino/oss/jcv/db/jdbc/MSSQLTypeTest.kt @@ -317,6 +317,21 @@ class MSSQLTypeTest { .isValidAgainst(expected) } + @Test + fun `Should valid single row against json object`() { + val expected = // language=json + """ + { + "id": "642A00A5-18F2-47CF-9E5C-0161156FF0A0", + "criteria_number": 3, + "content": "content 4" + } + """.trimIndent() + + assertThatQuery("SELECT * FROM table_test WHERE id='642A00A5-18F2-47CF-9E5C-0161156FF0A0'") + .isValidAgainst(expected) + } + private fun customValidator(): JsonValidator { return validator { templatedValidator("custom_notempty", comparator { actual, expected -> diff --git a/jcv-db-jdbc/src/test/kotlin/com/ekino/oss/jcv/db/jdbc/MySQLTypeTest.kt b/jcv-db-jdbc/src/test/kotlin/com/ekino/oss/jcv/db/jdbc/MySQLTypeTest.kt index 5be3170..7b8910c 100644 --- a/jcv-db-jdbc/src/test/kotlin/com/ekino/oss/jcv/db/jdbc/MySQLTypeTest.kt +++ b/jcv-db-jdbc/src/test/kotlin/com/ekino/oss/jcv/db/jdbc/MySQLTypeTest.kt @@ -326,6 +326,21 @@ class MySQLTypeTest { .isValidAgainst(expected) } + @Test + fun `Should valid single row against json object`() { + val expected = // language=json + """ + { + "id": 1234, + "criteria_number": 0, + "content": "content 1" + } + """.trimIndent() + + assertThatQuery("SELECT * FROM table_test WHERE id=1234") + .isValidAgainst(expected) + } + private fun customValidator(): JsonValidator { return validator { templatedValidator("custom_notempty", comparator { actual, expected -> diff --git a/jcv-db-jdbc/src/test/kotlin/com/ekino/oss/jcv/db/jdbc/PostgresTypeTest.kt b/jcv-db-jdbc/src/test/kotlin/com/ekino/oss/jcv/db/jdbc/PostgresTypeTest.kt index 9b8c007..f949cde 100644 --- a/jcv-db-jdbc/src/test/kotlin/com/ekino/oss/jcv/db/jdbc/PostgresTypeTest.kt +++ b/jcv-db-jdbc/src/test/kotlin/com/ekino/oss/jcv/db/jdbc/PostgresTypeTest.kt @@ -309,6 +309,21 @@ class PostgresTypeTest { .isValidAgainst(expected) } + @Test + fun `Should valid single row against json object`() { + val expected = // language=json + """ + { + "id": "07621b34-35dc-4b8f-94f4-b0f7e98b4088", + "criteria_number": 0, + "content": "content 1" + } + """.trimIndent() + + assertThatQuery("SELECT * FROM table_test WHERE id='07621b34-35dc-4b8f-94f4-b0f7e98b4088'") + .isValidAgainst(expected) + } + private fun customValidator(): JsonValidator { return validator { templatedValidator("custom_notempty", comparator { actual, expected -> diff --git a/jcv-db-mongo/src/main/kotlin/com/ekino/oss/jcv/db/mongo/DbComparatorMongo.kt b/jcv-db-mongo/src/main/kotlin/com/ekino/oss/jcv/db/mongo/DbComparatorMongo.kt index 730550c..c1c7f36 100644 --- a/jcv-db-mongo/src/main/kotlin/com/ekino/oss/jcv/db/mongo/DbComparatorMongo.kt +++ b/jcv-db-mongo/src/main/kotlin/com/ekino/oss/jcv/db/mongo/DbComparatorMongo.kt @@ -7,12 +7,10 @@ import com.ekino.oss.jcv.db.mongo.util.DBComparatorBuilder import com.ekino.oss.jcv.db.mongo.util.MongoConverter import com.ekino.oss.jcv.db.util.JsonConverter import com.ekino.oss.jcv.db.util.JsonConverter.compareJsonAndLogResult -import com.ekino.oss.jcv.db.util.takeIfIsJson import com.mongodb.client.FindIterable import org.bson.Document import org.json.JSONArray import org.skyscreamer.jsonassert.JSONCompareMode -import java.io.IOException import java.io.InputStream class DbComparatorMongo( @@ -27,15 +25,14 @@ class DbComparatorMongo( fun assertThatCollection(collection: FindIterable) = DBComparatorBuilder.create().build(collection) @JvmStatic - fun assertThatCollection(document: Document) = DBComparatorBuilder.create().build(document) + fun assertThatCollection(document: Document?) = document?.let { DBComparatorBuilder.create().build(it) } ?: throw DbAssertException("Mongo document is null") } - fun isValidAgainst(input: String) = input.takeIfIsJson()?.let { compareActualAndExcepted(it as JSONArray) } ?: throw DbAssertException( - "Unable to parse expected result from string to json array" + fun isValidAgainst(input: String) = JsonConverter.formatInput(input)?.let { compareActualAndExcepted(it) } ?: throw DbAssertException( + "Unable to parse expected result from string to json" ) - @Throws(IOException::class) - fun isValidAgainst(inputStream: InputStream) = compareActualAndExcepted(JsonConverter.loadJson(inputStream)) + fun isValidAgainst(inputStream: InputStream) = isValidAgainst(JsonConverter.loadFileAsString(inputStream)) private fun compareActualAndExcepted(expected: JSONArray) { val actualJson = mongoConverter.convertContentToTableModel(collection).getTableModelAsJson(mapper) diff --git a/jcv-db-mongo/src/test/kotlin/com/ekino/oss/jcv/db/mongo/MongoAssertDB.kt b/jcv-db-mongo/src/test/kotlin/com/ekino/oss/jcv/db/mongo/MongoAssertDB.kt index a23db61..481f3aa 100644 --- a/jcv-db-mongo/src/test/kotlin/com/ekino/oss/jcv/db/mongo/MongoAssertDB.kt +++ b/jcv-db-mongo/src/test/kotlin/com/ekino/oss/jcv/db/mongo/MongoAssertDB.kt @@ -95,12 +95,10 @@ class MongoAssertDB { val expected = // language=json """ - [ - { - "name": "test-mongo-db", - "_id": "{#mongo_id#}" - } - ] + { + "name": "test-mongo-db", + "_id": "{#mongo_id#}" + } """.trimIndent() DbComparatorMongo.assertThatCollection(database.getCollection("testCollection").find(eq("name", "test-mongo-db")).first()).isValidAgainst(expected)