using System; using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using DevHive.Services.Models; using DevHive.Services.Models.Post.Post; using DevHive.Services.Services; using Moq; using NUnit.Framework; namespace DevHive.Services.Tests { [TestFixture] public class FeedServiceTests { private Mock FeedRepositoryMock { get; set; } private Mock UserRepositoryMock { get; set; } private Mock MapperMock { get; set; } private FeedService FeedService { get; set; } #region SetUps [SetUp] public void Setup() { this.FeedRepositoryMock = new Mock(); this.UserRepositoryMock = new Mock(); this.MapperMock = new Mock(); this.FeedService = new FeedService(this.FeedRepositoryMock.Object, this.UserRepositoryMock.Object, this.MapperMock.Object); } #endregion #region GetPage [Test] public async Task GetPage_ReturnsReadPageServiceModel_WhenSuitablePostsExist() { GetPageServiceModel getPageServiceModel = new GetPageServiceModel { UserId = Guid.NewGuid() }; User dummyUser = CreateDummyUser(); User anotherDummyUser = CreateAnotherDummyUser(); HashSet friends = new HashSet(); friends.Add(anotherDummyUser); dummyUser.Friends = friends; List posts = new List { new Post{ Message = "Message"} }; ReadPostServiceModel readPostServiceModel = new ReadPostServiceModel { PostId = Guid.NewGuid(), Message = "Message" }; List readPostServiceModels = new List(); readPostServiceModels.Add(readPostServiceModel); ReadPageServiceModel readPageServiceModel = new ReadPageServiceModel { Posts = readPostServiceModels }; this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(dummyUser)); this.FeedRepositoryMock.Setup(p => p.GetFriendsPosts(It.IsAny>(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(Task.FromResult(posts)); this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(readPostServiceModel); ReadPageServiceModel result = await this.FeedService.GetPage(getPageServiceModel); Assert.GreaterOrEqual(1, result.Posts.Count, "GetPage does not correctly return the posts"); } [Test] public void GetPage_ThrowsException_WhenNoSuitablePostsExist() { const string EXCEPTION_MESSAGE = "No friends of user have posted anything yet!"; GetPageServiceModel getPageServiceModel = new GetPageServiceModel { UserId = Guid.NewGuid() }; User dummyUser = CreateDummyUser(); User anotherDummyUser = CreateAnotherDummyUser(); HashSet friends = new HashSet(); friends.Add(anotherDummyUser); dummyUser.Friends = friends; ReadPostServiceModel readPostServiceModel = new ReadPostServiceModel { PostId = Guid.NewGuid(), Message = "Message" }; List readPostServiceModels = new List(); readPostServiceModels.Add(readPostServiceModel); ReadPageServiceModel readPageServiceModel = new ReadPageServiceModel { Posts = readPostServiceModels }; this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(dummyUser)); this.FeedRepositoryMock.Setup(p => p.GetFriendsPosts(It.IsAny>(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(Task.FromResult(new List())); Exception ex = Assert.ThrowsAsync(() => this.FeedService.GetPage(getPageServiceModel)); Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Wrong exception message"); } [Test] public void GetPage_ThrowsException_WhenUserHasNoFriendsToGetPostsFrom() { const string EXCEPTION_MESSAGE = "User has no friends to get feed from!"; GetPageServiceModel getPageServiceModel = new GetPageServiceModel { UserId = Guid.NewGuid() }; User dummyUser = CreateDummyUser(); this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(dummyUser)); Exception ex = Assert.ThrowsAsync(() => this.FeedService.GetPage(getPageServiceModel)); Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Wrong exception message"); } #endregion #region HelperMethods private User CreateDummyUser() { return new() { Id = Guid.NewGuid(), UserName = "dummyUser", FirstName = "Spas", LastName = "Spasov", Email = "abv@abv.bg", }; } private User CreateAnotherDummyUser() { return new() { Id = Guid.NewGuid(), UserName = "anotherDummyUser", FirstName = "Alex", LastName = "Spiridonov", Email = "a_spiridonov@abv.bg", }; } #endregion } }