From e8c9223008fda73e42c6d85aa83ae2c624d5577b Mon Sep 17 00:00:00 2001 From: lealobanov Date: Thu, 25 Jan 2024 13:33:45 +0800 Subject: [PATCH 1/7] Working on unit tests for JSON Cadence serialization --- .../sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt diff --git a/src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt b/src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt new file mode 100644 index 00000000..e1245f6e --- /dev/null +++ b/src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt @@ -0,0 +1,4 @@ +package com.nftco.flow.sdk.cadence + +class JsonCadenceBuilderTypeSerializationTest { +} From 5d063d67d06d50d6eb3adb03bfb1710824ee8df2 Mon Sep 17 00:00:00 2001 From: lealobanov Date: Thu, 25 Jan 2024 13:49:00 +0800 Subject: [PATCH 2/7] Working on unit tests for JSON Cadence serialization --- src/main/kotlin/com/nftco/flow/sdk/cadence/json-cadence.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/nftco/flow/sdk/cadence/json-cadence.kt b/src/main/kotlin/com/nftco/flow/sdk/cadence/json-cadence.kt index e4859dab..d1ce69b9 100644 --- a/src/main/kotlin/com/nftco/flow/sdk/cadence/json-cadence.kt +++ b/src/main/kotlin/com/nftco/flow/sdk/cadence/json-cadence.kt @@ -311,7 +311,7 @@ open class FunctionType( @JsonDeserialize(using = JsonDeserializer.None::class) open class ReferenceType( - val typeID: String, + val typeID: String?, val authorized: Boolean, val type: CadenceType ) : CadenceType(TYPE_REFERENCE) @@ -390,6 +390,9 @@ class CadenceTypeDeserializer(vc: Class<*>?) : StdDeserializer(vc) TYPE_REFERENCE -> { p.codec.treeToValue(node, ReferenceType::class.java) } + TYPE_RESTRICTION -> { + p.codec.treeToValue(node, RestrictionType::class.java) + } TYPE_CAPABILITY -> { p.codec.treeToValue(node, CapabilityType::class.java) } From 57e17d496adba7a3bbe7845f40ef851e66fd4ae4 Mon Sep 17 00:00:00 2001 From: lealobanov Date: Thu, 25 Jan 2024 13:52:15 +0800 Subject: [PATCH 3/7] Working on unit tests for JSON Cadence serialization --- ...JsonCadenceBuilderTypeSerializationTest.kt | 305 ++++++++++++++++++ 1 file changed, 305 insertions(+) diff --git a/src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt b/src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt index e1245f6e..dee04163 100644 --- a/src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt +++ b/src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt @@ -1,4 +1,309 @@ package com.nftco.flow.sdk.cadence +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test + class JsonCadenceBuilderTypeSerializationTest { + + private val objectMapper: ObjectMapper = jacksonObjectMapper() + + // Simple types + @Test + fun `Test simple type serialization and deserialization`() { + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_ANY)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_ANYSTRUCT)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_ANYRESOURCE)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_TYPE)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_VOID)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_NEVER)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_BOOLEAN)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_STRING)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_CHARACTER)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_BYTES)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_NUMBER)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_SIGNEDNUMBER)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_INTEGER)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_SIGNEDINTEGER)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_FIXEDPOINT)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_SIGNEDFIXEDPOINT)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_INT)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_UINT)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_INT8)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_UINT8)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_INT16)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_UINT16)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_INT32)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_UINT32)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_INT64)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_UINT64)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_INT128)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_UINT128)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_INT256)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_UINT256)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_WORD8)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_WORD16)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_WORD32)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_WORD64)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_FIX64)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_UFIX64)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_ARRAY)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_ADDRESS)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_PATH)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_CAPABILITYPATH)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_STORAGEPATH)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_PUBLICPATH)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_PRIVATEPATH)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_AUTHACCOUNT)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_PUBLICACCOUNT)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_AUTHACCOUNT_KEYS)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_PUBLICACCOUNT_KEYS)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_AUTHACCOUNT_CONTRACTS)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_PUBLICACCOUNT_CONTRACTS)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_DEPLOYEDCONTRACT)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_ACCOUNTKEY)) + assertSimpleTypeSerializationAndDeserialization(SimpleType(TYPE_BLOCK)) + } + + private fun assertSimpleTypeSerializationAndDeserialization(originalType: CadenceType) { + val jsonString = objectMapper.writeValueAsString(originalType) + val deserializedType = objectMapper.readValue(jsonString, CadenceType::class.java) + + assertEquals(originalType.kind, deserializedType.kind, "Expected kind: ${originalType.kind}, Actual kind: ${deserializedType.kind}") + + assertEquals(objectMapper.writeValueAsString(originalType), objectMapper.writeValueAsString(deserializedType), "JSON strings do not match for type: ${originalType.kind}") + } + + // Complex types + @Test + fun `Test complex type serialization and deserialization`() { + assertComplexTypeSerializationAndDeserialization(OptionalType(createStringType())) + assertComplexTypeSerializationAndDeserializationOptionalType(OptionalType(createStringType())) + + assertComplexTypeSerializationAndDeserialization(VariableSizedArrayType(createStringType())) + assertComplexTypeSerializationAndDeserializationVariableSizedArrayType(VariableSizedArrayType(createStringType())) + + assertComplexTypeSerializationAndDeserialization(ConstantSizedArrayType(createStringType())) + assertComplexTypeSerializationAndDeserializationConstantSizedArrayType(ConstantSizedArrayType(createStringType())) + + assertComplexTypeSerializationAndDeserialization(DictionaryType(createStringType(), createIntType())) + assertComplexTypeSerializationAndDeserializationDictionaryType(DictionaryType(createStringType(), createIntType())) + + assertComplexTypeSerializationAndDeserialization(FunctionType("typeID", arrayOf(), createStringType())) + assertComplexTypeSerializationAndDeserializationFunctionType(FunctionType("typeID", arrayOf(), createStringType())) + + assertComplexTypeSerializationAndDeserialization(ReferenceType(null, true, createStringType())) + assertComplexTypeSerializationAndDeserializationReferenceType(ReferenceType(null, true, createStringType())) + + assertComplexTypeSerializationAndDeserialization(RestrictionType("typeID", createStringType(), arrayOf(createStringType()))) + assertComplexTypeSerializationAndDeserializationRestrictionType(RestrictionType("typeID", createStringType(), arrayOf(createStringType()))) + + assertComplexTypeSerializationAndDeserialization(CapabilityType(createStringType())) + assertComplexTypeSerializationAndDeserializationCapabilityType(CapabilityType(createStringType())) + + assertComplexTypeSerializationAndDeserialization(EnumType(createStringType(), "typeID", arrayOf(), arrayOf())) + assertComplexTypeSerializationAndDeserializationEnumType(EnumType(createStringType(), "typeID", arrayOf(), arrayOf())) + } + + private fun createStringType(): CadenceType { + // Replace this with the appropriate way to create a StringType instance in your code + return SimpleType(TYPE_STRING) + } + + private fun createIntType(): CadenceType { + // Replace this with the appropriate way to create an IntType instance in your code + return SimpleType(TYPE_INT) + } + + private fun assertComplexTypeSerializationAndDeserialization(originalType: CadenceType) { + val jsonString = objectMapper.writeValueAsString(originalType) + val deserializedType = objectMapper.readValue(jsonString, CadenceType::class.java) + + assertEquals(originalType.kind, deserializedType.kind, "Expected kind: ${originalType.kind}, Actual kind: ${deserializedType.kind}") + + assertEquals(objectMapper.writeValueAsString(originalType), objectMapper.writeValueAsString(deserializedType), "JSON strings do not match for type: ${originalType.kind}") + } + + private fun assertComplexTypeSerializationAndDeserializationOptionalType(originalType: OptionalType) { + val jsonString = objectMapper.writeValueAsString(originalType) + val deserializedType = objectMapper.readValue(jsonString, OptionalType::class.java) + + assertEquals(objectMapper.writeValueAsString(originalType.type), objectMapper.writeValueAsString(deserializedType.type), "Expected type: ${originalType.type}, Actual type: ${deserializedType.type}") + } + + private fun assertComplexTypeSerializationAndDeserializationVariableSizedArrayType(originalType: VariableSizedArrayType) { + val jsonString = objectMapper.writeValueAsString(originalType) + val deserializedType = objectMapper.readValue(jsonString, VariableSizedArrayType::class.java) + + assertEquals(objectMapper.writeValueAsString(originalType.type), objectMapper.writeValueAsString(deserializedType.type), "Expected type: ${originalType.type}, Actual type: ${deserializedType.type}") + } + + private fun assertComplexTypeSerializationAndDeserializationConstantSizedArrayType(originalType: ConstantSizedArrayType) { + val jsonString = objectMapper.writeValueAsString(originalType) + val deserializedType = objectMapper.readValue(jsonString, ConstantSizedArrayType::class.java) + + assertEquals(objectMapper.writeValueAsString(originalType.type), objectMapper.writeValueAsString(deserializedType.type), "Expected type: ${originalType.type}, Actual type: ${deserializedType.type}") + } + + private fun assertComplexTypeSerializationAndDeserializationDictionaryType(originalType: DictionaryType) { + val jsonString = objectMapper.writeValueAsString(originalType) + val deserializedType = objectMapper.readValue(jsonString, DictionaryType::class.java) + + assertEquals(objectMapper.writeValueAsString(originalType.key), objectMapper.writeValueAsString(deserializedType.key), "Expected key type: ${originalType.key}, Actual key type: ${deserializedType.key}") + assertEquals(objectMapper.writeValueAsString(originalType.value), objectMapper.writeValueAsString(deserializedType.value), "Expected value type: ${originalType.value}, Actual value type: ${deserializedType.value}") + } + + private fun assertComplexTypeSerializationAndDeserializationReferenceType(originalType: ReferenceType) { + val jsonString = objectMapper.writeValueAsString(originalType) + val deserializedType = objectMapper.readValue(jsonString, ReferenceType::class.java) + + assertEquals(originalType.typeID, deserializedType.typeID, "Expected typeID: ${originalType.typeID}, Actual typeID: ${deserializedType.typeID}") + assertEquals(objectMapper.writeValueAsString(originalType.type), objectMapper.writeValueAsString(deserializedType.type), "Expected referenced type: ${originalType.type}, Actual referenced type: ${deserializedType.type}") + } + + private fun assertComplexTypeSerializationAndDeserializationRestrictionType(originalType: RestrictionType) { + val jsonString = objectMapper.writeValueAsString(originalType) + val deserializedType = objectMapper.readValue(jsonString, RestrictionType::class.java) + + assertEquals(objectMapper.writeValueAsString(originalType.type), objectMapper.writeValueAsString(deserializedType.type), "Expected restricted type: ${originalType.type}, Actual restricted type: ${deserializedType.type}") + assertEquals(objectMapper.writeValueAsString(originalType.restrictions), objectMapper.writeValueAsString(deserializedType.restrictions), "Expected restrictions: ${originalType.restrictions}, Actual restrictions: ${deserializedType.restrictions}") + } + + private fun assertComplexTypeSerializationAndDeserializationCapabilityType(originalType: CapabilityType) { + val jsonString = objectMapper.writeValueAsString(originalType) + val deserializedType = objectMapper.readValue(jsonString, CapabilityType::class.java) + + assertEquals(objectMapper.writeValueAsString(originalType.type), objectMapper.writeValueAsString(deserializedType.type), "Expected referenced type: ${originalType.type}, Actual referenced type: ${deserializedType.type}") + } + + private fun assertComplexTypeSerializationAndDeserializationEnumType(originalType: EnumType) { + val jsonString = objectMapper.writeValueAsString(originalType) + val deserializedType = objectMapper.readValue(jsonString, EnumType::class.java) + + assertEquals(objectMapper.writeValueAsString(originalType.type), objectMapper.writeValueAsString(deserializedType.type), "Expected enum type: ${originalType.type}, Actual enum type: ${deserializedType.type}") + assertEquals(objectMapper.writeValueAsString(originalType.typeID), objectMapper.writeValueAsString(deserializedType.typeID), "Expected typeID: ${originalType.typeID}, Actual typeID: ${deserializedType.typeID}") + assertEquals(objectMapper.writeValueAsString(originalType.initializers), objectMapper.writeValueAsString(deserializedType.initializers), "Expected initializers: ${originalType.initializers}, Actual initializers: ${deserializedType.initializers}") + assertEquals(objectMapper.writeValueAsString(originalType.fields), objectMapper.writeValueAsString(deserializedType.fields), "Expected fields: ${originalType.fields}, Actual fields: ${deserializedType.fields}") + } + + private fun assertComplexTypeSerializationAndDeserializationFunctionType(originalType: FunctionType) { + val jsonString = objectMapper.writeValueAsString(originalType) + val deserializedType = objectMapper.readValue(jsonString, FunctionType::class.java) + + assertEquals( + objectMapper.writeValueAsString(originalType.parameters), + objectMapper.writeValueAsString(deserializedType.parameters), + "Expected parameters: ${originalType.parameters}, Actual parameters: ${deserializedType.parameters}" + ) + assertEquals( + objectMapper.writeValueAsString(originalType.`return`), + objectMapper.writeValueAsString(deserializedType.`return`), + "Expected return type: ${originalType.`return`}, Actual return type: ${deserializedType.`return`}" + ) + } + + // Composite types + @Test + fun `Test composite type serialization and deserialization`() { + val structType = CompositeType( + TYPE_STRUCT, + "StructType", + "id", + emptyArray(), + arrayOf(FieldType("field", SimpleType(TYPE_STRING))) + ) + assertCompositeTypeSerializationAndDeserialization(structType) + + val resourceType = CompositeType( + TYPE_RESOURCE, + "ResourceType", + "id", + emptyArray(), + arrayOf(FieldType("field", SimpleType(TYPE_STRING))) + ) + assertCompositeTypeSerializationAndDeserialization(resourceType) + + val eventType = CompositeType( + TYPE_EVENT, + "EventType", + "id", + emptyArray(), + arrayOf(FieldType("field", SimpleType(TYPE_STRING))) + ) + assertCompositeTypeSerializationAndDeserialization(eventType) + + val contractType = CompositeType( + TYPE_CONTRACT, + "ContractType", + "id", + emptyArray(), + arrayOf(FieldType("field", SimpleType(TYPE_STRING))) + ) + assertCompositeTypeSerializationAndDeserialization(contractType) + + val enumType = CompositeType( + TYPE_ENUM, + "EnumType", + "id", + emptyArray(), // No initializers + arrayOf(FieldType("field", SimpleType(TYPE_STRING))) + ) + assertCompositeTypeSerializationAndDeserialization(enumType) + + val structInterfaceType = CompositeType( + TYPE_STRUCT_INTERFACE, + "StructInterfaceType", + "id", + emptyArray(), // No initializers + arrayOf(FieldType("field", SimpleType(TYPE_STRING))) + ) + assertCompositeTypeSerializationAndDeserialization(structInterfaceType) + + val resourceInterfaceType = CompositeType( + TYPE_RESOURCE_INTERFACE, + "ResourceInterfaceType", + "id", + emptyArray(), // No initializers + arrayOf(FieldType("field", SimpleType(TYPE_STRING))) + ) + assertCompositeTypeSerializationAndDeserialization(resourceInterfaceType) + + val contractInterfaceType = CompositeType( + TYPE_CONTRACT_INTERFACE, + "ContractInterfaceType", + "id", + emptyArray(), // No initializers + arrayOf(FieldType("field", SimpleType(TYPE_STRING))) + ) + assertCompositeTypeSerializationAndDeserialization(contractInterfaceType) + + } + + private fun assertCompositeTypeSerializationAndDeserialization(originalType: CompositeType) { + val jsonString = objectMapper.writeValueAsString(originalType) + val deserializedType = objectMapper.readValue(jsonString, CompositeType::class.java) + + assertEquals( + objectMapper.writeValueAsString(originalType.type), + objectMapper.writeValueAsString(deserializedType.type), + "Expected type: ${originalType.type}, Actual type: ${deserializedType.type}" + ) + assertEquals( + objectMapper.writeValueAsString(originalType.typeID), + objectMapper.writeValueAsString(deserializedType.typeID), + "Expected typeID: ${originalType.typeID}, Actual typeID: ${deserializedType.typeID}" + ) + assertEquals( + objectMapper.writeValueAsString(originalType.initializers), + objectMapper.writeValueAsString(deserializedType.initializers), + "Expected initializers: ${originalType.initializers}, Actual initializers: ${deserializedType.initializers}" + ) + assertEquals( + objectMapper.writeValueAsString(originalType.fields), + objectMapper.writeValueAsString(deserializedType.fields), + "Expected fields: ${originalType.fields}, Actual fields: ${deserializedType.fields}" + ) + } + } From c778a4db4818e5cc6c3fad4c545c898ff1f4052c Mon Sep 17 00:00:00 2001 From: lealobanov Date: Thu, 25 Jan 2024 22:30:06 +0800 Subject: [PATCH 4/7] Test commit --- .../flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt b/src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt index dee04163..05b9d6fb 100644 --- a/src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt +++ b/src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt @@ -8,7 +8,7 @@ import org.junit.jupiter.api.Test class JsonCadenceBuilderTypeSerializationTest { private val objectMapper: ObjectMapper = jacksonObjectMapper() - + // // Simple types @Test fun `Test simple type serialization and deserialization`() { From e7e1adf9d36c92302515d2f6e2bf4704310784cc Mon Sep 17 00:00:00 2001 From: Lea Lobanov Date: Tue, 30 Jan 2024 15:08:55 +0800 Subject: [PATCH 5/7] Exception handling and fix test linting --- ...JsonCadenceBuilderTypeSerializationTest.kt | 69 ++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt b/src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt index 05b9d6fb..b6309e00 100644 --- a/src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt +++ b/src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt @@ -1,8 +1,10 @@ package com.nftco.flow.sdk.cadence import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.databind.exc.MismatchedInputException import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertThrows import org.junit.jupiter.api.Test class JsonCadenceBuilderTypeSerializationTest { @@ -277,7 +279,6 @@ class JsonCadenceBuilderTypeSerializationTest { arrayOf(FieldType("field", SimpleType(TYPE_STRING))) ) assertCompositeTypeSerializationAndDeserialization(contractInterfaceType) - } private fun assertCompositeTypeSerializationAndDeserialization(originalType: CompositeType) { @@ -306,4 +307,70 @@ class JsonCadenceBuilderTypeSerializationTest { ) } + // Exception handling + @Test + fun `test decode with invalid JSON`() { + val invalidJson = "{invalid: json}" + assertThrows(Exception::class.java) { + objectMapper.readValue(invalidJson, CadenceType::class.java) + } + } + + @Test + fun `test decode with missing kind field`() { + val invalidJson = "{\"type\": \"String\"}" + assertThrows(MismatchedInputException::class.java) { + objectMapper.readValue(invalidJson, CadenceType::class.java) + } + } + + @Test + fun `test decode with partial type and missing type field`() { + val invalidJson = "{\"kind\": \"PartialCadenceType\"}" + assertThrows(MismatchedInputException::class.java) { + objectMapper.readValue(invalidJson, CadenceType::class.java) + } + } + + @Test + fun `test decode with missing type field in simple type`() { + val invalidJson = "{\"kind\": \"String\"}" + assertThrows(MismatchedInputException::class.java) { + objectMapper.readValue(invalidJson, CadenceType::class.java) + } + } + + @Test + fun `test decode with missing type field in non-simple type`() { + val invalidJson = "{\"kind\": \"CompositeType\"}" + assertThrows(MismatchedInputException::class.java) { + objectMapper.readValue(invalidJson, CadenceType::class.java) + } + } + + @Test + fun `test decode with unsupported CompositeType kind`() { + val invalidJson = """ + { + "kind": "CompositeType", + "type": { + "type": "Struct", + "typeID": "1", + "initializers": [], + "fields": [] + } + } + """.trimIndent() + assertThrows(MismatchedInputException::class.java) { + objectMapper.readValue(invalidJson, CadenceType::class.java) + } + } + + @Test + fun `test decode with unknown CadenceType kind`() { + val invalidJson = "{\"kind\": \"UnknownKind\", \"type\": \"\"}" + assertThrows(MismatchedInputException::class.java) { + objectMapper.readValue(invalidJson, CadenceType::class.java) + } + } } From 497eb7a8c904fcc9fbe24175a83bb8360d5420c7 Mon Sep 17 00:00:00 2001 From: Lea Lobanov Date: Tue, 30 Jan 2024 15:23:38 +0800 Subject: [PATCH 6/7] Exception handling and fix test linting --- ...JsonCadenceBuilderTypeSerializationTest.kt | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt b/src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt index b6309e00..21b1c907 100644 --- a/src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt +++ b/src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt @@ -325,40 +325,44 @@ class JsonCadenceBuilderTypeSerializationTest { } @Test - fun `test decode with partial type and missing type field`() { - val invalidJson = "{\"kind\": \"PartialCadenceType\"}" + fun `test decode with missing type field in composite type`() { + val invalidJson = """ + { + "kind": "Struct" + } + """.trimIndent() assertThrows(MismatchedInputException::class.java) { objectMapper.readValue(invalidJson, CadenceType::class.java) } } @Test - fun `test decode with missing type field in simple type`() { - val invalidJson = "{\"kind\": \"String\"}" + fun `test decode with missing type field in complex type`() { + val invalidJson = """ + { + "kind": "Optional" + } + """.trimIndent() + assertThrows(MismatchedInputException::class.java) { objectMapper.readValue(invalidJson, CadenceType::class.java) } } @Test - fun `test decode with missing type field in non-simple type`() { - val invalidJson = "{\"kind\": \"CompositeType\"}" + fun `test decode with partial Cadence type and missing type field`() { + val invalidJson = "{\"kind\": \"PartialCadenceType\"}" assertThrows(MismatchedInputException::class.java) { objectMapper.readValue(invalidJson, CadenceType::class.java) } } @Test - fun `test decode with unsupported CompositeType kind`() { + fun `test decode with unknown CadenceType kind`() { val invalidJson = """ { - "kind": "CompositeType", - "type": { - "type": "Struct", - "typeID": "1", - "initializers": [], - "fields": [] - } + "kind": "UnknownKind", + "type": {} } """.trimIndent() assertThrows(MismatchedInputException::class.java) { @@ -367,7 +371,7 @@ class JsonCadenceBuilderTypeSerializationTest { } @Test - fun `test decode with unknown CadenceType kind`() { + fun `test decode with unknown CadenceType kind 2`() { val invalidJson = "{\"kind\": \"UnknownKind\", \"type\": \"\"}" assertThrows(MismatchedInputException::class.java) { objectMapper.readValue(invalidJson, CadenceType::class.java) From b07576810467efb9d9ee7e3002aec9ca98424a10 Mon Sep 17 00:00:00 2001 From: Lea Lobanov Date: Tue, 30 Jan 2024 15:50:34 +0800 Subject: [PATCH 7/7] Exception handling and fix test linting --- ...JsonCadenceBuilderTypeSerializationTest.kt | 140 ++++++++++++++++-- 1 file changed, 129 insertions(+), 11 deletions(-) diff --git a/src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt b/src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt index 21b1c907..2a5ffa09 100644 --- a/src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt +++ b/src/test/kotlin/com/nftco/flow/sdk/cadence/JsonCadenceBuilderTypeSerializationTest.kt @@ -317,7 +317,7 @@ class JsonCadenceBuilderTypeSerializationTest { } @Test - fun `test decode with missing kind field`() { + fun `test decode with missing kind field in generic Cadence type`() { val invalidJson = "{\"type\": \"String\"}" assertThrows(MismatchedInputException::class.java) { objectMapper.readValue(invalidJson, CadenceType::class.java) @@ -325,27 +325,115 @@ class JsonCadenceBuilderTypeSerializationTest { } @Test - fun `test decode with missing type field in composite type`() { - val invalidJson = """ - { - "kind": "Struct" - } - """.trimIndent() + fun `test decode with missing kind field in simple type`() { + val invalidJson = "{\"type\": \"String\"}" assertThrows(MismatchedInputException::class.java) { - objectMapper.readValue(invalidJson, CadenceType::class.java) + objectMapper.readValue(invalidJson, SimpleType::class.java) } } @Test fun `test decode with missing type field in complex type`() { - val invalidJson = """ + val invalidJsonOptional = """ { "kind": "Optional" } """.trimIndent() assertThrows(MismatchedInputException::class.java) { - objectMapper.readValue(invalidJson, CadenceType::class.java) + objectMapper.readValue(invalidJsonOptional, OptionalType::class.java) + } + + val invalidJsonVariableSizedArray = """ + { + "kind": "VariableSizedArray" + } + """.trimIndent() + + assertThrows(MismatchedInputException::class.java) { + objectMapper.readValue(invalidJsonVariableSizedArray, VariableSizedArrayType::class.java) + } + + val invalidJsonConstantSizedArray = """ + { + "kind": "ConstantSizedArray" + } + """.trimIndent() + + assertThrows(MismatchedInputException::class.java) { + objectMapper.readValue(invalidJsonConstantSizedArray, ConstantSizedArrayType::class.java) + } + + // Missing value field + val invalidJsonDictionary = """ + { + "kind": "Dictionary", + "key": {"kind": "String"} + } + """.trimIndent() + + assertThrows(MismatchedInputException::class.java) { + objectMapper.readValue(invalidJsonDictionary, DictionaryType::class.java) + } + + // Missing typeID field + val invalidJsonFunction = """ + { + "kind": "Function", + "parameters": [], + "`return`": {"kind": "String"} + } + """.trimIndent() + + assertThrows(MismatchedInputException::class.java) { + objectMapper.readValue(invalidJsonFunction, FunctionType::class.java) + } + + val invalidJsonReference = """ + { + "kind": "Reference", + "typeID": "id", + "authorized": true + } + """.trimIndent() + + assertThrows(MismatchedInputException::class.java) { + objectMapper.readValue(invalidJsonReference, ReferenceType::class.java) + } + + val invalidJsonRestriction = """ + { + "kind": "Restriction", + "typeID": "id", + "restrictions": [] + } + """.trimIndent() + + assertThrows(MismatchedInputException::class.java) { + objectMapper.readValue(invalidJsonRestriction, RestrictionType::class.java) + } + + val invalidJsonCapability = """ + { + "kind": "Capability" + } + """.trimIndent() + + assertThrows(MismatchedInputException::class.java) { + objectMapper.readValue(invalidJsonCapability, CapabilityType::class.java) + } + + val invalidJsonEnum = """ + { + "kind": "Enum", + "typeID": "id", + "initializers": [], + "fields": [] + } + """.trimIndent() + + assertThrows(MismatchedInputException::class.java) { + objectMapper.readValue(invalidJsonEnum, EnumType::class.java) } } @@ -353,7 +441,37 @@ class JsonCadenceBuilderTypeSerializationTest { fun `test decode with partial Cadence type and missing type field`() { val invalidJson = "{\"kind\": \"PartialCadenceType\"}" assertThrows(MismatchedInputException::class.java) { - objectMapper.readValue(invalidJson, CadenceType::class.java) + objectMapper.readValue(invalidJson, PartialCadenceType::class.java) + } + } + + @Test + fun `test decode with missing type field in composite type`() { + val invalidJson = """ + { + "kind": "Struct", + "typeID": "id", + "initializers": [], + "fields":[] + } + """.trimIndent() + assertThrows(MismatchedInputException::class.java) { + objectMapper.readValue(invalidJson, CompositeType::class.java) + } + } + + @Test + fun `test decode with missing typeID field in composite type`() { + val invalidJson = """ + { + "kind": "Struct", + "type": "Composite", + "initializers": [], + "fields":[] + } + """.trimIndent() + assertThrows(MismatchedInputException::class.java) { + objectMapper.readValue(invalidJson, CompositeType::class.java) } }