public async Task Put_Handles500() { // Arrange var controller = BuildController(); _mockWorkNotesService .Setup(inst => inst.UpdateItemAsync(It.IsAny <WorkNoteDto>())) .Throws(new Exception("Yeeeet!")); // Act WorkNoteDto entry = new WorkNoteDto { Id = 1000, Title = "I did this", Content = "Because it was hard" }; var response = await controller.UpdateAsync(1000, entry); // Assert response.Result.Should().BeOfType <StatusCodeResult>("Return a 500 response object"); var result = response.Result as StatusCodeResult; result.StatusCode.Should().Be(500); }
public async Task <ActionResult <WorkNoteDto> > UpdateAsync(int id, [FromBody] WorkNoteDto item) { WorkNoteDto workNote; try { if (item.Id > 0 && item.Id != id) { throw new ContentValidationException("'id' property must match URL"); } workNote = await _workNoteService.UpdateItemAsync(item); } catch (NotFoundException ex) { return(NotFound(ex.Message)); } catch (ContentValidationException ex) { return(BadRequest(ex.Errors)); } catch (Exception ex) { _logger.LogCritical(ex, $"Error updating WorkNote Id={id} Title=\"{item.Title}\""); return(StatusCode(StatusCodes.Status500InternalServerError)); } return(Ok(workNote)); }
public async Task Post_HappyPath() { // Arrange var testTitle = "I did this"; var controller = BuildController(); // Act WorkNoteDto entry = new WorkNoteDto { Title = testTitle, Content = "Because it was hard" }; var response = await controller.CreateAsync(entry); // Assert response.Result.Should().BeOfType <CreatedAtActionResult>("Return a 200 OK response object, with content"); var result = response.Result as CreatedAtActionResult; result.Value.Should().BeOfType <WorkNoteDto>("Content should be of type WorkNoteDto"); var item = result.Value as WorkNoteDto; item.Id.Should().Be(1000); item.CreatedDate.Should().BeSameDateAs(DateTime.Today); item.Title.Should().Be(testTitle); }
public async Task Put_HappyPath() { // Arrange var testId = 1001; var testTitle = "New title"; var testContent = "New content"; var controller = BuildController(); // Act WorkNoteDto entry = new WorkNoteDto { Id = testId, Title = testTitle, Content = testContent }; var response = await controller.UpdateAsync(testId, entry); // Assert response.Result.Should().BeOfType <OkObjectResult>("Return a 200 OK response object, with content"); var result = response.Result as OkObjectResult; result.Value.Should().BeOfType <WorkNoteDto>("Content should be of type WorkNoteDto"); var item = result.Value as WorkNoteDto; item.Id.Should().Be(testId); item.Title.Should().Be(testTitle); item.Content.Should().Be(testContent); }
public async void ReadItem_NoResultForId() { var service = BuildService(); WorkNoteDto result = await service.ReadItemAsync(1234); result.Should().BeNull(); }
public async void ReadItem_HappyPath() { var service = BuildService(); WorkNoteDto result = await service.ReadItemAsync(1001); result.Should().NotBeNull("Result should not be null"); result.Title.Should().Be("I did another thing"); result.Content.Should().Be("Went all right, mostly. I guess."); result.CreatedDate.Year.Should().Be(2019); }
public void CreateItem_Error_TitleBlank() { var service = BuildService(); var badItem = new WorkNoteDto { Title = " ", Content = "No issues found", }; Func <Task <WorkNoteDto> > func = async() => await service.CreateItemAsync(badItem); func.Should() .Throw <ContentValidationException>(); }
public async void CreateItem_NullContent() { var service = BuildService(); WorkNoteDto result = await service.CreateItemAsync(new WorkNoteDto { Title = "I did this", Content = null, }); result.Should().NotBeNull(); result.Id.Should().BeGreaterThan(0); result.Title.Should().Be("I did this"); result.Content.Should().Be(""); result.CreatedDate.Day.Should().Be(DateTime.Today.Day); }
public async void CreateItem_HappyPath() { var service = BuildService(); WorkNoteDto result = await service.CreateItemAsync(new WorkNoteDto { Title = "I did this", Content = "No issues found", }); result.Should().NotBeNull(); result.Id.Should().Be(1002); result.Title.Should().Be("I did this"); result.Content.Should().Be("No issues found"); result.CreatedDate.Day.Should().Be(DateTime.Today.Day); }
public async void UpdateItem_Error_ContentNull() { var service = BuildService(); WorkNoteDto result = await service.UpdateItemAsync(new WorkNoteDto { Id = 1001, Title = "Changed", Content = null }); result.Should().NotBeNull(); result.Id.Should().Be(1001); result.Title.Should().Be("Changed"); result.Content.Should().Be(""); result.CreatedDate.Year.Should().Be(2019); }
public void UpdateItem_Error_NotFound() { var service = BuildService(); WorkNoteDto badItem = new WorkNoteDto { Id = 999999, Title = "Changed", Content = "Changed" }; Func <Task <WorkNoteDto> > func = async() => await service.UpdateItemAsync(badItem); func.Should() .Throw <NotFoundException>() .WithMessage("Item with id=999999 not found."); }
public void UpdateItem_Error_TitleBlank() { var service = BuildService(); WorkNoteDto badItem = new WorkNoteDto { Id = 1001, Title = " ", Content = "Changed" }; Func <Task <WorkNoteDto> > func = async() => await service.UpdateItemAsync(badItem); func.Should() .Throw <ContentValidationException>() .WithMessage("'Title' must not be empty."); }
public async void CreateItem_TrimWhitespace() { var service = BuildService(); WorkNoteDto result = await service.CreateItemAsync(new WorkNoteDto { Title = @" I did this ", Content = @" No issues found ", }); result.Should().NotBeNull(); result.Id.Should().BeGreaterThan(0); result.Title.Should().Be("I did this"); result.Content.Should().Be("No issues found"); result.CreatedDate.Day.Should().Be(DateTime.Today.Day); }
public async Task Put_UrlIdMismatch() { // Arrange var testId = 1001; var testTitle = "New title"; var testContent = "New content"; var controller = BuildController(); // Act WorkNoteDto entry = new WorkNoteDto { Id = testId, Title = testTitle, Content = testContent }; var response = await controller.UpdateAsync(999, entry); // Assert response.Result.Should().BeOfType <BadRequestObjectResult>("Return a 400 result"); }
public async Task Post_ContentValidationFailure() { // Arrange var controller = BuildController(); _mockWorkNotesService .Setup(inst => inst.CreateItemAsync(It.IsAny <WorkNoteDto>())) .Throws(new ContentValidationException("Yeeeet!")); // Act WorkNoteDto entry = new WorkNoteDto { Title = "I did this", Content = null }; var response = await controller.CreateAsync(entry); // Assert response.Result.Should().BeOfType <BadRequestObjectResult>("Return a 400 result"); }
public async Task <WorkNoteDto> UpdateItemAsync(WorkNoteDto newEntry) { Validation <WorkNoteDtoValidator, WorkNoteDto> .ValidateObject(newEntry, "default,Update"); WorkNote entry = _repository.All.FirstOrDefault(item => item.Id == newEntry.Id); if (entry == null) { throw new NotFoundException($"Item with id={newEntry.Id} not found."); } entry.ProjectId = newEntry.ProjectId; entry.WorkItemId = newEntry.WorkItemId; entry.Title = newEntry.Title.Trim(); entry.Content = (newEntry.Content ?? "").Trim(); await _repository.UpdateAsync(entry); return(_mapper.Map <WorkNoteDto>(entry)); }
public async Task <WorkNoteDto> CreateItemAsync(WorkNoteDto entry) { entry.Id = 0; if (entry.Content == null) { entry.Content = ""; } Validation <WorkNoteDtoValidator, WorkNoteDto> .ValidateObject(entry); entry.Title = entry.Title.Trim(); entry.Content = entry.Content.Trim(); var entity = _mapper.Map <WorkNote>(entry); entity.CreatedDate = DateTime.Now; await _repository.CreateAsync(entity); return(_mapper.Map <WorkNoteDto>(entity)); }
public async Task <ActionResult <List <WorkNoteDto> > > CreateAsync([FromBody] WorkNoteDto newItem) { WorkNoteDto workNote; try { workNote = await _workNoteService.CreateItemAsync(newItem); } catch (ContentValidationException ex) { return(BadRequest(ex.Errors)); } catch (Exception ex) { _logger.LogCritical(ex, $"Error creating WorkNote Title=\"{newItem.Title}\""); return(StatusCode(StatusCodes.Status500InternalServerError)); } return(CreatedAtAction( "GetAsync", // Tell WebAPI to use the route-info for our GetAsync() method when creating the location header new { id = workNote.Id }, // Parameters that the GetAsync() method needs workNote)); // The new thing that was created. }
public async Task Put_NotFound() { // Arrange var testId = 999; var testTitle = "New title"; var testContent = "New content"; var controller = BuildController(); // Act WorkNoteDto entry = new WorkNoteDto { Id = testId, Title = testTitle, Content = testContent }; var response = await controller.UpdateAsync(testId, entry); // Assert response.Result.Should().BeOfType <NotFoundObjectResult>("Return a 404 result"); var result = response.Result as NotFoundObjectResult; result.StatusCode.Should().Be(404); }