public async Task CreateAsync_ShouldReturnErrorWhenExceptionInQuery1()
        {
            var mockMessageFilesQuery = new Mock <IMessageFileQuery>();

            mockMessageFilesQuery
            .Setup(m => m.Create(It.IsAny <string>(), It.IsAny <string>()))
            .ThrowsAsync(new Exception("Mock exception 1"));

            var mockFileService = new Mock <IFileService <ChatFilesConfiguration> >();

            mockFileService
            .Setup(f => f.UploadAsync(It.IsAny <IFormFile>()))
            .ReturnsAsync("Some name");

            var mockFormFile = new Mock <IFormFile>();

            mockFormFile.SetupGet(f => f.ContentType).Returns(GetTestFileTypes().First().Type);
            mockFormFile.SetupGet(f => f.FileName).Returns("Some name");

            var mockContextFactory = SetupContextFactory(null, mockMessageFilesQuery.Object, null);

            _sut = new StudentChatService(mockContextFactory.Object, mockFileService.Object, _mapper);
            var request = new MessageCreateRequest {
                Sender = 1, Recipients = new int[] { 1, 2, 3 }, Text = "NewMessageText", AppliedFile = mockFormFile.Object
            };

            // ACT
            var actual = await _sut.CreateAsync(request);

            // ASSERT
            Assert.Equal("Mock exception 1", actual.Error.Message);
        }
        public async Task CreateAsync_ShouldReturnErrorWhenExceptionInQuery2()
        {
            var newMessageFile = new MessageFile {
                Id = 10, Name = "NewFileName", FileTypeId = 1
            };

            var mockMessageFilesQuery = new Mock <IMessageFileQuery>();

            mockMessageFilesQuery
            .Setup(m => m.Create(It.IsAny <string>(), It.IsAny <string>()))
            .ReturnsAsync(newMessageFile);

            var mockMessagesQuery = new Mock <IMessagesQuery>();

            mockMessagesQuery.Setup(m => m.Create(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>(), It.IsAny <byte>(), It.IsAny <int?>()))
            .ThrowsAsync(new Exception("Mock exception"));

            var mockFileService = new Mock <IFileService <ChatFilesConfiguration> >();

            var mockContextFactory = SetupContextFactory(mockMessagesQuery.Object, mockMessageFilesQuery.Object, null);

            _sut = new StudentChatService(mockContextFactory.Object, mockFileService.Object, _mapper);
            var request = new MessageCreateRequest {
                Sender = 1, Recipients = new int[] { 1, 2, 3 }, Text = "NewMessageText"
            };

            // ACT
            var actual = await _sut.CreateAsync(request);

            // ASSERT
            Assert.Equal("Mock exception", actual.Error.Message);
        }
        public async Task CreateAsync_ShouldReturnErrorWhenRecipientsNumberIsLarge()
        {
            var mockMessagesQuery = new Mock <IMessagesQuery>();

            var mockFileService = new Mock <IFileService <ChatFilesConfiguration> >();

            var mockContextFactory = SetupContextFactory(mockMessagesQuery.Object, null, null);

            _sut = new StudentChatService(mockContextFactory.Object, mockFileService.Object, _mapper);
            var request = new MessageCreateRequest {
                Sender = 1, Recipients = GetTestRecipientsArray(), Text = "NewMessageText"
            };

            // ACT
            var actual = await _sut.CreateAsync(request);

            // ASSERT
            Assert.Equal("Количество получателей должно быть максимум 1000", actual.Error.Message);
        }
        public async Task CreateAsync_ShouldReturnErrorWhenRecipientsIsNull()
        {
            var mockMessagesQuery = new Mock <IMessagesQuery>();

            var mockFileService = new Mock <IFileService <ChatFilesConfiguration> >();

            var mockContextFactory = SetupContextFactory(mockMessagesQuery.Object, null, null);

            _sut = new StudentChatService(mockContextFactory.Object, mockFileService.Object, _mapper);
            var request = new MessageCreateRequest {
                Sender = 1, Recipients = null, Text = "NewMessageText"
            };

            // ACT
            var actual = await _sut.CreateAsync(request);

            // ASSERT
            Assert.Equal("Получатели не указаны", actual.Error.Message);
        }
        public async Task CreateAsync_ShouldReturnTrueWhenAplliedFileNotNull()
        {
            var newMessageFile = new MessageFile {
                Id = 10, Name = "NewFileName", FileTypeId = 1
            };

            var mockMessageFilesQuery = new Mock <IMessageFileQuery>();

            mockMessageFilesQuery
            .Setup(m => m.Create(It.IsAny <string>(), It.IsAny <string>()))
            .ReturnsAsync(newMessageFile);

            var mockMessagesQuery = new Mock <IMessagesQuery>();

            mockMessagesQuery
            .Setup(m => m.Create(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>(), It.IsAny <byte>(), It.IsAny <int?>()))
            .ReturnsAsync(0);

            var mockFormFile = new Mock <IFormFile>();

            mockFormFile.SetupGet(f => f.ContentType).Returns(GetTestFileTypes().First().Type);
            mockFormFile.SetupGet(f => f.FileName).Returns(newMessageFile.Name);

            var mockFileService = new Mock <IFileService <ChatFilesConfiguration> >();

            mockFileService
            .Setup(f => f.UploadAsync(It.IsAny <IFormFile>()))
            .ReturnsAsync(newMessageFile.Name);

            var mockContextFactory = SetupContextFactory(mockMessagesQuery.Object, mockMessageFilesQuery.Object, null);

            _sut = new StudentChatService(mockContextFactory.Object, mockFileService.Object, _mapper);
            var request = new MessageCreateRequest {
                Sender = 1, Recipients = new int[] { 2, 3 }, Text = "NewMessageText", AppliedFile = mockFormFile.Object
            };

            // ACT
            var actual = await _sut.CreateAsync(request);

            // ASSERT
            Assert.True(actual.Entity);
        }
        public async Task CreateAsync_ShouldReturnTrueWhenAplliedFileIsNull()
        {
            var mockMessagesQuery = new Mock <IMessagesQuery>();

            mockMessagesQuery
            .Setup(m => m.Create(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>(), It.IsAny <byte>(), It.IsAny <int?>()))
            .ReturnsAsync(0);

            var mockFileService = new Mock <IFileService <ChatFilesConfiguration> >();

            var mockContextFactory = SetupContextFactory(mockMessagesQuery.Object, null, null);

            _sut = new StudentChatService(mockContextFactory.Object, mockFileService.Object, _mapper);
            var request = new MessageCreateRequest {
                Sender = 1, Recipients = new int[] { 2, 3 }, Text = "NewMessageText"
            };

            // ACT
            var actual = await _sut.CreateAsync(request);

            // ASSERT
            Assert.True(actual.Entity);
        }
        public async Task CreateAsync_ShouldReturnErrorWhenAplliedFileTypeIsNotAllowed()
        {
            var mockMessagesQuery = new Mock <IMessagesQuery>();

            var mockFormFile = new Mock <IFormFile>();

            mockFormFile.SetupGet(f => f.ContentType).Returns("application/bat");
            mockFormFile.SetupGet(f => f.FileName).Returns("NewFileName.bat");

            var mockFileService = new Mock <IFileService <ChatFilesConfiguration> >();

            var mockContextFactory = SetupContextFactory(mockMessagesQuery.Object, null, null);

            _sut = new StudentChatService(mockContextFactory.Object, mockFileService.Object, _mapper);
            var request = new MessageCreateRequest {
                Sender = 1, Recipients = new int[] { 2, 3 }, Text = "NewMessageText", AppliedFile = mockFormFile.Object
            };

            // ACT
            var actual = await _sut.CreateAsync(request);

            // ASSERT
            Assert.Equal("Файл \"" + request.AppliedFile.FileName + "\" не может быть загружен", actual.Error.Message);
        }