|
| 1 | +namespace ByteBard.AsyncAPI.Tests.Bindings |
| 2 | +{ |
| 3 | + using System.Linq; |
| 4 | + using ByteBard.AsyncAPI.Bindings; |
| 5 | + using ByteBard.AsyncAPI.Bindings.Kafka; |
| 6 | + using ByteBard.AsyncAPI.Models; |
| 7 | + using ByteBard.AsyncAPI.Models.Interfaces; |
| 8 | + using ByteBard.AsyncAPI.Readers; |
| 9 | + using ByteBard.AsyncAPI.Readers.Interface; |
| 10 | + using FluentAssertions; |
| 11 | + using NUnit.Framework; |
| 12 | + |
| 13 | + public class CustomBindingKey_Should : TestBase |
| 14 | + { |
| 15 | + [Test] |
| 16 | + public void V3_CustomBinding_SerializesUnderExtensionKey() |
| 17 | + { |
| 18 | + // Arrange |
| 19 | + var channel = new AsyncApiChannel(); |
| 20 | + channel.Bindings.Add(new MyBinding { Custom = "someValue", Any = new AsyncApiAny("anyValue") }); |
| 21 | + |
| 22 | + // Act |
| 23 | + var actual = channel.SerializeAsYaml(AsyncApiVersion.AsyncApi3_0); |
| 24 | + |
| 25 | + // Assert |
| 26 | + actual.Should().BePlatformAgnosticEquivalentTo( |
| 27 | + """ |
| 28 | + bindings: |
| 29 | + x-my: |
| 30 | + custom: someValue |
| 31 | + any: anyValue |
| 32 | + """); |
| 33 | + } |
| 34 | + |
| 35 | + [Test] |
| 36 | + public void V3_SpecificationBinding_SerializesUnderItsOwnKey() |
| 37 | + { |
| 38 | + // Arrange |
| 39 | + var channel = new AsyncApiChannel(); |
| 40 | + channel.Bindings.Add(new KafkaChannelBinding { Topic = "orders" }); |
| 41 | + |
| 42 | + // Act |
| 43 | + var actual = channel.SerializeAsYaml(AsyncApiVersion.AsyncApi3_0); |
| 44 | + |
| 45 | + // Assert |
| 46 | + actual.Should().Contain("kafka:"); |
| 47 | + actual.Should().NotContain("x-kafka"); |
| 48 | + } |
| 49 | + |
| 50 | + [Test] |
| 51 | + public void V2_CustomBinding_SerializesUnderItsOwnKey() |
| 52 | + { |
| 53 | + // Arrange |
| 54 | + var channel = new AsyncApiChannel(); |
| 55 | + channel.Bindings.Add(new MyBinding { Custom = "someValue", Any = new AsyncApiAny("anyValue") }); |
| 56 | + |
| 57 | + // Act |
| 58 | + var actual = channel.SerializeAsYaml(AsyncApiVersion.AsyncApi2_0); |
| 59 | + |
| 60 | + // Assert |
| 61 | + actual.Should().Contain(" my:"); |
| 62 | + actual.Should().NotContain("x-my"); |
| 63 | + } |
| 64 | + |
| 65 | + [Test] |
| 66 | + public void V3_CustomBindingUnderExtensionKey_ReadsAsBindingWithoutDiagnostics() |
| 67 | + { |
| 68 | + // Arrange |
| 69 | + var input = |
| 70 | + """ |
| 71 | + bindings: |
| 72 | + x-my: |
| 73 | + custom: someValue |
| 74 | + any: anyValue |
| 75 | + """; |
| 76 | + |
| 77 | + // Act |
| 78 | + var channel = ReadChannel(input, AsyncApiVersion.AsyncApi3_0, out var diagnostic); |
| 79 | + |
| 80 | + // Assert |
| 81 | + diagnostic.Errors.Should().BeEmpty(); |
| 82 | + diagnostic.Warnings.Should().BeEmpty(); |
| 83 | + channel.Bindings.Keys.Should().BeEquivalentTo("my"); |
| 84 | + channel.Bindings.Extensions.Should().BeEmpty(); |
| 85 | + ((MyBinding)channel.Bindings["my"]).Custom.Should().Be("someValue"); |
| 86 | + channel.SerializeAsYaml(AsyncApiVersion.AsyncApi3_0).Should().BePlatformAgnosticEquivalentTo(input); |
| 87 | + } |
| 88 | + |
| 89 | + [Test] |
| 90 | + public void V3_CustomBindingWithoutExtensionPrefix_AddsErrorAndReadsBinding() |
| 91 | + { |
| 92 | + // Arrange |
| 93 | + var input = |
| 94 | + """ |
| 95 | + bindings: |
| 96 | + my: |
| 97 | + custom: someValue |
| 98 | + any: anyValue |
| 99 | + """; |
| 100 | + |
| 101 | + // Act |
| 102 | + var channel = ReadChannel(input, AsyncApiVersion.AsyncApi3_0, out var diagnostic); |
| 103 | + |
| 104 | + // Assert |
| 105 | + diagnostic.Errors.Select(e => e.Message).Should().BeEquivalentTo( |
| 106 | + "ChannelBinding my is not an AsyncAPI 3 binding name; write a custom binding as x-my"); |
| 107 | + channel.Bindings.Keys.Should().BeEquivalentTo("my"); |
| 108 | + } |
| 109 | + |
| 110 | + [Test] |
| 111 | + public void V2_CustomBindingWithoutExtensionPrefix_ReadsWithoutDiagnostics() |
| 112 | + { |
| 113 | + // Arrange |
| 114 | + var input = |
| 115 | + """ |
| 116 | + bindings: |
| 117 | + my: |
| 118 | + custom: someValue |
| 119 | + any: anyValue |
| 120 | + """; |
| 121 | + |
| 122 | + // Act |
| 123 | + var channel = ReadChannel(input, AsyncApiVersion.AsyncApi2_0, out var diagnostic); |
| 124 | + |
| 125 | + // Assert |
| 126 | + diagnostic.Errors.Should().BeEmpty(); |
| 127 | + channel.Bindings.Keys.Should().BeEquivalentTo("my"); |
| 128 | + } |
| 129 | + |
| 130 | + [Test] |
| 131 | + public void V3_ExtensionWithoutRegisteredBinding_ReadsAsExtension() |
| 132 | + { |
| 133 | + // Arrange |
| 134 | + var input = |
| 135 | + """ |
| 136 | + bindings: |
| 137 | + x-other: |
| 138 | + custom: someValue |
| 139 | + """; |
| 140 | + |
| 141 | + // Act |
| 142 | + var channel = ReadChannel(input, AsyncApiVersion.AsyncApi3_0, out var diagnostic); |
| 143 | + |
| 144 | + // Assert |
| 145 | + diagnostic.Errors.Should().BeEmpty(); |
| 146 | + channel.Bindings.Keys.Should().BeEmpty(); |
| 147 | + channel.Bindings.Extensions.Keys.Should().BeEquivalentTo("x-other"); |
| 148 | + } |
| 149 | + |
| 150 | + [TestCase("kafka", true)] |
| 151 | + [TestCase("ws", true)] |
| 152 | + [TestCase("ros2", true)] |
| 153 | + [TestCase("mqtt5", false)] |
| 154 | + [TestCase("signalr", false)] |
| 155 | + [TestCase("x-signalr", false)] |
| 156 | + public void IsSpecificationKey_ReturnsWhetherAsyncApi3DefinesTheBinding(string key, bool expected) |
| 157 | + { |
| 158 | + AsyncApiBindingKeys.IsSpecificationKey(key).Should().Be(expected); |
| 159 | + } |
| 160 | + |
| 161 | + [TestCase("kafka", "kafka")] |
| 162 | + [TestCase("signalr", "x-signalr")] |
| 163 | + [TestCase("x-signalr", "x-signalr")] |
| 164 | + [TestCase("mqtt5", "x-mqtt5")] |
| 165 | + public void ToV3Key_PrefixesKeysAsyncApi3DoesNotDefine(string key, string expected) |
| 166 | + { |
| 167 | + AsyncApiBindingKeys.ToV3Key(key).Should().Be(expected); |
| 168 | + } |
| 169 | + |
| 170 | + private static AsyncApiChannel ReadChannel(string input, AsyncApiVersion version, out AsyncApiDiagnostic diagnostic) |
| 171 | + { |
| 172 | + var settings = new AsyncApiReaderSettings(); |
| 173 | + settings.Bindings = BindingsCollection.Kafka; |
| 174 | + settings.Bindings.Add(new MyBinding()); |
| 175 | + return new AsyncApiStringReader(settings).ReadFragment<AsyncApiChannel>(input, version, out diagnostic); |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments