aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs
blob: 2f59376405e839802c1a64ddb027ac65c5084724 (plain) (blame)
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
using System;
using System.Threading.Tasks;
using AutoMapper;
using DevHive.Data.Interfaces.Repositories;
using DevHive.Data.Models;
using DevHive.Services.Models.Post.Comment;
using DevHive.Services.Models.Post.Post;
using DevHive.Services.Services;
using Moq;
using NUnit.Framework;

namespace DevHive.Services.Tests
{
	[TestFixture]
	public class PostServiceTests
	{
		private const string MESSAGE = "Gosho Trapov";
		private Mock<IPostRepository> PostRepositoryMock { get; set; }
		private Mock<IUserRepository> UserRepositoryMock { get; set; }
		private Mock<ICommentRepository> CommentRepositoryMock { get; set; }
		private Mock<IMapper> MapperMock { get; set; }
		private PostService PostService { get; set; }

		#region SetUps
		[SetUp]
		public void Setup()
		{
			this.PostRepositoryMock = new Mock<IPostRepository>();
			this.UserRepositoryMock = new Mock<IUserRepository>();
			this.CommentRepositoryMock = new Mock<ICommentRepository>();
			this.MapperMock = new Mock<IMapper>();
			this.PostService = new PostService(this.UserRepositoryMock.Object, this.PostRepositoryMock.Object, this.CommentRepositoryMock.Object, this.MapperMock.Object);
		}
		#endregion

		#region Comment
		#region AddComment
		[Test]
		public async Task AddComment_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully()
		{
			Guid id = Guid.NewGuid();
			CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel
			{
				Message = MESSAGE
			};
			Comment comment = new Comment
			{
				Message = MESSAGE,
				Id = id
			};

			this.CommentRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Comment>())).Returns(Task.FromResult(true));
			this.CommentRepositoryMock.Setup(p => p.GetCommentByIssuerAndTimeCreatedAsync(It.IsAny<Guid>(), It.IsAny<DateTime>())).Returns(Task.FromResult(comment));
			this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
			this.MapperMock.Setup(p => p.Map<Comment>(It.IsAny<CreateCommentServiceModel>())).Returns(comment);

			Guid result = await this.PostService.AddComment(createCommentServiceModel);

			Assert.AreEqual(id, result);
		}

		[Test]
		public async Task AddComment_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully()
		{
			CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel
			{
				Message = MESSAGE
			};
			Comment comment = new Comment
			{
				Message = MESSAGE,
			};

			this.CommentRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Comment>())).Returns(Task.FromResult(false));
			this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
			this.MapperMock.Setup(p => p.Map<Comment>(It.IsAny<CreateCommentServiceModel>())).Returns(comment);

			Guid result = await this.PostService.AddComment(createCommentServiceModel);

			Assert.IsTrue(result == Guid.Empty);
		}

		[Test]
		public void AddComment_ThrowsException_WhenPostDoesNotExist()
		{
			const string EXCEPTION_MESSAGE = "Post does not exist!";

			CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel
			{
				Message = MESSAGE
			};

			Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.AddComment(createCommentServiceModel), "AddComment does not throw excpeion when the post does not exist");

			Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorecct exception message");
		}
		#endregion

		#region GetCommentById
		[Test]
		public async Task GetCommentById_ReturnsTheComment_WhenItExists()
		{
			Guid creatorId = new Guid();
			Comment comment = new Comment
			{
				Message = MESSAGE,
				CreatorId = creatorId
			};
			ReadCommentServiceModel commentServiceModel = new ReadCommentServiceModel
			{
				Message = MESSAGE
			};
			User user = new User
			{
				Id = creatorId,
			};

			this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(comment));
			this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user));
			this.MapperMock.Setup(p => p.Map<ReadCommentServiceModel>(It.IsAny<Comment>())).Returns(commentServiceModel);

			ReadCommentServiceModel result = await this.PostService.GetCommentById(new Guid());

			Assert.AreEqual(MESSAGE, result.Message);
		}

		[Test]
		public void GetCommentById_ThorwsException_WhenTheUserDoesNotExist()
		{
			const string EXCEPTION_MESSAGE = "The user does not exist";
			Guid creatorId = new Guid();
			Comment comment = new Comment
			{
				Message = MESSAGE,
				CreatorId = creatorId
			};

			this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(comment));

			Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.GetCommentById(new Guid()), "GetCommentById does not throw exception when the user does not exist");

			Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message);
		}

		[Test]
		public void GetCommentById_ThrowsException_WhenCommentDoesNotExist()
		{
			string exceptionMessage = "The comment does not exist";
			Guid creatorId = new Guid();
			User user = new User
			{
				Id = creatorId,
			};

			this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult<Comment>(null));
			this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user));

			Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.GetCommentById(new Guid()));

			Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
		}
		#endregion

		#region UpdateComment
		[Test]
		public async Task UpdateComment_ReturnsTheIdOfTheComment_WhenUpdatedSuccessfully()
		{
			Guid id = Guid.NewGuid();
			Comment comment = new Comment
			{
				Id = id,
				Message = MESSAGE
			};
			UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel
			{
				CommentId = id,
				NewMessage = MESSAGE
			};

			this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
			this.CommentRepositoryMock.Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Comment>())).Returns(Task.FromResult(true));
			this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(comment));
			this.MapperMock.Setup(p => p.Map<Comment>(It.IsAny<UpdateCommentServiceModel>())).Returns(comment);

			Guid result = await this.PostService.UpdateComment(updateCommentServiceModel);

			Assert.AreEqual(updateCommentServiceModel.CommentId, result);
		}

		[Test]
		public async Task UpdateComment_ReturnsEmptyId_WhenTheCommentIsNotUpdatedSuccessfully()
		{
			Comment comment = new Comment
			{
				Message = MESSAGE
			};
			UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel
			{
				CommentId = Guid.NewGuid(),
				NewMessage = MESSAGE
			};

			this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
			this.CommentRepositoryMock.Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Comment>())).Returns(Task.FromResult(false));
			this.MapperMock.Setup(p => p.Map<Comment>(It.IsAny<UpdateCommentServiceModel>())).Returns(comment);

			Guid result = await this.PostService.UpdateComment(updateCommentServiceModel);

			Assert.AreEqual(Guid.Empty, result);
		}

		[Test]
		public void UpdateComment_ThrowsArgumentException_WhenCommentDoesNotExist()
		{
			string exceptionMessage = "Comment does not exist!";
			UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel
			{
			};

			this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));

			Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.UpdateComment(updateCommentServiceModel));

			Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
		}
		#endregion

		#region DeleteComment
		[Test]
		[TestCase(true)]
		[TestCase(false)]
		public async Task DeleteComment_ShouldReturnIfDeletionIsSuccessfull_WhenCommentExists(bool shouldPass)
		{
			Guid id = new Guid();
			Comment comment = new Comment();

			this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
			this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(comment));
			this.CommentRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny<Comment>())).Returns(Task.FromResult(shouldPass));

			bool result = await this.PostService.DeleteComment(id);

			Assert.AreEqual(shouldPass, result);
		}

		[Test]
		public void DeleteComment_ThrowsException_WhenCommentDoesNotExist()
		{
			string exceptionMessage = "Comment does not exist!";
			Guid id = new Guid();

			this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));

			Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.DeleteComment(id));

			Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
		}
		#endregion

		#region ValidateJwtForComment
		//TO DO: Implement
		#endregion
		#endregion

		#region Posts
		#region CreatePost
		[Test]
		public async Task CreatePost_ReturnsIdOfThePost_WhenItIsSuccessfullyCreated()
		{
			Guid postId = Guid.NewGuid();
			CreatePostServiceModel createPostServiceModel = new CreatePostServiceModel
			{
			};
			Post post = new Post
			{
				Message = MESSAGE,
				Id = postId
			};

			this.PostRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Post>())).Returns(Task.FromResult(true));
			this.PostRepositoryMock.Setup(p => p.GetPostByCreatorAndTimeCreatedAsync(It.IsAny<Guid>(), It.IsAny<DateTime>())).Returns(Task.FromResult(post));
			this.UserRepositoryMock.Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(true));
			this.MapperMock.Setup(p => p.Map<Post>(It.IsAny<CreatePostServiceModel>())).Returns(post);

			Guid result = await this.PostService.CreatePost(createPostServiceModel);

			Assert.AreEqual(postId, result, "CreatePost does not return the correct id");
		}

		[Test]
		public async Task CreatePost_ReturnsEmptyGuid_WhenItIsNotSuccessfullyCreated()
		{
			CreatePostServiceModel createPostServiceModel = new CreatePostServiceModel
			{
			};
			Post post = new Post
			{
				Message = MESSAGE,
			};

			this.PostRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Post>())).Returns(Task.FromResult(false));
			this.UserRepositoryMock.Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(true));
			this.MapperMock.Setup(p => p.Map<Post>(It.IsAny<CreatePostServiceModel>())).Returns(post);

			Guid result = await this.PostService.CreatePost(createPostServiceModel);

			Assert.AreEqual(Guid.Empty, result, "CreatePost does not return empty id");
		}

		[Test]
		public void CreatePost_ThrowsException_WhenUserDoesNotExist()
		{
			const string EXCEPTION_MESSAGE = "User does not exist!";
			CreatePostServiceModel createPostServiceModel = new CreatePostServiceModel
			{
			};
			Post post = new Post
			{
				Message = MESSAGE,
			};

			Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.CreatePost(createPostServiceModel), "CreatePost does not throw excpeion when the user does not exist");

			Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Excapetion message is not correct");
		}
		#endregion

		#region GetPostById
		[Test]
		public async Task GetPostById_ReturnsThePost_WhenItExists()
		{
			Guid creatorId = new Guid();
			Post post = new Post
			{
				Message = MESSAGE,
				CreatorId = creatorId
			};
			ReadPostServiceModel readPostServiceModel = new ReadPostServiceModel
			{
				Message = MESSAGE
			};
			User user = new User
			{
				Id = creatorId,
			};

			this.PostRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(post));
			this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user));
			this.MapperMock.Setup(p => p.Map<ReadPostServiceModel>(It.IsAny<Post>())).Returns(readPostServiceModel);

			ReadPostServiceModel result = await this.PostService.GetPostById(new Guid());

			Assert.AreEqual(MESSAGE, result.Message);
		}

		[Test]
		public void GetPostById_ThorwsException_WhenTheUserDoesNotExist()
		{
			const string EXCEPTION_MESSAGE = "The user does not exist!";
			Guid creatorId = new Guid();
			Post post = new Post
			{
				Message = MESSAGE,
				CreatorId = creatorId
			};

			this.PostRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(post));

			Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.GetPostById(new Guid()), "GetPostById does not throw exception when the user does not exist");

			Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message);
		}

		[Test]
		public void GetPostById_ThrowsException_WhenCommentDoesNotExist()
		{
			string exceptionMessage = "The post does not exist!";
			Guid creatorId = new Guid();
			User user = new User
			{
				Id = creatorId,
			};

			this.PostRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult<Post>(null));
			this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user));

			Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.GetPostById(new Guid()));

			Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
		}
		#endregion

		#region UpdatePost
		[Test]
		public async Task UpdatePost_ReturnsTheIdOfThePost_WhenUpdatedSuccessfully()
		{
			Guid id = Guid.NewGuid();
			Post post = new Post
			{
				Id = id,
				Message = MESSAGE
			};
			UpdatePostServiceModel updatePostServiceModel = new UpdatePostServiceModel
			{
				PostId = id,
				NewMessage = MESSAGE
			};

			this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
			this.PostRepositoryMock.Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Post>())).Returns(Task.FromResult(true));
			this.PostRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(post));
			this.MapperMock.Setup(p => p.Map<Post>(It.IsAny<UpdatePostServiceModel>())).Returns(post);

			Guid result = await this.PostService.UpdatePost(updatePostServiceModel);

			Assert.AreEqual(updatePostServiceModel.PostId, result);
		}

		[Test]
		public async Task UpdatePost_ReturnsEmptyId_WhenThePostIsNotUpdatedSuccessfully()
		{
			Post post = new Post
			{
				Message = MESSAGE
			};
			UpdatePostServiceModel updatePostServiceModel = new UpdatePostServiceModel
			{
				PostId = Guid.NewGuid(),
				NewMessage = MESSAGE
			};

			this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
			this.PostRepositoryMock.Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Post>())).Returns(Task.FromResult(false));
			this.MapperMock.Setup(p => p.Map<Post>(It.IsAny<UpdatePostServiceModel>())).Returns(post);

			Guid result = await this.PostService.UpdatePost(updatePostServiceModel);

			Assert.AreEqual(Guid.Empty, result);
		}

		[Test]
		public void UpdatePost_ThrowsArgumentException_WhenCommentDoesNotExist()
		{
			string exceptionMessage = "Post does not exist!";
			UpdatePostServiceModel updatePostServiceModel = new UpdatePostServiceModel
			{
			};

			this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));

			Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.UpdatePost(updatePostServiceModel));

			Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
		}
		#endregion

		#region DeletePost
		[Test]
		[TestCase(true)]
		[TestCase(false)]
		public async Task Deletepost_ShouldReturnIfDeletionIsSuccessfull_WhenPostExists(bool shouldPass)
		{
			Guid id = new Guid();
			Post post = new Post();

			this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
			this.PostRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(post));
			this.PostRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny<Post>())).Returns(Task.FromResult(shouldPass));

			bool result = await this.PostService.DeletePost(id);

			Assert.AreEqual(shouldPass, result);
		}

		[Test]
		public void DeletePost_ThrowsException_WhenPostDoesNotExist()
		{
			string exceptionMessage = "Post does not exist!";
			Guid id = new Guid();

			this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));

			Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.DeletePost(id));

			Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
		}
		#endregion

		#region ValidateJwtForPost
		//TO DO: Implement
		#endregion
		#endregion
	}
}