aboutsummaryrefslogtreecommitdiff
path: root/src/Services
diff options
context:
space:
mode:
Diffstat (limited to 'src/Services')
-rw-r--r--src/Services/DevHive.Services.Models/Rating/CreateRatingServiceModel.cs (renamed from src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs)2
-rw-r--r--src/Services/DevHive.Services.Models/Rating/ReadRatingServiceModel.cs (renamed from src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs)2
-rw-r--r--src/Services/DevHive.Services.Models/Rating/UpdateRatingServiceModel.cs (renamed from src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs)2
-rw-r--r--src/Services/DevHive.Services.Tests/CommentService.Tests.cs198
-rw-r--r--src/Services/DevHive.Services.Tests/LanguageService.Tests.cs152
-rw-r--r--src/Services/DevHive.Services.Tests/PostService.Tests.cs168
-rw-r--r--src/Services/DevHive.Services.Tests/RatingService.Tests.cs448
-rw-r--r--src/Services/DevHive.Services.Tests/RoleService.Tests.cs136
-rw-r--r--src/Services/DevHive.Services.Tests/TechnologyServices.Tests.cs156
-rw-r--r--src/Services/DevHive.Services.Tests/UserService.Tests.cs448
-rw-r--r--src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs2
-rw-r--r--src/Services/DevHive.Services/Interfaces/IRatingService.cs4
-rw-r--r--src/Services/DevHive.Services/Services/FeedService.cs3
-rw-r--r--src/Services/DevHive.Services/Services/PostService.cs10
-rw-r--r--src/Services/DevHive.Services/Services/RatingService.cs10
15 files changed, 1231 insertions, 510 deletions
diff --git a/src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Rating/CreateRatingServiceModel.cs
index aaeab73..486e3d2 100644
--- a/src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs
+++ b/src/Services/DevHive.Services.Models/Rating/CreateRatingServiceModel.cs
@@ -1,6 +1,6 @@
using System;
-namespace DevHive.Services.Models.Post.Rating
+namespace DevHive.Services.Models.Rating
{
public class CreateRatingServiceModel
{
diff --git a/src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Rating/ReadRatingServiceModel.cs
index 86b4957..56a5ab0 100644
--- a/src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs
+++ b/src/Services/DevHive.Services.Models/Rating/ReadRatingServiceModel.cs
@@ -1,6 +1,6 @@
using System;
-namespace DevHive.Services.Models.Post.Rating
+namespace DevHive.Services.Models.Rating
{
public class ReadRatingServiceModel
{
diff --git a/src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Rating/UpdateRatingServiceModel.cs
index 1ea8d8f..923e789 100644
--- a/src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs
+++ b/src/Services/DevHive.Services.Models/Rating/UpdateRatingServiceModel.cs
@@ -1,6 +1,6 @@
using System;
-namespace DevHive.Services.Models.Post.Rating
+namespace DevHive.Services.Models.Rating
{
public class UpdateRatingServiceModel
{
diff --git a/src/Services/DevHive.Services.Tests/CommentService.Tests.cs b/src/Services/DevHive.Services.Tests/CommentService.Tests.cs
index d511739..3d58bc6 100644
--- a/src/Services/DevHive.Services.Tests/CommentService.Tests.cs
+++ b/src/Services/DevHive.Services.Tests/CommentService.Tests.cs
@@ -14,21 +14,21 @@ namespace DevHive.Services.Tests
public class CommentServiceTests
{
private const string MESSAGE = "Gosho Trapov";
- private Mock<IUserRepository> UserRepositoryMock { get; set; }
- private Mock<IPostRepository> PostRepositoryMock { get; set; }
- private Mock<ICommentRepository> CommentRepositoryMock { get; set; }
- private Mock<IMapper> MapperMock { get; set; }
- private CommentService CommentService { get; set; }
+ 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);
+ 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
@@ -37,24 +37,34 @@ namespace DevHive.Services.Tests
public async Task AddComment_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully()
{
Guid id = Guid.NewGuid();
- User creator = new User { Id = Guid.NewGuid() };
- CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel
+ User creator = new() { Id = Guid.NewGuid() };
+ CreateCommentServiceModel createCommentServiceModel = new()
{
Message = MESSAGE
};
- Comment comment = new Comment
+ Comment comment = new()
{
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.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(creator));
- this.MapperMock.Setup(p => p.Map<Comment>(It.IsAny<CreateCommentServiceModel>())).Returns(comment);
-
- Guid result = await this.CommentService.AddComment(createCommentServiceModel);
+ 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);
}
@@ -62,20 +72,26 @@ namespace DevHive.Services.Tests
[Test]
public async Task AddComment_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully()
{
- CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel
+ CreateCommentServiceModel createCommentServiceModel = new()
{
Message = MESSAGE
};
- Comment comment = new Comment
+ Comment comment = new()
{
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);
+ 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);
+ Guid result = await this._commentService.AddComment(createCommentServiceModel);
Assert.IsTrue(result == Guid.Empty);
}
@@ -85,12 +101,12 @@ namespace DevHive.Services.Tests
{
const string EXCEPTION_MESSAGE = "Post does not exist!";
- CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel
+ CreateCommentServiceModel createCommentServiceModel = new()
{
Message = MESSAGE
};
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.CommentService.AddComment(createCommentServiceModel), "AddComment does not throw excpeion when the post does not exist");
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._commentService.AddComment(createCommentServiceModel), "AddComment does not throw excpeion when the post does not exist");
Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorecct exception message");
}
@@ -100,27 +116,33 @@ namespace DevHive.Services.Tests
[Test]
public async Task GetCommentById_ReturnsTheComment_WhenItExists()
{
- Guid creatorId = new Guid();
- User creator = new User { Id = creatorId };
- Comment comment = new Comment
+ Guid creatorId = new();
+ User creator = new() { Id = creatorId };
+ Comment comment = new()
{
Message = MESSAGE,
Creator = creator
};
- ReadCommentServiceModel commentServiceModel = new ReadCommentServiceModel
+ ReadCommentServiceModel commentServiceModel = new()
{
Message = MESSAGE
};
- User user = new User
+ User user = new()
{
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);
+ 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(new Guid());
+ ReadCommentServiceModel result = await this._commentService.GetCommentById(Guid.NewGuid());
Assert.AreEqual(MESSAGE, result.Message);
}
@@ -129,17 +151,19 @@ namespace DevHive.Services.Tests
public void GetCommentById_ThorwsException_WhenTheUserDoesNotExist()
{
const string EXCEPTION_MESSAGE = "The user does not exist";
- Guid creatorId = new Guid();
- User creator = new User { Id = creatorId };
- Comment comment = new Comment
+ Guid creatorId = new();
+ User creator = new() { Id = creatorId };
+ Comment comment = new()
{
Message = MESSAGE,
Creator = creator
};
- this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(comment));
+ this._commentRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(comment);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.CommentService.GetCommentById(new Guid()), "GetCommentById does not throw exception when the user does not exist");
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._commentService.GetCommentById(Guid.NewGuid()), "GetCommentById does not throw exception when the user does not exist");
Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message);
}
@@ -148,16 +172,20 @@ namespace DevHive.Services.Tests
public void GetCommentById_ThrowsException_WhenCommentDoesNotExist()
{
string exceptionMessage = "The comment does not exist";
- Guid creatorId = new Guid();
- User user = new User
+ 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>())).Returns(Task.FromResult(user));
+ 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<ArgumentException>(() => this.CommentService.GetCommentById(new Guid()));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._commentService.GetCommentById(Guid.NewGuid()));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
@@ -168,23 +196,31 @@ namespace DevHive.Services.Tests
public async Task UpdateComment_ReturnsTheIdOfTheComment_WhenUpdatedSuccessfully()
{
Guid id = Guid.NewGuid();
- Comment comment = new Comment
+ Comment comment = new()
{
Id = id,
Message = MESSAGE
};
- UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel
+ UpdateCommentServiceModel updateCommentServiceModel = new()
{
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.CommentService.UpdateComment(updateCommentServiceModel);
+ 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);
}
@@ -192,21 +228,27 @@ namespace DevHive.Services.Tests
[Test]
public async Task UpdateComment_ReturnsEmptyId_WhenTheCommentIsNotUpdatedSuccessfully()
{
- Comment comment = new Comment
+ Comment comment = new()
{
Message = MESSAGE
};
- UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel
+ UpdateCommentServiceModel updateCommentServiceModel = new()
{
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);
+ 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);
+ Guid result = await this._commentService.UpdateComment(updateCommentServiceModel);
Assert.AreEqual(Guid.Empty, result);
}
@@ -215,13 +257,15 @@ namespace DevHive.Services.Tests
public void UpdateComment_ThrowsArgumentException_WhenCommentDoesNotExist()
{
string exceptionMessage = "Comment does not exist!";
- UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel
+ UpdateCommentServiceModel updateCommentServiceModel = new()
{
};
- this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+ this._commentRepositoryMock
+ .Setup(p => p.DoesCommentExist(It.IsAny<Guid>()))
+ .ReturnsAsync(false);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.CommentService.UpdateComment(updateCommentServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._commentService.UpdateComment(updateCommentServiceModel));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
@@ -233,14 +277,20 @@ namespace DevHive.Services.Tests
[TestCase(false)]
public async Task DeleteComment_ShouldReturnIfDeletionIsSuccessfull_WhenCommentExists(bool shouldPass)
{
- Guid id = new Guid();
- Comment comment = new Comment();
+ Guid id = new();
+ Comment comment = new();
- 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));
+ 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);
+ bool result = await this._commentService.DeleteComment(id);
Assert.AreEqual(shouldPass, result);
}
@@ -249,11 +299,13 @@ namespace DevHive.Services.Tests
public void DeleteComment_ThrowsException_WhenCommentDoesNotExist()
{
string exceptionMessage = "Comment does not exist!";
- Guid id = new Guid();
+ Guid id = new();
- this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+ this._commentRepositoryMock
+ .Setup(p => p.DoesCommentExist(It.IsAny<Guid>()))
+ .ReturnsAsync(false);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.CommentService.DeleteComment(id));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._commentService.DeleteComment(id));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
diff --git a/src/Services/DevHive.Services.Tests/LanguageService.Tests.cs b/src/Services/DevHive.Services.Tests/LanguageService.Tests.cs
index 731091c..c95c38e 100644
--- a/src/Services/DevHive.Services.Tests/LanguageService.Tests.cs
+++ b/src/Services/DevHive.Services.Tests/LanguageService.Tests.cs
@@ -14,17 +14,17 @@ namespace DevHive.Services.Tests
[TestFixture]
public class LanguageServiceTests
{
- private Mock<ILanguageRepository> LanguageRepositoryMock { get; set; }
- private Mock<IMapper> MapperMock { get; set; }
- private LanguageService LanguageService { get; set; }
+ private Mock<ILanguageRepository> _languageRepositoryMock;
+ private Mock<IMapper> _mapperMock;
+ private LanguageService _languageService;
#region SetUps
[SetUp]
public void SetUp()
{
- this.LanguageRepositoryMock = new Mock<ILanguageRepository>();
- this.MapperMock = new Mock<IMapper>();
- this.LanguageService = new LanguageService(this.LanguageRepositoryMock.Object, this.MapperMock.Object);
+ this._languageRepositoryMock = new Mock<ILanguageRepository>();
+ this._mapperMock = new Mock<IMapper>();
+ this._languageService = new LanguageService(this._languageRepositoryMock.Object, this._mapperMock.Object);
}
#endregion
@@ -44,12 +44,20 @@ namespace DevHive.Services.Tests
Id = id
};
- this.LanguageRepositoryMock.Setup(p => p.DoesLanguageNameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(false));
- this.LanguageRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Language>())).Returns(Task.FromResult(true));
- this.LanguageRepositoryMock.Setup(p => p.GetByNameAsync(It.IsAny<string>())).Returns(Task.FromResult(language));
- this.MapperMock.Setup(p => p.Map<Language>(It.IsAny<CreateLanguageServiceModel>())).Returns(language);
-
- Guid result = await this.LanguageService.CreateLanguage(createLanguageServiceModel);
+ this._languageRepositoryMock
+ .Setup(p => p.DoesLanguageNameExistAsync(It.IsAny<string>()))
+ .ReturnsAsync(false);
+ this._languageRepositoryMock
+ .Setup(p => p.AddAsync(It.IsAny<Language>()))
+ .ReturnsAsync(true);
+ this._languageRepositoryMock
+ .Setup(p => p.GetByNameAsync(It.IsAny<string>()))
+ .ReturnsAsync(language);
+ this._mapperMock
+ .Setup(p => p.Map<Language>(It.IsAny<CreateLanguageServiceModel>()))
+ .Returns(language);
+
+ Guid result = await this._languageService.CreateLanguage(createLanguageServiceModel);
Assert.AreEqual(id, result);
}
@@ -68,11 +76,17 @@ namespace DevHive.Services.Tests
Name = languageName
};
- this.LanguageRepositoryMock.Setup(p => p.DoesLanguageNameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(false));
- this.LanguageRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Language>())).Returns(Task.FromResult(false));
- this.MapperMock.Setup(p => p.Map<Language>(It.IsAny<CreateLanguageServiceModel>())).Returns(language);
+ this._languageRepositoryMock
+ .Setup(p => p.DoesLanguageNameExistAsync(It.IsAny<string>()))
+ .ReturnsAsync(false);
+ this._languageRepositoryMock
+ .Setup(p => p.AddAsync(It.IsAny<Language>()))
+ .ReturnsAsync(false);
+ this._mapperMock
+ .Setup(p => p.Map<Language>(It.IsAny<CreateLanguageServiceModel>()))
+ .Returns(language);
- Guid result = await this.LanguageService.CreateLanguage(createLanguageServiceModel);
+ Guid result = await this._languageService.CreateLanguage(createLanguageServiceModel);
Assert.IsTrue(result == Guid.Empty);
@@ -93,9 +107,11 @@ namespace DevHive.Services.Tests
Name = languageName
};
- this.LanguageRepositoryMock.Setup(p => p.DoesLanguageNameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(true));
+ this._languageRepositoryMock
+ .Setup(p => p.DoesLanguageNameExistAsync(It.IsAny<string>()))
+ .ReturnsAsync(true);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.LanguageService.CreateLanguage(createLanguageServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._languageService.CreateLanguage(createLanguageServiceModel));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
@@ -105,7 +121,7 @@ namespace DevHive.Services.Tests
[Test]
public async Task GetLanguageById_ReturnsTheLanguage_WhenItExists()
{
- Guid id = new Guid();
+ Guid id = Guid.NewGuid();
string name = "Gosho Trapov";
Language language = new Language
{
@@ -116,10 +132,14 @@ namespace DevHive.Services.Tests
Name = name
};
- this.LanguageRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(language));
- this.MapperMock.Setup(p => p.Map<ReadLanguageServiceModel>(It.IsAny<Language>())).Returns(readLanguageServiceModel);
+ this._languageRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(language);
+ this._mapperMock
+ .Setup(p => p.Map<ReadLanguageServiceModel>(It.IsAny<Language>()))
+ .Returns(readLanguageServiceModel);
- ReadLanguageServiceModel result = await this.LanguageService.GetLanguageById(id);
+ ReadLanguageServiceModel result = await this._languageService.GetLanguageById(id);
Assert.AreEqual(name, result.Name);
}
@@ -128,10 +148,12 @@ namespace DevHive.Services.Tests
public void GetLanguageById_ThrowsException_WhenLanguageDoesNotExist()
{
string exceptionMessage = "The language does not exist";
- Guid id = new Guid();
- this.LanguageRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult<Language>(null));
+ Guid id = Guid.NewGuid();
+ this._languageRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .Returns(Task.FromResult<Language>(null));
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.LanguageService.GetLanguageById(id));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._languageService.GetLanguageById(id));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
@@ -147,10 +169,14 @@ namespace DevHive.Services.Tests
languges.Add(firstLanguage);
languges.Add(secondLanguage);
- this.LanguageRepositoryMock.Setup(p => p.GetLanguages()).Returns(new HashSet<Language>());
- this.MapperMock.Setup(p => p.Map<HashSet<ReadLanguageServiceModel>>(It.IsAny<HashSet<Language>>())).Returns(languges);
+ this._languageRepositoryMock
+ .Setup(p => p.GetLanguages())
+ .Returns(new HashSet<Language>());
+ this._mapperMock
+ .Setup(p => p.Map<HashSet<ReadLanguageServiceModel>>(It.IsAny<HashSet<Language>>()))
+ .Returns(languges);
- HashSet<ReadLanguageServiceModel> result = this.LanguageService.GetLanguages();
+ HashSet<ReadLanguageServiceModel> result = this._languageService.GetLanguages();
Assert.GreaterOrEqual(2, result.Count, "GetLanguages does not return all languages");
}
@@ -158,10 +184,14 @@ namespace DevHive.Services.Tests
[Test]
public void GetLanguages_ReturnsEmptyHashSet_IfNoLanguagesExist()
{
- this.LanguageRepositoryMock.Setup(p => p.GetLanguages()).Returns(new HashSet<Language>());
- this.MapperMock.Setup(p => p.Map<HashSet<ReadLanguageServiceModel>>(It.IsAny<HashSet<Language>>())).Returns(new HashSet<ReadLanguageServiceModel>());
+ this._languageRepositoryMock
+ .Setup(p => p.GetLanguages())
+ .Returns(new HashSet<Language>());
+ this._mapperMock
+ .Setup(p => p.Map<HashSet<ReadLanguageServiceModel>>(It.IsAny<HashSet<Language>>()))
+ .Returns(new HashSet<ReadLanguageServiceModel>());
- HashSet<ReadLanguageServiceModel> result = this.LanguageService.GetLanguages();
+ HashSet<ReadLanguageServiceModel> result = this._languageService.GetLanguages();
Assert.IsEmpty(result, "GetLanguages does not return empty string when no languages exist");
}
@@ -185,12 +215,20 @@ namespace DevHive.Services.Tests
Name = name,
};
- this.LanguageRepositoryMock.Setup(p => p.DoesLanguageExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(true));
- this.LanguageRepositoryMock.Setup(p => p.DoesLanguageNameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(false));
- this.LanguageRepositoryMock.Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Language>())).Returns(Task.FromResult(shouldPass));
- this.MapperMock.Setup(p => p.Map<Language>(It.IsAny<UpdateLanguageServiceModel>())).Returns(language);
-
- bool result = await this.LanguageService.UpdateLanguage(updateLanguageServiceModel);
+ this._languageRepositoryMock
+ .Setup(p => p.DoesLanguageExistAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._languageRepositoryMock
+ .Setup(p => p.DoesLanguageNameExistAsync(It.IsAny<string>()))
+ .ReturnsAsync(false);
+ this._languageRepositoryMock
+ .Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Language>()))
+ .ReturnsAsync(shouldPass);
+ this._mapperMock
+ .Setup(p => p.Map<Language>(It.IsAny<UpdateLanguageServiceModel>()))
+ .Returns(language);
+
+ bool result = await this._languageService.UpdateLanguage(updateLanguageServiceModel);
Assert.AreEqual(shouldPass, result);
}
@@ -203,9 +241,11 @@ namespace DevHive.Services.Tests
{
};
- this.LanguageRepositoryMock.Setup(p => p.DoesLanguageExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+ this._languageRepositoryMock
+ .Setup(p => p.DoesLanguageExistAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(false);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.LanguageService.UpdateLanguage(updateTechnologyServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._languageService.UpdateLanguage(updateTechnologyServiceModel));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
@@ -218,10 +258,14 @@ namespace DevHive.Services.Tests
{
};
- this.LanguageRepositoryMock.Setup(p => p.DoesLanguageExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(true));
- this.LanguageRepositoryMock.Setup(p => p.DoesLanguageNameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(true));
+ this._languageRepositoryMock
+ .Setup(p => p.DoesLanguageExistAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._languageRepositoryMock
+ .Setup(p => p.DoesLanguageNameExistAsync(It.IsAny<string>()))
+ .ReturnsAsync(true);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.LanguageService.UpdateLanguage(updateTechnologyServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._languageService.UpdateLanguage(updateTechnologyServiceModel));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
@@ -233,14 +277,20 @@ namespace DevHive.Services.Tests
[TestCase(false)]
public async Task DeleteLanguage_ShouldReturnIfDeletionIsSuccessfull_WhenLanguageExists(bool shouldPass)
{
- Guid id = new Guid();
+ Guid id = Guid.NewGuid();
Language language = new Language();
- this.LanguageRepositoryMock.Setup(p => p.DoesLanguageExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(true));
- this.LanguageRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(language));
- this.LanguageRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny<Language>())).Returns(Task.FromResult(shouldPass));
+ this._languageRepositoryMock
+ .Setup(p => p.DoesLanguageExistAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._languageRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(language);
+ this._languageRepositoryMock
+ .Setup(p => p.DeleteAsync(It.IsAny<Language>()))
+ .ReturnsAsync(shouldPass);
- bool result = await this.LanguageService.DeleteLanguage(id);
+ bool result = await this._languageService.DeleteLanguage(id);
Assert.AreEqual(shouldPass, result);
}
@@ -249,11 +299,13 @@ namespace DevHive.Services.Tests
public void DeleteLanguage_ThrowsException_WhenLanguageDoesNotExist()
{
string exceptionMessage = "Language does not exist!";
- Guid id = new Guid();
+ Guid id = Guid.NewGuid();
- this.LanguageRepositoryMock.Setup(p => p.DoesLanguageExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+ this._languageRepositoryMock
+ .Setup(p => p.DoesLanguageExistAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(false);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.LanguageService.DeleteLanguage(id));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._languageService.DeleteLanguage(id));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
diff --git a/src/Services/DevHive.Services.Tests/PostService.Tests.cs b/src/Services/DevHive.Services.Tests/PostService.Tests.cs
index 137b8d1..058485e 100644
--- a/src/Services/DevHive.Services.Tests/PostService.Tests.cs
+++ b/src/Services/DevHive.Services.Tests/PostService.Tests.cs
@@ -17,23 +17,23 @@ namespace DevHive.Services.Tests
public class PostServiceTests
{
private const string MESSAGE = "Gosho Trapov";
- private Mock<ICloudService> CloudServiceMock { get; set; }
- private Mock<IPostRepository> PostRepositoryMock { get; set; }
- private Mock<ICommentRepository> CommentRepositoryMock { get; set; }
- private Mock<IUserRepository> UserRepositoryMock { get; set; }
- private Mock<IMapper> MapperMock { get; set; }
- private PostService PostService { get; set; }
+ private Mock<ICloudService> _cloudServiceMock;
+ private Mock<IPostRepository> _postRepositoryMock;
+ private Mock<ICommentRepository> _commentRepositoryMock;
+ private Mock<IUserRepository> _userRepositoryMock;
+ private Mock<IMapper> _mapperMock;
+ private PostService _postService;
#region SetUps
[SetUp]
public void Setup()
{
- this.PostRepositoryMock = new Mock<IPostRepository>();
- this.CloudServiceMock = new Mock<ICloudService>();
- this.UserRepositoryMock = new Mock<IUserRepository>();
- this.CommentRepositoryMock = new Mock<ICommentRepository>();
- this.MapperMock = new Mock<IMapper>();
- this.PostService = new PostService(this.CloudServiceMock.Object, this.UserRepositoryMock.Object, this.PostRepositoryMock.Object, this.CommentRepositoryMock.Object, this.MapperMock.Object);
+ this._postRepositoryMock = new Mock<IPostRepository>();
+ this._cloudServiceMock = new Mock<ICloudService>();
+ this._userRepositoryMock = new Mock<IUserRepository>();
+ this._commentRepositoryMock = new Mock<ICommentRepository>();
+ this._mapperMock = new Mock<IMapper>();
+ this._postService = new PostService(this._cloudServiceMock.Object, this._userRepositoryMock.Object, this._postRepositoryMock.Object, this._commentRepositoryMock.Object, this._mapperMock.Object);
}
#endregion
@@ -53,13 +53,23 @@ namespace DevHive.Services.Tests
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.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(creator));
- this.MapperMock.Setup(p => p.Map<Post>(It.IsAny<CreatePostServiceModel>())).Returns(post);
-
- Guid result = await this.PostService.CreatePost(createPostServiceModel);
+ this._postRepositoryMock
+ .Setup(p => p.AddAsync(It.IsAny<Post>()))
+ .ReturnsAsync(true);
+ this._postRepositoryMock
+ .Setup(p => p.GetPostByCreatorAndTimeCreatedAsync(It.IsAny<Guid>(), It.IsAny<DateTime>()))
+ .ReturnsAsync(post);
+ this._userRepositoryMock
+ .Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._userRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(creator);
+ 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");
}
@@ -76,11 +86,17 @@ namespace DevHive.Services.Tests
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);
+ this._postRepositoryMock
+ .Setup(p => p.AddAsync(It.IsAny<Post>()))
+ .ReturnsAsync(false);
+ this._userRepositoryMock
+ .Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._mapperMock
+ .Setup(p => p.Map<Post>(It.IsAny<CreatePostServiceModel>()))
+ .Returns(post);
- Guid result = await this.PostService.CreatePost(createPostServiceModel);
+ Guid result = await this._postService.CreatePost(createPostServiceModel);
Assert.AreEqual(Guid.Empty, result, "CreatePost does not return empty id");
}
@@ -92,12 +108,8 @@ namespace DevHive.Services.Tests
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");
+ 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");
}
@@ -107,7 +119,7 @@ namespace DevHive.Services.Tests
[Test]
public async Task GetPostById_ReturnsThePost_WhenItExists()
{
- Guid creatorId = new Guid();
+ Guid creatorId = Guid.NewGuid();
User creator = new User { Id = creatorId };
Post post = new Post
{
@@ -123,11 +135,17 @@ namespace DevHive.Services.Tests
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);
+ this._postRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(post);
+ this._userRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(user);
+ this._mapperMock
+ .Setup(p => p.Map<ReadPostServiceModel>(It.IsAny<Post>()))
+ .Returns(readPostServiceModel);
- ReadPostServiceModel result = await this.PostService.GetPostById(new Guid());
+ ReadPostServiceModel result = await this._postService.GetPostById(Guid.NewGuid());
Assert.AreEqual(MESSAGE, result.Message);
}
@@ -136,7 +154,7 @@ namespace DevHive.Services.Tests
public void GetPostById_ThorwsException_WhenTheUserDoesNotExist()
{
const string EXCEPTION_MESSAGE = "The user does not exist!";
- Guid creatorId = new Guid();
+ Guid creatorId = Guid.NewGuid();
User creator = new User { Id = creatorId };
Post post = new Post
{
@@ -144,9 +162,11 @@ namespace DevHive.Services.Tests
Creator = creator
};
- this.PostRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(post));
+ this._postRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(post);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.GetPostById(new Guid()), "GetPostById does not throw exception when the user does not exist");
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._postService.GetPostById(Guid.NewGuid()), "GetPostById does not throw exception when the user does not exist");
Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message);
}
@@ -155,16 +175,20 @@ namespace DevHive.Services.Tests
public void GetPostById_ThrowsException_WhenCommentDoesNotExist()
{
string exceptionMessage = "The post does not exist!";
- Guid creatorId = new Guid();
+ Guid creatorId = Guid.NewGuid();
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));
+ this._postRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .Returns(Task.FromResult<Post>(null));
+ this._userRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(user);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.GetPostById(new Guid()));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._postService.GetPostById(Guid.NewGuid()));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
@@ -187,12 +211,20 @@ namespace DevHive.Services.Tests
Files = new List<IFormFile>()
};
- 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);
+ this._postRepositoryMock
+ .Setup(p => p.DoesPostExist(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._postRepositoryMock
+ .Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Post>()))
+ .ReturnsAsync(true);
+ this._postRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(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);
}
@@ -211,11 +243,17 @@ namespace DevHive.Services.Tests
Files = new List<IFormFile>()
};
- 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);
+ this._postRepositoryMock
+ .Setup(p => p.DoesPostExist(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._postRepositoryMock
+ .Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Post>()))
+ .ReturnsAsync(false);
+ this._mapperMock
+ .Setup(p => p.Map<Post>(It.IsAny<UpdatePostServiceModel>()))
+ .Returns(post);
- Guid result = await this.PostService.UpdatePost(updatePostServiceModel);
+ Guid result = await this._postService.UpdatePost(updatePostServiceModel);
Assert.AreEqual(Guid.Empty, result);
}
@@ -228,9 +266,11 @@ namespace DevHive.Services.Tests
{
};
- this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+ this._postRepositoryMock
+ .Setup(p => p.DoesPostExist(It.IsAny<Guid>()))
+ .ReturnsAsync(false);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.UpdatePost(updatePostServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._postService.UpdatePost(updatePostServiceModel));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
@@ -242,14 +282,20 @@ namespace DevHive.Services.Tests
[TestCase(false)]
public async Task Deletepost_ShouldReturnIfDeletionIsSuccessfull_WhenPostExists(bool shouldPass)
{
- Guid id = new Guid();
+ Guid id = Guid.NewGuid();
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));
+ this._postRepositoryMock
+ .Setup(p => p.DoesPostExist(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._postRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(post);
+ this._postRepositoryMock
+ .Setup(p => p.DeleteAsync(It.IsAny<Post>()))
+ .ReturnsAsync(shouldPass);
- bool result = await this.PostService.DeletePost(id);
+ bool result = await this._postService.DeletePost(id);
Assert.AreEqual(shouldPass, result);
}
@@ -258,11 +304,13 @@ namespace DevHive.Services.Tests
public void DeletePost_ThrowsException_WhenPostDoesNotExist()
{
string exceptionMessage = "Post does not exist!";
- Guid id = new Guid();
+ Guid id = Guid.NewGuid();
- this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+ this._postRepositoryMock
+ .Setup(p => p.DoesPostExist(It.IsAny<Guid>()))
+ .ReturnsAsync(false);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.DeletePost(id));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._postService.DeletePost(id));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
diff --git a/src/Services/DevHive.Services.Tests/RatingService.Tests.cs b/src/Services/DevHive.Services.Tests/RatingService.Tests.cs
new file mode 100644
index 0000000..5f84530
--- /dev/null
+++ b/src/Services/DevHive.Services.Tests/RatingService.Tests.cs
@@ -0,0 +1,448 @@
+using System;
+using System.Threading.Tasks;
+using AutoMapper;
+using DevHive.Data.Interfaces;
+using DevHive.Data.Models;
+using DevHive.Services.Models.Rating;
+using DevHive.Services.Services;
+using Moq;
+using NUnit.Framework;
+
+namespace DevHive.Services.Tests
+{
+ [TestFixture]
+ public class RatingServiceTests
+ {
+ private Mock<IPostRepository> _postRepositoryMock;
+ private Mock<IRatingRepository> _ratingRepositoryMock;
+ private Mock<IUserRepository> _userRepositoryMock;
+ private Mock<IMapper> _mapperMock;
+ private RatingService _ratingService;
+
+ #region SetUps
+ [SetUp]
+ public void SetUp()
+ {
+ this._postRepositoryMock = new Mock<IPostRepository>();
+ this._ratingRepositoryMock = new Mock<IRatingRepository>();
+ this._userRepositoryMock = new Mock<IUserRepository>();
+ this._mapperMock = new Mock<IMapper>();
+ this._ratingService = new RatingService(this._postRepositoryMock.Object, this._ratingRepositoryMock.Object, this._userRepositoryMock.Object, this._mapperMock.Object);
+ }
+ #endregion
+
+ #region Create
+ [Test]
+ public async Task RatePost_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully()
+ {
+ bool isLike = true;
+ Guid id = Guid.NewGuid();
+ Guid postId = Guid.NewGuid();
+ Guid userId = Guid.NewGuid();
+ CreateRatingServiceModel createRatingServiceModel = new CreateRatingServiceModel
+ {
+ PostId = postId,
+ UserId = userId,
+ IsLike = isLike
+ };
+ Rating rating = new Rating
+ {
+ Id = id,
+ IsLike = isLike
+ };
+ User user = new User
+ {
+ Id = userId
+ };
+ Post post = new Post
+ {
+ Id = postId
+ };
+
+ this._postRepositoryMock
+ .Setup(p => p.DoesPostExist(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._ratingRepositoryMock
+ .Setup(p => p.UserRatedPost(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .ReturnsAsync(false);
+ this._userRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(user);
+ this._postRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(post);
+ this._ratingRepositoryMock
+ .Setup(p => p.AddAsync(It.IsAny<Rating>()))
+ .ReturnsAsync(true);
+ this._ratingRepositoryMock
+ .Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .ReturnsAsync(rating);
+ this._mapperMock
+ .Setup(p => p.Map<Rating>(It.IsAny<CreateRatingServiceModel>()))
+ .Returns(rating);
+
+ Guid result = await this._ratingService.RatePost(createRatingServiceModel);
+
+ Assert.AreEqual(id, result);
+ }
+
+ [Test]
+ public async Task RatePost_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully()
+ {
+ bool isLike = true;
+ Guid id = Guid.NewGuid();
+ Guid postId = Guid.NewGuid();
+ Guid userId = Guid.NewGuid();
+ CreateRatingServiceModel createRatingServiceModel = new CreateRatingServiceModel
+ {
+ PostId = postId,
+ UserId = userId,
+ IsLike = isLike
+ };
+ Rating rating = new Rating
+ {
+ Id = id,
+ IsLike = isLike
+ };
+ User user = new User
+ {
+ Id = userId
+ };
+ Post post = new Post
+ {
+ Id = postId
+ };
+
+ this._postRepositoryMock
+ .Setup(p => p.DoesPostExist(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._ratingRepositoryMock
+ .Setup(p => p.UserRatedPost(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .ReturnsAsync(false);
+ this._userRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(user);
+ this._postRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(post);
+ this._ratingRepositoryMock
+ .Setup(p => p.AddAsync(It.IsAny<Rating>()))
+ .ReturnsAsync(false);
+ this._mapperMock
+ .Setup(p => p.Map<Rating>(It.IsAny<CreateRatingServiceModel>()))
+ .Returns(rating);
+
+ Guid result = await this._ratingService.RatePost(createRatingServiceModel);
+
+ Assert.AreEqual(result, Guid.Empty);
+ }
+ #endregion
+
+ #region Read
+ [Test]
+ public async Task GetRatingById_ReturnsTheRating_WhenItExists()
+ {
+ Guid id = Guid.NewGuid();
+ bool isLike = true;
+ User user = new User
+ {
+ Id = Guid.NewGuid()
+ };
+ Rating rating = new Rating
+ {
+ Id = id,
+ IsLike = isLike,
+ User = user
+ };
+ ReadRatingServiceModel readRatingServiceModel = new ReadRatingServiceModel
+ {
+ Id = id,
+ IsLike = isLike
+ };
+
+ this._ratingRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(rating);
+ this._mapperMock
+ .Setup(p => p.Map<ReadRatingServiceModel>(It.IsAny<Rating>()))
+ .Returns(readRatingServiceModel);
+
+ ReadRatingServiceModel result = await this._ratingService.GetRatingById(id);
+
+ Assert.AreEqual(isLike, result.IsLike);
+ }
+
+ [Test]
+ public void GetRatingById_ThrowsException_WhenRatingDoesNotExist()
+ {
+ string exceptionMessage = "The rating does not exist";
+ this._ratingRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .Returns(Task.FromResult<Rating>(null));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._ratingService.GetRatingById(Guid.Empty));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+
+ [Test]
+ public async Task GetRatingByPostAndUser_ReturnsTheRating_WhenItExists()
+ {
+ Guid id = Guid.NewGuid();
+ bool isLike = true;
+ User user = new User
+ {
+ Id = Guid.NewGuid()
+ };
+ Rating rating = new Rating
+ {
+ Id = id,
+ IsLike = isLike,
+ User = user
+ };
+ ReadRatingServiceModel readRatingServiceModel = new ReadRatingServiceModel
+ {
+ Id = id,
+ IsLike = isLike
+ };
+
+ this._ratingRepositoryMock
+ .Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .ReturnsAsync(rating);
+ this._mapperMock
+ .Setup(p => p.Map<ReadRatingServiceModel>(It.IsAny<Rating>()))
+ .Returns(readRatingServiceModel);
+
+ ReadRatingServiceModel result = await this._ratingService.GetRatingByPostAndUser(user.Id, Guid.Empty);
+
+ Assert.AreEqual(isLike, result.IsLike);
+ }
+
+ [Test]
+ public void GetRatingByPostAndUser_ThrowsException_WhenRatingDoesNotExist()
+ {
+ string exceptionMessage = "The rating does not exist";
+ this._ratingRepositoryMock
+ .Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .Returns(Task.FromResult<Rating>(null));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._ratingService.GetRatingById(Guid.Empty));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+
+ #region Update
+ [Test]
+ public async Task UpdateRating_ReturnsObject_WhenRatingIsUpdatedSuccessfully()
+ {
+ Guid id = Guid.NewGuid();
+ bool isLike = true;
+ UpdateRatingServiceModel updateRatingServiceModel = new UpdateRatingServiceModel
+ {
+ Id = id,
+ IsLike = isLike
+ };
+ User user = new User
+ {
+ Id = Guid.NewGuid()
+ };
+ Rating rating = new Rating
+ {
+ Id = id,
+ IsLike = isLike,
+ User = user
+ };
+ ReadRatingServiceModel readRatingServiceModel = new ReadRatingServiceModel
+ {
+ Id = id,
+ IsLike = isLike
+ };
+
+ this._ratingRepositoryMock
+ .Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .ReturnsAsync(rating);
+ this._userRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(user);
+ this._ratingRepositoryMock
+ .Setup(p => p.UserRatedPost(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._ratingRepositoryMock
+ .Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Rating>()))
+ .ReturnsAsync(true);
+ this._mapperMock
+ .Setup(p => p.Map<ReadRatingServiceModel>(It.IsAny<Rating>()))
+ .Returns(readRatingServiceModel);
+
+ ReadRatingServiceModel result = await this._ratingService.UpdateRating(updateRatingServiceModel);
+
+ Assert.AreEqual(result, readRatingServiceModel);
+ }
+
+ [Test]
+ public async Task UpdateRating_ReturnsNull_WhenRatingIsNotUpdatedSuccessfully()
+ {
+ Guid id = Guid.NewGuid();
+ bool isLike = true;
+ UpdateRatingServiceModel updateRatingServiceModel = new UpdateRatingServiceModel
+ {
+ Id = id,
+ IsLike = isLike
+ };
+ User user = new User
+ {
+ Id = Guid.NewGuid()
+ };
+ Rating rating = new Rating
+ {
+ Id = id,
+ IsLike = isLike,
+ User = user
+ };
+ ReadRatingServiceModel readRatingServiceModel = new ReadRatingServiceModel
+ {
+ Id = id,
+ IsLike = isLike
+ };
+
+ this._ratingRepositoryMock
+ .Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .ReturnsAsync(rating);
+ this._userRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(user);
+ this._ratingRepositoryMock
+ .Setup(p => p.UserRatedPost(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._ratingRepositoryMock
+ .Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Rating>()))
+ .ReturnsAsync(false);
+
+ ReadRatingServiceModel result = await this._ratingService.UpdateRating(updateRatingServiceModel);
+
+ Assert.IsNull(result);
+ }
+
+ [Test]
+ public void UpdateRating_ThrowsException_WhenRatingDoesNotExists()
+ {
+ string exceptionMessage = "Rating does not exist!";
+ UpdateRatingServiceModel updateRatingServiceModel = new UpdateRatingServiceModel
+ {
+ Id = Guid.Empty,
+ IsLike = true
+ };
+
+ this._ratingRepositoryMock
+ .Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .Returns(Task.FromResult<Rating>(null));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._ratingService.UpdateRating(updateRatingServiceModel));
+
+ Assert.AreEqual(ex.Message, exceptionMessage);
+ }
+
+ [Test]
+ public void UpdateRating_ThrowsException_WhenUserHasNotRatedPost()
+ {
+ string exceptionMessage = "User has not rated the post!";
+ Guid id = Guid.NewGuid();
+ bool isLike = true;
+ UpdateRatingServiceModel updateRatingServiceModel = new UpdateRatingServiceModel
+ {
+ Id = id,
+ IsLike = isLike
+ };
+ User user = new User
+ {
+ Id = Guid.NewGuid()
+ };
+ Rating rating = new Rating
+ {
+ Id = id,
+ IsLike = isLike,
+ User = user
+ };
+
+ this._ratingRepositoryMock
+ .Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .ReturnsAsync(rating);
+ this._userRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(user);
+ this._ratingRepositoryMock
+ .Setup(p => p.UserRatedPost(It.IsAny<Guid>(), It.IsAny<Guid>()))
+ .ReturnsAsync(false);
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._ratingService.UpdateRating(updateRatingServiceModel));
+
+ Assert.AreEqual(ex.Message, exceptionMessage);
+ }
+ #endregion
+
+ #region Delete
+ [Test]
+ public async Task DeleteRating_ReturnsTrue_WhenRatingIsDeletedSuccessfully()
+ {
+ Guid ratingId = Guid.NewGuid();
+ Rating rating = new Rating
+ {
+ Id = ratingId
+ };
+
+ this._ratingRepositoryMock
+ .Setup(p => p.DoesRatingExist(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._ratingRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .Returns(Task.FromResult<Rating>(null));
+ this._ratingRepositoryMock
+ .Setup(p => p.DeleteAsync(It.IsAny<Rating>()))
+ .ReturnsAsync(true);
+
+ bool result = await this._ratingService.DeleteRating(ratingId);
+
+ Assert.IsTrue(result);
+ }
+
+ [Test]
+ public async Task DeleteRating_ReturnsFalse_WhenRatingIsNotDeletedSuccessfully()
+ {
+ Guid ratingId = Guid.NewGuid();
+ Rating rating = new Rating
+ {
+ Id = ratingId
+ };
+
+ this._ratingRepositoryMock
+ .Setup(p => p.DoesRatingExist(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._ratingRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .Returns(Task.FromResult<Rating>(null));
+ this._ratingRepositoryMock
+ .Setup(p => p.DeleteAsync(It.IsAny<Rating>()))
+ .ReturnsAsync(false);
+
+ bool result = await this._ratingService.DeleteRating(ratingId);
+
+ Assert.IsFalse(result);
+ }
+
+ [Test]
+ public void DeleteRating_ThrowsException_WhenRatingDoesNotExist()
+ {
+ string exceptionMessage = "Rating does not exist!";
+
+ this._ratingRepositoryMock
+ .Setup(p => p.DoesRatingExist(It.IsAny<Guid>()))
+ .ReturnsAsync(false);
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._ratingService.DeleteRating(Guid.Empty));
+
+ Assert.AreEqual(ex.Message, exceptionMessage);
+ }
+ #endregion
+ }
+}
diff --git a/src/Services/DevHive.Services.Tests/RoleService.Tests.cs b/src/Services/DevHive.Services.Tests/RoleService.Tests.cs
index 29d5df2..83dabf9 100644
--- a/src/Services/DevHive.Services.Tests/RoleService.Tests.cs
+++ b/src/Services/DevHive.Services.Tests/RoleService.Tests.cs
@@ -13,17 +13,17 @@ namespace DevHive.Services.Tests
[TestFixture]
public class RoleServiceTests
{
- private Mock<IRoleRepository> RoleRepositoryMock { get; set; }
- private Mock<IMapper> MapperMock { get; set; }
- private RoleService RoleService { get; set; }
+ private Mock<IRoleRepository> _roleRepositoryMock;
+ private Mock<IMapper> _mapperMock;
+ private RoleService _roleService;
#region SetUps
[SetUp]
public void Setup()
{
- this.RoleRepositoryMock = new Mock<IRoleRepository>();
- this.MapperMock = new Mock<IMapper>();
- this.RoleService = new RoleService(this.RoleRepositoryMock.Object, this.MapperMock.Object);
+ this._roleRepositoryMock = new Mock<IRoleRepository>();
+ this._mapperMock = new Mock<IMapper>();
+ this._roleService = new RoleService(this._roleRepositoryMock.Object, this._mapperMock.Object);
}
#endregion
@@ -43,12 +43,20 @@ namespace DevHive.Services.Tests
Id = id
};
- this.RoleRepositoryMock.Setup(p => p.DoesNameExist(It.IsAny<string>())).Returns(Task.FromResult(false));
- this.RoleRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Role>())).Returns(Task.FromResult(true));
- this.RoleRepositoryMock.Setup(p => p.GetByNameAsync(It.IsAny<string>())).Returns(Task.FromResult(role));
- this.MapperMock.Setup(p => p.Map<Role>(It.IsAny<CreateRoleServiceModel>())).Returns(role);
-
- Guid result = await this.RoleService.CreateRole(createRoleServiceModel);
+ this._roleRepositoryMock
+ .Setup(p => p.DoesNameExist(It.IsAny<string>()))
+ .ReturnsAsync(false);
+ this._roleRepositoryMock
+ .Setup(p => p.AddAsync(It.IsAny<Role>()))
+ .ReturnsAsync(true);
+ this._roleRepositoryMock
+ .Setup(p => p.GetByNameAsync(It.IsAny<string>()))
+ .ReturnsAsync(role);
+ this._mapperMock
+ .Setup(p => p.Map<Role>(It.IsAny<CreateRoleServiceModel>()))
+ .Returns(role);
+
+ Guid result = await this._roleService.CreateRole(createRoleServiceModel);
Assert.AreEqual(id, result);
}
@@ -67,11 +75,17 @@ namespace DevHive.Services.Tests
Name = roleName
};
- this.RoleRepositoryMock.Setup(p => p.DoesNameExist(It.IsAny<string>())).Returns(Task.FromResult(false));
- this.RoleRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Role>())).Returns(Task.FromResult(false));
- this.MapperMock.Setup(p => p.Map<Role>(It.IsAny<CreateRoleServiceModel>())).Returns(role);
+ this._roleRepositoryMock
+ .Setup(p => p.DoesNameExist(It.IsAny<string>()))
+ .ReturnsAsync(false);
+ this._roleRepositoryMock
+ .Setup(p => p.AddAsync(It.IsAny<Role>()))
+ .ReturnsAsync(false);
+ this._mapperMock
+ .Setup(p => p.Map<Role>(It.IsAny<CreateRoleServiceModel>()))
+ .Returns(role);
- Guid result = await this.RoleService.CreateRole(createRoleServiceModel);
+ Guid result = await this._roleService.CreateRole(createRoleServiceModel);
Assert.IsTrue(result == Guid.Empty);
}
@@ -86,14 +100,12 @@ namespace DevHive.Services.Tests
{
Name = roleName
};
- Role role = new Role
- {
- Name = roleName
- };
- this.RoleRepositoryMock.Setup(p => p.DoesNameExist(It.IsAny<string>())).Returns(Task.FromResult(true));
+ this._roleRepositoryMock
+ .Setup(p => p.DoesNameExist(It.IsAny<string>()))
+ .ReturnsAsync(true);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.RoleService.CreateRole(createRoleServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._roleService.CreateRole(createRoleServiceModel));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
@@ -103,7 +115,7 @@ namespace DevHive.Services.Tests
[Test]
public async Task GetRoleById_ReturnsTheRole_WhenItExists()
{
- Guid id = new Guid();
+ Guid id = Guid.NewGuid();
string name = "Gosho Trapov";
Role role = new Role
{
@@ -114,10 +126,14 @@ namespace DevHive.Services.Tests
Name = name
};
- this.RoleRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(role));
- this.MapperMock.Setup(p => p.Map<RoleServiceModel>(It.IsAny<Role>())).Returns(roleServiceModel);
+ this._roleRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(role);
+ this._mapperMock
+ .Setup(p => p.Map<RoleServiceModel>(It.IsAny<Role>()))
+ .Returns(roleServiceModel);
- RoleServiceModel result = await this.RoleService.GetRoleById(id);
+ RoleServiceModel result = await this._roleService.GetRoleById(id);
Assert.AreEqual(name, result.Name);
}
@@ -126,10 +142,12 @@ namespace DevHive.Services.Tests
public void GetRoleById_ThrowsException_WhenRoleDoesNotExist()
{
string exceptionMessage = "Role does not exist!";
- Guid id = new Guid();
- this.RoleRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult<Role>(null));
+ Guid id = Guid.NewGuid();
+ this._roleRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .Returns(Task.FromResult<Role>(null));
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.RoleService.GetRoleById(id));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._roleService.GetRoleById(id));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
@@ -153,12 +171,20 @@ namespace DevHive.Services.Tests
Name = name,
};
- this.RoleRepositoryMock.Setup(p => p.DoesRoleExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
- this.RoleRepositoryMock.Setup(p => p.DoesNameExist(It.IsAny<string>())).Returns(Task.FromResult(false));
- this.RoleRepositoryMock.Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Role>())).Returns(Task.FromResult(shouldPass));
- this.MapperMock.Setup(p => p.Map<Role>(It.IsAny<UpdateRoleServiceModel>())).Returns(role);
-
- bool result = await this.RoleService.UpdateRole(updateRoleServiceModel);
+ this._roleRepositoryMock
+ .Setup(p => p.DoesRoleExist(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._roleRepositoryMock
+ .Setup(p => p.DoesNameExist(It.IsAny<string>()))
+ .ReturnsAsync(false);
+ this._roleRepositoryMock
+ .Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Role>()))
+ .ReturnsAsync(shouldPass);
+ this._mapperMock
+ .Setup(p => p.Map<Role>(It.IsAny<UpdateRoleServiceModel>()))
+ .Returns(role);
+
+ bool result = await this._roleService.UpdateRole(updateRoleServiceModel);
Assert.AreEqual(shouldPass, result);
}
@@ -171,9 +197,11 @@ namespace DevHive.Services.Tests
{
};
- this.RoleRepositoryMock.Setup(p => p.DoesRoleExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+ this._roleRepositoryMock
+ .Setup(p => p.DoesRoleExist(It.IsAny<Guid>()))
+ .ReturnsAsync(false);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.RoleService.UpdateRole(updateRoleServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._roleService.UpdateRole(updateRoleServiceModel));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
@@ -186,10 +214,14 @@ namespace DevHive.Services.Tests
{
};
- this.RoleRepositoryMock.Setup(p => p.DoesRoleExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
- this.RoleRepositoryMock.Setup(p => p.DoesNameExist(It.IsAny<string>())).Returns(Task.FromResult(true));
+ this._roleRepositoryMock
+ .Setup(p => p.DoesRoleExist(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._roleRepositoryMock
+ .Setup(p => p.DoesNameExist(It.IsAny<string>()))
+ .ReturnsAsync(true);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.RoleService.UpdateRole(updateRoleServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._roleService.UpdateRole(updateRoleServiceModel));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
@@ -201,14 +233,20 @@ namespace DevHive.Services.Tests
[TestCase(false)]
public async Task DeleteRole_ShouldReturnIfDeletionIsSuccessfull_WhenRoleExists(bool shouldPass)
{
- Guid id = new Guid();
+ Guid id = Guid.NewGuid();
Role role = new Role();
- this.RoleRepositoryMock.Setup(p => p.DoesRoleExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
- this.RoleRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(role));
- this.RoleRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny<Role>())).Returns(Task.FromResult(shouldPass));
+ this._roleRepositoryMock
+ .Setup(p => p.DoesRoleExist(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._roleRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(role);
+ this._roleRepositoryMock
+ .Setup(p => p.DeleteAsync(It.IsAny<Role>()))
+ .ReturnsAsync(shouldPass);
- bool result = await this.RoleService.DeleteRole(id);
+ bool result = await this._roleService.DeleteRole(id);
Assert.AreEqual(shouldPass, result);
}
@@ -217,11 +255,13 @@ namespace DevHive.Services.Tests
public void DeleteRole_ThrowsException_WhenRoleDoesNotExist()
{
string exceptionMessage = "Role does not exist!";
- Guid id = new Guid();
+ Guid id = Guid.NewGuid();
- this.RoleRepositoryMock.Setup(p => p.DoesRoleExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+ this._roleRepositoryMock
+ .Setup(p => p.DoesRoleExist(It.IsAny<Guid>()))
+ .ReturnsAsync(false);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.RoleService.DeleteRole(id));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._roleService.DeleteRole(id));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
diff --git a/src/Services/DevHive.Services.Tests/TechnologyServices.Tests.cs b/src/Services/DevHive.Services.Tests/TechnologyServices.Tests.cs
index a43d4b9..f9c599c 100644
--- a/src/Services/DevHive.Services.Tests/TechnologyServices.Tests.cs
+++ b/src/Services/DevHive.Services.Tests/TechnologyServices.Tests.cs
@@ -14,17 +14,17 @@ namespace DevHive.Services.Tests
[TestFixture]
public class TechnologyServicesTests
{
- private Mock<ITechnologyRepository> TechnologyRepositoryMock { get; set; }
- private Mock<IMapper> MapperMock { get; set; }
- private TechnologyService TechnologyService { get; set; }
+ private Mock<ITechnologyRepository> _technologyRepositoryMock;
+ private Mock<IMapper> _mapperMock;
+ private TechnologyService _technologyService;
#region SetUps
[SetUp]
public void Setup()
{
- this.TechnologyRepositoryMock = new Mock<ITechnologyRepository>();
- this.MapperMock = new Mock<IMapper>();
- this.TechnologyService = new TechnologyService(this.TechnologyRepositoryMock.Object, this.MapperMock.Object);
+ this._technologyRepositoryMock = new Mock<ITechnologyRepository>();
+ this._mapperMock = new Mock<IMapper>();
+ this._technologyService = new TechnologyService(this._technologyRepositoryMock.Object, this._mapperMock.Object);
}
#endregion
@@ -44,12 +44,20 @@ namespace DevHive.Services.Tests
Id = id
};
- this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(false));
- this.TechnologyRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Technology>())).Returns(Task.FromResult(true));
- this.TechnologyRepositoryMock.Setup(p => p.GetByNameAsync(It.IsAny<string>())).Returns(Task.FromResult(technology));
- this.MapperMock.Setup(p => p.Map<Technology>(It.IsAny<CreateTechnologyServiceModel>())).Returns(technology);
-
- Guid result = await this.TechnologyService.CreateTechnology(createTechnologyServiceModel);
+ this._technologyRepositoryMock
+ .Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny<string>()))
+ .ReturnsAsync(false);
+ this._technologyRepositoryMock
+ .Setup(p => p.AddAsync(It.IsAny<Technology>()))
+ .ReturnsAsync(true);
+ this._technologyRepositoryMock
+ .Setup(p => p.GetByNameAsync(It.IsAny<string>()))
+ .ReturnsAsync(technology);
+ this._mapperMock
+ .Setup(p => p.Map<Technology>(It.IsAny<CreateTechnologyServiceModel>()))
+ .Returns(technology);
+
+ Guid result = await this._technologyService.CreateTechnology(createTechnologyServiceModel);
Assert.AreEqual(id, result);
}
@@ -68,11 +76,17 @@ namespace DevHive.Services.Tests
Name = technologyName
};
- this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(false));
- this.TechnologyRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Technology>())).Returns(Task.FromResult(false));
- this.MapperMock.Setup(p => p.Map<Technology>(It.IsAny<CreateTechnologyServiceModel>())).Returns(technology);
+ this._technologyRepositoryMock
+ .Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny<string>()))
+ .ReturnsAsync(false);
+ this._technologyRepositoryMock
+ .Setup(p => p.AddAsync(It.IsAny<Technology>()))
+ .ReturnsAsync(false);
+ this._mapperMock
+ .Setup(p => p.Map<Technology>(It.IsAny<CreateTechnologyServiceModel>()))
+ .Returns(technology);
- Guid result = await this.TechnologyService.CreateTechnology(createTechnologyServiceModel);
+ Guid result = await this._technologyService.CreateTechnology(createTechnologyServiceModel);
Assert.IsTrue(result == Guid.Empty);
}
@@ -87,14 +101,12 @@ namespace DevHive.Services.Tests
{
Name = technologyName
};
- Technology technology = new()
- {
- Name = technologyName
- };
- this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(true));
+ this._technologyRepositoryMock
+ .Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny<string>()))
+ .ReturnsAsync(true);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.TechnologyService.CreateTechnology(createTechnologyServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._technologyService.CreateTechnology(createTechnologyServiceModel));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
@@ -104,7 +116,7 @@ namespace DevHive.Services.Tests
[Test]
public async Task GetTechnologyById_ReturnsTheTechnology_WhenItExists()
{
- Guid id = new Guid();
+ Guid id = Guid.NewGuid();
string name = "Gosho Trapov";
Technology technology = new()
{
@@ -115,10 +127,14 @@ namespace DevHive.Services.Tests
Name = name
};
- this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(technology));
- this.MapperMock.Setup(p => p.Map<ReadTechnologyServiceModel>(It.IsAny<Technology>())).Returns(readTechnologyServiceModel);
+ this._technologyRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(technology);
+ this._mapperMock
+ .Setup(p => p.Map<ReadTechnologyServiceModel>(It.IsAny<Technology>()))
+ .Returns(readTechnologyServiceModel);
- ReadTechnologyServiceModel result = await this.TechnologyService.GetTechnologyById(id);
+ ReadTechnologyServiceModel result = await this._technologyService.GetTechnologyById(id);
Assert.AreEqual(name, result.Name);
}
@@ -127,10 +143,12 @@ namespace DevHive.Services.Tests
public void GetTechnologyById_ThrowsException_WhenTechnologyDoesNotExist()
{
string exceptionMessage = "The technology does not exist";
- Guid id = new Guid();
- this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult<Technology>(null));
+ Guid id = Guid.NewGuid();
+ this._technologyRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .Returns(Task.FromResult<Technology>(null));
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.TechnologyService.GetTechnologyById(id));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._technologyService.GetTechnologyById(id));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
@@ -146,10 +164,14 @@ namespace DevHive.Services.Tests
technologies.Add(firstTechnology);
technologies.Add(secondTechnology);
- this.TechnologyRepositoryMock.Setup(p => p.GetTechnologies()).Returns(new HashSet<Technology>());
- this.MapperMock.Setup(p => p.Map<HashSet<ReadTechnologyServiceModel>>(It.IsAny<HashSet<Technology>>())).Returns(technologies);
+ this._technologyRepositoryMock
+ .Setup(p => p.GetTechnologies())
+ .Returns(new HashSet<Technology>());
+ this._mapperMock
+ .Setup(p => p.Map<HashSet<ReadTechnologyServiceModel>>(It.IsAny<HashSet<Technology>>()))
+ .Returns(technologies);
- HashSet<ReadTechnologyServiceModel> result = this.TechnologyService.GetTechnologies();
+ HashSet<ReadTechnologyServiceModel> result = this._technologyService.GetTechnologies();
Assert.GreaterOrEqual(2, result.Count, "GetTechnologies does not return all technologies");
}
@@ -157,10 +179,14 @@ namespace DevHive.Services.Tests
[Test]
public void GetLanguages_ReturnsEmptyHashSet_IfNoLanguagesExist()
{
- this.TechnologyRepositoryMock.Setup(p => p.GetTechnologies()).Returns(new HashSet<Technology>());
- this.MapperMock.Setup(p => p.Map<HashSet<ReadTechnologyServiceModel>>(It.IsAny<HashSet<Technology>>())).Returns(new HashSet<ReadTechnologyServiceModel>());
+ this._technologyRepositoryMock
+ .Setup(p => p.GetTechnologies())
+ .Returns(new HashSet<Technology>());
+ this._mapperMock
+ .Setup(p => p.Map<HashSet<ReadTechnologyServiceModel>>(It.IsAny<HashSet<Technology>>()))
+ .Returns(new HashSet<ReadTechnologyServiceModel>());
- HashSet<ReadTechnologyServiceModel> result = this.TechnologyService.GetTechnologies();
+ HashSet<ReadTechnologyServiceModel> result = this._technologyService.GetTechnologies();
Assert.IsEmpty(result, "GetTechnologies does not return empty string when no technologies exist");
}
@@ -184,12 +210,20 @@ namespace DevHive.Services.Tests
Name = name,
};
- this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(true));
- this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(false));
- this.TechnologyRepositoryMock.Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Technology>())).Returns(Task.FromResult(shouldPass));
- this.MapperMock.Setup(p => p.Map<Technology>(It.IsAny<UpdateTechnologyServiceModel>())).Returns(technology);
-
- bool result = await this.TechnologyService.UpdateTechnology(updatetechnologyServiceModel);
+ this._technologyRepositoryMock
+ .Setup(p => p.DoesTechnologyExistAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._technologyRepositoryMock
+ .Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny<string>()))
+ .ReturnsAsync(false);
+ this._technologyRepositoryMock
+ .Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Technology>()))
+ .ReturnsAsync(shouldPass);
+ this._mapperMock
+ .Setup(p => p.Map<Technology>(It.IsAny<UpdateTechnologyServiceModel>()))
+ .Returns(technology);
+
+ bool result = await this._technologyService.UpdateTechnology(updatetechnologyServiceModel);
Assert.AreEqual(shouldPass, result);
}
@@ -202,9 +236,11 @@ namespace DevHive.Services.Tests
{
};
- this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+ this._technologyRepositoryMock
+ .Setup(p => p.DoesTechnologyExistAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(false);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.TechnologyService.UpdateTechnology(updateTechnologyServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._technologyService.UpdateTechnology(updateTechnologyServiceModel));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
@@ -217,10 +253,14 @@ namespace DevHive.Services.Tests
{
};
- this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(true));
- this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(true));
+ this._technologyRepositoryMock
+ .Setup(p => p.DoesTechnologyExistAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._technologyRepositoryMock
+ .Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny<string>()))
+ .ReturnsAsync(true);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.TechnologyService.UpdateTechnology(updateTechnologyServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._technologyService.UpdateTechnology(updateTechnologyServiceModel));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
@@ -233,14 +273,20 @@ namespace DevHive.Services.Tests
[TestCase(false)]
public async Task DeleteTechnology_ShouldReturnIfDeletionIsSuccessfull_WhenTechnologyExists(bool shouldPass)
{
- Guid id = new Guid();
+ Guid id = Guid.NewGuid();
Technology technology = new Technology();
- this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(true));
- this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(technology));
- this.TechnologyRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny<Technology>())).Returns(Task.FromResult(shouldPass));
+ this._technologyRepositoryMock
+ .Setup(p => p.DoesTechnologyExistAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._technologyRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(technology);
+ this._technologyRepositoryMock
+ .Setup(p => p.DeleteAsync(It.IsAny<Technology>()))
+ .ReturnsAsync(shouldPass);
- bool result = await this.TechnologyService.DeleteTechnology(id);
+ bool result = await this._technologyService.DeleteTechnology(id);
Assert.AreEqual(shouldPass, result);
}
@@ -249,11 +295,13 @@ namespace DevHive.Services.Tests
public void DeleteTechnology_ThrowsException_WhenTechnologyDoesNotExist()
{
string exceptionMessage = "Technology does not exist!";
- Guid id = new Guid();
+ Guid id = Guid.NewGuid();
- this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+ this._technologyRepositoryMock
+ .Setup(p => p.DoesTechnologyExistAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(false);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.TechnologyService.DeleteTechnology(id));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._technologyService.DeleteTechnology(id));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
diff --git a/src/Services/DevHive.Services.Tests/UserService.Tests.cs b/src/Services/DevHive.Services.Tests/UserService.Tests.cs
index 550106f..7990b32 100644
--- a/src/Services/DevHive.Services.Tests/UserService.Tests.cs
+++ b/src/Services/DevHive.Services.Tests/UserService.Tests.cs
@@ -1,20 +1,14 @@
using System;
using System.Collections.Generic;
-using System.IdentityModel.Tokens.Jwt;
-using System.Security.Claims;
-using System.Text;
using System.Threading.Tasks;
using AutoMapper;
+using DevHive.Common.Jwt.Interfaces;
using DevHive.Common.Models.Identity;
-using DevHive.Common.Models.Misc;
using DevHive.Data.Interfaces;
using DevHive.Data.Models;
using DevHive.Services.Interfaces;
using DevHive.Services.Models.User;
-using DevHive.Services.Options;
using DevHive.Services.Services;
-using Microsoft.AspNetCore.Identity;
-using Microsoft.IdentityModel.Tokens;
using Moq;
using NUnit.Framework;
@@ -23,28 +17,34 @@ namespace DevHive.Services.Tests
[TestFixture]
public class UserServiceTests
{
- private Mock<ICloudService> CloudServiceMock { get; set; }
- private Mock<IUserRepository> UserRepositoryMock { get; set; }
- private Mock<IRoleRepository> RoleRepositoryMock { get; set; }
- private Mock<ILanguageRepository> LanguageRepositoryMock { get; set; }
- private Mock<ITechnologyRepository> TechnologyRepositoryMock { get; set; }
- private Mock<IMapper> MapperMock { get; set; }
- private JwtOptions JwtOptions { get; set; }
- private UserService UserService { get; set; }
+ private Mock<ICloudService> _cloudServiceMock;
+ private Mock<IUserRepository> _userRepositoryMock;
+ private Mock<IRoleRepository> _roleRepositoryMock;
+ private Mock<ILanguageRepository> _languageRepositoryMock;
+ private Mock<ITechnologyRepository> _technologyRepositoryMock;
+ private Mock<IMapper> _mapperMock;
+ private Mock<IJwtService> _jwtServiceMock;
+ private UserService _userService;
#region SetUps
[SetUp]
public void Setup()
{
- this.UserRepositoryMock = new Mock<IUserRepository>();
- this.RoleRepositoryMock = new Mock<IRoleRepository>();
- this.CloudServiceMock = new Mock<ICloudService>();
- this.LanguageRepositoryMock = new Mock<ILanguageRepository>();
- this.TechnologyRepositoryMock = new Mock<ITechnologyRepository>();
- this.JwtOptions = new JwtOptions("gXfQlU6qpDleFWyimscjYcT3tgFsQg3yoFjcvSLxG56n1Vu2yptdIUq254wlJWjm");
- this.MapperMock = new Mock<IMapper>();
- // TODO: give actual UserManager and RoleManager to UserService
- this.UserService = new UserService(this.UserRepositoryMock.Object, this.LanguageRepositoryMock.Object, this.RoleRepositoryMock.Object, this.TechnologyRepositoryMock.Object, null, null, this.MapperMock.Object, this.JwtOptions, this.CloudServiceMock.Object);
+ this._userRepositoryMock = new Mock<IUserRepository>();
+ this._roleRepositoryMock = new Mock<IRoleRepository>();
+ this._cloudServiceMock = new Mock<ICloudService>();
+ this._languageRepositoryMock = new Mock<ILanguageRepository>();
+ this._technologyRepositoryMock = new Mock<ITechnologyRepository>();
+ this._jwtServiceMock = new Mock<IJwtService>();
+ this._mapperMock = new Mock<IMapper>();
+ this._userService = new UserService(
+ this._userRepositoryMock.Object,
+ this._languageRepositoryMock.Object,
+ this._roleRepositoryMock.Object,
+ this._technologyRepositoryMock.Object,
+ this._mapperMock.Object,
+ this._cloudServiceMock.Object,
+ this._jwtServiceMock.Object);
}
#endregion
@@ -52,132 +52,166 @@ namespace DevHive.Services.Tests
[Test]
public async Task LoginUser_ReturnsTokenModel_WhenLoggingUserIn()
{
- string somePassword = "GoshoTrapovImaGolemChep";
- const string name = "GoshoTrapov";
- string hashedPassword = PasswordModifications.GeneratePasswordHash(somePassword);
- LoginServiceModel loginServiceModel = new LoginServiceModel
+ string somePassword = "I'm_Nigga";
+
+ LoginServiceModel loginServiceModel = new()
{
Password = somePassword
};
- User user = new User
+ User user = new()
{
Id = Guid.NewGuid(),
- PasswordHash = hashedPassword,
- UserName = name
+ PasswordHash = somePassword,
+ UserName = "g_trapov"
};
- this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(true));
- this.UserRepositoryMock.Setup(p => p.GetByUsernameAsync(It.IsAny<string>())).Returns(Task.FromResult(user));
-
- string JWTSecurityToken = this.WriteJWTSecurityToken(user.Id, user.UserName, user.Roles);
-
- TokenModel tokenModel = await this.UserService.LoginUser(loginServiceModel);
-
- Assert.AreEqual(JWTSecurityToken, tokenModel.Token, "LoginUser does not return the correct token");
+ this._userRepositoryMock
+ .Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>()))
+ .ReturnsAsync(true);
+ this._userRepositoryMock
+ .Setup(p => p.VerifyPassword(It.IsAny<User>(), It.IsAny<string>()))
+ .ReturnsAsync(true);
+ this._userRepositoryMock
+ .Setup(p => p.GetByUsernameAsync(It.IsAny<string>()))
+ .ReturnsAsync(user);
+
+ string jwtSecurityToken = "akjdhfakndvlahdfkljahdlfkjhasldf";
+ this._jwtServiceMock
+ .Setup(p => p.GenerateJwtToken(It.IsAny<Guid>(), It.IsAny<string>(), It.IsAny<List<string>>()))
+ .Returns(jwtSecurityToken);
+ TokenModel tokenModel = await this._userService.LoginUser(loginServiceModel);
+
+ Assert.AreEqual(jwtSecurityToken, tokenModel.Token, "LoginUser does not return the correct token");
}
[Test]
public void LoginUser_ThrowsException_WhenUserNameDoesNotExist()
{
- const string EXCEPTION_MESSAGE = "Invalid username!";
- LoginServiceModel loginServiceModel = new LoginServiceModel
- {
- };
+ LoginServiceModel loginServiceModel = new();
- this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(false));
+ this._userRepositoryMock
+ .Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>()))
+ .ReturnsAsync(false);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.LoginUser(loginServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(
+ () => this._userService.LoginUser(loginServiceModel));
- Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorect Exception message");
+ Assert.AreEqual("Invalid username!", ex.Message, "Incorrect Exception message");
}
[Test]
- public void LoginUser_ThroiwsException_WhenPasswordIsIncorect()
+ public void LoginUser_ThrowsException_WhenPasswordIsIncorrect()
{
- const string EXCEPTION_MESSAGE = "Incorrect password!";
- string somePassword = "GoshoTrapovImaGolemChep";
- LoginServiceModel loginServiceModel = new LoginServiceModel
+ string somePassword = "I'm_Nigga";
+
+ LoginServiceModel loginServiceModel = new()
{
Password = somePassword
};
- User user = new User
+ User user = new()
{
Id = Guid.NewGuid(),
- PasswordHash = "InvalidPasswordHas"
};
- this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(true));
- this.UserRepositoryMock.Setup(p => p.GetByUsernameAsync(It.IsAny<string>())).Returns(Task.FromResult(user));
+ this._userRepositoryMock
+ .Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>()))
+ .ReturnsAsync(true);
+ this._userRepositoryMock
+ .Setup(p => p.GetByUsernameAsync(It.IsAny<string>()))
+ .ReturnsAsync(user);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.LoginUser(loginServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._userService.LoginUser(loginServiceModel));
- Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorect Exception message");
+ Assert.AreEqual("Incorrect password!", ex.Message, "Incorrect Exception message");
}
#endregion
#region RegisterUser
- // [Test]
- // public async Task RegisterUser_ReturnsTokenModel_WhenUserIsSuccessfull()
- // {
- // string somePassword = "GoshoTrapovImaGolemChep";
- // const string name = "GoshoTrapov";
- // RegisterServiceModel registerServiceModel = new RegisterServiceModel
- // {
- // Password = somePassword
- // };
- // User user = new User
- // {
- // Id = Guid.NewGuid(),
- // UserName = name
- // };
- // Role role = new Role { Name = Role.DefaultRole };
- // HashSet<Role> roles = new HashSet<Role> { role };
- //
- // this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(false));
- // this.UserRepositoryMock.Setup(p => p.DoesEmailExistAsync(It.IsAny<string>())).Returns(Task.FromResult(false));
- // this.RoleRepositoryMock.Setup(p => p.DoesNameExist(It.IsAny<string>())).Returns(Task.FromResult(true));
- // this.RoleRepositoryMock.Setup(p => p.GetByNameAsync(It.IsAny<string>())).Returns(Task.FromResult(role));
- // this.MapperMock.Setup(p => p.Map<User>(It.IsAny<RegisterServiceModel>())).Returns(user);
- // this.UserRepositoryMock.Setup(p => p.AddAsync(It.IsAny<User>())).Verifiable();
- //
- // string JWTSecurityToken = this.WriteJWTSecurityToken(user.Id, user.UserName, roles);
- //
- // TokenModel tokenModel = await this.UserService.RegisterUser(registerServiceModel);
- //
- // Mock.Verify();
- // Assert.AreEqual(JWTSecurityToken, tokenModel.Token, "RegisterUser does not return the correct token");
- // }
+ [Test]
+ public async Task RegisterUser_ReturnsTokenModel_WhenUserIsSuccessfull()
+ {
+ Guid userId = Guid.NewGuid();
+ RegisterServiceModel registerServiceModel = new()
+ {
+ Password = "ImNigga"
+ };
+ User user = new()
+ {
+ Id = userId,
+ UserName = "g_trapov"
+ };
+ Role role = new() { Name = Role.DefaultRole };
+
+ this._userRepositoryMock
+ .Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>()))
+ .ReturnsAsync(false);
+ this._userRepositoryMock
+ .Setup(p => p.VerifyPassword(It.IsAny<User>(), It.IsAny<string>()))
+ .ReturnsAsync(true);
+ this._userRepositoryMock
+ .Setup(p => p.DoesEmailExistAsync(It.IsAny<string>()))
+ .ReturnsAsync(false);
+ this._userRepositoryMock
+ .Setup(p => p.AddAsync(It.IsAny<User>()))
+ .ReturnsAsync(true);
+ this._userRepositoryMock
+ .Setup(p => p.AddRoleToUser(It.IsAny<User>(), It.IsAny<string>()))
+ .ReturnsAsync(true);
+ this._userRepositoryMock
+ .Setup(p => p.GetByUsernameAsync(It.IsAny<string>()))
+ .ReturnsAsync(user);
+
+ this._roleRepositoryMock
+ .Setup(p => p.DoesNameExist(It.IsAny<string>()))
+ .ReturnsAsync(true);
+ this._roleRepositoryMock
+ .Setup(p => p.GetByNameAsync(It.IsAny<string>()))
+ .ReturnsAsync(role);
+
+ this._mapperMock
+ .Setup(p => p.Map<User>(It.IsAny<RegisterServiceModel>()))
+ .Returns(user);
+
+ string jwtSecurityToken = "akjdhfakndvlahdfkljahdlfkjhasldf";
+ this._jwtServiceMock
+ .Setup(p => p.GenerateJwtToken(It.IsAny<Guid>(), It.IsAny<string>(), It.IsAny<List<string>>()))
+ .Returns(jwtSecurityToken);
+ TokenModel tokenModel = await this._userService.RegisterUser(registerServiceModel);
+
+ Assert.AreEqual(jwtSecurityToken, tokenModel.Token, "RegisterUser does not return the correct token");
+ }
[Test]
public void RegisterUser_ThrowsException_WhenUsernameAlreadyExists()
{
const string EXCEPTION_MESSAGE = "Username already exists!";
- RegisterServiceModel registerServiceModel = new RegisterServiceModel
- {
- };
+ RegisterServiceModel registerServiceModel = new();
- this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(true));
+ this._userRepositoryMock
+ .Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>()))
+ .ReturnsAsync(true);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.RegisterUser(registerServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(
+ () => this._userService.RegisterUser(registerServiceModel));
- Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorect Exception message");
+ Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorrect Exception message");
}
[Test]
public void RegisterUser_ThrowsException_WhenEmailAlreadyExists()
{
- const string EXCEPTION_MESSAGE = "Email already exists!";
-
- RegisterServiceModel registerServiceModel = new RegisterServiceModel
- {
- };
+ RegisterServiceModel registerServiceModel = new();
- this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(false));
- this.UserRepositoryMock.Setup(p => p.DoesEmailExistAsync(It.IsAny<string>())).Returns(Task.FromResult(true));
+ this._userRepositoryMock
+ .Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>()))
+ .ReturnsAsync(false);
+ this._userRepositoryMock
+ .Setup(p => p.DoesEmailExistAsync(It.IsAny<string>()))
+ .ReturnsAsync(true);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.RegisterUser(registerServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._userService.RegisterUser(registerServiceModel));
- Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorect Exception message");
+ Assert.AreEqual("Email already exists!", ex.Message, "Incorrect Exception message");
}
#endregion
@@ -185,34 +219,37 @@ namespace DevHive.Services.Tests
[Test]
public async Task GetUserById_ReturnsTheUser_WhenItExists()
{
- Guid id = new Guid();
- string name = "Gosho Trapov";
- User user = new()
- {
- };
- UserServiceModel userServiceModel = new UserServiceModel
+ Guid id = new();
+ string username = "g_trapov";
+ User user = new();
+ UserServiceModel userServiceModel = new()
{
- UserName = name
+ UserName = username
};
- this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user));
- this.MapperMock.Setup(p => p.Map<UserServiceModel>(It.IsAny<User>())).Returns(userServiceModel);
+ this._userRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(user);
+ this._mapperMock
+ .Setup(p => p.Map<UserServiceModel>(It.IsAny<User>()))
+ .Returns(userServiceModel);
- UserServiceModel result = await this.UserService.GetUserById(id);
+ UserServiceModel result = await this._userService.GetUserById(id);
- Assert.AreEqual(name, result.UserName);
+ Assert.AreEqual(username, result.UserName);
}
[Test]
public void GetTechnologyById_ThrowsException_WhenTechnologyDoesNotExist()
{
- const string EXCEPTION_MESSEGE = "User does not exist!";
- Guid id = new Guid();
- this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult<User>(null));
+ Guid id = new();
+ this._userRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .Returns(Task.FromResult<User>(null));
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.GetUserById(id));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._userService.GetUserById(id));
- Assert.AreEqual(EXCEPTION_MESSEGE, ex.Message, "Incorecct exception message");
+ Assert.AreEqual("User does not exist!", ex.Message, "Incorrect exception message");
}
#endregion
@@ -220,32 +257,37 @@ namespace DevHive.Services.Tests
[Test]
public async Task GetUserByUsername_ReturnsTheCorrectUser_IfItExists()
{
- string username = "Gosho Trapov";
- User user = new User();
- UserServiceModel userServiceModel = new UserServiceModel
+ string username = "g_trapov";
+ User user = new();
+ UserServiceModel userServiceModel = new()
{
UserName = username
};
- this.UserRepositoryMock.Setup(p => p.GetByUsernameAsync(It.IsAny<string>())).Returns(Task.FromResult(user));
- this.MapperMock.Setup(p => p.Map<UserServiceModel>(It.IsAny<User>())).Returns(userServiceModel);
+ this._userRepositoryMock
+ .Setup(p => p.GetByUsernameAsync(It.IsAny<string>()))
+ .ReturnsAsync(user);
+ this._mapperMock
+ .Setup(p => p.Map<UserServiceModel>(It.IsAny<User>()))
+ .Returns(userServiceModel);
- UserServiceModel result = await this.UserService.GetUserByUsername(username);
+ UserServiceModel result = await this._userService.GetUserByUsername(username);
Assert.AreEqual(username, result.UserName, "GetUserByUsername does not return the correct user");
}
[Test]
- public async Task GetUserByUsername_ThrowsException_IfUserDoesNotExist()
+ public void GetUserByUsername_ThrowsException_IfUserDoesNotExist()
{
- string username = "Gosho Trapov";
- const string EXCEPTION_MESSEGE = "User does not exist!";
+ string username = "g_trapov";
- this.UserRepositoryMock.Setup(p => p.GetByUsernameAsync(It.IsAny<string>())).Returns(Task.FromResult<User>(null));
+ this._userRepositoryMock
+ .Setup(p => p.GetByUsernameAsync(It.IsAny<string>()))
+ .Returns(Task.FromResult<User>(null));
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.GetUserByUsername(username));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._userService.GetUserByUsername(username));
- Assert.AreEqual(EXCEPTION_MESSEGE, ex.Message, "Incorecct exception message");
+ Assert.AreEqual("User does not exist!", ex.Message, "Incorrect exception message");
}
#endregion
@@ -255,76 +297,90 @@ namespace DevHive.Services.Tests
// [TestCase(false)]
// public async Task UpdateUser_ReturnsIfUpdateIsSuccessfull_WhenUserExistsy(bool shouldPass)
// {
- // string name = "Gosho Trapov";
+ // string username = "g_trapov";
// Guid id = Guid.NewGuid();
// User user = new User
// {
- // UserName = name,
+ // UserName = username,
// Id = id,
// };
// UpdateUserServiceModel updateUserServiceModel = new UpdateUserServiceModel
// {
- // UserName = name,
+ // UserName = username,
// };
// UserServiceModel userServiceModel = new UserServiceModel
// {
- // UserName = name,
+ // UserName = username,
// };
// Role role = new Role { };
- //
- // this.UserRepositoryMock.Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(true));
- // this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(false));
- // this.UserRepositoryMock.Setup(p => p.DoesUserHaveThisUsernameAsync(It.IsAny<Guid>(), It.IsAny<string>())).Returns(true);
- // this.UserRepositoryMock.Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<User>())).Returns(Task.FromResult(shouldPass));
- // this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user));
- // this.MapperMock.Setup(p => p.Map<User>(It.IsAny<UpdateUserServiceModel>())).Returns(user);
- // this.MapperMock.Setup(p => p.Map<UserServiceModel>(It.IsAny<User>())).Returns(userServiceModel);
- //
+ //
+ // this._userRepositoryMock.Setup(p =>
+ // p.DoesUserExistAsync(It.IsAny<Guid>()))
+ // .ReturnsAsync(true);
+ // this._userRepositoryMock.Setup(p =>
+ // p.DoesUsernameExistAsync(It.IsAny<string>()))
+ // .ReturnsAsync(false);
+ // this._userRepositoryMock.Setup(p =>
+ // p.DoesUserHaveThisUsernameAsync(It.IsAny<Guid>(), It.IsAny<string>()))
+ // .Returns(true);
+ // this._userRepositoryMock.Setup(p =>
+ // p.EditAsync(It.IsAny<Guid>(), It.IsAny<User>()))
+ // .ReturnsAsync(shouldPass);
+ // this._userRepositoryMock.Setup(p =>
+ // p.GetByIdAsync(It.IsAny<Guid>()))
+ // .ReturnsAsync(user);
+ // this._mapperMock.Setup(p =>
+ // p.Map<User>(It.IsAny<UpdateUserServiceModel>()))
+ // .Returns(user);
+ // this._mapperMock.Setup(p =>
+ // p.Map<UserServiceModel>(It.IsAny<User>()))
+ // .Returns(userServiceModel);
+ //
// if (shouldPass)
// {
- // UserServiceModel result = await this.UserService.UpdateUser(updateUserServiceModel);
- //
+ // UserServiceModel result = await this._userService.UpdateUser(updateUserServiceModel);
+ //
// Assert.AreEqual(updateUserServiceModel.UserName, result.UserName);
// }
// else
// {
// const string EXCEPTION_MESSAGE = "Unable to edit user!";
- //
- // Exception ex = Assert.ThrowsAsync<InvalidOperationException>(() => this.UserService.UpdateUser(updateUserServiceModel));
- //
- // Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorecct exception message");
+ //
+ // Exception ex = Assert.ThrowsAsync<InvalidOperationException>(() => this._userService.UpdateUser(updateUserServiceModel);
+ //
+ // Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorrect exception message");
// }
// }
[Test]
public void UpdateUser_ThrowsException_WhenUserDoesNotExist()
{
- const string EXCEPTION_MESSAGE = "User does not exist!";
- UpdateUserServiceModel updateUserServiceModel = new UpdateUserServiceModel
- {
- };
+ UpdateUserServiceModel updateUserServiceModel = new();
- this.UserRepositoryMock.Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+ this._userRepositoryMock
+ .Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(false);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.UpdateUser(updateUserServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._userService.UpdateUser(updateUserServiceModel));
- Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorecct exception message");
+ Assert.AreEqual("User does not exist!", ex.Message, "Incorrect exception message");
}
[Test]
public void UpdateUser_ThrowsException_WhenUserNameAlreadyExists()
{
- const string EXCEPTION_MESSAGE = "Username already exists!";
- UpdateUserServiceModel updateUserServiceModel = new UpdateUserServiceModel
- {
- };
+ UpdateUserServiceModel updateUserServiceModel = new();
- this.UserRepositoryMock.Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(true));
- this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(true));
+ this._userRepositoryMock
+ .Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._userRepositoryMock
+ .Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>()))
+ .ReturnsAsync(true);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.UpdateUser(updateUserServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._userService.UpdateUser(updateUserServiceModel));
- Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorecct exception message");
+ Assert.AreEqual("Username already exists!", ex.Message, "Incorrect exception message");
}
#endregion
@@ -335,59 +391,37 @@ namespace DevHive.Services.Tests
// [TestCase(false)]
// public async Task DeleteUser_ShouldReturnIfDeletionIsSuccessfull_WhenUserExists(bool shouldPass)
// {
- // Guid id = new Guid();
+ // Guid id = Guid.NewGuid();
// User user = new User();
- //
- // this.UserRepositoryMock.Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(true));
- // this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user));
- // this.UserRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny<User>())).Returns(Task.FromResult(shouldPass));
- //
- // bool result = await this.UserService.DeleteUser(id);
- //
+ //
+ // this._userRepositoryMock.Setup(p =>
+ // p.DoesUserExistAsync(It.IsAny<Guid>()))
+ // .ReturnsAsync(true);
+ // this._userRepositoryMock.Setup(p =>
+ // p.GetByIdAsync(It.IsAny<Guid>()))
+ // .ReturnsAsync(user);
+ // this._userRepositoryMock.Setup(p =>
+ // p.DeleteAsync(It.IsAny<User>()))
+ // .ReturnsAsync(shouldPass);
+ //
+ // bool result = await this._userService.DeleteUser(id);
+ //
// Assert.AreEqual(shouldPass, result);
// }
- //
+ //
[Test]
public void DeleteUser_ThrowsException_WhenUserDoesNotExist()
{
string exceptionMessage = "User does not exist!";
- Guid id = new Guid();
-
- this.UserRepositoryMock.Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+ Guid id = new();
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.DeleteUser(id));
+ this._userRepositoryMock
+ .Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(false);
- Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
- }
- #endregion
-
- #region HelperMethods
- private string WriteJWTSecurityToken(Guid userId, string username, HashSet<Role> roles)
- {
- byte[] signingKey = Encoding.ASCII.GetBytes(this.JwtOptions.Secret);
- HashSet<Claim> claims = new()
- {
- new Claim("ID", $"{userId}"),
- new Claim("Username", username),
- };
-
- foreach (var role in roles)
- {
- claims.Add(new Claim(ClaimTypes.Role, role.Name));
- }
-
- SecurityTokenDescriptor tokenDescriptor = new()
- {
- Subject = new ClaimsIdentity(claims),
- Expires = DateTime.Today.AddDays(7),
- SigningCredentials = new SigningCredentials(
- new SymmetricSecurityKey(signingKey),
- SecurityAlgorithms.HmacSha512Signature)
- };
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._userService.DeleteUser(id));
- JwtSecurityTokenHandler tokenHandler = new();
- SecurityToken token = tokenHandler.CreateToken(tokenDescriptor);
- return tokenHandler.WriteToken(token);
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorrect exception message");
}
#endregion
}
diff --git a/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs b/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs
index 4534511..9ad5f25 100644
--- a/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs
+++ b/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs
@@ -1,6 +1,6 @@
using AutoMapper;
using DevHive.Data.Models;
-using DevHive.Services.Models.Post.Rating;
+using DevHive.Services.Models.Rating;
namespace DevHive.Services.Configurations.Mapping
{
diff --git a/src/Services/DevHive.Services/Interfaces/IRatingService.cs b/src/Services/DevHive.Services/Interfaces/IRatingService.cs
index beea821..be33300 100644
--- a/src/Services/DevHive.Services/Interfaces/IRatingService.cs
+++ b/src/Services/DevHive.Services/Interfaces/IRatingService.cs
@@ -1,7 +1,7 @@
using System;
using System.Threading.Tasks;
using DevHive.Data.Models;
-using DevHive.Services.Models.Post.Rating;
+using DevHive.Services.Models.Rating;
namespace DevHive.Services.Interfaces
{
@@ -16,7 +16,5 @@ namespace DevHive.Services.Interfaces
Task<ReadRatingServiceModel> UpdateRating(UpdateRatingServiceModel updateRatingServiceModel);
Task<bool> DeleteRating(Guid ratingId);
-
- Task<bool> HasUserRatedThisPost(Guid userId, Guid postId);
}
}
diff --git a/src/Services/DevHive.Services/Services/FeedService.cs b/src/Services/DevHive.Services/Services/FeedService.cs
index c16ffae..0af8093 100644
--- a/src/Services/DevHive.Services/Services/FeedService.cs
+++ b/src/Services/DevHive.Services/Services/FeedService.cs
@@ -42,9 +42,6 @@ namespace DevHive.Services.Services
if (user == null)
throw new ArgumentException("User doesn't exist!");
- if (user.Friends.Count == 0)
- throw new ArgumentException("User has no friends to get feed from!");
-
List<Post> posts = await this._feedRepository
.GetFriendsPosts(user.Friends.ToList(), getPageServiceModel.FirstRequestIssued, getPageServiceModel.PageNumber, getPageServiceModel.PageSize);
diff --git a/src/Services/DevHive.Services/Services/PostService.cs b/src/Services/DevHive.Services/Services/PostService.cs
index a3d5117..a565473 100644
--- a/src/Services/DevHive.Services/Services/PostService.cs
+++ b/src/Services/DevHive.Services/Services/PostService.cs
@@ -76,11 +76,21 @@ namespace DevHive.Services.Services
User user = await this._userRepository.GetByIdAsync(post.Creator.Id) ??
throw new ArgumentException("The user does not exist!");
+ int currentRating = 0;
+ foreach (Rating rating in post.Ratings)
+ {
+ if (rating.IsLike)
+ currentRating++;
+ else
+ currentRating--;
+ }
+
ReadPostServiceModel readPostServiceModel = this._postMapper.Map<ReadPostServiceModel>(post);
readPostServiceModel.CreatorFirstName = user.FirstName;
readPostServiceModel.CreatorLastName = user.LastName;
readPostServiceModel.CreatorUsername = user.UserName;
readPostServiceModel.FileUrls = post.Attachments.Select(x => x.FileUrl).ToList();
+ readPostServiceModel.CurrentRating = currentRating;
return readPostServiceModel;
}
diff --git a/src/Services/DevHive.Services/Services/RatingService.cs b/src/Services/DevHive.Services/Services/RatingService.cs
index 6ddba1c..1f77a6e 100644
--- a/src/Services/DevHive.Services/Services/RatingService.cs
+++ b/src/Services/DevHive.Services/Services/RatingService.cs
@@ -8,7 +8,7 @@ using AutoMapper;
using DevHive.Data.Interfaces;
using DevHive.Data.Models;
using DevHive.Services.Interfaces;
-using DevHive.Services.Models.Post.Rating;
+using DevHive.Services.Models.Rating;
namespace DevHive.Services.Services
{
@@ -81,7 +81,7 @@ namespace DevHive.Services.Services
#region Update
public async Task<ReadRatingServiceModel> UpdateRating(UpdateRatingServiceModel updateRatingServiceModel)
{
- Rating rating = await this._ratingRepository.GetByIdAsync(updateRatingServiceModel.Id) ??
+ Rating rating = await this._ratingRepository.GetRatingByUserAndPostId(updateRatingServiceModel.UserId, updateRatingServiceModel.PostId) ??
throw new ArgumentException("Rating does not exist!");
User user = await this._userRepository.GetByIdAsync(updateRatingServiceModel.UserId) ??
@@ -115,11 +115,5 @@ namespace DevHive.Services.Services
return await this._ratingRepository.DeleteAsync(rating);
}
#endregion
-
- public async Task<bool> HasUserRatedThisPost(Guid userId, Guid postId)
- {
- return await this._ratingRepository
- .UserRatedPost(userId, postId);
- }
}
}