Skip to content

Commit

Permalink
fix(2.6.0): check correctness of realisation by reading AsyncAPI exam…
Browse files Browse the repository at this point in the history
…ple - AnyOf example

asyncapi#187
  • Loading branch information
Pakisan committed Feb 29, 2024
1 parent 9bac326 commit 46f9cac
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.asyncapi.examples.v2._0_0

import com.asyncapi.v3.ClasspathUtils
import com.asyncapi.v2._0_0.model.AsyncAPI
import com.asyncapi.v2._0_0.model.component.Components
import com.asyncapi.v2._0_0.model.info.Info
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

abstract class AbstractExampleValidationTest {

private val objectMapper = ObjectMapper(YAMLFactory())
.setSerializationInclusion(JsonInclude.Include.NON_NULL)

abstract fun specificationLocation(): String

private fun specification(): AsyncAPI {
return objectMapper.readValue(
ClasspathUtils.readAsString(specificationLocation()),
AsyncAPI::class.java
)
}

open fun expectedId(): String? = null

@Test
fun `ensure that id was read correctly`() {
Assertions.assertEquals(
specification().id,
expectedId(),
"id must be read correctly"
)
}

open fun expectedDefaultContentType(): String? = null

@Test
fun `ensure that defaultContentType was read correctly`() {
Assertions.assertEquals(
specification().defaultContentType,
expectedDefaultContentType(),
"defaultContentType must be read correctly"
)
}

abstract fun expectedInfo(): Info

@Test
fun `ensure that info was read correctly`() {
Assertions.assertEquals(
specification().info,
expectedInfo(),
"Info must be read correctly"
)
}

abstract fun expectedServers(): Map<String, Any>?

@Test
fun `ensure that servers were read correctly`() {
Assertions.assertEquals(
specification().servers,
expectedServers(),
"Servers must be read correctly"
)
}

abstract fun expectedChannels(): Map<String, Any>

@Test
fun `ensure that channels were read correctly`() {
Assertions.assertEquals(
specification().channels,
expectedChannels(),
"Channels must be read correctly"
)
}

abstract fun expectedComponents(): Components?

@Test
fun `ensure that components were read correctly`() {
Assertions.assertEquals(
specification().components,
expectedComponents(),
"Components must be read correctly"
)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.asyncapi.examples.v2._0_0

import com.asyncapi.v2.Reference
import com.asyncapi.v2._0_0.model.channel.ChannelItem
import com.asyncapi.v2._0_0.model.channel.message.Message
import com.asyncapi.v2._0_0.model.channel.operation.Operation
import com.asyncapi.v2._0_0.model.component.Components
import com.asyncapi.v2._0_0.model.info.Info
import com.asyncapi.v2.schema.Schema

class AnyOf: AbstractExampleValidationTest() {

override fun specificationLocation(): String = "/examples/v2.0.0/anyof.yml"

override fun expectedInfo(): Info {
return Info.builder()
.title("AnyOf example")
.version("1.0.0")
.build()
}

override fun expectedServers(): Map<String, Any>? = null

override fun expectedChannels(): Map<String, Any> {
return mapOf(
Pair("test", ChannelItem.builder()
.publish(Operation.builder()
.message(Reference("#/components/messages/testMessages"))
.build()
)
.build()
)
)
}

override fun expectedComponents(): Components? {
return Components.builder()
.messages(mapOf(
Pair("testMessages", Message.builder()
.payload(Schema.builder()
.anyOf(listOf(
Schema.builder().ref("#/components/schemas/objectWithKey").build(),
Schema.builder().ref("#/components/schemas/objectWithKey2").build(),
))
.build()
)
.build()
)
))
.schemas(mapOf(
Pair("objectWithKey", Schema.builder()
.type("object")
.properties(mapOf(
Pair("key", Schema.builder()
.type("string")
.additionalProperties(false)
.build()
)
))
.build()
),
Pair("objectWithKey2", Schema.builder()
.type("object")
.properties(mapOf(
Pair("key2", Schema.builder()
.type("string")
.build()
)
))
.build()
)
))
.build()
}

}
31 changes: 31 additions & 0 deletions asyncapi-core/src/test/resources/examples/v2.0.0/anyof.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
asyncapi: 2.0.0
info:
title: AnyOf example
version: '1.0.0'

channels:
test:
publish:
message:
$ref: '#/components/messages/testMessages'

components:
messages:
testMessages:
payload:
anyOf: # anyOf in payload schema
- $ref: "#/components/schemas/objectWithKey"
- $ref: "#/components/schemas/objectWithKey2"

schemas:
objectWithKey:
type: object
properties:
key:
type: string
additionalProperties: false
objectWithKey2:
type: object
properties:
key2:
type: string

0 comments on commit 46f9cac

Please sign in to comment.