diff --git a/PWManager.UnitTests/Services/GroupServiceTest.cs b/PWManager.UnitTests/Services/GroupServiceTest.cs new file mode 100644 index 0000000..8c7c2d0 --- /dev/null +++ b/PWManager.UnitTests/Services/GroupServiceTest.cs @@ -0,0 +1,94 @@ +using NSubstitute; +using NSubstitute.ReceivedExtensions; +using PWManager.Application.Context; +using PWManager.Application.Exceptions; +using PWManager.Application.Services; +using PWManager.Domain.Entities; +using PWManager.Domain.Repositories; + +namespace PWManager.UnitTests.Services; + +public class GroupServiceTest { + + private GroupService _sut; + + [Fact] + public void GroupService_Should_AddGroup() { + var groupRepo = MockGroupRepo(); + + _sut = new GroupService(null, groupRepo); + + _sut.AddGroup("userIdlel", "NewIdentifier"); + + groupRepo.Received().AddGroup(Arg.Is(e => e.Identifier == "NewIdentifier" && e.UserId == "userIdlel")); + } + + [Fact] + public void GroupService_ShouldNot_AddExistingGroup() { + var groupRepo = MockGroupRepo(); + groupRepo.GetGroup(Arg.Any()).Returns(new Group("", "")); + + _sut = new GroupService(null, groupRepo); + + var ex = Assert.Throws(() => _sut.AddGroup("yes", "yes")); + + Assert.Equal(MessageStrings.GroupAlreadyExist("yes"), ex.Message); + } + + [Fact] + public void GroupService_Should_DeleteGroup() { + var repo = MockGroupRepo(); + + _sut = new GroupService(null, repo); + + _sut.DeleteGroup("identifier"); + + repo.Received().RemoveGroup(Arg.Is(e => e == "identifier")); + } + + [Fact] + public void GroupService_ShouldNot_DeleteGroupFailed() { + var repo = MockGroupRepo(); + repo.RemoveGroup(Arg.Any()).Returns(false); + + _sut = new GroupService(null, repo); + + var ex = Assert.Throws(() => _sut.DeleteGroup("identifier")); + + Assert.Equal(MessageStrings.FailedDeletingGroup("identifier"), ex.Message); + } + + [Fact] + public void GroupService_Should_GetAllNames() { + var repo = MockGroupRepo(); + + _sut = new GroupService(null, repo); + + var names = _sut.GetAllGroupNames(); + + Assert.Equal(2, names.Count); + Assert.Equal("Name1", names[0]); + Assert.Equal("Name2", names[1]); + } + + [Fact] + public void GroupService_Should_SwitchGroup() { + var env = Substitute.For(); + var repo = MockGroupRepo(); + var group = new Group("a", "asd"); + repo.GetGroup(Arg.Any()).Returns(group); + + _sut = new GroupService(env, repo); + + _sut.SwitchGroup("a"); + + env.Received().CurrentGroup = group; + } + + private IGroupRepository MockGroupRepo() { + var groupRepo = Substitute.For(); + groupRepo.RemoveGroup(Arg.Any()).Returns(true); + groupRepo.GetAllGroupNames().Returns(new List { "Name1", "Name2" }); + return groupRepo; + } +} \ No newline at end of file