aboutsummaryrefslogtreecommitdiff
path: root/src/Services/DevHive.Services.Tests/CommentService.Tests.cs
blob: 12229e86d73296dee07549c9c8adc2479bf6a564 (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
using System;
using System.Threading.Tasks;
using AutoMapper;
using DevHive.Data.Interfaces;
using DevHive.Data.Models;
using DevHive.Services.Models.Comment;
using DevHive.Services.Services;
using Moq;
using NUnit.Framework;

namespace DevHive.Services.Tests
{
	[TestFixture]
	public class CommentServiceTests
	{
		private const string MESSAGE = "Gosho Trapov";
		private Mock<IUserRepository> _userRepositoryMock;
		private Mock<IPostRepository> _postRepositoryMock;
		private Mock<ICommentRepository> _commentRepositoryMock;
		private Mock<IMapper> _mapperMock;
		private CommentService _commentService;

		#region Setup
		[SetUp]
		public void Setup()
		{
			this._userRepositoryMock = new Mock<IUserRepository>();
			this._postRepositoryMock = new Mock<IPostRepository>();
			this._commentRepositoryMock = new Mock<ICommentRepository>();
			this._mapperMock = new Mock<IMapper>();
			this._commentService = new CommentService(this._userRepositoryMock.Object, this._postRepositoryMock.Object, this._commentRepositoryMock.Object, this._mapperMock.Object);
		}
		#endregion

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

			this._commentRepositoryMock
				.Setup(p => p.AddAsync(It.IsAny<Comment>()))
				.ReturnsAsync(true);
			this._commentRepositoryMock
				.Setup(p => p.GetCommentByIssuerAndTimeCreatedAsync(It.IsAny<Guid>(), It.IsAny<DateTime>()))
				.ReturnsAsync(comment);
			this._postRepositoryMock
				.Setup(p => p.DoesPostExist(It.IsAny<Guid>()))
				.ReturnsAsync(true);
			this._userRepositoryMock
				.Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
				.ReturnsAsync(creator);
			this._mapperMock
				.Setup(p => p.Map<Comment>(It.IsAny<CreateCommentServiceModel>()))
				.Returns(comment);

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

			Assert.AreEqual(id, result);
		}

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

			this._commentRepositoryMock
				.Setup(p => p.AddAsync(It.IsAny<Comment>()))
				.ReturnsAsync(false);
			this._postRepositoryMock
				.Setup(p => p.DoesPostExist(It.IsAny<Guid>()))
				.ReturnsAsync(true);
			this._mapperMock
				.Setup(p => p.Map<Comment>(It.IsAny<CreateCommentServiceModel>()))
				.Returns(comment);

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

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

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

			CreateCommentServiceModel createCommentServiceModel = new()
			{
				Message = MESSAGE
			};

			Exception ex = Assert.ThrowsAsync<ArgumentNullException>(() => this._commentService.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();
			User creator = new() { Id = creatorId };
			Comment comment = new()
			{
				Message = MESSAGE,
				Creator = creator
			};
			ReadCommentServiceModel commentServiceModel = new()
			{
				Message = MESSAGE
			};
			User user = new()
			{
				Id = creatorId,
			};

			this._commentRepositoryMock
				.Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
				.ReturnsAsync(comment);
			this._userRepositoryMock
				.Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
				.ReturnsAsync(user);
			this._mapperMock
				.Setup(p => p.Map<ReadCommentServiceModel>(It.IsAny<Comment>()))
				.Returns(commentServiceModel);

			ReadCommentServiceModel result = await this._commentService.GetCommentById(Guid.NewGuid());

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

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

			this._commentRepositoryMock
				.Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
				.ReturnsAsync(comment);

			Exception ex = Assert.ThrowsAsync<ArgumentNullException>(() => this._commentService.GetCommentById(Guid.NewGuid()), "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();
			User user = new()
			{
				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>()))
				.ReturnsAsync(user);

			Exception ex = Assert.ThrowsAsync<ArgumentNullException>(() => this._commentService.GetCommentById(Guid.NewGuid()));

			// 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()
			{
				Id = id,
				Message = MESSAGE
			};
			UpdateCommentServiceModel updateCommentServiceModel = new()
			{
				CommentId = id,
				NewMessage = MESSAGE
			};

			this._commentRepositoryMock
				.Setup(p => p.DoesCommentExist(It.IsAny<Guid>()))
				.ReturnsAsync(true);
			this._commentRepositoryMock
				.Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Comment>()))
				.ReturnsAsync(true);
			this._commentRepositoryMock
				.Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
				.ReturnsAsync(comment);
			this._mapperMock
				.Setup(p => p.Map<Comment>(It.IsAny<UpdateCommentServiceModel>()))
				.Returns(comment);

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

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

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

			this._commentRepositoryMock
				.Setup(p => p.DoesCommentExist(It.IsAny<Guid>()))
				.ReturnsAsync(true);
			this._commentRepositoryMock
				.Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Comment>()))
				.ReturnsAsync(false);
			this._mapperMock
				.Setup(p => p.Map<Comment>(It.IsAny<UpdateCommentServiceModel>()))
				.Returns(comment);

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

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

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

			this._commentRepositoryMock
				.Setup(p => p.DoesCommentExist(It.IsAny<Guid>()))
				.ReturnsAsync(false);

			Exception ex = Assert.ThrowsAsync<ArgumentNullException>(() => this._commentService.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();
			Comment comment = new();

			this._commentRepositoryMock
				.Setup(p => p.DoesCommentExist(It.IsAny<Guid>()))
				.ReturnsAsync(true);
			this._commentRepositoryMock
				.Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
				.ReturnsAsync(comment);
			this._commentRepositoryMock
				.Setup(p => p.DeleteAsync(It.IsAny<Comment>()))
				.ReturnsAsync(shouldPass);

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

			Assert.AreEqual(shouldPass, result);
		}

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

			this._commentRepositoryMock
				.Setup(p => p.DoesCommentExist(It.IsAny<Guid>()))
				.ReturnsAsync(false);

			Exception ex = Assert.ThrowsAsync<ArgumentNullException>(() => this._commentService.DeleteComment(id));

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