Skip to content

Commit

Permalink
Add tests for function layer
Browse files Browse the repository at this point in the history
  • Loading branch information
justinyoo committed Oct 27, 2017
1 parent e7503ac commit 0e69261
Show file tree
Hide file tree
Showing 9 changed files with 622 additions and 12 deletions.
7 changes: 7 additions & 0 deletions AglCodingTest.sln
Expand Up @@ -36,6 +36,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AglCodingTest.Extensions.Te
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AglCodingTest.Settings.Tests", "test\AglCodingTest.Settings.Tests\AglCodingTest.Settings.Tests.csproj", "{F0762A7A-5248-4861-A55E-D20A79F2AD2D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AglCodingTest.Functions.Tests", "test\AglCodingTest.Functions.Tests\AglCodingTest.Functions.Tests.csproj", "{1CAF0093-DD43-45FB-9E21-CF7147A4F49E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -82,6 +84,10 @@ Global
{F0762A7A-5248-4861-A55E-D20A79F2AD2D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0762A7A-5248-4861-A55E-D20A79F2AD2D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F0762A7A-5248-4861-A55E-D20A79F2AD2D}.Release|Any CPU.Build.0 = Release|Any CPU
{1CAF0093-DD43-45FB-9E21-CF7147A4F49E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1CAF0093-DD43-45FB-9E21-CF7147A4F49E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1CAF0093-DD43-45FB-9E21-CF7147A4F49E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1CAF0093-DD43-45FB-9E21-CF7147A4F49E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -97,6 +103,7 @@ Global
{074176DE-E0ED-455C-814B-C96C5C3E85DF} = {74F77911-DFA1-428E-9C09-71334FD246A4}
{54E3283A-BF53-4997-8A27-F224A1394918} = {DFC84156-3B71-474C-A6AD-91D119C04EAF}
{F0762A7A-5248-4861-A55E-D20A79F2AD2D} = {DFC84156-3B71-474C-A6AD-91D119C04EAF}
{1CAF0093-DD43-45FB-9E21-CF7147A4F49E} = {DFC84156-3B71-474C-A6AD-91D119C04EAF}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {938129C8-CBE0-4346-B677-9B44A7D7A0D9}
Expand Down
1 change: 1 addition & 0 deletions src/AglCodingTest.Functions/AglCodingTest.Functions.csproj
Expand Up @@ -53,6 +53,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AglCodingTestHttpTriggerFunction.cs" />
<Compile Include="Formatters\HtmlMediaTypeFormatter.cs" />
<Compile Include="FunctionOptions\AglCodingTestHttpTriggerFunctionOptions.cs" />
<Compile Include="FunctionOptions\FunctionOptionsBase.cs" />
<Compile Include="IAglCodingTestHttpTriggerFunction.cs" />
Expand Down
30 changes: 18 additions & 12 deletions src/AglCodingTest.Functions/AglCodingTestHttpTriggerFunction.cs
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;

using AglCodingTest.Extensions;
using AglCodingTest.Functions.Formatters;
using AglCodingTest.Functions.FunctionOptions;
using AglCodingTest.Services;
using AglCodingTest.Services.ServiceOptions;
Expand All @@ -18,6 +19,9 @@ public class AglCodingTestHttpTriggerFunction : IAglCodingTestHttpTriggerFunctio
private readonly IAglPayloadLoadingService _loadingService;
private readonly IAglPayloadProcessingService _processingService;

private AglPayloadLoadingServiceOptions _loadingServiceOptions;
private AglPayloadProcessingServiceOptions _processingServiceOptions;

private bool _disposed;

/// <summary>
Expand All @@ -35,6 +39,8 @@ public AglCodingTestHttpTriggerFunction(IAglPayloadLoadingService loadingService
public async Task<object> InvokeAsync<TInput, TOptions>(TInput input, TOptions options = default(TOptions))
where TOptions : FunctionOptionsBase
{
input.ThrowIfNullOrDefault();

var req = input as HttpRequestMessage;
req.ThrowIfNullOrDefault();

Expand All @@ -44,36 +50,36 @@ public AglCodingTestHttpTriggerFunction(IAglPayloadLoadingService loadingService
functionOptions.ThrowIfNullOrDefault();

// STEP #1: Load payload.
var loadingServiceOptions = new AglPayloadLoadingServiceOptions();
this._loadingServiceOptions = new AglPayloadLoadingServiceOptions();

await this._loadingService.InvokeAsync(loadingServiceOptions).ConfigureAwait(false);
await this._loadingService.InvokeAsync(this._loadingServiceOptions).ConfigureAwait(false);

if (!loadingServiceOptions.IsInvoked)
if (!this._loadingServiceOptions.IsInvoked)
{
return req.CreateErrorResponse(HttpStatusCode.InternalServerError, "Payload couldn't be loaded");
}

// STEP #2: Process payload.
var processingServiceOptions = new AglPayloadProcessingServiceOptions()
{
People = loadingServiceOptions.People,
PetType = functionOptions.PetType
};
this._processingServiceOptions = new AglPayloadProcessingServiceOptions()
{
People = this._loadingServiceOptions.People,
PetType = functionOptions.PetType
};

await this._processingService.InvokeAsync(processingServiceOptions).ConfigureAwait(false);
await this._processingService.InvokeAsync(this._processingServiceOptions).ConfigureAwait(false);

if (!processingServiceOptions.IsInvoked)
if (!this._processingServiceOptions.IsInvoked)
{
return req.CreateErrorResponse(HttpStatusCode.InternalServerError, "Payload couldn't be processed");
}

// STEP #3: Create response.
var html = new StringBuilder();
html.AppendLine("<html><body>");
html.AppendLine(string.Join(string.Empty, processingServiceOptions.Groups));
html.AppendLine(string.Join(string.Empty, this._processingServiceOptions.Groups));
html.AppendLine("</body></html>");

return req.CreateResponse(HttpStatusCode.OK, html.ToString(), "text/html");
return req.CreateResponse(HttpStatusCode.OK, html.ToString(), new HtmlMediaTypeFormatter());
}

/// <summary>
Expand Down
52 changes: 52 additions & 0 deletions src/AglCodingTest.Functions/Formatters/HtmlMediaTypeFormatter.cs
@@ -0,0 +1,52 @@
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;

namespace AglCodingTest.Functions.Formatters
{
/// <summary>
/// This represents the media type formatter entity for HTML.
/// </summary>
public class HtmlMediaTypeFormatter : MediaTypeFormatter
{
/// <summary>
/// Initializes a new instance of the <see cref="HtmlMediaTypeFormatter"/> class.
/// </summary>
public HtmlMediaTypeFormatter()
{
this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
}

/// <inheritdoc />
public override bool CanReadType(Type type)
{
return false;
}

/// <inheritdoc />
public override bool CanWriteType(Type type)
{
return typeof(string) == type;
}

/// <inheritdoc />
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)
{
using (var writer = new StreamWriter(writeStream))
{
writer.WriteLine(value.ToString());
}

var tcs = new TaskCompletionSource<Stream>();
tcs.SetResult(writeStream);

return tcs.Task;
}
}
}
@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{1CAF0093-DD43-45FB-9E21-CF7147A4F49E}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AglCodingTest.Functions.Tests</RootNamespace>
<AssemblyName>AglCodingTest.Functions.Tests</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoFixture">
<Version>3.51.0</Version>
</PackageReference>
<PackageReference Include="AutoFixture.AutoMoq">
<Version>3.51.0</Version>
</PackageReference>
<PackageReference Include="AutoFixture.Xunit2">
<Version>3.51.0</Version>
</PackageReference>
<PackageReference Include="FluentAssertions">
<Version>4.19.4</Version>
</PackageReference>
<PackageReference Include="Legacy2CPSWorkaround" Version="1.0.0">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Moq">
<Version>4.7.142</Version>
</PackageReference>
<PackageReference Include="Newtonsoft.Json">
<Version>9.0.1</Version>
</PackageReference>
<PackageReference Include="xunit">
<Version>2.3.0</Version>
</PackageReference>
<PackageReference Include="xunit.runner.console">
<Version>2.3.0</Version>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio">
<Version>2.3.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AglCodingTestHttpTriggerFunctionTests.cs" />
<Compile Include="Fixtures\FooFunctionOptions.cs" />
<Compile Include="Fixtures\FunctionFixture.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\AglCodingTest.Functions\AglCodingTest.Functions.csproj">
<Project>{974e7b3f-80de-486c-8968-27219d2f079f}</Project>
<Name>AglCodingTest.Functions</Name>
</ProjectReference>
<ProjectReference Include="..\..\src\AglCodingTest.Models\AglCodingTest.Models.csproj">
<Project>{c7d5bbb5-482d-49f6-9e0e-9f33f95a64cd}</Project>
<Name>AglCodingTest.Models</Name>
</ProjectReference>
<ProjectReference Include="..\..\src\AglCodingTest.Services\AglCodingTest.Services.csproj">
<Project>{2d1b8fd2-f4f5-4354-80fb-569d3491d78b}</Project>
<Name>AglCodingTest.Services</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

0 comments on commit 0e69261

Please sign in to comment.