-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created Testing library for the CDK assertion template. Defined first…
… assertion resource.
- Loading branch information
Showing
13 changed files
with
234 additions
and
7 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,12 @@ | ||
using Sagittaras.CDK.Testing.Resources; | ||
|
||
namespace Sagittaras.CDK.Testing.Route53; | ||
|
||
/// <summary> | ||
/// Assertion for AWS::Route53::HostedZone. | ||
/// </summary> | ||
public class HostedZoneAssertion : ResourceAssertion<HostedZoneProperties> | ||
{ | ||
/// <inheritdoc /> | ||
public override string Type => "AWS::Route53::HostedZone"; | ||
} |
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,23 @@ | ||
using Sagittaras.CDK.Testing.Resources; | ||
|
||
namespace Sagittaras.CDK.Testing.Route53; | ||
|
||
/// <summary> | ||
/// Properties describing the Hosted Zone in the CloudFormation template. | ||
/// </summary> | ||
public class HostedZoneProperties : ResourceProperties | ||
{ | ||
private string? _name; | ||
|
||
/// <summary> | ||
/// Name of the hosted zone. | ||
/// </summary> | ||
/// <remarks> | ||
/// Automatically appends the trailing dot that is generated for CloudFormation template. | ||
/// </remarks> | ||
public string? Name | ||
{ | ||
get => _name; | ||
set => _name = value?.TrimEnd('.') + "."; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
Sagittaras.CDK.Testing.Route53/Sagittaras.CDK.Testing.Route53.csproj
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,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Sagittaras.CDK.Testing\Sagittaras.CDK.Testing.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
22 changes: 22 additions & 0 deletions
22
Sagittaras.CDK.Testing/Extensions/TemplateAssertionExtension.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,22 @@ | ||
using Amazon.CDK.Assertions; | ||
using Sagittaras.CDK.Testing.Resources; | ||
|
||
namespace Sagittaras.CDK.Testing.Extensions; | ||
|
||
/// <summary> | ||
/// Extends the assertion template from the CDK by custom assertion methods. | ||
/// </summary> | ||
public static class TemplateAssertionExtension | ||
{ | ||
/// <summary> | ||
/// Asserts that the template has a resource with the given description. | ||
/// </summary> | ||
/// <param name="template"></param> | ||
/// <param name="assertion"></param> | ||
/// <typeparam name="TResourceAssertion"></typeparam> | ||
public static void Assert<TResourceAssertion>(this Template template, TResourceAssertion assertion) | ||
where TResourceAssertion : IResourceAssertion | ||
{ | ||
template.HasResource(assertion.Type, assertion.GetResourceDescription()); | ||
} | ||
} |
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,31 @@ | ||
namespace Sagittaras.CDK.Testing.Resources; | ||
|
||
/// <summary> | ||
/// Basic interface describing the assertion for AWS resource. | ||
/// </summary> | ||
public interface IResourceAssertion | ||
{ | ||
/// <summary> | ||
/// AWS Resource type. | ||
/// </summary> | ||
string Type { get; } | ||
|
||
/// <summary> | ||
/// Converts the resource description to a dictionary suitable for template assertion. | ||
/// </summary> | ||
/// <returns></returns> | ||
IDictionary<string, object> GetResourceDescription(); | ||
} | ||
|
||
/// <summary> | ||
/// Extends the basic resource assertion with properties. | ||
/// </summary> | ||
/// <typeparam name="TProperties">Type of the properties used for the resource.</typeparam> | ||
public interface IResourceAssertion<TProperties> : IResourceAssertion | ||
where TProperties : IResourceProperties, new() | ||
{ | ||
/// <summary> | ||
/// Properties that helps to identify the resource. | ||
/// </summary> | ||
TProperties Properties { get; set; } | ||
} |
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,13 @@ | ||
namespace Sagittaras.CDK.Testing.Resources; | ||
|
||
/// <summary> | ||
/// Describes the properties of a resource. | ||
/// </summary> | ||
public interface IResourceProperties | ||
{ | ||
/// <summary> | ||
/// Converts the properties to a dictionary suitable for template assertion. | ||
/// </summary> | ||
/// <returns></returns> | ||
IDictionary<string, object> ToDictionary(); | ||
} |
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,43 @@ | ||
using System.Reflection; | ||
|
||
namespace Sagittaras.CDK.Testing.Resources; | ||
|
||
/// <summary> | ||
/// Abstract implementation of <see cref="IResourceAssertion{TProperties}"/>. | ||
/// </summary> | ||
public abstract class ResourceAssertion<TProperties> : IResourceAssertion<TProperties> | ||
where TProperties : IResourceProperties, new() | ||
{ | ||
/// <inheritdoc /> | ||
public abstract string Type { get; } | ||
|
||
/// <inheritdoc /> | ||
public TProperties Properties { get; set; } = new(); | ||
|
||
/// <summary> | ||
/// Gets the property members of the derived class. | ||
/// </summary> | ||
private IEnumerable<PropertyInfo> PropertyMembers => GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); | ||
|
||
/// <inheritdoc /> | ||
public IDictionary<string, object> GetResourceDescription() | ||
{ | ||
Dictionary<string, object> description = new(); | ||
foreach (PropertyInfo member in PropertyMembers) | ||
{ | ||
object? value = member.GetValue(this); | ||
switch (value) | ||
{ | ||
case null: | ||
continue; | ||
case IResourceProperties properties: | ||
value = properties.ToDictionary(); | ||
break; | ||
} | ||
|
||
description.Add(member.Name, value); | ||
} | ||
|
||
return description; | ||
} | ||
} |
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,35 @@ | ||
using System.Reflection; | ||
|
||
namespace Sagittaras.CDK.Testing.Resources; | ||
|
||
/// <summary> | ||
/// Abstract implementation of <see cref="IResourceProperties"/> that provides a default implementation of <see cref="IResourceProperties.ToDictionary"/>. | ||
/// </summary> | ||
/// <remarks> | ||
/// The resource properties should be nullable types to exclude them from the dictionary. | ||
/// </remarks> | ||
public abstract class ResourceProperties : IResourceProperties | ||
{ | ||
/// <summary> | ||
/// Gets all properties members of the class. | ||
/// </summary> | ||
private IEnumerable<PropertyInfo> Properties => GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); | ||
|
||
/// <inheritdoc /> | ||
public IDictionary<string, object> ToDictionary() | ||
{ | ||
Dictionary<string, object> dict = new(); | ||
foreach (PropertyInfo property in Properties) | ||
{ | ||
object? value = property.GetValue(this); | ||
if (value is null) | ||
{ | ||
continue; | ||
} | ||
|
||
dict[property.Name] = value; | ||
} | ||
|
||
return dict; | ||
} | ||
} |
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,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Sagittaras.CDK.Framework\Sagittaras.CDK.Framework.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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
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