Skip to content

Commit

Permalink
WIP rewrite vector tests for new vector structure
Browse files Browse the repository at this point in the history
  • Loading branch information
diehuxx committed Jan 9, 2024
1 parent 3068c65 commit f87f493
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 84 deletions.
1 change: 1 addition & 0 deletions protocol/src/main/kotlin/tbdex/sdk/protocol/Validator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ object Validator {
val factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)

val definitionsStream = object {}.javaClass.getResourceAsStream("definitions.json")
println("definitionsStream : $definitionsStream")
factory.getSchema(definitionsStream)

val schemaNames = listOf("message" to "message.schema.json", "resource" to "resource.schema.json") +
Expand Down
55 changes: 15 additions & 40 deletions protocol/src/test/kotlin/tbdex/sdk/protocol/MessagesVectorTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package tbdex.sdk.protocol

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import org.junit.jupiter.api.Test
import tbdex.sdk.protocol.models.Close
import tbdex.sdk.protocol.models.Message
Expand All @@ -10,52 +13,24 @@ import tbdex.sdk.protocol.models.Rfq
import tbdex.sdk.protocol.serialization.Json
import kotlin.test.assertEquals
import kotlin.test.assertIs
import kotlin.test.assertNotNull

class MessagesVectorTest {
@Test
fun `parse rfq`() {
testParsing<Rfq>(TestVectors.rfq())
}

@Test
fun `serialize rfq`() {
testSerialisation(TestVectors.rfq())
}
@Test
fun `parse quote`() {
testParsing<Quote>(TestVectors.quote())
}

@Test
fun `serialize quote`() {
testSerialisation(TestVectors.quote())
}
@Test
fun `parse order`() {
testParsing<Order>(TestVectors.order())
}
fun `parse-close json`() {
val vector = TestVectors.getVector("parse-rfq.json")
assertNotNull(vector)

@Test
fun `serialize order`() {
testSerialisation(TestVectors.order())
}
@Test
fun `parse order status`() {
testParsing<OrderStatus>(TestVectors.orderStatus())
}
// `input` is stringified JSON, which we must parse separately
val input = vector["input"].textValue()
assertNotNull(input)

@Test
fun `serialize order status`() {
testSerialisation(TestVectors.orderStatus())
}
@Test
fun `parse close`() {
testParsing<Close>(TestVectors.close())
}
val tbDEXMessage = Message.parse(input)
assertIs<Rfq>(tbDEXMessage)

@Test
fun `serialize close`() {
testSerialisation(TestVectors.close())
// Test parsed input json is the same as output json
// val output = vector["output"]
// assertEquals(output, Json.jsonMapper.readTree(tbDEXMessage.toString()))
}

/**
Expand Down
16 changes: 0 additions & 16 deletions protocol/src/test/kotlin/tbdex/sdk/protocol/ResourceVectorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,4 @@ import kotlin.test.assertEquals
import kotlin.test.assertIs

class ResourceVectorTest {
@Test
fun `parse offering`() {
val serializedOffering = TestVectors.offering()
val offering = Resource.parse(serializedOffering)
assertIs<Offering>(offering)
}

@Test
fun `serialized offering matches original`() {
val serializedOffering = TestVectors.offering()
val offering = Resource.parse(serializedOffering)
val serializedOffering2 = Json.stringify(offering)

assertEquals(serializedOffering, serializedOffering2)
}

}
47 changes: 19 additions & 28 deletions protocol/src/test/kotlin/tbdex/sdk/protocol/TestVectors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,27 @@ import com.fasterxml.jackson.databind.JsonNode
import tbdex.sdk.protocol.serialization.Json

object TestVectors {
// val vectors = readVectors()
//
// fun readVectors(): JsonNode {
// val loader = Thread.currentThread().contextClassLoader
// val vectorsJson = loader.getResourceAsStream("testVectors.json")?.bufferedReader()?.readText()!!
// return Json.jsonMapper.readTree(vectorsJson)
// }
val vectors = readVectors()

fun offering(): String {
fun readVectors(): MutableMap<String, JsonNode> {
val loader = Thread.currentThread().contextClassLoader
return loader.getResourceAsStream("test-vectors/parse-offering.json")?.bufferedReader()?.readText()!!
val vectors = mutableMapOf<String, JsonNode>();
val vectorFiles = arrayOf(
"parse-close.json",
"parse-offering.json",
"parse-order.json",
"parse-orderstatus.json",
"parse-quote.json",
"parse-rfq.json"
)
for (vectorFile in vectorFiles) {
val vectorJson = loader.getResourceAsStream("test-vectors/$vectorFile")?.bufferedReader()?.readText()!!
vectors[vectorFile] = Json.jsonMapper.readTree(vectorJson)
}
return vectors
}
fun rfq(): String {
val loader = Thread.currentThread().contextClassLoader
return loader.getResourceAsStream("test-vectors/parse-rfq.json")?.bufferedReader()?.readText()!!
}
fun quote(): String {
val loader = Thread.currentThread().contextClassLoader
return loader.getResourceAsStream("test-vectors/parse-quote.json")?.bufferedReader()?.readText()!!
}
fun order(): String {
val loader = Thread.currentThread().contextClassLoader
return loader.getResourceAsStream("test-vectors/parse-order.json")?.bufferedReader()?.readText()!!
}
fun orderStatus(): String {
val loader = Thread.currentThread().contextClassLoader
return loader.getResourceAsStream("test-vectors/parse-orderstatus.json")?.bufferedReader()?.readText()!!
}
fun close(): String {
val loader = Thread.currentThread().contextClassLoader
return loader.getResourceAsStream("test-vectors/parse-close.json")?.bufferedReader()?.readText()!!

fun getVector(vectorFile: String): JsonNode? {
return vectors[vectorFile]
}
}

0 comments on commit f87f493

Please sign in to comment.