Skip to content

Commit fedf798

Browse files
committed
fixed up some reference tests
1 parent c785ee8 commit fedf798

File tree

4 files changed

+92
-58
lines changed

4 files changed

+92
-58
lines changed

src/LEGO.AsyncAPI.Bindings/Sns/SnsOperationBinding.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class SnsOperationBinding : OperationBinding<SnsOperationBinding>
5959

6060
private FixedFieldMap<RedrivePolicy> redrivePolicyFixedFields => new()
6161
{
62-
{ "deadLetterQueue", (a, n) => { a.DeadLetterQueue = n.ParseMapWithExtensions(identifierFixFields); } },
62+
{ "deadLetterQueue", (a, n) => { a.DeadLetterQueue = n.ParseMapWithExtensions(this.identifierFixFields); } },
6363
{ "maxReceiveCount", (a, n) => { a.MaxReceiveCount = n.GetIntegerValue(); } },
6464
};
6565

src/LEGO.AsyncAPI/Models/References/AsyncApiAvroSchemaReference.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public override T As<T>()
4444

4545
public override bool Is<T>()
4646
{
47-
if (Target == null)
47+
if (this.Target == null)
4848
{
4949
return false;
5050
}

test/LEGO.AsyncAPI.Tests/Models/AsyncApiMessage_Should.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,11 @@ public void AsyncApiMessage_WithAvroAsReference_Deserializes()
232232
var deserializedMessage = new AsyncApiStringReader().ReadFragment<AsyncApiMessage>(input, AsyncApiVersion.AsyncApi2_0, out _);
233233

234234
// Assert
235-
//TODO: reimplement this test.
236-
//var payloadReference = deserializedMessage.Payload as AsyncApiAvroSchemaReference;
237-
//deserializedMessage.Payload.UnresolvedReference.Should().BeTrue();
238-
//deserializedMessage.Payload.Reference.Should().NotBeNull();
239-
//deserializedMessage.Payload.Reference.IsExternal.Should().BeTrue();
240-
//deserializedMessage.Payload.Reference.IsFragment.Should().BeTrue();
235+
var payloadReference = deserializedMessage.Payload as AsyncApiAvroSchemaReference;
236+
payloadReference.UnresolvedReference.Should().BeTrue();
237+
payloadReference.Reference.Should().NotBeNull();
238+
payloadReference.Reference.IsExternal.Should().BeTrue();
239+
payloadReference.Reference.IsFragment.Should().BeTrue();
241240

242241
}
243242

test/LEGO.AsyncAPI.Tests/Models/AsyncApiReference_Should.cs

Lines changed: 85 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,50 @@ public void Reference()
7272
}
7373

7474
[Test]
75-
public void ResolveExternalReference()
75+
public void ExternalFragmentReference_ResolvesFragtment()
7676
{
77+
var externalJson =
78+
"""
79+
{
80+
"servers": [
81+
{
82+
"url": "wss://production.gigantic-server.com:443",
83+
"protocol": "wss",
84+
"protocolVersion": "1.0.0",
85+
"description": "The production API server",
86+
"variables": {
87+
"username": {
88+
"default": "demo",
89+
"description": "This value is assigned by the service provider"
90+
},
91+
"password": {
92+
"default": "demo",
93+
"description": "This value is assigned by the service provider"
94+
}
95+
}
96+
}
97+
]
98+
}
99+
""";
100+
77101
var json =
78102
"""
79-
{
80-
"asyncapi": "2.6.0",
81-
"info": { },
82-
"servers": {
83-
"production": {
84-
"$ref": "https://gist.githubusercontent.com/VisualBean/7dc9607d735122483e1bb7005ff3ad0e/raw/458729e4d56636ef3bb34762f4a5731ea5043bdf/servers.json#/servers/0"
85-
}
86-
}
87-
}
88-
""";
89-
90-
var doc = new AsyncApiStringReader(new AsyncApiReaderSettings { ReferenceResolution = ReferenceResolutionSetting.ResolveAllReferences }).Read(json, out var diag);
103+
{
104+
"asyncapi": "2.6.0",
105+
"info": { },
106+
"servers": {
107+
"production": {
108+
"$ref": "whatever/whatever.json#/servers/0"
109+
}
110+
}
111+
}
112+
""";
113+
114+
var doc = new AsyncApiStringReader(new AsyncApiReaderSettings { ReferenceResolution = ReferenceResolutionSetting.ResolveAllReferences, ExternalReferenceLoader = new MockStringLoader(externalJson) }).Read(json, out var diag);
91115
var reference = doc.Servers.First().Value as AsyncApiServerReference;
92-
//reference.Reference.Id.Should().Be("whatever");
93-
//reference.Reference.HostDocument.Should().Be(doc);
94-
//reference.Reference.IsFragment.Should().BeTrue();
116+
reference.Reference.FragmentId.Should().Be("/servers/0");
117+
reference.Reference.IsFragment.Should().BeTrue();
118+
reference.Url.Should().Be("wss://production.gigantic-server.com:443");
95119
}
96120

97121

@@ -416,70 +440,80 @@ public void AsyncApiReference_WithExternalResourcesInterface_DeserializesCorrect
416440
[Test]
417441
public void AsyncApiReference_WithExternalAvroResource_DeserializesCorrectly()
418442
{
443+
var avroPayload =
444+
"""
445+
{
446+
"type": "record",
447+
"name": "thecodebuzz_schema",
448+
"namespace": "thecodebuzz.avro",
449+
"fields": [
450+
{
451+
"name": "username",
452+
"type": "string",
453+
"doc": "Name of the user account on Thecodebuzz.com"
454+
},
455+
{
456+
"name": "email",
457+
"type": "string",
458+
"doc": "The email of the user logging message on the blog"
459+
},
460+
{
461+
"name": "timestamp",
462+
"type": "long",
463+
"doc": "time in seconds"
464+
}
465+
],
466+
"doc:": "A basic schema for storing thecodebuzz blogs messages"
467+
}
468+
""";
469+
419470
var yaml = """
420-
asyncapi: 2.3.0
471+
asyncapi: 2.6.0
421472
info:
422473
title: test
423474
version: 1.0.0
424475
channels:
425476
workspace:
426477
publish:
427478
message:
479+
payload:
480+
$ref: ./some/path/to/external/payload.json
481+
schemaFormat: application/vnd.apache.avro
428482
name: Test
429483
title: Test message
430484
summary: Test.
431-
schemaFormat: application/vnd.apache.avro
432-
contentType: application/cloudevents+json
433-
payload:
434-
$ref: "./some/path/to/external/payload.json"
435485
""";
436486
var settings = new AsyncApiReaderSettings
437487
{
438488
ReferenceResolution = ReferenceResolutionSetting.ResolveAllReferences,
439-
ExternalReferenceLoader = new MockAvroSchemaLoader(),
489+
ExternalReferenceLoader = new MockStringLoader(avroPayload),
440490
};
441491
var reader = new AsyncApiStringReader(settings);
442492
var doc = reader.Read(yaml, out var diagnostic);
443493
var message = doc.Channels["workspace"].Publish.Message.First();
444494
var payload = message.Payload.As<AsyncApiAvroSchema>();
445495
payload.As<AvroRecord>().Name.Should().Be("thecodebuzz_schema");
496+
497+
doc.SerializeAsYaml(AsyncApiVersion.AsyncApi2_0).Should()
498+
.BePlatformAgnosticEquivalentTo(yaml);
499+
446500
}
447501
}
448502

449-
public class MockAvroSchemaLoader : IStreamLoader
503+
public class MockStringLoader : IStreamLoader
450504
{
451-
const string Payload =
452-
"""
453-
{
454-
"type": "record",
455-
"name": "thecodebuzz_schema",
456-
"namespace": "thecodebuzz.avro",
457-
"fields": [
458-
{
459-
"name": "username",
460-
"type": "string",
461-
"doc": "Name of the user account on Thecodebuzz.com"
462-
},
463-
{
464-
"name": "email",
465-
"type": "string",
466-
"doc": "The email of the user logging message on the blog"
467-
},
468-
{
469-
"name": "timestamp",
470-
"type": "long",
471-
"doc": "time in seconds"
472-
}
473-
],
474-
"doc:": "A basic schema for storing thecodebuzz blogs messages"
475-
}
476-
""";
505+
public MockStringLoader(string input)
506+
{
507+
this.input = input;
508+
}
509+
510+
private readonly string input;
477511

478512
public Stream Load(Uri uri)
479513
{
480514
var stream = new MemoryStream();
481515
var writer = new StreamWriter(stream);
482-
writer.Write(Payload);
516+
writer.Write(this.input);
483517
writer.Flush();
484518
stream.Position = 0;
485519
return stream;
@@ -490,6 +524,7 @@ public Task<Stream> LoadAsync(Uri uri)
490524
return Task.FromResult(this.Load(uri));
491525
}
492526
}
527+
493528
public class MockJsonSchemaLoader : IStreamLoader
494529
{
495530
const string Message =

0 commit comments

Comments
 (0)