Skip to content

Commit

Permalink
add initial tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualBean committed Feb 26, 2024
1 parent 45b8d6a commit 5c18d93
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 17 deletions.
69 changes: 65 additions & 4 deletions src/LEGO.AsyncAPI/Models/Any/AsyncApiAny.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

namespace LEGO.AsyncAPI.Models
{
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using LEGO.AsyncAPI.Models.Interfaces;
Expand All @@ -26,19 +23,27 @@ public class AsyncApiAny : IAsyncApiElement, IAsyncApiExtension
private JsonNode node;

/// <summary>
/// Initializes a new instance of the <see cref="AsyncApiAny"/> class.
/// Initializes a new instance of the <see cref="AsyncApiAny" /> class.
/// </summary>
/// <param name="node">The node.</param>
public AsyncApiAny(JsonNode node)
{
this.node = node;
}

/// <summary>
/// Initializes a new instance of the <see cref="AsyncApiAny"/> class.
/// </summary>
/// <param name="obj">The object.</param>
public AsyncApiAny(object obj)
{
this.node = JsonNode.Parse(JsonSerializer.Serialize(obj, this.options));
}

/// <summary>
/// Initializes a new instance of the <see cref="AsyncApiAny"/> class.
/// </summary>
/// <param name="dictionary">The dictionary.</param>
public AsyncApiAny(Dictionary<string, AsyncApiAny> dictionary)
{
var jsonObject = new JsonObject();
Expand All @@ -50,6 +55,10 @@ public AsyncApiAny(Dictionary<string, AsyncApiAny> dictionary)
this.node = jsonObject;
}

/// <summary>
/// Initializes a new instance of the <see cref="AsyncApiAny"/> class.
/// </summary>
/// <param name="list">The list.</param>
public AsyncApiAny(List<object> list)
{
var jsonArray = new JsonArray();
Expand All @@ -62,6 +71,10 @@ public AsyncApiAny(List<object> list)
this.node = jsonArray;
}

/// <summary>
/// Initializes a new instance of the <see cref="AsyncApiAny"/> class.
/// </summary>
/// <param name="dictionary">The dictionary.</param>
public AsyncApiAny(Dictionary<string, object> dictionary)
{
var jsonObject = new JsonObject();
Expand All @@ -74,6 +87,7 @@ public AsyncApiAny(Dictionary<string, object> dictionary)
this.node = jsonObject;
}

Check warning on line 89 in src/LEGO.AsyncAPI/Models/Any/AsyncApiAny.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Code should not contain multiple blank lines in a row

Check warning on line 89 in src/LEGO.AsyncAPI/Models/Any/AsyncApiAny.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)


/// <summary>
/// Initializes a new instance of the <see cref="AsyncApiAny"/> class.
/// </summary>
Expand All @@ -92,19 +106,66 @@ public AsyncApiAny(JsonObject node)
this.node = node;
}

/// <summary>
/// Converts to <see cref="{T}" /> from an Extension.
/// </summary>
/// <typeparam name="T">T.</typeparam>
/// <param name="extension">The extension.</param>
/// <returns><see cref="{T}"/>.</returns>
public static T FromExtension<T>(IAsyncApiExtension extension)
{
if (extension is AsyncApiAny any)
{
return any.GetValueOrDefault<T>();
}
else
{
return default(T);
}
}

/// <summary>
/// Gets the node.
/// </summary>
/// <returns><see cref="JsonNode"/>.</returns>
/// <value>
/// The node.
/// </value>
public JsonNode GetNode() => this.node;

/// <summary>
/// Gets the value.
/// </summary>
/// <typeparam name="T"><see cref="{T}" />.</typeparam>
/// <returns><see cref="{T}" />.</returns>
public T GetValue<T>()
{
return JsonSerializer.Deserialize<T>(this.node.ToJsonString());
}

/// <summary>
/// Gets the value or default.
/// </summary>
/// <typeparam name="T"><see cref="{T}" />.</typeparam>
/// <returns><see cref="{T}" /> or default.</returns>
public T GetValueOrDefault<T>()
{
try
{
return this.GetValue<T>();
}
catch (System.Exception)
{
return default(T);
}
}

/// <summary>
/// Tries the get value.
/// </summary>
/// <typeparam name="T"><see cref="{T}" />.</typeparam>
/// <param name="value">The value.</param>
/// <returns>true if the value could be converted, otherwise false.</returns>
public bool TryGetValue<T>(out T value)
{
try
Expand Down
7 changes: 7 additions & 0 deletions test/LEGO.AsyncAPI.Tests/Bindings/Sns/SnsBindings_Should.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,12 +381,19 @@ public void SnsOperationBinding_WithFilledObject_SerializesAndDeserializes()
var settings = new AsyncApiReaderSettings();
settings.Bindings = BindingsCollection.Sns;
var binding = new AsyncApiStringReader(settings).ReadFragment<AsyncApiOperation>(actual, AsyncApiVersion.AsyncApi2_0, out _);
var binding2 = new AsyncApiStringReader(settings).ReadFragment<AsyncApiOperation>(expected, AsyncApiVersion.AsyncApi2_0, out _);
binding2.Bindings.First().Value.Extensions.TryGetValue("x-bindingExtension", out IAsyncApiExtension any);
var val = AsyncApiAny.FromExtension<ExtensionClass>(any);

// Assert
Assert.AreEqual(actual, expected);

var expectedSnsBinding = (SnsOperationBinding)operation.Bindings.Values.First();
expectedSnsBinding.Should().BeEquivalentTo((SnsOperationBinding)binding.Bindings.Values.First(), options => options.IgnoringCyclicReferences());
}
class ExtensionClass
{
public string bindingXPropertyName { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,7 @@ namespace LEGO.AsyncAPI.Tests
public class AsyncApiAnyTests
{
[Test]
public void Test()
{
// Arrange
// Act
// Assert
}


[Test]
public void ctor_Primitives()
public void GetValue_ReturnsCorrectConversions()
{
// Arrange
// Act
Expand All @@ -35,18 +26,27 @@ public void ctor_Primitives()
var h = new AsyncApiAny(new Dictionary<string, int>() { { "t", 2 } });
var i = new AsyncApiAny(new Dictionary<string, MyType>() { { "t", new MyType("test") } });

var v = e.GetValue<MyType>();
var k = i.GetValue<Dictionary<string, MyType>>();
// Assert
Assert.AreEqual("string", a.GetValue<string>());
Assert.AreEqual(1, b.GetValue<int>());
Assert.AreEqual(1.1, c.GetValue<decimal>());
Assert.AreEqual(true, d.GetValue<bool>());
Assert.NotNull(e.GetValue<MyType>());
Assert.IsNotEmpty(f.GetValue<List<string>>());
Assert.IsNotEmpty(f.GetValue<IEnumerable<string>>());
Assert.IsNotEmpty(g.GetValue<List<MyType>>());
Assert.IsNotEmpty(g.GetValue<IEnumerable<MyType>>());
Assert.IsNotEmpty(h.GetValue<Dictionary<string, int>>());
Assert.IsNotEmpty(i.GetValue<Dictionary<string, MyType>>());
}


class MyType
{
public MyType(string value)
{
this.Value = value;
}

public string Value { get; set; }
}
}
Expand Down

0 comments on commit 5c18d93

Please sign in to comment.