public virtual async void TestDelete() { var builder = new WebHostBuilder() .UseEnvironment("Production") .UseStartup <TestStartup>(); TestServer testServer = new TestServer(builder); var client = new ApiClient(testServer.CreateClient()); client.SetBearerToken(JWTTestHelper.GenerateBearerToken()); ApplicationDbContext context = testServer.Host.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext; INoteService service = testServer.Host.Services.GetService(typeof(INoteService)) as INoteService; var model = new ApiNoteServerRequestModel(); model.SetProperties(1, DateTime.Parse("1/1/1988 12:00:00 AM"), "B", 1); CreateResponse <ApiNoteServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.NoteDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiNoteServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public virtual async Task <IActionResult> Create([FromBody] ApiNoteServerRequestModel model) { CreateResponse <ApiNoteServerResponseModel> result = await this.NoteService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Notes/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
public void MapServerResponseToRequest() { var mapper = new ApiNoteServerModelMapper(); var model = new ApiNoteServerResponseModel(); model.SetProperties(1, 1, DateTime.Parse("1/1/1987 12:00:00 AM"), "A", 1); ApiNoteServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.CallId.Should().Be(1); response.DateCreated.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.NoteText.Should().Be("A"); response.OfficerId.Should().Be(1); }
private async Task <ApiNoteServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiNoteServerRequestModel> patch) { var record = await this.NoteService.Get(id); if (record == null) { return(null); } else { ApiNoteServerRequestModel request = this.NoteModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public void CreatePatch() { var mapper = new ApiNoteServerModelMapper(); var model = new ApiNoteServerRequestModel(); model.SetProperties(1, DateTime.Parse("1/1/1987 12:00:00 AM"), "A", 1); JsonPatchDocument <ApiNoteServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiNoteServerRequestModel(); patch.ApplyTo(response); response.CallId.Should().Be(1); response.DateCreated.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.NoteText.Should().Be("A"); response.OfficerId.Should().Be(1); }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <INoteService, INoteRepository>(); var model = new ApiNoteServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new NoteService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.NoteModelValidatorMock.Object, mock.DALMapperMockFactory.DALNoteMapperMock); ActionResponse response = await service.Delete(default(int)); response.Should().NotBeNull(); response.Success.Should().BeTrue(); mock.RepositoryMock.Verify(x => x.Delete(It.IsAny <int>())); mock.ModelValidatorMockFactory.NoteModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <NoteDeletedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiNoteServerRequestModel model) { ApiNoteServerRequestModel request = await this.PatchModel(id, this.NoteModelMapper.CreatePatch(model)) as ApiNoteServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiNoteServerResponseModel> result = await this.NoteService.Update(id, request); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }
public async void Delete_ErrorsOccurred_ShouldReturnErrorResponse() { var mock = new ServiceMockFacade <INoteService, INoteRepository>(); var model = new ApiNoteServerRequestModel(); var validatorMock = new Mock <IApiNoteServerRequestModelValidator>(); validatorMock.Setup(x => x.ValidateDeleteAsync(It.IsAny <int>())).Returns(Task.FromResult(new FluentValidation.Results.ValidationResult(new List <ValidationFailure>() { new ValidationFailure("text", "test") }))); var service = new NoteService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALNoteMapperMock); ActionResponse response = await service.Delete(default(int)); response.Should().NotBeNull(); response.Success.Should().BeFalse(); validatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <NoteDeletedNotification>(), It.IsAny <CancellationToken>()), Times.Never()); }
public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiNoteServerRequestModel> patch) { ApiNoteServerResponseModel record = await this.NoteService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiNoteServerRequestModel model = await this.PatchModel(id, patch) as ApiNoteServerRequestModel; UpdateResponse <ApiNoteServerResponseModel> result = await this.NoteService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }