-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDocumentServiceTests.cs
354 lines (281 loc) · 15.2 KB
/
DocumentServiceTests.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
using DocuTest.Application.Interfaces;
using DocuTest.Application.Services;
using DocuTest.Data.Main.DAL.Interfaces;
using DocuTest.Shared.Models;
using Moq;
using NUnit.Framework;
using System.Data;
namespace DocuTest.Tests.Unit.Services
{
[TestFixture]
public class DocumentServiceTests
{
private readonly Guid userId = new Guid("d9aefc6d-1a69-4548-82c2-39c087e7c739");
private readonly Guid typeId = new Guid("0d66cfc7-4ac7-473e-8256-63e20f7444cd");
private readonly Guid d1Id = new Guid("c1eef796-af0c-41ce-8c31-4ecd7b1ca3f4");
private readonly Guid d2Id = new Guid("b4d1fb42-82b0-4fd1-8066-14b6ff8acc43");
private readonly Guid d1f1Id = new Guid("027ccb5d-d5fe-44fb-8038-17a2bb537cf4");
private readonly Guid d1f2Id = new Guid("6b259824-14cf-41ad-a481-008a1ac50b2c");
private readonly Guid d2f1Id = new Guid("d50b2e9b-d017-4719-b929-de89f347c69f");
private Metadata d1f1m1;
private Metadata d1f1m2;
private Metadata d1f2m1;
private Metadata d1f2m2;
private Metadata d2f1m1;
private Shared.Models.File d1f1;
private Shared.Models.File d1f2;
private Shared.Models.File d2f1;
private Document d1;
private Document d2;
private Mock<IDbConnection> connectionMock = new Mock<IDbConnection>();
private Mock<IDbTransaction> transactionMock = new Mock<IDbTransaction>();
private Mock<IDocumentReadStrategy> documentReadStrategyMock = new Mock<IDocumentReadStrategy>();
private Mock<IDocumentWriteStrategy> documentWriteStrategyMock = new Mock<IDocumentWriteStrategy>();
private Mock<IDbConnectionFactory> connectionFactoryMock = new Mock<IDbConnectionFactory>();
private Mock<IDocumentRepository> documentRepositoryMock = new Mock<IDocumentRepository>();
private Mock<IFileRepository> fileRepositoryMock = new Mock<IFileRepository>();
private Mock<IMetadataRepository> metadataRepositoryMock = new Mock<IMetadataRepository>();
[SetUp]
public void Setup()
{
d1f1m1 = new Metadata() { FileId = d1f1Id, Key = "1", Value = "A" };
d1f1m2 = new Metadata() { FileId = d1f1Id, Key = "2", Value = "B" };
d1f2m1 = new Metadata() { FileId = d1f2Id, Key = "1", Value = "C" };
d1f2m2 = new Metadata() { FileId = d1f2Id, Key = "2", Value = "D" };
d2f1m1 = new Metadata() { FileId = d2f1Id, Key = "1", Value = "A" };
d1f1 = new Shared.Models.File()
{
Id = d1f1Id,
DocumentId = d1Id,
Name = "Document 1, File 1",
Extension = "txt",
Content = new byte[] { },
Metadata = new List<Metadata>() { d1f1m1, d1f1m2 }
};
d1f2 = new Shared.Models.File()
{
Id = d1f2Id,
DocumentId = d1Id,
Name = "Document 1, File 2",
Extension = "txt",
Content = new byte[] { },
Metadata = new List<Metadata>() { d1f2m1, d1f2m2 }
};
d2f1 = new Shared.Models.File()
{
Id = d2f1Id,
DocumentId = d2Id,
Name = "Document 2, File 1",
Extension = "txt",
Content = new byte[] { },
Metadata = new List<Metadata>() { d2f1m1 }
};
d1 = new Document()
{
Id = d1Id,
UserId = userId,
DocumentTypeId = typeId,
Name = "Document 1",
Files = new List<Shared.Models.File>() { d1f1, d1f2 }
};
d2 = new Document()
{
Id = d2Id,
UserId = userId,
DocumentTypeId = typeId,
Name = "Document 2",
Files = new List<Shared.Models.File>() { d2f1 }
};
this.connectionMock = new Mock<IDbConnection>();
this.transactionMock = new Mock<IDbTransaction>();
this.connectionFactoryMock = new Mock<IDbConnectionFactory>();
this.documentRepositoryMock = new Mock<IDocumentRepository>();
this.fileRepositoryMock = new Mock<IFileRepository>();
this.metadataRepositoryMock = new Mock<IMetadataRepository>();
this.connectionMock.Setup(x => x.BeginTransaction()).Returns(this.transactionMock.Object);
this.connectionMock.Setup(x => x.Open()).Verifiable();
this.connectionMock.Setup(x => x.Close()).Verifiable();
this.transactionMock.Setup(x => x.Connection).Returns(this.connectionMock.Object);
this.transactionMock.Setup(x => x.Commit()).Verifiable();
this.transactionMock.Setup(x => x.Rollback()).Verifiable();
this.connectionFactoryMock.Setup(x => x.Create()).Returns(this.connectionMock.Object);
}
[Test]
public async Task GetSingle_ReturnsFullDocument()
{
//Arrange
this.documentRepositoryMock
.Setup(x => x.Get(this.connectionMock.Object, d1.Id, this.documentReadStrategyMock.Object, CancellationToken.None))
.ReturnsAsync(d1);
this.fileRepositoryMock
.Setup(x => x.Get(this.connectionMock.Object, new Guid[] { d1.Id }, CancellationToken.None))
.ReturnsAsync(d1.Files.ToArray());
this.metadataRepositoryMock
.Setup(x => x.Get(this.connectionMock.Object, d1.Files.Select(f => f.Id).ToArray(), CancellationToken.None))
.ReturnsAsync(d1.Files.SelectMany(f => f.Metadata).ToArray());
DocumentService documentServiceSUT = Construct();
//Act
Document result = await documentServiceSUT.Get(d1.Id, CancellationToken.None);
//Assert
Assert.That(result.Equals(d1));
}
[Test]
public async Task GetSingle_NotExisting_ThrowsException()
{
//Arrange
DocumentService documentServiceSUT = Construct();
//Act & Assert
Assert.ThrowsAsync<ArgumentException>(async () => await documentServiceSUT.Get(d1.Id, CancellationToken.None));
}
[Test]
public async Task GetMultiple_ReturnsFullDocuments()
{
//Arrange
this.documentRepositoryMock
.Setup(x => x.Get(this.connectionMock.Object, new Guid[] { d1.Id, d2.Id }, this.documentReadStrategyMock.Object, CancellationToken.None))
.ReturnsAsync(new Document[] { d1, d2 });
this.fileRepositoryMock
.Setup(x => x.Get(this.connectionMock.Object, new Guid[] { d1.Id, d2.Id }, CancellationToken.None))
.ReturnsAsync(d1.Files.Union(d2.Files).ToArray());
this.metadataRepositoryMock
.Setup(x => x.Get(this.connectionMock.Object, d1.Files.Union(d2.Files).Select(f => f.Id).ToArray(), CancellationToken.None))
.ReturnsAsync(d1.Files.Union(d2.Files).SelectMany(f => f.Metadata).ToArray());
DocumentService documentServiceSUT = Construct();
//Act
IEnumerable<Document> result = await documentServiceSUT.Get(new Guid[] { d1.Id, d2.Id }, CancellationToken.None);
//Assert
CollectionAssert.AreEquivalent(new Document[] { d1, d2 }, result);
}
[Test]
public async Task GetByMetadata_GetsAllDocuments()
{
//Arrange
this.documentRepositoryMock
.Setup(x => x.Get(this.connectionMock.Object, new Guid[] { d1.Id, d2.Id }, this.documentReadStrategyMock.Object, CancellationToken.None))
.ReturnsAsync(new Document[] { d1, d2 });
this.fileRepositoryMock
.Setup(x => x.Get(this.connectionMock.Object, new Guid[] { d1.Id, d2.Id }, CancellationToken.None))
.ReturnsAsync(d1.Files.Union(d2.Files).ToArray());
this.fileRepositoryMock
.Setup(x => x.GetDocumentIds(this.connectionMock.Object, new Guid[] { d1f1.Id, d2f1.Id }, CancellationToken.None))
.ReturnsAsync(new Guid[] { d1.Id, d2.Id });
this.metadataRepositoryMock
.Setup(x => x.GetFileIds(this.connectionMock.Object, "1", "A", CancellationToken.None))
.ReturnsAsync(new Guid[] { d1f1.Id, d2f1.Id });
this.metadataRepositoryMock
.Setup(x => x.Get(this.connectionMock.Object, d1.Files.Union(d2.Files).Select(f => f.Id).ToArray(), CancellationToken.None))
.ReturnsAsync(d1.Files.Union(d2.Files).SelectMany(f => f.Metadata).ToArray());
DocumentService documentServiceSUT = Construct();
//Act
IEnumerable<Document> result = await documentServiceSUT.GetByMetadata("1", "A", CancellationToken.None);
//Assert
CollectionAssert.AreEquivalent(new Document[] { d1, d2 }, result);
}
[Test]
public async Task InsertDocument_CommitsTransaction_ReturnsDocumentId()
{
//Arrange
this.documentRepositoryMock
.Setup(x => x.Insert(this.transactionMock.Object, d1, this.documentWriteStrategyMock.Object, CancellationToken.None))
.ReturnsAsync(d1.Id);
this.fileRepositoryMock
.Setup(x => x.Insert(this.transactionMock.Object, d1f1, CancellationToken.None))
.ReturnsAsync(d1f1.Id);
this.fileRepositoryMock
.Setup(x => x.Insert(this.transactionMock.Object, d1f2, CancellationToken.None))
.ReturnsAsync(d1f2.Id);
this.metadataRepositoryMock
.Setup(x => x.Insert(this.transactionMock.Object, d1.Files.SelectMany(f => f.Metadata).ToArray(), CancellationToken.None))
.Verifiable();
DocumentService documentServiceSUT = Construct();
//Act
Guid result = await documentServiceSUT.Insert(d1, CancellationToken.None);
//Assert
this.documentRepositoryMock.Verify(x => x.Insert(this.transactionMock.Object, It.IsAny<Document>(), this.documentWriteStrategyMock.Object, CancellationToken.None), Times.Once);
this.fileRepositoryMock.Verify(x => x.Insert(this.transactionMock.Object, It.IsAny<Shared.Models.File>(), CancellationToken.None), Times.Exactly(2));
this.metadataRepositoryMock.Verify(x => x.Insert(this.transactionMock.Object, It.Is<IEnumerable<Metadata>>(x => x.Count() == 2), CancellationToken.None), Times.Exactly(2));
this.transactionMock.Verify(x => x.Commit(), Times.Once);
Assert.That(result.Equals(d1.Id));
}
[Test]
public async Task InsertDocument_ThrowsException_RollbacksTransaction()
{
//Arrange
this.documentRepositoryMock
.Setup(x => x.Insert(this.transactionMock.Object, d1, this.documentWriteStrategyMock.Object, CancellationToken.None))
.Throws<ArgumentException>();
DocumentService documentServiceSUT = Construct();
//Act & Assert
Assert.ThrowsAsync<ArgumentException>(async () => await documentServiceSUT.Insert(d1, CancellationToken.None));
transactionMock.Verify(x => x.Rollback(), Times.Once);
}
[Test]
public async Task UpdateDocument_CommitsTransaction()
{
//Arrange
this.documentRepositoryMock
.Setup(x => x.Get(this.connectionMock.Object, d1.Id, this.documentReadStrategyMock.Object, CancellationToken.None))
.ReturnsAsync(d1);
this.fileRepositoryMock
.Setup(x => x.Get(this.connectionMock.Object, new Guid[] { d1.Id }, CancellationToken.None))
.ReturnsAsync(d1.Files.ToArray());
this.metadataRepositoryMock
.Setup(x => x.Get(this.connectionMock.Object, d1.Files.Select(f => f.Id).ToArray(), CancellationToken.None))
.ReturnsAsync(d1.Files.SelectMany(f => f.Metadata).ToArray());
this.documentRepositoryMock
.Setup(x => x.Update(this.transactionMock.Object, d1, this.documentWriteStrategyMock.Object, CancellationToken.None))
.Verifiable();
DocumentService documentServiceSUT = Construct();
//Act
await documentServiceSUT.Update(d1, CancellationToken.None);
//Assert
this.documentRepositoryMock.Verify(x => x.Update(this.transactionMock.Object, It.Is<Document>(x => x == d1), this.documentWriteStrategyMock.Object, CancellationToken.None), Times.Once);
this.transactionMock.Verify(x => x.Commit(), Times.Once);
}
[Test]
public void UpdateDocument_NotExisting_ThrowsException()
{
//Arrange
DocumentService documentServiceSUT = Construct();
//Act & Assert
Assert.ThrowsAsync<ArgumentException>(async () => await documentServiceSUT.Update(d1, CancellationToken.None));
}
[Test]
public async Task DeleteDocument_CommitsTransaction()
{
//Arrange
this.fileRepositoryMock
.Setup(x => x.GetFileIds(this.connectionMock.Object, d1.Id, CancellationToken.None))
.ReturnsAsync(d1.Files.Select(f => f.Id));
DocumentService documentServiceSUT = Construct();
//Act
await documentServiceSUT.Delete(d1.Id, CancellationToken.None);
//Assert
this.metadataRepositoryMock.Verify(x => x.Delete(this.transactionMock.Object, It.Is<IEnumerable<Guid>>(x => x.SequenceEqual(d1.Files.Select(f => f.Id))), CancellationToken.None), Times.Once);
this.fileRepositoryMock.Verify(x => x.Delete(this.transactionMock.Object, It.Is<IEnumerable<Guid>>(x => x.SequenceEqual(d1.Files.Select(f => f.Id))), CancellationToken.None), Times.Once);
this.documentRepositoryMock.Verify(x => x.Delete(this.transactionMock.Object, It.Is<Guid>(x => x == d1.Id), this.documentWriteStrategyMock.Object, CancellationToken.None), Times.Once);
this.transactionMock.Verify(x => x.Commit(), Times.Once);
}
[Test]
public void DeleteDocument_ThrowsException_RollbacksTransaction()
{
//Arrange
this.metadataRepositoryMock
.Setup(x => x.Delete(this.transactionMock.Object, It.IsAny<IEnumerable<Guid>>(), CancellationToken.None))
.Throws<ArgumentException>();
DocumentService documentServiceSUT = Construct();
//Act & Assert
Assert.ThrowsAsync<ArgumentException>(async () => await documentServiceSUT.Delete(d1.Id, CancellationToken.None));
transactionMock.Verify(x => x.Rollback(), Times.Once);
}
private DocumentService Construct() =>
new DocumentService(
this.documentReadStrategyMock.Object,
this.documentWriteStrategyMock.Object,
this.documentRepositoryMock.Object,
this.fileRepositoryMock.Object,
this.metadataRepositoryMock.Object,
this.connectionFactoryMock.Object
);
}
}