|
| 1 | +namespace ByteBard.AsyncAPI.Tests.Bindings |
| 2 | +{ |
| 3 | + using FluentAssertions; |
| 4 | + using ByteBard.AsyncAPI.Bindings; |
| 5 | + using ByteBard.AsyncAPI.Models; |
| 6 | + using ByteBard.AsyncAPI.Readers; |
| 7 | + using NUnit.Framework; |
| 8 | + |
| 9 | + internal class BindingInvalidScalars_Should : TestBase |
| 10 | + { |
| 11 | + [TestCase("mqtt", "cleanSession: yes-please", "bool")] |
| 12 | + [TestCase("mqtt", "keepAlive: abc", "integer")] |
| 13 | + [TestCase("mqtt", "lastWill:\n topic: /wills\n qos: high", "integer")] |
| 14 | + [TestCase("ros2", "domainId: abc", "integer")] |
| 15 | + public void ServerBinding_WithInvalidScalar_ReportsError(string bindingKey, string field, string expectedType) |
| 16 | + { |
| 17 | + var yaml = |
| 18 | + "host: localhost\n" + |
| 19 | + "protocol: " + bindingKey + "\n" + |
| 20 | + "bindings:\n" + |
| 21 | + " " + bindingKey + ":\n" + |
| 22 | + Indent(field, 4); |
| 23 | + |
| 24 | + new AsyncApiStringReader(Settings()).ReadFragment<AsyncApiServer>(yaml, AsyncApiVersion.AsyncApi3_0, out var diagnostics); |
| 25 | + |
| 26 | + diagnostics.Errors.Should().ContainSingle().Which.Message.Should().Contain($"could not parse to {expectedType}"); |
| 27 | + } |
| 28 | + |
| 29 | + [Test] |
| 30 | + public void MessageBinding_WithInvalidPayloadFormatIndicator_ReportsError() |
| 31 | + { |
| 32 | + var yaml = |
| 33 | + "bindings:\n" + |
| 34 | + " mqtt:\n" + |
| 35 | + " payloadFormatIndicator: abc\n"; |
| 36 | + |
| 37 | + new AsyncApiStringReader(Settings()).ReadFragment<AsyncApiMessage>(yaml, AsyncApiVersion.AsyncApi3_0, out var diagnostics); |
| 38 | + |
| 39 | + diagnostics.Errors.Should().ContainSingle().Which.Message.Should().Contain("could not parse to integer"); |
| 40 | + } |
| 41 | + |
| 42 | + [TestCase("lifespan")] |
| 43 | + [TestCase("deadline")] |
| 44 | + [TestCase("leaseDuration")] |
| 45 | + public void ROS2OperationBinding_WithInvalidQosDuration_ReportsError(string field) |
| 46 | + { |
| 47 | + var yaml = |
| 48 | + "action: send\n" + |
| 49 | + "channel:\n" + |
| 50 | + " $ref: '#/channels/camera'\n" + |
| 51 | + "bindings:\n" + |
| 52 | + " ros2:\n" + |
| 53 | + " qosPolicies:\n" + |
| 54 | + " " + field + ": forever\n"; |
| 55 | + |
| 56 | + new AsyncApiStringReader(Settings()).ReadFragment<AsyncApiOperation>(yaml, AsyncApiVersion.AsyncApi3_0, out var diagnostics); |
| 57 | + |
| 58 | + diagnostics.Errors.Should().ContainSingle().Which.Message.Should().Contain("could not parse to integer"); |
| 59 | + } |
| 60 | + |
| 61 | + private static AsyncApiReaderSettings Settings() => new() { Bindings = BindingsCollection.All }; |
| 62 | + |
| 63 | + private static string Indent(string value, int spaces) |
| 64 | + { |
| 65 | + var prefix = new string(' ', spaces); |
| 66 | + return prefix + value.Replace("\n", "\n" + prefix) + "\n"; |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments