aboutsummaryrefslogtreecommitdiff
path: root/src/Services/DevHive.Services.Tests
diff options
context:
space:
mode:
Diffstat (limited to 'src/Services/DevHive.Services.Tests')
-rw-r--r--src/Services/DevHive.Services.Tests/CommentService.Tests.cs262
-rw-r--r--src/Services/DevHive.Services.Tests/DevHive.Services.Tests.csproj29
-rw-r--r--src/Services/DevHive.Services.Tests/FeedService.Tests.cs155
-rw-r--r--src/Services/DevHive.Services.Tests/LanguageService.Tests.cs262
-rw-r--r--src/Services/DevHive.Services.Tests/PostService.Tests.cs271
-rw-r--r--src/Services/DevHive.Services.Tests/RoleService.Tests.cs230
-rw-r--r--src/Services/DevHive.Services.Tests/TechnologyServices.Tests.cs262
-rw-r--r--src/Services/DevHive.Services.Tests/UserService.Tests.cs394
8 files changed, 1865 insertions, 0 deletions
diff --git a/src/Services/DevHive.Services.Tests/CommentService.Tests.cs b/src/Services/DevHive.Services.Tests/CommentService.Tests.cs
new file mode 100644
index 0000000..ac022ea
--- /dev/null
+++ b/src/Services/DevHive.Services.Tests/CommentService.Tests.cs
@@ -0,0 +1,262 @@
+using System;
+using System.Threading.Tasks;
+using AutoMapper;
+using DevHive.Data.Interfaces.Repositories;
+using DevHive.Data.Models;
+using DevHive.Services.Models.Comment;
+using DevHive.Services.Services;
+using Moq;
+using NUnit.Framework;
+
+namespace DevHive.Services.Tests
+{
+ [TestFixture]
+ 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; }
+
+ #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);
+ }
+ #endregion
+
+ #region AddComment
+ [Test]
+ public async Task AddComment_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully()
+ {
+ Guid id = Guid.NewGuid();
+ User creator = new User { Id = Guid.NewGuid() };
+ CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel
+ {
+ Message = MESSAGE
+ };
+ Comment comment = new Comment
+ {
+ 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);
+
+ Assert.AreEqual(id, result);
+ }
+
+ [Test]
+ public async Task AddComment_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully()
+ {
+ CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel
+ {
+ Message = MESSAGE
+ };
+ Comment comment = new Comment
+ {
+ 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);
+
+ Guid result = await this.CommentService.AddComment(createCommentServiceModel);
+
+ Assert.IsTrue(result == Guid.Empty);
+ }
+
+ [Test]
+ public void AddComment_ThrowsException_WhenPostDoesNotExist()
+ {
+ const string EXCEPTION_MESSAGE = "Post does not exist!";
+
+ CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel
+ {
+ Message = MESSAGE
+ };
+
+ 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");
+ }
+ #endregion
+
+ #region GetCommentById
+ [Test]
+ public async Task GetCommentById_ReturnsTheComment_WhenItExists()
+ {
+ Guid creatorId = new Guid();
+ User creator = new User { Id = creatorId };
+ Comment comment = new Comment
+ {
+ Message = MESSAGE,
+ Creator = creator
+ };
+ ReadCommentServiceModel commentServiceModel = new ReadCommentServiceModel
+ {
+ Message = MESSAGE
+ };
+ User user = new User
+ {
+ 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);
+
+ ReadCommentServiceModel result = await this.CommentService.GetCommentById(new Guid());
+
+ Assert.AreEqual(MESSAGE, result.Message);
+ }
+
+ [Test]
+ 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
+ {
+ Message = MESSAGE,
+ Creator = creator
+ };
+
+ this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(comment));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.CommentService.GetCommentById(new Guid()), "GetCommentById does not throw exception when the user does not exist");
+
+ Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message);
+ }
+
+ [Test]
+ public void GetCommentById_ThrowsException_WhenCommentDoesNotExist()
+ {
+ string exceptionMessage = "The comment does not exist";
+ Guid creatorId = new Guid();
+ User user = new User
+ {
+ 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));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.CommentService.GetCommentById(new Guid()));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+
+ #region UpdateComment
+ [Test]
+ public async Task UpdateComment_ReturnsTheIdOfTheComment_WhenUpdatedSuccessfully()
+ {
+ Guid id = Guid.NewGuid();
+ Comment comment = new Comment
+ {
+ Id = id,
+ Message = MESSAGE
+ };
+ UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel
+ {
+ 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);
+
+ Assert.AreEqual(updateCommentServiceModel.CommentId, result);
+ }
+
+ [Test]
+ public async Task UpdateComment_ReturnsEmptyId_WhenTheCommentIsNotUpdatedSuccessfully()
+ {
+ Comment comment = new Comment
+ {
+ Message = MESSAGE
+ };
+ UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel
+ {
+ 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);
+
+ Guid result = await this.CommentService.UpdateComment(updateCommentServiceModel);
+
+ Assert.AreEqual(Guid.Empty, result);
+ }
+
+ [Test]
+ public void UpdateComment_ThrowsArgumentException_WhenCommentDoesNotExist()
+ {
+ string exceptionMessage = "Comment does not exist!";
+ UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel
+ {
+ };
+
+ this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.CommentService.UpdateComment(updateCommentServiceModel));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+
+ #region DeleteComment
+ [Test]
+ [TestCase(true)]
+ [TestCase(false)]
+ public async Task DeleteComment_ShouldReturnIfDeletionIsSuccessfull_WhenCommentExists(bool shouldPass)
+ {
+ Guid id = new Guid();
+ Comment comment = new Comment();
+
+ 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));
+
+ bool result = await this.CommentService.DeleteComment(id);
+
+ Assert.AreEqual(shouldPass, result);
+ }
+
+ [Test]
+ public void DeleteComment_ThrowsException_WhenCommentDoesNotExist()
+ {
+ string exceptionMessage = "Comment does not exist!";
+ Guid id = new Guid();
+
+ this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.CommentService.DeleteComment(id));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+ }
+}
diff --git a/src/Services/DevHive.Services.Tests/DevHive.Services.Tests.csproj b/src/Services/DevHive.Services.Tests/DevHive.Services.Tests.csproj
new file mode 100644
index 0000000..af419df
--- /dev/null
+++ b/src/Services/DevHive.Services.Tests/DevHive.Services.Tests.csproj
@@ -0,0 +1,29 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <TargetFramework>net5.0</TargetFramework>
+
+ <IsPackable>false</IsPackable>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.1" />
+ <PackageReference Include="Moq" Version="4.16.0" />
+ <PackageReference Include="NUnit" Version="3.13.0" />
+ <PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
+ <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
+ </ItemGroup>
+
+ <ItemGroup>
+ <ProjectReference Include="..\DevHive.Services\DevHive.Services.csproj" />
+
+ <ProjectReference Include="..\DevHive.Services.Models\DevHive.Services.Models.csproj" />
+
+ <ProjectReference Include="..\..\Common\DevHive.Common.Models\DevHive.Common.csproj" />
+ </ItemGroup>
+
+ <PropertyGroup>
+ <EnableNETAnalyzers>true</EnableNETAnalyzers>
+ <AnalysisLevel>latest</AnalysisLevel>
+ </PropertyGroup>
+</Project>
diff --git a/src/Services/DevHive.Services.Tests/FeedService.Tests.cs b/src/Services/DevHive.Services.Tests/FeedService.Tests.cs
new file mode 100644
index 0000000..e4020c5
--- /dev/null
+++ b/src/Services/DevHive.Services.Tests/FeedService.Tests.cs
@@ -0,0 +1,155 @@
+//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;
+//using DevHive.Services.Services;
+//using Moq;
+//using NUnit.Framework;
+
+//namespace DevHive.Services.Tests
+//{
+// [TestFixture]
+// public class FeedServiceTests
+// {
+// private Mock<IFeedRepository> FeedRepositoryMock { get; set; }
+// private Mock<IUserRepository> UserRepositoryMock { get; set; }
+// private Mock<IMapper> MapperMock { get; set; }
+// private FeedService FeedService { get; set; }
+
+// #region SetUps
+// [SetUp]
+// public void Setup()
+// {
+// this.FeedRepositoryMock = new Mock<IFeedRepository>();
+// this.UserRepositoryMock = new Mock<IUserRepository>();
+// this.MapperMock = new Mock<IMapper>();
+// 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<User> friends = new HashSet<User>();
+// friends.Add(anotherDummyUser);
+// dummyUser.Friends = friends;
+
+// List<Post> posts = new List<Post>
+// {
+// new Post{ Message = "Message"}
+// };
+
+// ReadPostServiceModel readPostServiceModel = new ReadPostServiceModel
+// {
+// PostId = Guid.NewGuid(),
+// Message = "Message"
+// };
+// List<ReadPostServiceModel> readPostServiceModels = new List<ReadPostServiceModel>();
+// readPostServiceModels.Add(readPostServiceModel);
+// ReadPageServiceModel readPageServiceModel = new ReadPageServiceModel
+// {
+// Posts = readPostServiceModels
+// };
+
+// this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(dummyUser));
+// this.FeedRepositoryMock.Setup(p => p.GetFriendsPosts(It.IsAny<List<User>>(), It.IsAny<DateTime>(), It.IsAny<int>(), It.IsAny<int>())).Returns(Task.FromResult(posts));
+// this.MapperMock.Setup(p => p.Map<ReadPostServiceModel>(It.IsAny<Post>())).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<User> friends = new HashSet<User>();
+// friends.Add(anotherDummyUser);
+// dummyUser.Friends = friends;
+
+// ReadPostServiceModel readPostServiceModel = new ReadPostServiceModel
+// {
+// PostId = Guid.NewGuid(),
+// Message = "Message"
+// };
+// List<ReadPostServiceModel> readPostServiceModels = new List<ReadPostServiceModel>();
+// readPostServiceModels.Add(readPostServiceModel);
+// ReadPageServiceModel readPageServiceModel = new ReadPageServiceModel
+// {
+// Posts = readPostServiceModels
+// };
+
+// this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(dummyUser));
+// this.FeedRepositoryMock.Setup(p => p.GetFriendsPosts(It.IsAny<List<User>>(), It.IsAny<DateTime>(), It.IsAny<int>(), It.IsAny<int>())).Returns(Task.FromResult(new List<Post>()));
+
+// Exception ex = Assert.ThrowsAsync<ArgumentException>(() => 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<Guid>())).Returns(Task.FromResult(dummyUser));
+
+// Exception ex = Assert.ThrowsAsync<ArgumentException>(() => 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
+// }
+//}
diff --git a/src/Services/DevHive.Services.Tests/LanguageService.Tests.cs b/src/Services/DevHive.Services.Tests/LanguageService.Tests.cs
new file mode 100644
index 0000000..1b59f91
--- /dev/null
+++ b/src/Services/DevHive.Services.Tests/LanguageService.Tests.cs
@@ -0,0 +1,262 @@
+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.Language;
+using DevHive.Services.Services;
+using Moq;
+using NUnit.Framework;
+
+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; }
+
+ #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);
+ }
+ #endregion
+
+ #region CreateLanguage
+ [Test]
+ public async Task CreateLanguage_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully()
+ {
+ string technologyName = "Gosho Trapov";
+ Guid id = Guid.NewGuid();
+ CreateLanguageServiceModel createLanguageServiceModel = new CreateLanguageServiceModel
+ {
+ Name = technologyName
+ };
+ Language language = new Language
+ {
+ Name = technologyName,
+ 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);
+
+ Assert.AreEqual(id, result);
+ }
+
+ [Test]
+ public async Task CreateLanguage_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully()
+ {
+ string languageName = "Gosho Trapov";
+
+ CreateLanguageServiceModel createLanguageServiceModel = new CreateLanguageServiceModel
+ {
+ Name = languageName
+ };
+ Language language = new Language
+ {
+ 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);
+
+ Guid result = await this.LanguageService.CreateLanguage(createLanguageServiceModel);
+
+ Assert.IsTrue(result == Guid.Empty);
+
+ }
+
+ [Test]
+ public void CreateLanguage_ThrowsArgumentException_WhenEntityAlreadyExists()
+ {
+ string exceptionMessage = "Language already exists!";
+ string languageName = "Gosho Trapov";
+
+ CreateLanguageServiceModel createLanguageServiceModel = new CreateLanguageServiceModel
+ {
+ Name = languageName
+ };
+ Language language = new Language
+ {
+ Name = languageName
+ };
+
+ this.LanguageRepositoryMock.Setup(p => p.DoesLanguageNameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(true));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.LanguageService.CreateLanguage(createLanguageServiceModel));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+
+ #region GetLanguageById
+ [Test]
+ public async Task GetLanguageById_ReturnsTheLanguage_WhenItExists()
+ {
+ Guid id = new Guid();
+ string name = "Gosho Trapov";
+ Language language = new Language
+ {
+ Name = name
+ };
+ ReadLanguageServiceModel readLanguageServiceModel = new ReadLanguageServiceModel
+ {
+ 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);
+
+ ReadLanguageServiceModel result = await this.LanguageService.GetLanguageById(id);
+
+ Assert.AreEqual(name, result.Name);
+ }
+
+ [Test]
+ 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));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.LanguageService.GetLanguageById(id));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+
+ #region GetLanguages
+ [Test]
+ public void GetLanguages_ReturnsAllLanguages_IfAnyExist()
+ {
+ ReadLanguageServiceModel firstLanguage = new ReadLanguageServiceModel();
+ ReadLanguageServiceModel secondLanguage = new ReadLanguageServiceModel();
+ HashSet<ReadLanguageServiceModel> languges = new HashSet<ReadLanguageServiceModel>();
+ 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);
+
+ HashSet<ReadLanguageServiceModel> result = this.LanguageService.GetLanguages();
+
+ Assert.GreaterOrEqual(2, result.Count, "GetLanguages does not return all languages");
+ }
+
+ [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>());
+
+ HashSet<ReadLanguageServiceModel> result = this.LanguageService.GetLanguages();
+
+ Assert.IsEmpty(result, "GetLanguages does not return empty string when no languages exist");
+ }
+ #endregion
+
+ #region UpdateLanguage
+ [Test]
+ [TestCase(true)]
+ [TestCase(false)]
+ public async Task UpdateLanguage_ReturnsIfUpdateIsSuccessfull_WhenLanguageExistsy(bool shouldPass)
+ {
+ string name = "Gosho Trapov";
+ Guid id = Guid.NewGuid();
+ Language language = new Language
+ {
+ Name = name,
+ Id = id
+ };
+ UpdateLanguageServiceModel updateLanguageServiceModel = new UpdateLanguageServiceModel
+ {
+ 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);
+
+ Assert.AreEqual(shouldPass, result);
+ }
+
+ [Test]
+ public void UpdateLanguage_ThrowsArgumentException_WhenLanguageDoesNotExist()
+ {
+ string exceptionMessage = "Language does not exist!";
+ UpdateLanguageServiceModel updateTechnologyServiceModel = new UpdateLanguageServiceModel
+ {
+ };
+
+ this.LanguageRepositoryMock.Setup(p => p.DoesLanguageExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.LanguageService.UpdateLanguage(updateTechnologyServiceModel));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+
+ [Test]
+ public void UpdateLanguage_ThrowsException_WhenLanguageNameAlreadyExists()
+ {
+ string exceptionMessage = "Language name already exists in our data base!";
+ UpdateLanguageServiceModel updateTechnologyServiceModel = new UpdateLanguageServiceModel
+ {
+ };
+
+ 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));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.LanguageService.UpdateLanguage(updateTechnologyServiceModel));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+
+ #region DeleteLanguage
+ [Test]
+ [TestCase(true)]
+ [TestCase(false)]
+ public async Task DeleteLanguage_ShouldReturnIfDeletionIsSuccessfull_WhenLanguageExists(bool shouldPass)
+ {
+ Guid id = new Guid();
+ 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));
+
+ bool result = await this.LanguageService.DeleteLanguage(id);
+
+ Assert.AreEqual(shouldPass, result);
+ }
+
+ [Test]
+ public void DeleteLanguage_ThrowsException_WhenLanguageDoesNotExist()
+ {
+ string exceptionMessage = "Language does not exist!";
+ Guid id = new Guid();
+
+ this.LanguageRepositoryMock.Setup(p => p.DoesLanguageExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.LanguageService.DeleteLanguage(id));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+ }
+}
diff --git a/src/Services/DevHive.Services.Tests/PostService.Tests.cs b/src/Services/DevHive.Services.Tests/PostService.Tests.cs
new file mode 100644
index 0000000..900608c
--- /dev/null
+++ b/src/Services/DevHive.Services.Tests/PostService.Tests.cs
@@ -0,0 +1,271 @@
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using AutoMapper;
+using DevHive.Data.Interfaces.Repositories;
+using DevHive.Data.Models;
+using DevHive.Services.Interfaces;
+using DevHive.Services.Models.Post;
+using DevHive.Services.Services;
+using Microsoft.AspNetCore.Http;
+using Moq;
+using NUnit.Framework;
+
+namespace DevHive.Services.Tests
+{
+ [TestFixture]
+ 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; }
+
+ #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);
+ }
+ #endregion
+
+ #region CreatePost
+ [Test]
+ public async Task CreatePost_ReturnsIdOfThePost_WhenItIsSuccessfullyCreated()
+ {
+ Guid postId = Guid.NewGuid();
+ User creator = new User { Id = Guid.NewGuid() };
+ CreatePostServiceModel createPostServiceModel = new CreatePostServiceModel
+ {
+ Files = new List<IFormFile>()
+ };
+ Post post = new Post
+ {
+ Message = MESSAGE,
+ 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);
+
+ Assert.AreEqual(postId, result, "CreatePost does not return the correct id");
+ }
+
+ [Test]
+ public async Task CreatePost_ReturnsEmptyGuid_WhenItIsNotSuccessfullyCreated()
+ {
+ CreatePostServiceModel createPostServiceModel = new CreatePostServiceModel
+ {
+ Files = new List<IFormFile>()
+ };
+ Post post = new Post
+ {
+ 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);
+
+ Guid result = await this.PostService.CreatePost(createPostServiceModel);
+
+ Assert.AreEqual(Guid.Empty, result, "CreatePost does not return empty id");
+ }
+
+ [Test]
+ public void CreatePost_ThrowsException_WhenUserDoesNotExist()
+ {
+ const string EXCEPTION_MESSAGE = "User does not exist!";
+ 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");
+
+ Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Excapetion message is not correct");
+ }
+ #endregion
+
+ #region GetPostById
+ [Test]
+ public async Task GetPostById_ReturnsThePost_WhenItExists()
+ {
+ Guid creatorId = new Guid();
+ User creator = new User { Id = creatorId };
+ Post post = new Post
+ {
+ Message = MESSAGE,
+ Creator = creator
+ };
+ ReadPostServiceModel readPostServiceModel = new ReadPostServiceModel
+ {
+ Message = MESSAGE
+ };
+ User user = new User
+ {
+ 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);
+
+ ReadPostServiceModel result = await this.PostService.GetPostById(new Guid());
+
+ Assert.AreEqual(MESSAGE, result.Message);
+ }
+
+ [Test]
+ public void GetPostById_ThorwsException_WhenTheUserDoesNotExist()
+ {
+ const string EXCEPTION_MESSAGE = "The user does not exist!";
+ Guid creatorId = new Guid();
+ User creator = new User { Id = creatorId };
+ Post post = new Post
+ {
+ Message = MESSAGE,
+ Creator = creator
+ };
+
+ this.PostRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(post));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.GetPostById(new Guid()), "GetPostById does not throw exception when the user does not exist");
+
+ Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message);
+ }
+
+ [Test]
+ public void GetPostById_ThrowsException_WhenCommentDoesNotExist()
+ {
+ string exceptionMessage = "The post does not exist!";
+ Guid creatorId = new Guid();
+ 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));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.GetPostById(new Guid()));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+
+ #region UpdatePost
+ [Test]
+ public async Task UpdatePost_ReturnsTheIdOfThePost_WhenUpdatedSuccessfully()
+ {
+ Guid id = Guid.NewGuid();
+ Post post = new Post
+ {
+ Id = id,
+ Message = MESSAGE
+ };
+ UpdatePostServiceModel updatePostServiceModel = new UpdatePostServiceModel
+ {
+ PostId = id,
+ NewMessage = MESSAGE,
+ 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);
+
+ Assert.AreEqual(updatePostServiceModel.PostId, result);
+ }
+
+ [Test]
+ public async Task UpdatePost_ReturnsEmptyId_WhenThePostIsNotUpdatedSuccessfully()
+ {
+ Post post = new Post
+ {
+ Message = MESSAGE
+ };
+ UpdatePostServiceModel updatePostServiceModel = new UpdatePostServiceModel
+ {
+ PostId = Guid.NewGuid(),
+ NewMessage = MESSAGE,
+ 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);
+
+ Guid result = await this.PostService.UpdatePost(updatePostServiceModel);
+
+ Assert.AreEqual(Guid.Empty, result);
+ }
+
+ [Test]
+ public void UpdatePost_ThrowsArgumentException_WhenCommentDoesNotExist()
+ {
+ string exceptionMessage = "Post does not exist!";
+ UpdatePostServiceModel updatePostServiceModel = new UpdatePostServiceModel
+ {
+ };
+
+ this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.UpdatePost(updatePostServiceModel));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+
+ #region DeletePost
+ [Test]
+ [TestCase(true)]
+ [TestCase(false)]
+ public async Task Deletepost_ShouldReturnIfDeletionIsSuccessfull_WhenPostExists(bool shouldPass)
+ {
+ Guid id = new Guid();
+ 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));
+
+ bool result = await this.PostService.DeletePost(id);
+
+ Assert.AreEqual(shouldPass, result);
+ }
+
+ [Test]
+ public void DeletePost_ThrowsException_WhenPostDoesNotExist()
+ {
+ string exceptionMessage = "Post does not exist!";
+ Guid id = new Guid();
+
+ this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.DeletePost(id));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+ }
+}
diff --git a/src/Services/DevHive.Services.Tests/RoleService.Tests.cs b/src/Services/DevHive.Services.Tests/RoleService.Tests.cs
new file mode 100644
index 0000000..e500dd1
--- /dev/null
+++ b/src/Services/DevHive.Services.Tests/RoleService.Tests.cs
@@ -0,0 +1,230 @@
+using System;
+using System.Threading.Tasks;
+using AutoMapper;
+using DevHive.Data.Interfaces.Repositories;
+using DevHive.Data.Models;
+using DevHive.Services.Models.Identity.Role;
+using DevHive.Services.Services;
+using Moq;
+using NUnit.Framework;
+
+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; }
+
+ #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);
+ }
+ #endregion
+
+ #region CreateRole
+ [Test]
+ public async Task CreateRole_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully()
+ {
+ string roleName = "Gosho Trapov";
+ Guid id = Guid.NewGuid();
+ CreateRoleServiceModel createRoleServiceModel = new CreateRoleServiceModel
+ {
+ Name = roleName
+ };
+ Role role = new()
+ {
+ Name = roleName,
+ 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);
+
+ Assert.AreEqual(id, result);
+ }
+
+ [Test]
+ public async Task CreateRoley_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully()
+ {
+ string roleName = "Gosho Trapov";
+
+ CreateRoleServiceModel createRoleServiceModel = new CreateRoleServiceModel
+ {
+ Name = roleName
+ };
+ Role role = new Role
+ {
+ 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);
+
+ Guid result = await this.RoleService.CreateRole(createRoleServiceModel);
+
+ Assert.IsTrue(result == Guid.Empty);
+ }
+
+ [Test]
+ public void CreateTechnology_ThrowsArgumentException_WhenEntityAlreadyExists()
+ {
+ string exceptionMessage = "Role already exists!";
+ string roleName = "Gosho Trapov";
+
+ CreateRoleServiceModel createRoleServiceModel = new CreateRoleServiceModel
+ {
+ Name = roleName
+ };
+ Role role = new Role
+ {
+ Name = roleName
+ };
+
+ this.RoleRepositoryMock.Setup(p => p.DoesNameExist(It.IsAny<string>())).Returns(Task.FromResult(true));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.RoleService.CreateRole(createRoleServiceModel));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+
+ #region GetRoleById
+ [Test]
+ public async Task GetRoleById_ReturnsTheRole_WhenItExists()
+ {
+ Guid id = new Guid();
+ string name = "Gosho Trapov";
+ Role role = new Role
+ {
+ Name = name
+ };
+ RoleServiceModel roleServiceModel = new RoleServiceModel
+ {
+ 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);
+
+ RoleServiceModel result = await this.RoleService.GetRoleById(id);
+
+ Assert.AreEqual(name, result.Name);
+ }
+
+ [Test]
+ 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));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.RoleService.GetRoleById(id));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+
+ #region UpdateRole
+ [Test]
+ [TestCase(true)]
+ [TestCase(false)]
+ public async Task UpdateRole_ReturnsIfUpdateIsSuccessfull_WhenRoleExistsy(bool shouldPass)
+ {
+ string name = "Gosho Trapov";
+ Guid id = Guid.NewGuid();
+ Role role = new Role
+ {
+ Name = name,
+ Id = id
+ };
+ UpdateRoleServiceModel updateRoleServiceModel = new UpdateRoleServiceModel
+ {
+ 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);
+
+ Assert.AreEqual(shouldPass, result);
+ }
+
+ [Test]
+ public void UpdateRole_ThrowsException_WhenRoleDoesNotExist()
+ {
+ string exceptionMessage = "Role does not exist!";
+ UpdateRoleServiceModel updateRoleServiceModel = new UpdateRoleServiceModel
+ {
+ };
+
+ this.RoleRepositoryMock.Setup(p => p.DoesRoleExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.RoleService.UpdateRole(updateRoleServiceModel));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+
+ [Test]
+ public void UpdateRole_ThrowsException_WhenRoleNameAlreadyExists()
+ {
+ string exceptionMessage = "Role name already exists!";
+ UpdateRoleServiceModel updateRoleServiceModel = new UpdateRoleServiceModel
+ {
+ };
+
+ 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));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.RoleService.UpdateRole(updateRoleServiceModel));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+
+ #region DeleteRole
+ [Test]
+ [TestCase(true)]
+ [TestCase(false)]
+ public async Task DeleteRole_ShouldReturnIfDeletionIsSuccessfull_WhenRoleExists(bool shouldPass)
+ {
+ Guid id = new Guid();
+ 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));
+
+ bool result = await this.RoleService.DeleteRole(id);
+
+ Assert.AreEqual(shouldPass, result);
+ }
+
+ [Test]
+ public void DeleteRole_ThrowsException_WhenRoleDoesNotExist()
+ {
+ string exceptionMessage = "Role does not exist!";
+ Guid id = new Guid();
+
+ this.RoleRepositoryMock.Setup(p => p.DoesRoleExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.RoleService.DeleteRole(id));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+ }
+}
diff --git a/src/Services/DevHive.Services.Tests/TechnologyServices.Tests.cs b/src/Services/DevHive.Services.Tests/TechnologyServices.Tests.cs
new file mode 100644
index 0000000..e671adb
--- /dev/null
+++ b/src/Services/DevHive.Services.Tests/TechnologyServices.Tests.cs
@@ -0,0 +1,262 @@
+using AutoMapper;
+using DevHive.Data.Interfaces.Repositories;
+using DevHive.Data.Models;
+using DevHive.Services.Models.Technology;
+using DevHive.Services.Services;
+using Moq;
+using NUnit.Framework;
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+
+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; }
+
+ #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);
+ }
+ #endregion
+
+ #region CreateTechnology
+ [Test]
+ public async Task CreateTechnology_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully()
+ {
+ string technologyName = "Gosho Trapov";
+ Guid id = Guid.NewGuid();
+ CreateTechnologyServiceModel createTechnologyServiceModel = new()
+ {
+ Name = technologyName
+ };
+ Technology technology = new()
+ {
+ Name = technologyName,
+ 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);
+
+ Assert.AreEqual(id, result);
+ }
+
+ [Test]
+ public async Task CreateTechnology_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully()
+ {
+ string technologyName = "Gosho Trapov";
+
+ CreateTechnologyServiceModel createTechnologyServiceModel = new()
+ {
+ Name = technologyName
+ };
+ Technology technology = new()
+ {
+ 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);
+
+ Guid result = await this.TechnologyService.CreateTechnology(createTechnologyServiceModel);
+
+ Assert.IsTrue(result == Guid.Empty);
+ }
+
+ [Test]
+ public void CreateTechnology_ThrowsArgumentException_WhenEntityAlreadyExists()
+ {
+ string exceptionMessage = "Technology already exists!";
+ string technologyName = "Gosho Trapov";
+
+ CreateTechnologyServiceModel createTechnologyServiceModel = new()
+ {
+ Name = technologyName
+ };
+ Technology technology = new()
+ {
+ Name = technologyName
+ };
+
+ this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(true));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.TechnologyService.CreateTechnology(createTechnologyServiceModel));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+
+ #region GetTechnologyById
+ [Test]
+ public async Task GetTechnologyById_ReturnsTheTechnology_WhenItExists()
+ {
+ Guid id = new Guid();
+ string name = "Gosho Trapov";
+ Technology technology = new()
+ {
+ Name = name
+ };
+ ReadTechnologyServiceModel readTechnologyServiceModel = new()
+ {
+ 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);
+
+ ReadTechnologyServiceModel result = await this.TechnologyService.GetTechnologyById(id);
+
+ Assert.AreEqual(name, result.Name);
+ }
+
+ [Test]
+ 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));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.TechnologyService.GetTechnologyById(id));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+
+ #region GetTechnologies
+ [Test]
+ public void GetTechnologies_ReturnsAllLanguages_IfAnyExist()
+ {
+ ReadTechnologyServiceModel firstTechnology = new ReadTechnologyServiceModel();
+ ReadTechnologyServiceModel secondTechnology = new ReadTechnologyServiceModel();
+ HashSet<ReadTechnologyServiceModel> technologies = new HashSet<ReadTechnologyServiceModel>();
+ 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);
+
+ HashSet<ReadTechnologyServiceModel> result = this.TechnologyService.GetTechnologies();
+
+ Assert.GreaterOrEqual(2, result.Count, "GetTechnologies does not return all technologies");
+ }
+
+ [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>());
+
+ HashSet<ReadTechnologyServiceModel> result = this.TechnologyService.GetTechnologies();
+
+ Assert.IsEmpty(result, "GetTechnologies does not return empty string when no technologies exist");
+ }
+ #endregion
+
+ #region UpdateTechnology
+ [Test]
+ [TestCase(true)]
+ [TestCase(false)]
+ public async Task UpdateTechnology_ReturnsIfUpdateIsSuccessfull_WhenTechnologyExistsy(bool shouldPass)
+ {
+ string name = "Gosho Trapov";
+ Guid id = Guid.NewGuid();
+ Technology technology = new Technology
+ {
+ Name = name,
+ Id = id
+ };
+ UpdateTechnologyServiceModel updatetechnologyServiceModel = new UpdateTechnologyServiceModel
+ {
+ 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);
+
+ Assert.AreEqual(shouldPass, result);
+ }
+
+ [Test]
+ public void UpdateTechnology_ThrowsException_WhenTechnologyDoesNotExist()
+ {
+ string exceptionMessage = "Technology does not exist!";
+ UpdateTechnologyServiceModel updateTechnologyServiceModel = new UpdateTechnologyServiceModel
+ {
+ };
+
+ this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.TechnologyService.UpdateTechnology(updateTechnologyServiceModel));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+
+ [Test]
+ public void UpdateTechnology_ThrowsException_WhenTechnologyNameAlreadyExists()
+ {
+ string exceptionMessage = "Technology name already exists!";
+ UpdateTechnologyServiceModel updateTechnologyServiceModel = new UpdateTechnologyServiceModel
+ {
+ };
+
+ 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));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.TechnologyService.UpdateTechnology(updateTechnologyServiceModel));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+
+ #region DeleteTechnology
+
+ [Test]
+ [TestCase(true)]
+ [TestCase(false)]
+ public async Task DeleteTechnology_ShouldReturnIfDeletionIsSuccessfull_WhenTechnologyExists(bool shouldPass)
+ {
+ Guid id = new Guid();
+ 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));
+
+ bool result = await this.TechnologyService.DeleteTechnology(id);
+
+ Assert.AreEqual(shouldPass, result);
+ }
+
+ [Test]
+ public void DeleteTechnology_ThrowsException_WhenTechnologyDoesNotExist()
+ {
+ string exceptionMessage = "Technology does not exist!";
+ Guid id = new Guid();
+
+ this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.TechnologyService.DeleteTechnology(id));
+
+ Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+ }
+}
diff --git a/src/Services/DevHive.Services.Tests/UserService.Tests.cs b/src/Services/DevHive.Services.Tests/UserService.Tests.cs
new file mode 100644
index 0000000..21d4862
--- /dev/null
+++ b/src/Services/DevHive.Services.Tests/UserService.Tests.cs
@@ -0,0 +1,394 @@
+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.Models.Identity;
+using DevHive.Common.Models.Misc;
+using DevHive.Data.Interfaces.Repositories;
+using DevHive.Data.Models;
+using DevHive.Services.Interfaces;
+using DevHive.Services.Models.Identity.User;
+using DevHive.Services.Options;
+using DevHive.Services.Services;
+using Microsoft.AspNetCore.Identity;
+using Microsoft.IdentityModel.Tokens;
+using Moq;
+using NUnit.Framework;
+
+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; }
+
+ #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);
+ }
+ #endregion
+
+ #region LoginUser
+ [Test]
+ public async Task LoginUser_ReturnsTokenModel_WhenLoggingUserIn()
+ {
+ string somePassword = "GoshoTrapovImaGolemChep";
+ const string name = "GoshoTrapov";
+ string hashedPassword = PasswordModifications.GeneratePasswordHash(somePassword);
+ LoginServiceModel loginServiceModel = new LoginServiceModel
+ {
+ Password = somePassword
+ };
+ User user = new User
+ {
+ Id = Guid.NewGuid(),
+ PasswordHash = hashedPassword,
+ UserName = name
+ };
+
+ 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");
+ }
+
+ [Test]
+ public void LoginUser_ThrowsException_WhenUserNameDoesNotExist()
+ {
+ const string EXCEPTION_MESSAGE = "Invalid username!";
+ LoginServiceModel loginServiceModel = new LoginServiceModel
+ {
+ };
+
+ this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(false));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.LoginUser(loginServiceModel));
+
+ Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorect Exception message");
+ }
+
+ [Test]
+ public void LoginUser_ThroiwsException_WhenPasswordIsIncorect()
+ {
+ const string EXCEPTION_MESSAGE = "Incorrect password!";
+ string somePassword = "GoshoTrapovImaGolemChep";
+ LoginServiceModel loginServiceModel = new LoginServiceModel
+ {
+ Password = somePassword
+ };
+ User user = new User
+ {
+ 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));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.LoginUser(loginServiceModel));
+
+ Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorect 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 void RegisterUser_ThrowsException_WhenUsernameAlreadyExists()
+ {
+ const string EXCEPTION_MESSAGE = "Username already exists!";
+ RegisterServiceModel registerServiceModel = new RegisterServiceModel
+ {
+ };
+
+ this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(true));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.RegisterUser(registerServiceModel));
+
+ Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorect Exception message");
+ }
+
+ [Test]
+ public void RegisterUser_ThrowsException_WhenEmailAlreadyExists()
+ {
+ const string EXCEPTION_MESSAGE = "Email already exists!";
+
+ RegisterServiceModel registerServiceModel = new RegisterServiceModel
+ {
+ };
+
+ 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));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.RegisterUser(registerServiceModel));
+
+ Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorect Exception message");
+ }
+ #endregion
+
+ #region GetUserById
+ [Test]
+ public async Task GetUserById_ReturnsTheUser_WhenItExists()
+ {
+ Guid id = new Guid();
+ string name = "Gosho Trapov";
+ User user = new()
+ {
+ };
+ UserServiceModel userServiceModel = new UserServiceModel
+ {
+ UserName = name
+ };
+
+ 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);
+
+ UserServiceModel result = await this.UserService.GetUserById(id);
+
+ Assert.AreEqual(name, 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));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.GetUserById(id));
+
+ Assert.AreEqual(EXCEPTION_MESSEGE, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+
+ #region GetUserByUsername
+ [Test]
+ public async Task GetUserByUsername_ReturnsTheCorrectUser_IfItExists()
+ {
+ string username = "Gosho Trapov";
+ User user = new User();
+ UserServiceModel userServiceModel = new UserServiceModel
+ {
+ 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);
+
+ 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()
+ {
+ string username = "Gosho Trapov";
+ const string EXCEPTION_MESSEGE = "User does not exist!";
+
+ this.UserRepositoryMock.Setup(p => p.GetByUsernameAsync(It.IsAny<string>())).Returns(Task.FromResult<User>(null));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.GetUserByUsername(username));
+
+ Assert.AreEqual(EXCEPTION_MESSEGE, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+
+ #region UpdateUser
+ // [Test]
+ // [TestCase(true)]
+ // [TestCase(false)]
+ // public async Task UpdateUser_ReturnsIfUpdateIsSuccessfull_WhenUserExistsy(bool shouldPass)
+ // {
+ // string name = "Gosho Trapov";
+ // Guid id = Guid.NewGuid();
+ // User user = new User
+ // {
+ // UserName = name,
+ // Id = id,
+ // };
+ // UpdateUserServiceModel updateUserServiceModel = new UpdateUserServiceModel
+ // {
+ // UserName = name,
+ // };
+ // UserServiceModel userServiceModel = new UserServiceModel
+ // {
+ // UserName = name,
+ // };
+ // 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.DoesUserHaveThisUsername(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);
+ //
+ // if (shouldPass)
+ // {
+ // 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");
+ // }
+ // }
+
+ [Test]
+ public void UpdateUser_ThrowsException_WhenUserDoesNotExist()
+ {
+ const string EXCEPTION_MESSAGE = "User does not exist!";
+ UpdateUserServiceModel updateUserServiceModel = new UpdateUserServiceModel
+ {
+ };
+
+ this.UserRepositoryMock.Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.UpdateUser(updateUserServiceModel));
+
+ Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorecct exception message");
+ }
+
+ [Test]
+ public void UpdateUser_ThrowsException_WhenUserNameAlreadyExists()
+ {
+ const string EXCEPTION_MESSAGE = "Username already exists!";
+ UpdateUserServiceModel updateUserServiceModel = new UpdateUserServiceModel
+ {
+ };
+
+ 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));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.UpdateUser(updateUserServiceModel));
+
+ Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorecct exception message");
+ }
+ #endregion
+
+ #region DeleteUser
+ //TO DO: compleate once Viko has looked into the return type of UserService.DeleteUser
+ // [Test]
+ // [TestCase(true)]
+ // [TestCase(false)]
+ // public async Task DeleteUser_ShouldReturnIfDeletionIsSuccessfull_WhenUserExists(bool shouldPass)
+ // {
+ // Guid id = new Guid();
+ // 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);
+ //
+ // 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));
+
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.UserService.DeleteUser(id));
+
+ 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)
+ };
+
+ JwtSecurityTokenHandler tokenHandler = new();
+ SecurityToken token = tokenHandler.CreateToken(tokenDescriptor);
+ return tokenHandler.WriteToken(token);
+ }
+ #endregion
+ }
+}