From 350b9f77195181fa55c4f67075a5a00e40817911 Mon Sep 17 00:00:00 2001 From: Pavel Bodiachevskii Date: Sun, 25 Feb 2024 16:31:31 +0400 Subject: [PATCH] test(2.6.0): check correctness of realisation by reading AsyncAPI example - OneOf Example https://github.com/asyncapi/jasyncapi/issues/165 --- .../com/asyncapi/examples/v2/_6_0/OneOf.kt | 98 +++++++++++++++++++ .../test/resources/examples/v2.6.0/oneof.yml | 46 +++++++++ 2 files changed, 144 insertions(+) create mode 100644 asyncapi-core/src/test/kotlin/com/asyncapi/examples/v2/_6_0/OneOf.kt create mode 100644 asyncapi-core/src/test/resources/examples/v2.6.0/oneof.yml diff --git a/asyncapi-core/src/test/kotlin/com/asyncapi/examples/v2/_6_0/OneOf.kt b/asyncapi-core/src/test/kotlin/com/asyncapi/examples/v2/_6_0/OneOf.kt new file mode 100644 index 00000000..36d33248 --- /dev/null +++ b/asyncapi-core/src/test/kotlin/com/asyncapi/examples/v2/_6_0/OneOf.kt @@ -0,0 +1,98 @@ +package com.asyncapi.examples.v2._6_0 + +import com.asyncapi.v2.Reference +import com.asyncapi.v2._6_0.model.channel.ChannelItem +import com.asyncapi.v2._6_0.model.channel.message.Message +import com.asyncapi.v2._6_0.model.channel.message.OneOfMessages +import com.asyncapi.v2._6_0.model.channel.operation.Operation +import com.asyncapi.v2._6_0.model.component.Components +import com.asyncapi.v2._6_0.model.info.Info +import com.asyncapi.v2.schema.Schema + +class OneOf: AbstractExampleValidationTest() { + + override fun specificationLocation(): String = "/examples/v2.6.0/oneof.yml" + + override fun expectedInfo(): Info { + return Info.builder() + .title("OneOf example") + .version("1.0.0") + .build() + } + + override fun expectedServers(): Map? = null + + override fun expectedChannels(): Map { + return mapOf( + Pair("test", ChannelItem.builder() + .publish(Operation.builder() + .message(Reference("#/components/messages/testMessages")) + .build() + ) + .build() + ), + Pair("test2", ChannelItem.builder() + .subscribe(Operation.builder() + .message(OneOfMessages(listOf( + Message.builder() + .payload(Schema.builder().ref("#/components/schemas/objectWithKey").build()) + .build(), + Message.builder() + .payload(Schema.builder().ref("#/components/schemas/objectWithKey2").build()) + .build(), + ))) + .build() + ) + .build() + ) + ) + } + + override fun expectedComponents(): Components? { + return Components.builder() + .messages(mapOf( + Pair("testMessages", Message.builder() + .payload(Schema.builder() + .oneOf(listOf( + Schema.builder().ref("#/components/schemas/objectWithKey").build(), + Schema.builder().ref("#/components/schemas/objectWithKey2").build() + )) + .build() + ) + .build() + ), + Pair("testMessage1", Message.builder() + .payload(Schema.builder().ref("#/components/schemas/objectWithKey").build()) + .build() + ), + Pair("testMessage2", Message.builder() + .payload(Schema.builder().ref("#/components/schemas/objectWithKey2").build()) + .build() + ) + )) + .schemas(mapOf( + Pair("objectWithKey", Schema.builder() + .type("object") + .properties(mapOf( + Pair("key", Schema.builder() + .type("string") + .build() + ) + )) + .build() + ), + Pair("objectWithKey2", Schema.builder() + .type("object") + .properties(mapOf( + Pair("key2", Schema.builder() + .type("string") + .build() + ) + )) + .build() + ) + )) + .build() + } + +} \ No newline at end of file diff --git a/asyncapi-core/src/test/resources/examples/v2.6.0/oneof.yml b/asyncapi-core/src/test/resources/examples/v2.6.0/oneof.yml new file mode 100644 index 00000000..69e9ef8b --- /dev/null +++ b/asyncapi-core/src/test/resources/examples/v2.6.0/oneof.yml @@ -0,0 +1,46 @@ +asyncapi: "2.6.0" +info: + title: OneOf example + version: '1.0.0' + +channels: + test: + publish: + message: + $ref: '#/components/messages/testMessages' + + test2: + subscribe: + message: + # Use oneOf here if different messages are published on test2 topic. + oneOf: + - payload: + $ref: "#/components/schemas/objectWithKey" + - payload: + $ref: "#/components/schemas/objectWithKey2" + +components: + messages: + testMessages: + payload: + oneOf: # oneOf in payload schema + - $ref: "#/components/schemas/objectWithKey" + - $ref: "#/components/schemas/objectWithKey2" + testMessage1: + payload: + $ref: "#/components/schemas/objectWithKey" + testMessage2: + payload: + $ref: "#/components/schemas/objectWithKey2" + + schemas: + objectWithKey: + type: object + properties: + key: + type: string + objectWithKey2: + type: object + properties: + key2: + type: string \ No newline at end of file