aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs
blob: bf6817b0d179b84b022d66ed49edf88c54a04514 (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
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.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<IMapper> MapperMock { get; set; }
		private PostService PostService { get; set; }

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

		#region Comment
		#region Create
		[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.PostRepositoryMock.Setup(p => p.AddCommentAsync(It.IsAny<Comment>())).Returns(Task.FromResult(true));
			this.PostRepositoryMock.Setup(p => p.GetCommentByIssuerAndTimeCreatedAsync(It.IsAny<Guid>(), It.IsAny<DateTime>())).Returns(Task.FromResult(comment));
			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 CreateLanguage_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully()
		{
			CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel
			{
				Message = MESSAGE
			};
			Comment comment = new Comment
			{
				Message = MESSAGE,
			};

			this.PostRepositoryMock.Setup(p => p.AddCommentAsync(It.IsAny<Comment>())).Returns(Task.FromResult(false));
			this.MapperMock.Setup(p => p.Map<Comment>(It.IsAny<CreateCommentServiceModel>())).Returns(comment);

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

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

		#region Read
		[Test]
		public async Task GetCommentById_ReturnsTheComment_WhenItExists()
		{
			Guid id = new Guid();
			Comment comment = new Comment
			{
				Message = MESSAGE
			};
			CommentServiceModel commentServiceModel = new CommentServiceModel
			{
				Message = MESSAGE
			};

			this.PostRepositoryMock.Setup(p => p.GetCommentByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(comment));
			this.MapperMock.Setup(p => p.Map<CommentServiceModel>(It.IsAny<Comment>())).Returns(commentServiceModel);

			CommentServiceModel result = await this.PostService.GetCommentById(id);

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

		[Test]
		public void GetLanguageById_ThrowsException_WhenLanguageDoesNotExist()
		{
			string exceptionMessage = "The comment does not exist";
			Guid id = new Guid();
			this.PostRepositoryMock.Setup(p => p.GetCommentByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult<Comment>(null));

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

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

		#region Update
		[Test]
		[TestCase(true)]
		[TestCase(false)]
		public async Task UpdateComment_ReturnsIfUpdateIsSuccessfull_WhenCommentExistsy(bool shouldPass)
		{
			Comment comment = new Comment
			{
				Message = MESSAGE
			};
			UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel
			{
				Message = MESSAGE
			};

			this.PostRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
			this.PostRepositoryMock.Setup(p => p.EditCommentAsync(It.IsAny<Comment>())).Returns(Task.FromResult(shouldPass));
			this.MapperMock.Setup(p => p.Map<Comment>(It.IsAny<UpdateCommentServiceModel>())).Returns(comment);

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

			Assert.AreEqual(shouldPass, result);
		}

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

			this.PostRepositoryMock.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 Delete
		[Test]
		[TestCase(true)]
		[TestCase(false)]
		public async Task DeleteComment_ShouldReturnIfDeletionIsSuccessfull_WhenCommentExists(bool shouldPass)
		{
			Guid id = new Guid();
			Comment comment = new Comment();

			this.PostRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
			this.PostRepositoryMock.Setup(p => p.GetCommentByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(comment));
			this.PostRepositoryMock.Setup(p => p.DeleteCommentAsync(It.IsAny<Comment>())).Returns(Task.FromResult(shouldPass));

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

			Assert.AreEqual(shouldPass, result);
		}

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

			this.PostRepositoryMock.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
	}
}