Skip to content

Commit

Permalink
Added tests for SendNotificationTask and ResolveIncidentTask functions.
Browse files Browse the repository at this point in the history
Fixed bugs and refactored DDB persistance to IncidentRepository
  • Loading branch information
sliedig committed Sep 6, 2018
1 parent db1e68a commit 55cd3c5
Show file tree
Hide file tree
Showing 18 changed files with 333 additions and 163 deletions.
12 changes: 12 additions & 0 deletions AWSStepFunctionsPlagiarismDemo/AWSStepFunctionsPlagiarismDemo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IncidentState", "IncidentSt
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IncidentPersistence", "IncidentPersistence\IncidentPersistence.csproj", "{7547FBCA-C693-4190-BED3-7E7B59ABB95C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SendNotificationTask.Tests", "SendNotificationTask.Tests\SendNotificationTask.Tests.csproj", "{7BC85B28-1084-4439-A601-E456631088DC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ResolveIncidentTask.Tests", "ResolveIncidentTask.Tests\ResolveIncidentTask.Tests.csproj", "{033FDAFD-267E-4BA0-95EB-8EB112D98A03}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -76,6 +80,14 @@ Global
{7547FBCA-C693-4190-BED3-7E7B59ABB95C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7547FBCA-C693-4190-BED3-7E7B59ABB95C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7547FBCA-C693-4190-BED3-7E7B59ABB95C}.Release|Any CPU.Build.0 = Release|Any CPU
{7BC85B28-1084-4439-A601-E456631088DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7BC85B28-1084-4439-A601-E456631088DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7BC85B28-1084-4439-A601-E456631088DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7BC85B28-1084-4439-A601-E456631088DC}.Release|Any CPU.Build.0 = Release|Any CPU
{033FDAFD-267E-4BA0-95EB-8EB112D98A03}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{033FDAFD-267E-4BA0-95EB-8EB112D98A03}.Debug|Any CPU.Build.0 = Debug|Any CPU
{033FDAFD-267E-4BA0-95EB-8EB112D98A03}.Release|Any CPU.ActiveCfg = Release|Any CPU
{033FDAFD-267E-4BA0-95EB-8EB112D98A03}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Amazon.Lambda.Core" Version="1.0.0" />
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.3.0" />
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.3.10.3" />
<PackageReference Include="AWSXRayRecorder.Core" Version="2.2.1-beta" />
<PackageReference Include="AWSXRayRecorder.Handlers.AwsSdk" Version="2.2.1-beta" />
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.3.14" />
<PackageReference Include="AWSXRayRecorder.Core" Version="2.3.0-beta" />
<PackageReference Include="AWSXRayRecorder.Handlers.AwsSdk" Version="2.3.0-beta" />
</ItemGroup>

<ItemGroup>
<DotNetCliToolReference Include="Amazon.Lambda.Tools" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\IncidentPersistence\IncidentPersistence.csproj" />
<ProjectReference Include="..\IncidentState\IncidentState.csproj" />
</ItemGroup>

</Project>
</Project>
40 changes: 12 additions & 28 deletions AWSStepFunctionsPlagiarismDemo/AdminActionTask/Function.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System;
using Amazon;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DocumentModel;
using Amazon.Lambda.Core;
using Amazon.Runtime;
using Amazon.XRay.Recorder.Handlers.AwsSdk;
using IncidentPersistence;
using IncidentState;
using Newtonsoft.Json;

// Assembly attribute to enable the Lambda function's JSON state to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
Expand All @@ -15,14 +15,19 @@ namespace AdminActionTask
{
public class Function
{
private readonly AmazonDynamoDBClient _client;
private readonly string _table_name;

private readonly IIncidentRepository _incidentRepository;

public Function()
{
var tableName = Environment.GetEnvironmentVariable("TABLE_NAME");
_incidentRepository = new IncidentRepository(tableName);
}

public Function(IIncidentRepository incidentRepository)
{
_incidentRepository = incidentRepository;
AWSSDKHandler.RegisterXRayForAllServices();
_client = new AmazonDynamoDBClient(RegionEndpoint.APSoutheast2);
_table_name = Environment.GetEnvironmentVariable("TABLE_NAME");
}

/// <summary>
Expand All @@ -36,30 +41,9 @@ public void FunctionHandler(State state, ILambdaContext context)
state.AdminActionRequired = true;
state.IncidentResolved = false;
state.ResolutionDate = DateTime.Now;

Document incidentDocument = IncidentDocument.BuildDynamoDbDocument(state);

try
{
Console.WriteLine("");
var table = Table.LoadTable(_client, _table_name);
table.PutItemAsync(incidentDocument);
}
catch (AmazonDynamoDBException e)
{
Console.WriteLine(e.Message);
throw;
}
catch (AmazonServiceException e)
{
Console.WriteLine(e.Message);
throw;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw;
}
_incidentRepository.SaveIncident(state);

}


Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.3.10.3" />
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.3.14" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\IncidentState\IncidentState.csproj" />
</ItemGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DocumentModel;
using Amazon.Runtime;
using IncidentState;

namespace IncidentPersistence
{

public interface IIncidentRepository
{
void SaveIncident(State state);
}

public class IncidentRepository : IIncidentRepository
{
private readonly IAmazonDynamoDB _dynamoDb;
private readonly string _tableName;

public IncidentRepository(string tableName)
{
_dynamoDb = new AmazonDynamoDBClient();
_tableName = tableName;
}

public void SaveIncident(State state)
{
var incidentDocument = new IncidentDocument().CreateDocumentFromState(state);

try
{
Console.WriteLine("Saving incident:{0} to {1}", state.IncidentId.ToString("N"), _tableName);
var table = Table.LoadTable(_dynamoDb, _tableName);
table.PutItemAsync(incidentDocument);
}
catch (AmazonDynamoDBException e)
{
Console.WriteLine(e.Message);
throw;
}
catch (AmazonServiceException e)
{
Console.WriteLine(e.Message);
throw;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw;
}
}
}

internal class IncidentDocument
{
internal Document CreateDocumentFromState(State state)
{
var examsList = new DynamoDBList();

foreach (var exam in state.Exams)
{
var examMap = new Document
{
{"ExamId", exam.ExamId},
{"ExamDate", exam.ExamDate},
{"Score", exam.Score}
};

examsList.Add(examMap);
}

var incidentDocument = new Document
{
["IncidentId"] = state.IncidentId,
["StudentId"] = state.StudentId,
["IncidentDate"] = state.IncidentDate,
["AdminActionRequired"] = new DynamoDBBool(state.AdminActionRequired),
["IncidentResolved"] = new DynamoDBBool(state.IncidentResolved),
["ResolutionDate"] = state.ResolutionDate,
["Exams"] = examsList
};

return incidentDocument;
}

}
}
3 changes: 3 additions & 0 deletions AWSStepFunctionsPlagiarismDemo/IncidentState/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ public class Exam
public DateTime ExamDate { get; set; }
public int Score { get; set; }
public ExamResult Result { get; set; }
public bool NotifcationSent { get; set; }

public Exam(Guid examId, DateTime examDate, int score)
{
ExamId = examId;
ExamDate = examDate;
Score = score;
NotifcationSent = false;
}

}

public enum ExamResult
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Amazon.Lambda.Core" Version="1.0.0" />
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.3.0" />
<PackageReference Include="AWSXRayRecorder.Core" Version="2.2.1-beta" />
<PackageReference Include="AWSXRayRecorder.Handlers.AwsSdk" Version="2.2.1-beta" />
<PackageReference Include="AWSXRayRecorder.Core" Version="2.3.0-beta" />
<PackageReference Include="AWSXRayRecorder.Handlers.AwsSdk" Version="2.3.0-beta" />
</ItemGroup>

<ItemGroup>
<DotNetCliToolReference Include="Amazon.Lambda.Tools" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\IncidentState\IncidentState.csproj" />
</ItemGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using Xunit;
using Amazon.Lambda.TestUtilities;
using IncidentPersistence;
using IncidentState;
using NSubstitute;

namespace ResolveIncidentTask.Tests
{
public class FunctionTests
{
public FunctionTests()
{
}

[Fact]
public void ResolveIncidentFunctionTest()
{
var incidentRepository
= Substitute.For<IIncidentRepository>();


var function = new Function(incidentRepository);
var context = new TestLambdaContext();

var state = new State
{
IncidentId = Guid.NewGuid(),
StudentId = "123",
IncidentDate = new DateTime(2018, 02, 03),
Exams = new List<Exam>()
{
new Exam(Guid.NewGuid(), new DateTime(2018, 02, 10), 10),
new Exam(Guid.NewGuid(), new DateTime(2018, 02, 17), 65)
},
ResolutionDate = null
};


incidentRepository.SaveIncident(state);
incidentRepository.ReceivedCalls();

function.FunctionHandler(state, context);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.Core" Version="1.0.0" />
<PackageReference Include="Amazon.Lambda.TestUtilities" Version="1.0.0" />
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="1.1.3" />
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.3.14" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.2" />
<PackageReference Include="NSubstitute" Version="3.1.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ResolveIncidentTask\ResolveIncidentTask.csproj" />
</ItemGroup>
</Project>
Loading

0 comments on commit 55cd3c5

Please sign in to comment.