public static async Task DownloadAsync_DocumentRepositoryException_IsLogged() { var expectedException = new DocumentRepositoryException( new InvalidOperationException(), StatusCodes.Status502BadGateway); var mockStorage = new Mock <IDocumentRepository>(); mockStorage.Setup(s => s.DownloadAsync(It.IsAny <string>(), It.IsAny <string>())) .Throws(expectedException); var mockLogger = new Mock <ILogger <SolutionsController> >(); var controller = new SolutionsController(mockStorage.Object, mockLogger.Object); await controller.DownloadAsync("ID", "directory"); Expression <Action <ILogger <SolutionsController> > > expression = l => l.Log( It.Is <LogLevel>(l => l == LogLevel.Error), It.IsAny <EventId>(), It.Is <It.IsAnyType>((v, t) => true), It.Is <Exception>(e => e == expectedException), It.Is <Func <It.IsAnyType, Exception, string> >((v, t) => true)); mockLogger.Verify(expression); }
public async Task DownloadAsync_DocumentRepositoryException_IsLogged() { var exception = new DocumentRepositoryException( new InvalidOperationException(), StatusCodes.Status502BadGateway); var mockStorage = new Mock <IDocumentRepository>(); mockStorage.Setup(s => s.DownloadAsync(It.IsAny <string>(), It.IsAny <string>())) .Throws(exception); var logLevel = LogLevel.None; Exception actualException = null; void Callback(LogLevel l, Exception e) { logLevel = l; actualException = e; } var mockLogger = new MockLogger <SolutionsController>(Callback); var controller = new SolutionsController(mockStorage.Object, mockLogger); await controller.DownloadAsync("ID", "directory"); logLevel.Should().Be(LogLevel.Error); actualException.Should().Be(exception); }
public async Task Details_ReturnsCorrectViewModel() { var solutions = new[] { new Solution { Id = 1, Title = "First Solution", Content = "First Content", Author = new User() }, new Solution { Id = 2, Title = "Second Solution", Content = "Second Content", Author = new User() }, new Solution { Id = 3, Title = "Third Solution", Content = "Third Content", Author = new User() }, }; var context = TestsDbContext.GetDbContext(); context.Solutions.AddRange(solutions); context.SaveChanges(); var controller = new SolutionsController(context, TestsAutoMapper.GetMapper()); var expectedSolution = solutions[1]; var result = await controller.Details(expectedSolution.Id); var model = (result as ViewResult).ViewData.Model as SolutionDetailsViewModel; Assert.IsNotNull(model); Assert.AreEqual(model.Id, expectedSolution.Id); Assert.AreEqual(model.Title, expectedSolution.Title); Assert.AreEqual(model.Content, expectedSolution.Content); Assert.IsInstanceOfType(model, typeof(SolutionDetailsViewModel)); }
public async Task Index__ReturnsAllSolutions() { var solutions = new[] { new Solution { Id = 1, Title = "First Solution", Content = "First Content", Author = new User() }, new Solution { Id = 2, Title = "Second Solution", Content = "Second Content", Author = new User() }, new Solution { Id = 3, Title = "Third Solution", Content = "Third Content", Author = new User() }, }; this.dbContext.Solutions.AddRange(solutions); this.dbContext.SaveChanges(); var controller = new SolutionsController(this.dbContext, TestsAutoMapper.GetMapper()); var result = await controller.Index() as ViewResult; var model = result.Model as ICollection <SolutionListingViewModel>; var expectedCollection = solutions.Select(r => r.Id).ToArray(); var resultCollection = model.Select(rv => rv.Id).ToArray(); var expectedCount = solutions.Count(); Assert.IsNotNull(result); Assert.AreEqual(expectedCount, model.Count); CollectionAssert.AreEqual(expectedCollection, resultCollection); }
public async Task FailedDetailCall() { _solutionRepository.Setup(repo => repo.Get(2, true)).Returns(Task.FromResult <Solution>(null)); var controller = new SolutionsController(_solutionRepository.Object, null, _itemTranslationRepository.Object); var result = await controller.Details(2); Assert.IsType <NotFoundResult>(result); }
public async Task Index() { var controller = new SolutionsController(_solutionRepository.Object, null, _itemTranslationRepository.Object); var model = new List <Solution>().AsQueryable().BuildMock(); _solutionRepository.Setup(repo => repo.GetAll()).Returns(model.Object); var result = await controller.Index(null, null, null, null); Assert.IsType <ViewResult>(result); }
public void DownloadAsync_Exception_DoesNotSwallow() { var exception = new InvalidOperationException(); var mockStorage = new Mock <IDocumentRepository>(); mockStorage.Setup(s => s.DownloadAsync(It.IsAny <string>(), It.IsAny <string>())) .Throws(exception); var controller = new SolutionsController(mockStorage.Object, Mock.Of <ILogger <SolutionsController> >()); Assert.ThrowsAsync <InvalidOperationException>(() => controller.DownloadAsync("ID", "directory")); }
public async Task InvalidDetailModel() { var model = new Solution { Id = 0, LastModified = DateTime.UtcNow, Title = "Title 1" }; _solutionRepository.Setup(repo => repo.Get(0, true)).Returns(Task.FromResult <Solution>(null)); var controller = new SolutionsController(_solutionRepository.Object, null, _itemTranslationRepository.Object); var result = await controller.Details(model.Id); Assert.IsType <NotFoundResult>(result); }
public async Task DownloadAsync_DocumentRepositoryException_ReturnsStatusCodeResult() { var exception = new DocumentRepositoryException( new InvalidOperationException(), StatusCodes.Status502BadGateway); var mockStorage = new Mock <IDocumentRepository>(); mockStorage.Setup(s => s.DownloadAsync(It.IsAny <string>(), It.IsAny <string>())) .Throws(exception); var controller = new SolutionsController(mockStorage.Object, Mock.Of <ILogger <SolutionsController> >()); var result = await controller.DownloadAsync("ID", "directory") as StatusCodeResult; Assert.NotNull(result); result.StatusCode.Should().Be(exception.HttpStatusCode); }
public void GetFileNamesAsync_ReturnsStorageResult() { var mockEnumerable = new Mock <IAsyncEnumerable <string> >(); var mockStorage = new Mock <IDocumentRepository>(); mockStorage.Setup(s => s.GetFileNamesAsync(It.IsAny <string>())) .Returns(mockEnumerable.Object); var controller = new SolutionsController(mockStorage.Object, Mock.Of <ILogger <SolutionsController> >()); var result = controller.GetDocumentsBySolutionId("Foobar"); result.Result.Should().BeOfType <OkObjectResult>(); var okResult = (OkObjectResult)result.Result; okResult.StatusCode.Should().Be(200); okResult.Value.Should().Be(mockEnumerable.Object); mockStorage.Verify(x => x.GetFileNamesAsync("Foobar"), Times.Once); }
public async Task SuccesfulDetailCall() { var model = new Solution { Id = 0, LastModified = DateTime.UtcNow, Title = "Title 1" }; _solutionRepository.Setup(repo => repo.Get(0, true)).Returns(Task.FromResult <Solution>(model)); var controller = new SolutionsController(_solutionRepository.Object, null, _itemTranslationRepository.Object); var result = await controller.Details(model.Id); var viewResult = Assert.IsType <ViewResult>(result); Assert.Equal(model, viewResult.Model); }
public async Task DownloadAsync_ReturnsFileStreamResult() { const string expectedContentType = "test/content-type"; using var expectedStream = new MemoryStream(Encoding.UTF8.GetBytes("Hello world!")); var downloadInfo = new Mock <IDocument>(); downloadInfo.Setup(d => d.Content).Returns(expectedStream); downloadInfo.Setup(d => d.ContentType).Returns(expectedContentType); var mockStorage = new Mock <IDocumentRepository>(); mockStorage.Setup(s => s.DownloadAsync(It.IsAny <string>(), It.IsAny <string>())) .ReturnsAsync(downloadInfo.Object); var controller = new SolutionsController(mockStorage.Object, Mock.Of <ILogger <SolutionsController> >()); var result = await controller.DownloadAsync("ID", "directory") as FileStreamResult; Assert.NotNull(result); result.FileStream.IsSameOrEqualTo(expectedStream); result.ContentType.Should().Be(expectedContentType); }
public void Setup() { _mockMediator = new Mock <IMediator>(); _solutionsController = new SolutionsController(_mockMediator.Object); }
public void SetUp() { mockMediator = new Mock <IMediator>(); solutionsController = new SolutionsController(mockMediator.Object); }
public async Task Setup() { context = await TestDatabase.GetDatabaseContext(); mockSolutionsService = new MockSolutionsService(context); mockAppsService = new MockAppsService(context); baseRequest = new BaseRequest(); solutionRequest = new SolutionRequest() { FirstRow = new List <int> { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, SecondRow = new List <int> { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, ThirdRow = new List <int> { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, FourthRow = new List <int> { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, FifthRow = new List <int> { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, SixthRow = new List <int> { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, SeventhRow = new List <int> { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, EighthRow = new List <int> { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, NinthRow = new List <int> { 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; addSolutionRequest = new AddSolutionRequest() { Limit = 1000 }; invalidAddSolutionRequest = new AddSolutionRequest() { Limit = 1001 }; sutSuccess = new SolutionsController( mockSolutionsService.SolutionsServiceSuccessfulRequest.Object, mockAppsService.AppsServiceSuccessfulRequest.Object); sutFailure = new SolutionsController( mockSolutionsService.SolutionsServiceFailedRequest.Object, mockAppsService.AppsServiceSuccessfulRequest.Object); sutSolvedFailure = new SolutionsController( mockSolutionsService.SolutionsServiceSolveFailedRequest.Object, mockAppsService.AppsServiceSuccessfulRequest.Object); }
public void NullMediatorShouldThrowNullException() { Assert.Throws <ArgumentNullException>(() => _ = new SolutionsController(null)); }