Skip to content

Commit

Permalink
Add public API verification test.
Browse files Browse the repository at this point in the history
Fixes #72
  • Loading branch information
SeanFeldman committed Dec 24, 2018
1 parent 8974041 commit 2e9e419
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,4 @@ __pycache__/
*.odx.cs
*.xsd.cs
/binaries
/**/ApiApprovals.CompressionPlugin.received.txt
18 changes: 18 additions & 0 deletions src/ServiceBus.AttachmentPlugin.Tests/ApiApprovals.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace ServiceBus.AttachmentPlugin.Tests
{
using PublicApiGenerator;
using Xunit;

public class ApiApprovals
{
[Fact]
public void CompressionPlugin()
{
var publicApi = ApiGenerator.GeneratePublicApi(typeof(AzureStorageAttachment).Assembly,
whitelistedNamespacePrefixes: new[] { "Microsoft.Azure.ServiceBus." },
excludeAttributes: new[] { "System.Runtime.Versioning.TargetFrameworkAttribute" });

Approver.Verify(publicApi);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[assembly: System.Runtime.CompilerServices.InternalsVisibleToAttribute("ServiceBus.AttachmentPlugin.Tests")]
namespace Microsoft.Azure.ServiceBus
{
public class AzureStorageAttachmentConfiguration
{
public AzureStorageAttachmentConfiguration(string connectionString, string containerName = "attachments", string messagePropertyToIdentifyAttachmentBlob = "$attachment.blob", System.Func<Microsoft.Azure.ServiceBus.Message, bool> messageMaxSizeReachedCriteria = null) { }
public AzureStorageAttachmentConfiguration(Microsoft.Azure.ServiceBus.IProvideStorageConnectionString connectionStringProvider, string containerName = "attachments", string messagePropertyToIdentifyAttachmentBlob = "$attachment.blob", System.Func<Microsoft.Azure.ServiceBus.Message, bool> messageMaxSizeReachedCriteria = null) { }
}
public class static AzureStorageAttachmentConfigurationExtensions
{
public static Microsoft.Azure.ServiceBus.AzureStorageAttachmentConfiguration WithSasUri(this Microsoft.Azure.ServiceBus.AzureStorageAttachmentConfiguration azureStorageAttachmentConfiguration, string messagePropertyToIdentifySasUri = "$attachment.sas.uri", System.Nullable<System.TimeSpan> sasTokenValidationTime = null) { }
}
public class static AzureStorageAttachmentExtensions
{
public static Microsoft.Azure.ServiceBus.Core.ServiceBusPlugin RegisterAzureStorageAttachmentPlugin(this Microsoft.Azure.ServiceBus.ClientEntity client, Microsoft.Azure.ServiceBus.AzureStorageAttachmentConfiguration configuration) { }
public static Microsoft.Azure.ServiceBus.Core.ServiceBusPlugin RegisterAzureStorageAttachmentPluginForReceivingOnly(this Microsoft.Azure.ServiceBus.ClientEntity client, string messagePropertyToIdentifySasUri = "$attachment.sas.uri") { }
}
public interface IProvideStorageConnectionString
{
System.Threading.Tasks.Task<string> GetConnectionString();
}
public class PlainTextConnectionStringProvider : Microsoft.Azure.ServiceBus.IProvideStorageConnectionString
{
public PlainTextConnectionStringProvider(string connectionString) { }
public System.Threading.Tasks.Task<string> GetConnectionString() { }
}
}
89 changes: 89 additions & 0 deletions src/ServiceBus.AttachmentPlugin.Tests/ApprovalFiles/Approver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Adopted from https://github.com/Particular/Particular.Approvals/blob/master/src/Particular.Approvals/Approver.cs

namespace Xunit
{
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

/// <summary>
/// Verifies that values contain approved content.
/// </summary>
static class Approver
{
static string TestDirectory
{
get
{
var codeBaseUrl = new Uri(Assembly.GetExecutingAssembly().CodeBase);
var codeBasePath = Uri.UnescapeDataString(codeBaseUrl.AbsolutePath);
var dirPath = Path.GetDirectoryName(codeBasePath);
return Path.Combine(dirPath, "..", "..", "..", "ApprovalFiles");
}
}

static readonly string approvalFilesPath = TestDirectory;
static readonly JsonSerializerSettings jsonSerializerSettings;

static Approver()
{
jsonSerializerSettings = new JsonSerializerSettings
{
Formatting = Formatting.Indented
};

jsonSerializerSettings.Converters.Add(new StringEnumConverter());
}

/// <summary>
/// Verifies that the received string matches the contents of the corresponding approval file.
/// </summary>
/// <param name="value">The string to verify.</param>
/// <param name="scrubber">A delegate that modifies the received string before comparing it to the approval file.</param>
/// <param name="scenario">A value that will be added to the name of the approval file.</param>
public static void Verify(string value, Func<string, string> scrubber = null, string scenario = null)
{
var st = new StackTrace();
var sf = st.GetFrame(1);
var currentMethodName = sf.GetMethod();

var className = currentMethodName.DeclaringType.Name;
var methodName = currentMethodName.Name;
var scenarioName = string.IsNullOrEmpty(scenario) ? "" : scenario + ".";

if (scrubber != null)
{
value = scrubber(value);
}

var receivedFile = Path.Combine(approvalFilesPath, $"{className}.{methodName}.{scenarioName}received.txt");
File.WriteAllText(receivedFile, value);

var approvedFile = Path.Combine(approvalFilesPath, $"{className}.{methodName}.{scenarioName}approved.txt");
var approvedText = File.ReadAllText(approvedFile);

var normalizedApprovedText = approvedText.Replace("\r\n", "\n");
var normalizedReceivedText = value.Replace("\r\n", "\n");

Assert.Equal(normalizedApprovedText, normalizedReceivedText);

File.Delete(receivedFile);
}

/// <summary>
/// Verifies that the received object, after it has been serialized, matches the contents of the corresponding approval file.
/// </summary>
/// <param name="value">The object to verify.</param>
/// <param name="scrubber">A delegate that modifies the received object, after it has been serialized, before comparing it to the approval file.</param>
/// <param name="scenario">A value that will be added to the name of the approval file.</param>
public static void Verify(object value, Func<string, string> scrubber = null, string scenario = null)
{
var json = JsonConvert.SerializeObject(value, jsonSerializerSettings);

Verify(json, scrubber, scenario);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;net461</TargetFrameworks>
<RootNamespace>ServiceBus.AttachmentPlugin.Tests</RootNamespace>
<Optimize>false</Optimize>
</PropertyGroup>

<ItemGroup>
Expand All @@ -15,4 +17,14 @@
<ProjectReference Include="..\ServiceBus.AttachmentPlugin\ServiceBus.AttachmentPlugin.csproj" />
</ItemGroup>

<ItemGroup Label="API Approvals">
<PackageReference Include="PublicApiGenerator" Version="8.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="[12.0.1, 13.0.0)" />
<None Remove="ApprovalFiles\ApiApprovals.CompressionPlugin.received.txt" />
</ItemGroup>

<ItemGroup Label="Force the latest of the transitive dependency">
<PackageReference Include="Microsoft.Azure.ServiceBus" Version="3.2.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ public override void UnregisterPlugin(string serviceBusPluginName)
RegisteredPlugins.Remove(toRemove);
}

public override string Path { get; }
public override TimeSpan OperationTimeout { get; set; }
public override ServiceBusConnection ServiceBusConnection { get; }
public override IList<ServiceBusPlugin> RegisteredPlugins { get; }

protected override Task OnClosingAsync()
Expand Down
2 changes: 2 additions & 0 deletions src/ServiceBus.AttachmentPlugin.Tests/When_reusing_plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public override void UnregisterPlugin(string serviceBusPluginName)
RegisteredPlugins.Remove(toRemove);
}

public override string Path { get; }
public override TimeSpan OperationTimeout { get; set; }
public override ServiceBusConnection ServiceBusConnection { get; }
public override IList<ServiceBusPlugin> RegisteredPlugins { get; }

protected override Task OnClosingAsync()
Expand Down

0 comments on commit 2e9e419

Please sign in to comment.