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 _postRepositoryMock; private Mock _ratingRepositoryMock; private Mock _userRepositoryMock; private Mock _mapperMock; private RatingService _ratingService; #region SetUps [SetUp] public void SetUp() { this._postRepositoryMock = new Mock(); this._ratingRepositoryMock = new Mock(); this._userRepositoryMock = new Mock(); this._mapperMock = new Mock(); 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())) .ReturnsAsync(true); this._ratingRepositoryMock .Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())) .ReturnsAsync(false); this._userRepositoryMock .Setup(p => p.GetByIdAsync(It.IsAny())) .ReturnsAsync(user); this._postRepositoryMock .Setup(p => p.GetByIdAsync(It.IsAny())) .ReturnsAsync(post); this._ratingRepositoryMock .Setup(p => p.AddAsync(It.IsAny())) .ReturnsAsync(true); this._ratingRepositoryMock .Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())) .ReturnsAsync(rating); this._mapperMock .Setup(p => p.Map(It.IsAny())) .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())) .ReturnsAsync(true); this._ratingRepositoryMock .Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())) .ReturnsAsync(false); this._userRepositoryMock .Setup(p => p.GetByIdAsync(It.IsAny())) .ReturnsAsync(user); this._postRepositoryMock .Setup(p => p.GetByIdAsync(It.IsAny())) .ReturnsAsync(post); this._ratingRepositoryMock .Setup(p => p.AddAsync(It.IsAny())) .ReturnsAsync(false); this._mapperMock .Setup(p => p.Map(It.IsAny())) .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())) .ReturnsAsync(rating); this._mapperMock .Setup(p => p.Map(It.IsAny())) .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())) // .Returns(Task.FromResult(null)); // // Exception ex = Assert.ThrowsAsync(() => 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(), It.IsAny())) .ReturnsAsync(rating); this._mapperMock .Setup(p => p.Map(It.IsAny())) .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(), It.IsAny())) // .Returns(Task.FromResult(null)); // // Exception ex = Assert.ThrowsAsync(() => 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(), It.IsAny())) .ReturnsAsync(rating); this._userRepositoryMock .Setup(p => p.GetByIdAsync(It.IsAny())) .ReturnsAsync(user); this._ratingRepositoryMock .Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())) .ReturnsAsync(true); this._ratingRepositoryMock .Setup(p => p.EditAsync(It.IsAny(), It.IsAny())) .ReturnsAsync(true); this._mapperMock .Setup(p => p.Map(It.IsAny())) .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(), It.IsAny())) .ReturnsAsync(rating); this._userRepositoryMock .Setup(p => p.GetByIdAsync(It.IsAny())) .ReturnsAsync(user); this._ratingRepositoryMock .Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())) .ReturnsAsync(true); this._ratingRepositoryMock .Setup(p => p.EditAsync(It.IsAny(), It.IsAny())) .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(), It.IsAny())) .Returns(Task.FromResult(null)); Exception ex = Assert.ThrowsAsync(() => 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(), It.IsAny())) .ReturnsAsync(rating); this._userRepositoryMock .Setup(p => p.GetByIdAsync(It.IsAny())) .ReturnsAsync(user); this._ratingRepositoryMock .Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())) .ReturnsAsync(false); Exception ex = Assert.ThrowsAsync(() => 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())) .ReturnsAsync(true); this._ratingRepositoryMock .Setup(p => p.GetByIdAsync(It.IsAny())) .Returns(Task.FromResult(null)); this._ratingRepositoryMock .Setup(p => p.DeleteAsync(It.IsAny())) .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())) .ReturnsAsync(true); this._ratingRepositoryMock .Setup(p => p.GetByIdAsync(It.IsAny())) .Returns(Task.FromResult(null)); this._ratingRepositoryMock .Setup(p => p.DeleteAsync(It.IsAny())) .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())) .ReturnsAsync(false); Exception ex = Assert.ThrowsAsync(() => this._ratingService.DeleteRating(Guid.Empty)); // Assert.AreEqual(ex.Message, exceptionMessage); } #endregion } }