public async Task AddUserToGroupAsyncTest_WhenUserIdExistsAndUserIdInMembersIsEmpty_ReturnsGroupResponseModel()
        {
            var groupMockRepository   = new Mock <IGroupRepository>();
            var mockThreadRepository  = new Mock <IThreadRepository>();
            var contactMockRepository = new Mock <IContactRepository>();
            var mapper = GetMapperForGroupProfile();

            var userId      = _rnd.Next(111, 1000);
            var groupEntity = EntityModellers.CreateGroupEntity();

            groupEntity.Members.Add(userId);

            groupMockRepository.Setup(x => x.GetAsync(groupEntity.Id)).ReturnsAsync(groupEntity);
            groupMockRepository.Setup(x => x.AppendUsersAsync(groupEntity.Id, It.Is <IList <int> >(y => true))).ReturnsAsync(groupEntity);
            var expected = mapper.Map <GroupEntity, GroupResponseModel>(groupEntity);
            var newUsers = new List <int> {
                userId
            };
            var groupManager = new GroupManager(groupMockRepository.Object, mockThreadRepository.Object, mapper, contactMockRepository.Object);
            var actual       = await groupManager.AddUsersToGroupAsync(groupEntity.Id, newUsers);

            Assert.Equal(expected, actual, new LogicEqualityComparer <GroupResponseModel>());
            Assert.Contains(userId, actual.Members);

            groupMockRepository.Verify(x => x.AppendUsersAsync(groupEntity.Id, newUsers), Times.Never);

            groupMockRepository.VerifyAll();
        }
        public async Task FetchByIdAsyncTest_WhenGroupIdExists_ReturnsGroupWithUsersResponseModel()
        {
            var groupMockRepository   = new Mock <IGroupRepository>();
            var mockThreadRepository  = new Mock <IThreadRepository>();
            var contactMockRepository = new Mock <IContactRepository>();
            var mapper             = GetMapperForGroupProfile();
            var groupEntity        = EntityModellers.CreateGroupEntity();
            var chatUserEntity     = EntityModellers.CreateChatUserEntity();
            var chatUserEntityList = new List <ChatUser> {
                chatUserEntity
            };

            groupMockRepository.Setup(x => x.GetAsync(groupEntity.Id)).ReturnsAsync(groupEntity);
            contactMockRepository.Setup(x => x.GetChatUserDetailsAsync(groupEntity.Members)).ReturnsAsync(chatUserEntityList);

            var userResponse  = mapper.Map <IEnumerable <ChatUser>, IEnumerable <UserContactResponseModel> >(chatUserEntityList);
            var groupResponse = mapper.Map <GroupEntity, GroupWithUsersResponseModel>(groupEntity);

            var expected = mapper.Map(userResponse, groupResponse);

            var groupManager = new GroupManager(groupMockRepository.Object, mockThreadRepository.Object, mapper, contactMockRepository.Object);
            var actual       = await groupManager.FetchByIdAsync(groupEntity.Id);

            Assert.Equal(expected, actual, new LogicEqualityComparer <GroupWithUsersResponseModel>());
            Assert.NotNull(actual);

            groupMockRepository.VerifyAll();
        }
        public async Task DeleteUserFromGroupAsyncTest_WhenGroupIdExistsAndUserIdInMembersDoesntExists_ReturnsGroupResponseModelWithOutUserIdInMemebers()
        {
            var groupMockRepository   = new Mock <IGroupRepository>();
            var mockThreadRepository  = new Mock <IThreadRepository>();
            var contactMockRepository = new Mock <IContactRepository>();
            var mapper = GetMapperForGroupProfile();

            var userId        = _rnd.Next(111, 1000);
            var groupEntity   = EntityModellers.CreateGroupEntity();
            var expected      = mapper.Map <GroupEntity, GroupResponseModel>(groupEntity);
            var usersToRemove = new List <int> {
                userId
            };

            groupMockRepository.Setup(x => x.GetAsync(groupEntity.Id)).ReturnsAsync(groupEntity);
            groupMockRepository.Setup(x => x.RemoveUsersAsync(groupEntity.Id, It.Is <IList <int> >(y => true))).ReturnsAsync(groupEntity);
            var groupManager = new GroupManager(groupMockRepository.Object, mockThreadRepository.Object, mapper, contactMockRepository.Object);
            var actual       = await groupManager.DeleteUsersFromGroupAsync(groupEntity.Id, usersToRemove);

            Assert.Equal(expected, actual, new LogicEqualityComparer <GroupResponseModel>());
            Assert.DoesNotContain(userId, actual.Members);

            groupMockRepository.Verify(x => x.RemoveUsersAsync(groupEntity.Id, usersToRemove), Times.Never);

            groupMockRepository.VerifyAll();
        }
        public async Task CreateNewGroupAsyncTest_WhenMemberCreatedExists_ReturnsGroupResponseModel()
        {
            var groupMockRepository   = new Mock <IGroupRepository>();
            var mockThreadRepository  = new Mock <IThreadRepository>();
            var contactMockRepository = new Mock <IContactRepository>();
            var mapper               = GetMapperForGroupProfile();
            var groupEntity          = EntityModellers.CreateGroupEntity();
            var groupNewrequestModel = EntityModellers.CreateNewGroupRequestModel();

            var groupResponse = mapper.Map <NewGroupRequestModel, GroupEntity>(groupNewrequestModel);

            groupResponse.Members = new List <int> {
                groupNewrequestModel.CreatedUserId
            };

            groupMockRepository.Setup(x => x.CreateAsync(It.Is <GroupEntity>(y => y.Name == groupNewrequestModel.Name &&
                                                                             y.CreatedUserId == groupNewrequestModel.CreatedUserId &&
                                                                             y.CreatedUsername == groupNewrequestModel.CreatedUserName))).ReturnsAsync(groupResponse);

            var expected = mapper.Map <GroupEntity, GroupResponseModel>(groupResponse);

            var groupManager = new GroupManager(groupMockRepository.Object, mockThreadRepository.Object, mapper, contactMockRepository.Object);
            var actual       = await groupManager.CreateNewGroupAsync(groupNewrequestModel);

            Assert.Equal(expected, actual, new LogicEqualityComparer <GroupResponseModel>());
        }
        public async Task FetchByUserIdAsyncTest_WhenUserIdDoesntExists__ReturnsEmptyGroupResponseModel()
        {
            var groupMockRepository   = new Mock <IGroupRepository>();
            var mockThreadRepository  = new Mock <IThreadRepository>();
            var contactMockRepository = new Mock <IContactRepository>();
            var mapper      = GetMapperForGroupProfile();
            var groupEntity = EntityModellers.CreateGroupEntity();

            var userId   = _rnd.Next(111, 1000);
            var expected = mapper.Map <IEnumerable <GroupEntity>, IEnumerable <GroupResponseModel> >(new List <GroupEntity>());

            groupMockRepository.Setup(x => x.GetByUserIdAsync(userId)).ReturnsAsync((IEnumerable <GroupEntity>)null);

            var groupManager = new GroupManager(groupMockRepository.Object, mockThreadRepository.Object, mapper, contactMockRepository.Object);
            var actual       = await groupManager.FetchByUserIdAsync(userId);

            Assert.Equal(expected, actual, new LogicEqualityComparer <GroupResponseModel>());
            Assert.Empty(actual);
        }
        public async Task DeleteUserFromGroupAsyncTest_WhenGroupIdDoesntExists_ThrowsInvalidRequestException()
        {
            var groupMockRepository   = new Mock <IGroupRepository>();
            var mockThreadRepository  = new Mock <IThreadRepository>();
            var contactMockRepository = new Mock <IContactRepository>();
            var mapper = GetMapperForGroupProfile();

            var groupEntity = EntityModellers.CreateGroupEntity();

            groupMockRepository.Setup(x => x.GetAsync(groupEntity.Id)).ReturnsAsync((GroupEntity)null);

            var groupManager = new GroupManager(groupMockRepository.Object, mockThreadRepository.Object, mapper, contactMockRepository.Object);
            var exception    = await Assert.ThrowsAsync <InvalidRequestException>(() => groupManager.DeleteUsersFromGroupAsync(groupEntity.Id, new List <int> {
                _rnd.Next(111, 1000)
            }));

            Assert.IsType <InvalidRequestException>(exception);
            Assert.StartsWith(ErrorConstants.InvalidInputMessage, exception.Message);

            groupMockRepository.VerifyAll();
        }
        public async Task DeleteGroupByIdAsync_WhenGroupIdDoesntExists_ReturnsGroupResponseWithGroupRemoved()
        {
            var groupMockRepository   = new Mock <IGroupRepository>();
            var mockThreadRepository  = new Mock <IThreadRepository>();
            var contactMockRepository = new Mock <IContactRepository>();

            var mapper = GetMapperForGroupProfile();

            var groupEntity    = EntityModellers.CreateGroupEntity();
            var chatUserEntity = EntityModellers.CreateChatUserEntity();
            var threadEntity   = EntityModellers.CreateThreadEntity();

            var expected = mapper.Map <GroupEntity, GroupResponseModel>(new GroupEntity());

            groupMockRepository.Setup(x => x.DeleteAsync(groupEntity.Id)).ReturnsAsync(new GroupEntity());
            var groupManager = new GroupManager(groupMockRepository.Object, mockThreadRepository.Object, mapper, contactMockRepository.Object);
            var actual       = await groupManager.DeleteGroupByIdAsync(groupEntity.Id);

            Assert.Equal(expected, actual, new LogicEqualityComparer <GroupResponseModel>());
            mockThreadRepository.Verify(x => x.DeleteByMultiIdAsync(groupEntity.Id), Times.Never);

            groupMockRepository.VerifyAll();
            mockThreadRepository.VerifyAll();
        }
Beispiel #8
0
        public async Task SearchOrCreateThreadAsyncTest_WhenThreadTypeIsTeamAndParticipantGuidExist_ThreadWithMessagesResponseModel()
        {
            var threadMockRepository  = new Mock <IThreadRepository>();
            var userMockRepository    = new Mock <IUserRepository>();
            var messageMockRepository = new Mock <IMessageRepository>();
            var groupMockRepository   = new Mock <IGroupRepository>();
            var partnerMockRepository = new Mock <IPartnerRepository>();
            var contactMockRepository = new Mock <IContactRepository>();
            var logMockRepository     = new Mock <ILogRepository>();
            var teamMockRepository    = new Mock <ITeamRepository>();

            var mapper         = GetMapperForThreadProfile();
            var threadEntity   = EntityModellers.CreateThreadEntity();
            var chatUserEntity = EntityModellers.CreateChatUserEntity();
            var messageEntity  = EntityModellers.CreateMessageEntity();
            var groupEntity    = EntityModellers.CreateGroupEntity();
            var teamEntity     = EntityModellers.CreateTeamEntity();

            var userId            = _rnd.Next(111, 1000);
            var participantGuid   = Guid.NewGuid();
            const ThreadType type = ThreadType.Team;
            var teamMembers       = new List <int> {
                teamEntity.TeamId
            };
            var chatUsers = new List <ChatUser> {
                chatUserEntity
            };

            teamEntity.Id  = participantGuid;
            groupEntity.Id = participantGuid;

            threadEntity.Participants = new List <int> {
                userId
            };

            teamMockRepository.Setup(x => x.GetAsync(teamEntity.Id)).ReturnsAsync(teamEntity);
            partnerMockRepository.Setup(x => x.GetTeamMembers(teamEntity.TeamId)).Returns(teamMembers);
            threadMockRepository.Setup(x => x.GetAsync(participantGuid)).ReturnsAsync(threadEntity);
            messageMockRepository.Setup(x => x.GetByThreadIdAsync(threadEntity.Id)).ReturnsAsync(new List <MessageEntity> {
                messageEntity
            });
            contactMockRepository.Setup(x => x.GetChatUserDetailsFromUserIdsAsync(teamMembers)).ReturnsAsync(chatUsers);
            contactMockRepository.Setup(x => x.GetChatUserDetailsAsync(It.Is <IEnumerable <int> >(l => new CompareLogic().Compare(l, threadEntity.Participants).AreEqual)))
            .ReturnsAsync(chatUsers);

            var participantResponse = mapper.Map <IEnumerable <ChatUser>, IEnumerable <UserContactResponseModel> >(new List <ChatUser> {
                chatUserEntity
            });
            var messagesResponse = mapper.Map <IEnumerable <MessageEntity>, IEnumerable <MessageResponseModel> >(new List <MessageEntity> {
                messageEntity
            });
            var threadResponse     = mapper.Map <ThreadEntity, ThreadWithMessagesResponseModel>(threadEntity);
            var threadWithMessages = mapper.Map(messagesResponse, threadResponse);
            var expected           = mapper.Map(participantResponse, threadWithMessages);

            var threadManager = new ThreadManager(partnerMockRepository.Object, threadMockRepository.Object, messageMockRepository.Object, groupMockRepository.Object, mapper, contactMockRepository.Object, teamMockRepository.Object, logMockRepository.Object);
            var actual        = await threadManager.SearchOrCreateThreadAsync(userId, participantGuid.ToString(), type);

            Assert.Equal(expected, actual, new LogicEqualityComparer <ThreadWithMessagesResponseModel>());

            threadMockRepository.VerifyAll();
            userMockRepository.VerifyAll();
            messageMockRepository.VerifyAll();
            groupMockRepository.VerifyAll();
        }