Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/ByteBard.AsyncAPI.Bindings/Sns/Condition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace ByteBard.AsyncAPI.Bindings.Sns;
using System.Collections.Generic;
using System.Linq;
using ByteBard.AsyncAPI.Models.Interfaces;
using ByteBard.AsyncAPI.Readers.Exceptions;
using ByteBard.AsyncAPI.Readers.ParseNodes;
using ByteBard.AsyncAPI.Writers;

Expand Down Expand Up @@ -49,17 +50,21 @@ public static Condition Parse(ParseNode node)
.ToDictionary(x => x.Key, x => x.Value)));
break;
default:
throw new ArgumentException($"An error occured while parsing a {nameof(Condition)} node. " +
$"AWS condition values should be one or more key value pairs.");
throw new AsyncApiReaderException(
$"An error occured while parsing a {nameof(Condition)} node. " +
$"AWS condition values should be one or more key value pairs.",
node.Context);
}
}

return new Condition(conditionValues);
}

default:
throw new ArgumentException($"An error occured while parsing a {nameof(Condition)} node. " +
$"Node should contain a collection of condition types.");
throw new AsyncApiReaderException(
$"An error occured while parsing a {nameof(Condition)} node. " +
$"Node should contain a collection of condition types.",
node.Context);
}
}
}
23 changes: 15 additions & 8 deletions src/ByteBard.AsyncAPI.Bindings/Sns/Principal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace ByteBard.AsyncAPI.Bindings.Sns;
using System.Text.Json;
using System.Text.Json.Nodes;
using ByteBard.AsyncAPI.Models.Interfaces;
using ByteBard.AsyncAPI.Readers.Exceptions;
using ByteBard.AsyncAPI.Readers.ParseNodes;
using ByteBard.AsyncAPI.Writers;

Expand All @@ -21,19 +22,23 @@ public static Principal Parse(ParseNode node)
var nodeValue = node.GetScalarValue();
if (!IsStarString(nodeValue))
{
throw new ArgumentException($"An error occured while parsing a {nameof(Principal)} node. " +
$"Principal value without a property name can only be a string value of '*'.");
throw new AsyncApiReaderException(
$"An error occured while parsing a {nameof(Principal)} node. " +
$"Principal value without a property name can only be a string value of '*'.",
node.Context);
}

return new PrincipalStar();

case MapNode mapNode:
{
var propertyNode = mapNode.First();
if (!IsValidPrincipalProperty(propertyNode.Name))
var propertyNode = mapNode.FirstOrDefault();
if (propertyNode == null || !IsValidPrincipalProperty(propertyNode.Name))
{
throw new ArgumentException($"An error occured while parsing a {nameof(Principal)} node. " +
$"Node should contain a valid AWS principal property name.");
throw new AsyncApiReaderException(
$"An error occured while parsing a {nameof(Principal)} node. " +
$"Node should contain a valid AWS principal property name.",
node.Context);
}

var principalValue = new KeyValuePair<string, StringOrStringList>(
Expand All @@ -44,8 +49,10 @@ public static Principal Parse(ParseNode node)
}

default:
throw new ArgumentException($"An error occured while parsing a {nameof(Principal)} node. " +
$"Node should contain a string value of '*' or a valid AWS principal property.");
throw new AsyncApiReaderException(
$"An error occured while parsing a {nameof(Principal)} node. " +
$"Node should contain a string value of '*' or a valid AWS principal property.",
node.Context);
}
}

Expand Down
13 changes: 9 additions & 4 deletions src/ByteBard.AsyncAPI.Bindings/Sqs/Condition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace ByteBard.AsyncAPI.Bindings.Sqs;
using System.Collections.Generic;
using System.Linq;
using ByteBard.AsyncAPI.Models.Interfaces;
using ByteBard.AsyncAPI.Readers.Exceptions;
using ByteBard.AsyncAPI.Readers.ParseNodes;
using ByteBard.AsyncAPI.Writers;

Expand Down Expand Up @@ -49,17 +50,21 @@ public static Condition Parse(ParseNode node)
.ToDictionary(x => x.Key, x => x.Value)));
break;
default:
throw new ArgumentException($"An error occured while parsing a {nameof(Condition)} node. " +
$"AWS condition values should be one or more key value pairs.");
throw new AsyncApiReaderException(
$"An error occured while parsing a {nameof(Condition)} node. " +
$"AWS condition values should be one or more key value pairs.",
node.Context);
}
}

return new Condition(conditionValues);
}

default:
throw new ArgumentException($"An error occured while parsing a {nameof(Condition)} node. " +
$"Node should contain a collection of AWS condition types.");
throw new AsyncApiReaderException(
$"An error occured while parsing a {nameof(Condition)} node. " +
$"Node should contain a collection of AWS condition types.",
node.Context);
}
}
}
23 changes: 15 additions & 8 deletions src/ByteBard.AsyncAPI.Bindings/Sqs/Principal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace ByteBard.AsyncAPI.Bindings.Sqs;
using System.Text.Json;
using System.Text.Json.Nodes;
using ByteBard.AsyncAPI.Models.Interfaces;
using ByteBard.AsyncAPI.Readers.Exceptions;
using ByteBard.AsyncAPI.Readers.ParseNodes;
using ByteBard.AsyncAPI.Writers;

Expand All @@ -21,19 +22,23 @@ public static Principal Parse(ParseNode node)
var nodeValue = node.GetScalarValue();
if (!IsStarString(nodeValue))
{
throw new ArgumentException($"An error occured while parsing a {nameof(Principal)} node. " +
$"Principal value without a property name can only be a string value of '*'.");
throw new AsyncApiReaderException(
$"An error occured while parsing a {nameof(Principal)} node. " +
$"Principal value without a property name can only be a string value of '*'.",
node.Context);
}

return new PrincipalStar();

case MapNode mapNode:
{
var propertyNode = mapNode.First();
if (!IsValidPrincipalProperty(propertyNode.Name))
var propertyNode = mapNode.FirstOrDefault();
if (propertyNode == null || !IsValidPrincipalProperty(propertyNode.Name))
{
throw new ArgumentException($"An error occured while parsing a {nameof(Principal)} node. " +
$"Node should contain a valid AWS principal property name.");
throw new AsyncApiReaderException(
$"An error occured while parsing a {nameof(Principal)} node. " +
$"Node should contain a valid AWS principal property name.",
node.Context);
}

var principalValue = new KeyValuePair<string, StringOrStringList>(
Expand All @@ -44,8 +49,10 @@ public static Principal Parse(ParseNode node)
}

default:
throw new ArgumentException($"An error occured while parsing a {nameof(Principal)} node. " +
$"Node should contain a string value of '*' or a valid AWS principal property.");
throw new AsyncApiReaderException(
$"An error occured while parsing a {nameof(Principal)} node. " +
$"Node should contain a string value of '*' or a valid AWS principal property.",
node.Context);
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/ByteBard.AsyncAPI.Bindings/StringOrStringList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace ByteBard.AsyncAPI.Bindings
using System.Text.Json.Nodes;
using ByteBard.AsyncAPI.Models;
using ByteBard.AsyncAPI.Models.Interfaces;
using ByteBard.AsyncAPI.Readers.Exceptions;
using ByteBard.AsyncAPI.Readers.ParseNodes;

public class StringOrStringList : IAsyncApiElement
Expand Down Expand Up @@ -40,8 +41,10 @@ public static StringOrStringList Parse(ParseNode node)
}

default:
throw new ArgumentException($"An error occured while parsing a {nameof(StringOrStringList)} node. " +
$"Node should contain a string value or a list of strings.");
throw new AsyncApiReaderException(
$"An error occured while parsing a {nameof(StringOrStringList)} node. " +
$"Node should contain a string value or a list of strings.",
node.Context);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
namespace ByteBard.AsyncAPI.Tests.Bindings
{
using FluentAssertions;
using ByteBard.AsyncAPI.Models;
using ByteBard.AsyncAPI.Readers;
using NUnit.Framework;
using BindingsCollection = ByteBard.AsyncAPI.Bindings.BindingsCollection;

internal class AwsPolicyParsing_Should : TestBase
{
[TestCase("action:\n foo: bar", "StringOrStringList")]
[TestCase("principal: someone", "Principal")]
[TestCase("principal:\n Unknown: arn", "Principal")]
[TestCase("principal: {}", "Principal")]
[TestCase("principal:\n - '*'", "Principal")]
[TestCase("condition: invalid", "Condition")]
[TestCase("condition:\n StringEquals: invalid", "Condition")]
public void SqsChannelBinding_WithInvalidPolicyStatement_ReportsDiagnosticInsteadOfThrowing(string statementField, string expectedType)
{
var yaml =
"bindings:\n" +
" sqs:\n" +
" queue:\n" +
" name: myQueue\n" +
" policy:\n" +
" statements:\n" +
" - effect: Allow\n" +
Indent(statementField, 12);

var settings = new AsyncApiReaderSettings { Bindings = BindingsCollection.Sqs };

AsyncApiDiagnostic diagnostic = null;
var act = () => new AsyncApiStringReader(settings).ReadFragment<AsyncApiChannel>(yaml, AsyncApiVersion.AsyncApi2_0, out diagnostic);

act.Should().NotThrow();
diagnostic.Errors.Should().ContainSingle().Which.Message.Should().Contain(expectedType);
}

[TestCase("action:\n foo: bar", "StringOrStringList")]
[TestCase("principal: someone", "Principal")]
[TestCase("condition: invalid", "Condition")]
public void SnsChannelBinding_WithInvalidPolicyStatement_ReportsDiagnosticInsteadOfThrowing(string statementField, string expectedType)
{
var yaml =
"bindings:\n" +
" sns:\n" +
" name: myTopic\n" +
" policy:\n" +
" statements:\n" +
" - effect: Allow\n" +
Indent(statementField, 10);

var settings = new AsyncApiReaderSettings { Bindings = BindingsCollection.Sns };

AsyncApiDiagnostic diagnostic = null;
var act = () => new AsyncApiStringReader(settings).ReadFragment<AsyncApiChannel>(yaml, AsyncApiVersion.AsyncApi2_0, out diagnostic);

act.Should().NotThrow();
diagnostic.Errors.Should().ContainSingle().Which.Message.Should().Contain(expectedType);
}

private static string Indent(string value, int spaces)
{
var prefix = new string(' ', spaces);
return prefix + value.Replace("\n", "\n" + prefix) + "\n";
}
}
}
Loading