public async Task UpdateIssueAsync_Where_SaveChangesAsync_Fails_Should__return_a_IssueNotCreatedException_Test() { // Arrange const int id = 1; var expectedLog = $"Updating Issue: {id} Failed!"; var issueToUpdateDto = new IssueForUpdateDto { Id = 1, IssueDescription = "Test 1 updated", DateAdded = DateTimeOffset.Now }; RepositoryWrapperMock .Setup(x => x.Issue.GetIssueByIdAsync(id)) .ReturnsAsync(Issues[0]).Verifiable(); RepositoryWrapperMock .Setup(x => x.SaveAsync()) .Throws(new Exception("Save Changes Failed")).Verifiable(); // Act await Assert.ThrowsAsync <IssueNotUpdatedException>(() => ServiceUnderTest.UpdateIssueAsync(issueToUpdateDto)); // Assert LoggerMock.VerifyLog(LogLevel.Error, expectedLog); RepositoryWrapperMock.Verify(x => x.SaveAsync(), Times.Once); RepositoryWrapperMock .Verify(x => x.Issue.GetIssueByIdAsync(id), Times.Once); }
public async Task UpdateIssueAsync_Should_Update_and_return_the_specified_Issue_Test() { // Arrange const int id = 1; var issueToUpdateDto = new IssueForUpdateDto() { Id = 1, IssueDescription = "Test 1 updated", DateAdded = DateTimeOffset.Now }; RepositoryWrapperMock .Setup(x => x.Issue.GetIssueByIdAsync(id)) .ReturnsAsync(Issues[0]).Verifiable(); RepositoryWrapperMock .Setup(x => x.SaveAsync()) .Returns(() => Task.Run(() => { return(1); })).Verifiable(); // Act var result = await ServiceUnderTest.UpdateIssueAsync(issueToUpdateDto); // Assert result.Should().BeTrue(); RepositoryWrapperMock.Verify(x => x.SaveAsync(), Times.Once); RepositoryWrapperMock .Verify(x => x.Issue.GetIssueByIdAsync(id), Times.Once); }
public async Task <IActionResult> UpdateIssueAsync([FromBody] IssueForUpdateDto issue) { try { if (issue == null) { _logger.LogError("Issue object sent from client is null"); return(BadRequest("Issue object sent from client is null.")); } if (!ModelState.IsValid) { _logger.LogError("Invalid issue object sent from client"); return(BadRequest(ModelState)); } await _issueService.UpdateIssueAsync(issue); return(NoContent()); } catch (IssueNotFoundException) { return(NotFound()); } catch (Exception ex) { _logger.LogError("Something went wrong inside UpdateIssueAsync action: {Message}", ex.Message); return(StatusCode(500, "Internal server error")); } }
public async Task UpdateIssueAsync_With_An_Exception_Should_Return_StatusCode_500_Test() { // Arrange var expectedIssue = new IssueForUpdateDto() { Id = 1, IssueDescription = "Test 1", DateAdded = DateTimeOffset.Now }; const string expectedLog = "Something went wrong inside UpdateIssueAsync action: Some Error"; const string expectedValue = "Internal server error"; IssueServiceMock .Setup(x => x.UpdateIssueAsync(It.IsAny <IssueForUpdateDto>())) .ThrowsAsync(new Exception("Some Error")); // Act var result = await ControllerUnderTest.UpdateIssueAsync(expectedIssue); // Assert result.Should().BeOfType <ObjectResult>(); LoggerMock.Invocations.Count.Should().Be(1); LoggerMock.Invocations[0].Arguments[2].ToString().Should().Contain(expectedLog); var content = (ObjectResult)result; content.StatusCode.Should().Be(500); content.Value.Should().Be(expectedValue); }
public async Task UpdateIssueAsync(IssueForUpdateDto issue) { using var response = await _apiHelper.ApiClient.GetAsync("/api/GetAllIssuesAsync"); if (response.IsSuccessStatusCode) { var result = await response.Content.ReadAsAsync <List <IssueDto> >(); } else { throw new Exception(response.ReasonPhrase); } }
public async Task <bool> UpdateIssueAsync(IssueForUpdateDto issue) { var issueEntity = await EnforceIssueExistenceAsync(issue.Id); _mapper.Map(issue, issueEntity); _repositoryWrapper.Issue.UpdateIssue(issueEntity); try { await _repositoryWrapper.SaveAsync(); return(true); } catch (Exception ex) { _logger.LogError(ex, "Updating Issue: {Id} Failed!", issue.Id); throw new IssueNotUpdatedException(issueEntity); } }
public async Task UpdateIssueAsync_Should_return_BadRequestResult() { // Arrange var issue = new IssueForUpdateDto() { Id = 1, IssueDescription = "Test 1", DateAdded = DateTimeOffset.Now }; ControllerUnderTest.ModelState.AddModelError("Id", "Some error"); // Act var result = await ControllerUnderTest.UpdateIssueAsync(issue); // Assert var badRequestResult = Assert.IsType <BadRequestObjectResult>(result); Assert.IsType <SerializableError>(badRequestResult.Value); }
public async Task UpdateIssueAsync_Should_return_NoContentResult_Test() { // Arrange var expectedIssue = new IssueForUpdateDto() { Id = 1, IssueDescription = "Test 1", DateAdded = DateTimeOffset.Now }; IssueServiceMock .Setup(x => x.UpdateIssueAsync(expectedIssue)) .ReturnsAsync(true); // Act var result = await ControllerUnderTest.UpdateIssueAsync(expectedIssue); // Assert result.Should().BeOfType <NoContentResult>(); }
public async Task UpdateIssueAsync_With_A_NonExisting_Issue_Should_Return_IssueNotUpdatedException_Test() { // Arrange var issueToUpdateDto = new IssueForUpdateDto() { Id = 1, IssueDescription = "Test 1 updated" }; RepositoryWrapperMock .Setup(x => x.Issue.GetIssueByIdAsync(issueToUpdateDto.Id)) .ReturnsAsync(default(Issue)).Verifiable(); // Act await Assert.ThrowsAsync <IssueNotFoundException>(() => ServiceUnderTest.UpdateIssueAsync(issueToUpdateDto)); // Assert RepositoryWrapperMock .Verify(x => x.Issue.GetIssueByIdAsync(issueToUpdateDto.Id), Times.Once); }
public async Task UpdateIssueAsync_Should_return_NotFoundResult_when_IssueNotFoundException_is_thrown() { // Arrange var unExpectedIssue = new IssueForUpdateDto() { Id = 1, IssueDescription = "Test 1", DateAdded = DateTimeOffset.Now }; var expectedResult = new Issue { Id = 1, IssueDescription = "Test 1", DateAdded = DateTimeOffset.Now }; IssueServiceMock .Setup(x => x.UpdateIssueAsync(unExpectedIssue)) .ThrowsAsync(new IssueNotFoundException(expectedResult)); // Act var result = await ControllerUnderTest.UpdateIssueAsync(unExpectedIssue); // Assert Assert.IsType <NotFoundResult>(result); }
public async Task <IActionResult> UpdateIssueForProject(Guid projectId, Guid issueId, IssueForUpdateDto issueForUpdate) { if (issueForUpdate.DueDate != null && issueForUpdate.DueDate < DateTime.Today) { return(BadRequest("Due Date cannot be before today")); } var projectFromRepo = await _repository.Project.GetProjectWithUsers(projectId); if (projectFromRepo == null) { return(BadRequest("Project doesn't exist")); } var issueFromRepo = await _repository.Issue.GetIssue(issueId); if (issueFromRepo == null) { return(BadRequest("Issue doesn't exist")); } if (issueForUpdate.AssigneeEmail != null) { var user = await _repository.User.GetUser(issueForUpdate.AssigneeEmail); if (user == null) { return(BadRequest("User doesn't exist")); } var userOnProject = projectFromRepo.Users.FirstOrDefault(up => up.UserId.Equals(user.Id)); if (userOnProject == null) { return(BadRequest("User not on project")); } issueFromRepo.AssigneeId = user.Id; } Severity severity; Classification classify; Stage status; Enum.TryParse(issueForUpdate.Severity, out severity); Enum.TryParse(issueForUpdate.Classification, out classify); Enum.TryParse(issueForUpdate.Status, out status); _mapper.Map(issueForUpdate, issueFromRepo); issueFromRepo.Severity = severity; issueFromRepo.Classification = classify; issueFromRepo.Status = status; _repository.Issue.UpdateIssue(issueFromRepo); var updateIssue = await _repository.SaveAsync(); if (!updateIssue) { throw new Exception("Failed to update issue for project"); } return(NoContent()); }