-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathKinesisEventHandlerTests.cs
153 lines (124 loc) · 5.08 KB
/
KinesisEventHandlerTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Amazon.Lambda.TestUtilities;
using FakeItEasy;
using FluentAssertions;
using KinesisEventHandler.Handlers;
using KinesisEventHandler.Models;
using KinesisEventHandler.UnitTests.Utilities;
using Xunit;
namespace KinesisEventHandler.UnitTests.Handlers;
public class KinesisEventHandlerTests
{
private KinesisEventHandler<Employee> InitializeMockKinesisEventHandler()
{
var fakeKinesisEventHandler = A.Fake<KinesisEventHandler<Employee>>();
A.CallTo(() =>
fakeKinesisEventHandler.ProcessKinesisRecord(A<Employee>._, A<ILambdaContext>._))
.Returns(Task.CompletedTask);
A.CallTo(() => fakeKinesisEventHandler.ValidateKinesisRecord(A<Employee>._))
.Returns(Task.FromResult(true));
return fakeKinesisEventHandler;
}
[Fact]
public async Task KinesisEventHandler_With_OneRecord_Should_CallProcessKinesisRecord_Once()
{
//Arrange
var expected = new EmployeeBuilder().Build();
var kinesisEvent = new KinesisEventBuilder().WithEmployees(new[] { expected });
var lambdaContext = new TestLambdaContext();
//Setup
var mockKinesisEventTrigger = InitializeMockKinesisEventHandler();
//Act
var result = await mockKinesisEventTrigger.Handler(kinesisEvent, lambdaContext);
//Assert
result.BatchItemFailures.Should().BeEmpty();
A.CallTo(() => mockKinesisEventTrigger.ProcessKinesisRecord(
A<Employee>.That.Matches(employee => employee.Equals(expected)),
A<ILambdaContext>._)).MustHaveHappened();
}
[Fact]
public async Task KinesisEventHandler_With_10_Records_Should_CallProcessKinesisRecord_10_Times()
{
//Arrange
var employees = new List<Employee>();
for (var i = 0; i < 10; i++)
{
employees.Add(new EmployeeBuilder().Build());
}
var kinesisEvent = new KinesisEventBuilder().WithEmployees(employees);
var lambdaContext = new TestLambdaContext();
//Setup
var mockKinesisEventTrigger = InitializeMockKinesisEventHandler();
//Act
var result = await mockKinesisEventTrigger.Handler(kinesisEvent, lambdaContext);
//Assert
result.BatchItemFailures.Should().BeEmpty();
A.CallTo(() =>
mockKinesisEventTrigger.ProcessKinesisRecord(A<Employee>._, A<ILambdaContext>._))
.MustHaveHappened(employees.Count, Times.Exactly);
}
[Fact]
public async Task KinesisEventHandler_With_Zero_Records_Should_Not_CallProcessKinesisRecord()
{
//Arrange
var kinesisEvent = new KinesisEventBuilder().WithoutEmployees();
var lambdaContext = new TestLambdaContext();
//Setup
var mockKinesisEventTrigger = InitializeMockKinesisEventHandler();
//Act
await mockKinesisEventTrigger.Handler(kinesisEvent, lambdaContext);
//Assert
A.CallTo(() =>
mockKinesisEventTrigger.ProcessKinesisRecord(
A<Employee>._, A<ILambdaContext>._))
.MustNotHaveHappened();
}
[Fact]
public async Task KinesisEventHandler_With_InvalidRecords_Should_Return_BatchItemFailures()
{
//Arrange
var randomNumber = (new Random()).Next(2, 20);
var validEmployees = new List<Employee>();
var invalidEmployees = new List<Employee>();
//Adding valid Employees
for (var i = 0; i < randomNumber; i++)
{
validEmployees.Add(new EmployeeBuilder().Build());
}
//Adding invalid Employees
for (var i = 0; i < randomNumber; i++)
{
invalidEmployees.Add(new EmployeeBuilder().WithEmployeeId(null));
}
var employees = new List<Employee>();
employees.AddRange(validEmployees);
employees.AddRange(invalidEmployees);
var kinesisEvent = new KinesisEventBuilder().WithEmployees(employees);
var lambdaContext = new TestLambdaContext();
//Setup
var mockKinesisEventTrigger = InitializeMockKinesisEventHandler();
//Specific mock for this test
A.CallTo(() => mockKinesisEventTrigger.ValidateKinesisRecord(
A<Employee>.That.Matches(e => invalidEmployees.Contains(e))))
.Throws<ValidationException>();
A.CallTo(() => mockKinesisEventTrigger.ValidateKinesisRecord(
A<Employee>.That.Matches(e => validEmployees.Contains(e))))
.Returns(Task.FromResult(true));
//Act
var result = await mockKinesisEventTrigger.Handler(kinesisEvent, lambdaContext);
//Assert
result.BatchItemFailures.Should().HaveCount(invalidEmployees.Count).And.OnlyHaveUniqueItems();
result.BatchItemFailures
.Select(b => b.ItemIdentifier)
.Should()
.BeSubsetOf(
kinesisEvent.Records
.Select(k => k.Kinesis.SequenceNumber)
);
}
}