aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/DevHive.Tests/DevHive.Services.Tests/LanguageService.Tests.cs4
-rw-r--r--src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs189
-rw-r--r--src/DevHive.Tests/DevHive.Web.Tests/LanguageController.Tests.cs4
-rw-r--r--src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs231
-rw-r--r--src/DevHive.Tests/DevHive.Web.Tests/TechnologyController.Tests.cs4
-rw-r--r--src/DevHive.Web/Controllers/PostController.cs2
6 files changed, 423 insertions, 11 deletions
diff --git a/src/DevHive.Tests/DevHive.Services.Tests/LanguageService.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/LanguageService.Tests.cs
index 67f1284..fd4a828 100644
--- a/src/DevHive.Tests/DevHive.Services.Tests/LanguageService.Tests.cs
+++ b/src/DevHive.Tests/DevHive.Services.Tests/LanguageService.Tests.cs
@@ -104,7 +104,7 @@ namespace DevHive.Services.Tests
{
Guid id = new Guid();
string name = "Gosho Trapov";
- Language language = new Language()
+ Language language = new Language
{
Name = name
};
@@ -161,7 +161,7 @@ namespace DevHive.Services.Tests
}
[Test]
- public void UpdateLanguage_ReturnsIfUpdateIsSuccessfull_WhenLanguageExistsy()
+ public void UpdateLanguage_ThrowsArgumentException_WhenLanguageDoesNotExist()
{
string exceptionMessage = "Language does not exist!";
UpdateLanguageServiceModel updateTechnologyServiceModel = new UpdateLanguageServiceModel
diff --git a/src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs
new file mode 100644
index 0000000..bf6817b
--- /dev/null
+++ b/src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs
@@ -0,0 +1,189 @@
+using System;
+using System.Threading.Tasks;
+using AutoMapper;
+using DevHive.Data.Interfaces.Repositories;
+using DevHive.Data.Models;
+using DevHive.Services.Models.Post.Comment;
+using DevHive.Services.Services;
+using Moq;
+using NUnit.Framework;
+
+namespace DevHive.Services.Tests
+{
+ [TestFixture]
+ public class PostServiceTests
+ {
+ private const string MESSAGE = "Gosho Trapov";
+ private Mock<IPostRepository> PostRepositoryMock { get; set; }
+ private Mock<IUserRepository> UserRepositoryMock { get; set; }
+ private Mock<IMapper> MapperMock { get; set; }
+ private PostService PostService { get; set; }
+
+ [SetUp]
+ public void Setup()
+ {
+ this.PostRepositoryMock = new Mock<IPostRepository>();
+ this.MapperMock = new Mock<IMapper>();
+ this.UserRepositoryMock = new Mock<IUserRepository>();
+ this.PostService = new PostService(this.PostRepositoryMock.Object, this.UserRepositoryMock.Object, this.MapperMock.Object);
+ }
+
+ #region Comment
+ #region Create
+ [Test]
+ public async Task AddComment_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully()
+ {
+ Guid id = Guid.NewGuid();
+ CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel
+ {
+ Message = MESSAGE
+ };
+ Comment comment = new Comment
+ {
+ Message = MESSAGE,
+ Id = id
+ };
+
+ this.PostRepositoryMock.Setup(p => p.AddCommentAsync(It.IsAny<Comment>())).Returns(Task.FromResult(true));
+ this.PostRepositoryMock.Setup(p => p.GetCommentByIssuerAndTimeCreatedAsync(It.IsAny<Guid>(), It.IsAny<DateTime>())).Returns(Task.FromResult(comment));
+ this.MapperMock.Setup(p => p.Map<Comment>(It.IsAny<CreateCommentServiceModel>())).Returns(comment);
+
+ Guid result = await this.PostService.AddComment(createCommentServiceModel);
+
+ Assert.AreEqual(id, result);
+ }
+
+ [Test]
+ public async Task CreateLanguage_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully()
+ {
+ CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel
+ {
+ Message = MESSAGE
+ };
+ Comment comment = new Comment
+ {
+ Message = MESSAGE,
+ };
+
+ this.PostRepositoryMock.Setup(p => p.AddCommentAsync(It.IsAny<Comment>())).Returns(Task.FromResult(false));
+ this.MapperMock.Setup(p => p.Map<Comment>(It.IsAny<CreateCommentServiceModel>())).Returns(comment);
+
+ Guid result = await this.PostService.AddComment(createCommentServiceModel);
+
+ Assert.IsTrue(result == Guid.Empty);
+ }
+ #endregion
+
+ #region Read
+ [Test]
+ public async Task GetCommentById_ReturnsTheComment_WhenItExists()
+ {
+ Guid id = new Guid();
+ Comment comment = new Comment
+ {
+ Message = MESSAGE
+ };
+ CommentServiceModel commentServiceModel = new CommentServiceModel
+ {
+ Message = MESSAGE
+ };
+
+ this.PostRepositoryMock.Setup(p => p.GetCommentByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(comment));
+ this.MapperMock.Setup(p => p.Map<CommentServiceModel>(It.IsAny<Comment>())).Returns(commentServiceModel);
+
+ CommentServiceModel result = await this.PostService.GetCommentById(id);
+
+ Assert.AreEqual(MESSAGE, result.Message);
+ }
+
+ [Test]
+ public void GetLanguageById_ThrowsException_WhenLanguageDoesNotExist()
+ {
+ string exceptionMessage = "The comment does not exist";
+ Guid id = new Guid();
+ this.PostRepositoryMock.Setup(p => p.GetCommentByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult<Comment>(null));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.GetCommentById(id));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+
+ #region Update
+ [Test]
+ [TestCase(true)]
+ [TestCase(false)]
+ public async Task UpdateComment_ReturnsIfUpdateIsSuccessfull_WhenCommentExistsy(bool shouldPass)
+ {
+ Comment comment = new Comment
+ {
+ Message = MESSAGE
+ };
+ UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel
+ {
+ Message = MESSAGE
+ };
+
+ this.PostRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
+ this.PostRepositoryMock.Setup(p => p.EditCommentAsync(It.IsAny<Comment>())).Returns(Task.FromResult(shouldPass));
+ this.MapperMock.Setup(p => p.Map<Comment>(It.IsAny<UpdateCommentServiceModel>())).Returns(comment);
+
+ bool result = await this.PostService.UpdateComment(updateCommentServiceModel);
+
+ Assert.AreEqual(shouldPass, result);
+ }
+
+ [Test]
+ public void UpdateLanguage_ThrowsArgumentException_WhenCommentDoesNotExist()
+ {
+ string exceptionMessage = "Comment does not exist!";
+ UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel
+ {
+ };
+
+ this.PostRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.UpdateComment(updateCommentServiceModel));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+
+ #region Delete
+ [Test]
+ [TestCase(true)]
+ [TestCase(false)]
+ public async Task DeleteComment_ShouldReturnIfDeletionIsSuccessfull_WhenCommentExists(bool shouldPass)
+ {
+ Guid id = new Guid();
+ Comment comment = new Comment();
+
+ this.PostRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
+ this.PostRepositoryMock.Setup(p => p.GetCommentByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(comment));
+ this.PostRepositoryMock.Setup(p => p.DeleteCommentAsync(It.IsAny<Comment>())).Returns(Task.FromResult(shouldPass));
+
+ bool result = await this.PostService.DeleteComment(id);
+
+ Assert.AreEqual(shouldPass, result);
+ }
+
+ [Test]
+ public void DeleteLanguage_ThrowsException_WhenLanguageDoesNotExist()
+ {
+ string exceptionMessage = "Comment does not exist!";
+ Guid id = new Guid();
+
+ this.PostRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.DeleteComment(id));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+
+ #region ValidateJwtForComment
+ //TO DO: Implement
+ #endregion
+ #endregion
+ }
+}
diff --git a/src/DevHive.Tests/DevHive.Web.Tests/LanguageController.Tests.cs b/src/DevHive.Tests/DevHive.Web.Tests/LanguageController.Tests.cs
index 913d9df..7c8d64e 100644
--- a/src/DevHive.Tests/DevHive.Web.Tests/LanguageController.Tests.cs
+++ b/src/DevHive.Tests/DevHive.Web.Tests/LanguageController.Tests.cs
@@ -140,7 +140,7 @@ namespace DevHive.Web.Tests
}
[Test]
- public void Update_ShouldReturnOkResult_WhenLanguageIsNotUpdatedSuccessfully()
+ public void Update_ShouldReturnBadObjectResult_WhenLanguageIsNotUpdatedSuccessfully()
{
Guid id = Guid.NewGuid();
string message = "Could not update Language";
@@ -168,7 +168,7 @@ namespace DevHive.Web.Tests
#region Delete
[Test]
- public void Delete_ReturnsOkResult_When_LanguageIsDeletedSuccessfully()
+ public void Delete_ReturnsOkResult_WhenLanguageIsDeletedSuccessfully()
{
Guid id = Guid.NewGuid();
diff --git a/src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs b/src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs
index 18ed1b9..ace1cae 100644
--- a/src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs
+++ b/src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs
@@ -1,12 +1,235 @@
-using System;
-using System.Collections.Generic;
+using System;
using System.Linq;
-using System.Text;
using System.Threading.Tasks;
+using AutoMapper;
+using DevHive.Services.Interfaces;
+using DevHive.Services.Models.Post.Comment;
+using DevHive.Web.Controllers;
+using DevHive.Web.Models.Post.Comment;
+using Microsoft.AspNetCore.Mvc;
+using Moq;
+using NUnit.Framework;
namespace DevHive.Web.Tests
{
- class PostController
+ [TestFixture]
+ public class PostControllerTests
{
+ const string MESSAGE = "Gosho Trapov";
+ private Mock<IPostService> PostServiceMock { get; set; }
+ private Mock<IMapper> MapperMock { get; set; }
+ private PostController PostController { get; set; }
+
+ [SetUp]
+ public void SetUp()
+ {
+ this.PostServiceMock = new Mock<IPostService>();
+ this.MapperMock = new Mock<IMapper>();
+ this.PostController = new PostController(this.PostServiceMock.Object, this.MapperMock.Object);
+ }
+
+ #region Comment
+ #region Create
+ [Test]
+ public void AddComment_ReturnsOkObjectResult_WhenCommentIsSuccessfullyCreated()
+ {
+ CommentWebModel commentWebModel = new CommentWebModel
+ {
+ Message = MESSAGE
+ };
+ CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel
+ {
+ Message = MESSAGE
+ };
+ Guid id = Guid.NewGuid();
+
+ this.MapperMock.Setup(p => p.Map<CreateCommentServiceModel>(It.IsAny<CommentWebModel>())).Returns(createCommentServiceModel);
+ this.PostServiceMock.Setup(p => p.AddComment(It.IsAny<CreateCommentServiceModel>())).Returns(Task.FromResult(id));
+
+ IActionResult result = this.PostController.AddComment(commentWebModel).Result;
+
+ Assert.IsInstanceOf<OkObjectResult>(result);
+
+ var splitted = (result as OkObjectResult).Value
+ .ToString()
+ .Split('{', '}', '=', ' ')
+ .Where(x => !string.IsNullOrEmpty(x))
+ .ToArray();
+
+ Guid resultId = Guid.Parse(splitted[1]);
+
+ Assert.AreEqual(id, resultId);
+ }
+
+ [Test]
+ public void AddComment_ReturnsBadRequestObjectResult_WhenCommentIsNotCreatedSuccessfully()
+ {
+ CommentWebModel commentWebModel = new CommentWebModel
+ {
+ Message = MESSAGE
+ };
+ CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel
+ {
+ Message = MESSAGE
+ };
+ Guid id = Guid.Empty;
+ string errorMessage = $"Could not create comment";
+
+ this.MapperMock.Setup(p => p.Map<CreateCommentServiceModel>(It.IsAny<CommentWebModel>())).Returns(createCommentServiceModel);
+ this.PostServiceMock.Setup(p => p.AddComment(It.IsAny<CreateCommentServiceModel>())).Returns(Task.FromResult(id));
+
+ IActionResult result = this.PostController.AddComment(commentWebModel).Result;
+
+ Assert.IsInstanceOf<BadRequestObjectResult>(result);
+
+ BadRequestObjectResult badRequsetObjectResult = result as BadRequestObjectResult;
+ string resultMessage = badRequsetObjectResult.Value.ToString();
+
+ Assert.AreEqual(errorMessage, resultMessage);
+ }
+ #endregion
+
+ #region Read
+ [Test]
+ public void GetCommentById_ReturnsTheComment_WhenItExists()
+ {
+ Guid id = Guid.NewGuid();
+
+ CommentServiceModel commentServiceModel = new CommentServiceModel
+ {
+ Message = MESSAGE
+ };
+ CommentWebModel commentWebModel = new CommentWebModel
+ {
+ Message = MESSAGE
+ };
+
+ this.PostServiceMock.Setup(p => p.GetCommentById(It.IsAny<Guid>())).Returns(Task.FromResult(commentServiceModel));
+ this.MapperMock.Setup(p => p.Map<CommentWebModel>(It.IsAny<CommentServiceModel>())).Returns(commentWebModel);
+
+ IActionResult result = this.PostController.GetCommentById(id).Result;
+
+ Assert.IsInstanceOf<OkObjectResult>(result);
+
+ OkObjectResult okObjectResult = result as OkObjectResult;
+ CommentWebModel resultModel = okObjectResult.Value as Models.Post.Comment.CommentWebModel;
+
+ Assert.AreEqual(MESSAGE, resultModel.Message);
+ }
+ #endregion
+
+ #region Update
+ [Test]
+ public void UpdateComment_ShouldReturnOkResult_WhenCommentIsUpdatedSuccessfully()
+ {
+ Guid id = Guid.NewGuid();
+ CommentWebModel commentWebModel = new CommentWebModel
+ {
+ Message = MESSAGE
+ };
+ UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel
+ {
+ Message = MESSAGE
+ };
+
+ this.PostServiceMock.Setup(p => p.UpdateComment(It.IsAny<UpdateCommentServiceModel>())).Returns(Task.FromResult(true));
+ this.MapperMock.Setup(p => p.Map<UpdateCommentServiceModel>(It.IsAny<CommentWebModel>())).Returns(updateCommentServiceModel);
+ this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true));
+
+ IActionResult result = this.PostController.UpdateComment(id, commentWebModel, null).Result;
+
+ Assert.IsInstanceOf<OkResult>(result);
+ }
+
+ [Test]
+ public void UpdateComment_ShouldReturnBadObjectResult_WhenCommentIsNotUpdatedSuccessfully()
+ {
+ Guid id = Guid.NewGuid();
+ string message = "Could not update Comment";
+ CommentWebModel commentWebModel = new CommentWebModel
+ {
+ Message = MESSAGE
+ };
+ UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel
+ {
+ Message = MESSAGE
+ };
+
+ this.PostServiceMock.Setup(p => p.UpdateComment(It.IsAny<UpdateCommentServiceModel>())).Returns(Task.FromResult(false));
+ this.MapperMock.Setup(p => p.Map<UpdateCommentServiceModel>(It.IsAny<CommentWebModel>())).Returns(updateCommentServiceModel);
+ this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true));
+
+ IActionResult result = this.PostController.UpdateComment(id, commentWebModel, null).Result;
+ Assert.IsInstanceOf<BadRequestObjectResult>(result);
+
+ BadRequestObjectResult badRequestObjectResult = result as BadRequestObjectResult;
+ string resultModel = badRequestObjectResult.Value.ToString();
+
+ Assert.AreEqual(message, resultModel);
+ }
+
+ [Test]
+ public void UpdateComment_ShouldReturnUnauthorizedResult_WhenJwtIsNotValid()
+ {
+ Guid id = Guid.NewGuid();
+
+ CommentWebModel commentWebModel = new CommentWebModel
+ {
+ Message = MESSAGE
+ };
+
+ this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(false));
+
+ IActionResult result = this.PostController.UpdateComment(id, commentWebModel, null).Result;
+ Assert.IsInstanceOf<UnauthorizedResult>(result);
+ }
+ #endregion
+
+ #region Delete
+ [Test]
+ public void DeleteComment_ReturnsOkResult_WhenLanguageIsDeletedSuccessfully()
+ {
+ Guid id = Guid.NewGuid();
+
+ this.PostServiceMock.Setup(p => p.DeleteComment(It.IsAny<Guid>())).Returns(Task.FromResult(true));
+ this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true));
+
+ IActionResult result = this.PostController.DeleteComment(id, null).Result;
+
+ Assert.IsInstanceOf<OkResult>(result);
+ }
+
+ [Test]
+ public void DeletComment_ReturnsBadRequestObjectResult_WhenLanguageIsNotDeletedSuccessfully()
+ {
+ string message = "Could not delete Comment";
+ Guid id = Guid.NewGuid();
+
+ this.PostServiceMock.Setup(p => p.DeleteComment(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+ this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true));
+
+ IActionResult result = this.PostController.DeleteComment(id, null).Result;
+
+ Assert.IsInstanceOf<BadRequestObjectResult>(result);
+
+ BadRequestObjectResult badRequestObjectResult = result as BadRequestObjectResult;
+ string resultModel = badRequestObjectResult.Value.ToString();
+
+ Assert.AreEqual(message, resultModel);
+ }
+
+ [Test]
+ public void DeletComment_ReturnsUnauthorizedResult_WhenJwtIsNotValid()
+ {
+ Guid id = Guid.NewGuid();
+
+ this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(false));
+
+ IActionResult result = this.PostController.DeleteComment(id, null).Result;
+
+ Assert.IsInstanceOf<UnauthorizedResult>(result);
+ }
+ #endregion
+ #endregion
}
}
diff --git a/src/DevHive.Tests/DevHive.Web.Tests/TechnologyController.Tests.cs b/src/DevHive.Tests/DevHive.Web.Tests/TechnologyController.Tests.cs
index 40434d6..72118b3 100644
--- a/src/DevHive.Tests/DevHive.Web.Tests/TechnologyController.Tests.cs
+++ b/src/DevHive.Tests/DevHive.Web.Tests/TechnologyController.Tests.cs
@@ -141,7 +141,7 @@ namespace DevHive.Web.Tests
}
[Test]
- public void Update_ShouldReturnOkResult_WhenTechnologyIsNotUpdatedSuccessfully()
+ public void Update_ShouldReturnBadObjectResult_WhenTechnologyIsNotUpdatedSuccessfully()
{
Guid id = Guid.NewGuid();
string message = "Could not update Technology";
@@ -169,7 +169,7 @@ namespace DevHive.Web.Tests
#region Delete
[Test]
- public void Delete_ReturnsOkResult_When_TechnologyIsDeletedSuccessfully()
+ public void Delete_ReturnsOkResult_WhenTechnologyIsDeletedSuccessfully()
{
Guid id = Guid.NewGuid();
diff --git a/src/DevHive.Web/Controllers/PostController.cs b/src/DevHive.Web/Controllers/PostController.cs
index 2a08605..50923d2 100644
--- a/src/DevHive.Web/Controllers/PostController.cs
+++ b/src/DevHive.Web/Controllers/PostController.cs
@@ -48,7 +48,7 @@ namespace DevHive.Web.Controllers
Guid id = await this._postService.AddComment(createCommentServiceModel);
return id == Guid.Empty ?
- new BadRequestObjectResult("Could not create language") :
+ new BadRequestObjectResult("Could not create comment") :
new OkObjectResult(new { Id = id });
}