-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve bindings TryGetValue (#190)
- Loading branch information
1 parent
fb50d00
commit a554e41
Showing
4 changed files
with
153 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) The LEGO Group. All rights reserved. | ||
|
||
namespace LEGO.AsyncAPI.Models | ||
{ | ||
using System; | ||
using LEGO.AsyncAPI.Models.Interfaces; | ||
|
||
public static class BindingExtensions | ||
{ | ||
public static bool TryGetValue<TBinding>(this AsyncApiBindings<IServerBinding> bindings, out TBinding binding) | ||
where TBinding : class, IServerBinding | ||
{ | ||
if (bindings.TryGetValue(Activator.CreateInstance<TBinding>().BindingKey, out var serverBinding)) | ||
{ | ||
binding = serverBinding as TBinding; | ||
return true; | ||
} | ||
|
||
binding = default; | ||
return false; | ||
} | ||
|
||
public static bool TryGetValue<TBinding>(this AsyncApiBindings<IChannelBinding> bindings, out TBinding binding) | ||
where TBinding : class, IChannelBinding | ||
{ | ||
if (bindings.TryGetValue(Activator.CreateInstance<TBinding>().BindingKey, out var channelBinding)) | ||
{ | ||
binding = channelBinding as TBinding; | ||
return true; | ||
} | ||
|
||
binding = default; | ||
return false; | ||
} | ||
|
||
public static bool TryGetValue<TBinding>(this AsyncApiBindings<IOperationBinding> bindings, out TBinding binding) | ||
where TBinding : class, IOperationBinding | ||
{ | ||
if (bindings.TryGetValue(Activator.CreateInstance<TBinding>().BindingKey, out var operationBinding)) | ||
{ | ||
binding = operationBinding as TBinding; | ||
return true; | ||
} | ||
|
||
binding = default; | ||
return false; | ||
} | ||
|
||
public static bool TryGetValue<TBinding>(this AsyncApiBindings<IMessageBinding> bindings, out TBinding binding) | ||
where TBinding : class, IMessageBinding | ||
{ | ||
if (bindings.TryGetValue(Activator.CreateInstance<TBinding>().BindingKey, out var messageBinding)) | ||
{ | ||
binding = messageBinding as TBinding; | ||
return true; | ||
} | ||
|
||
binding = default; | ||
return false; | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
test/LEGO.AsyncAPI.Tests/Bindings/BindingExtensions_Should.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) The LEGO Group. All rights reserved. | ||
|
||
namespace LEGO.AsyncAPI.Tests.Bindings.WebSockets | ||
{ | ||
using System.Linq; | ||
using FluentAssertions; | ||
using LEGO.AsyncAPI.Bindings.MQTT; | ||
using LEGO.AsyncAPI.Bindings.Pulsar; | ||
using LEGO.AsyncAPI.Bindings.WebSockets; | ||
using LEGO.AsyncAPI.Models; | ||
using NUnit.Framework; | ||
|
||
public class BindingExtensions_Should | ||
{ | ||
[Test] | ||
public void TryGetValue_WithChannelBinding_ReturnsBinding() | ||
{ | ||
var channel = new AsyncApiChannel(); | ||
channel.Bindings.Add(new WebSocketsChannelBinding | ||
{ | ||
Method = "POST", | ||
Query = new AsyncApiSchema | ||
{ | ||
Description = "this mah query", | ||
}, | ||
Headers = new AsyncApiSchema | ||
{ | ||
Description = "this mah binding", | ||
}, | ||
}); | ||
|
||
var result = channel.Bindings.TryGetValue<WebSocketsChannelBinding>(out var channelBinding); | ||
result.Should().BeTrue(); | ||
channelBinding.Should().NotBeNull(); | ||
channelBinding.Should().BeEquivalentTo(channel.Bindings.First().Value); | ||
} | ||
|
||
[Test] | ||
public void TryGetValue_WithServerBinding_ReturnsBinding() | ||
{ | ||
var server = new AsyncApiServer(); | ||
server.Bindings.Add(new PulsarServerBinding | ||
{ | ||
Tenant = "test tenant", | ||
}); | ||
|
||
var result = server.Bindings.TryGetValue<PulsarServerBinding>(out var serverBinding); | ||
result.Should().BeTrue(); | ||
serverBinding.Should().NotBeNull(); | ||
serverBinding.Should().BeEquivalentTo(server.Bindings.First().Value); | ||
} | ||
|
||
[Test] | ||
public void TryGetValue_WithOperationBinding_ReturnsBinding() | ||
{ | ||
var operation = new AsyncApiOperation(); | ||
operation.Bindings.Add(new MQTTOperationBinding | ||
{ | ||
QoS = 23, | ||
MessageExpiryInterval = 1, | ||
Retain = true, | ||
}); | ||
|
||
var result = operation.Bindings.TryGetValue<MQTTOperationBinding>(out var operationBinding); | ||
result.Should().BeTrue(); | ||
operationBinding.Should().NotBeNull(); | ||
operationBinding.Should().BeEquivalentTo(operation.Bindings.First().Value); | ||
} | ||
|
||
[Test] | ||
public void TryGetValue_WithMessageBinding_ReturnsBinding() | ||
{ | ||
var message = new AsyncApiMessage(); | ||
message.Bindings.Add(new MQTTMessageBinding | ||
{ | ||
PayloadFormatIndicator = 2, | ||
CorrelationData = new AsyncApiSchema | ||
{ | ||
Description = "Test", | ||
}, | ||
}); | ||
|
||
var result = message.Bindings.TryGetValue<MQTTMessageBinding>(out var messageBinding); | ||
result.Should().BeTrue(); | ||
messageBinding.Should().NotBeNull(); | ||
messageBinding.Should().BeEquivalentTo(message.Bindings.First().Value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters