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; IPostHistoryService service = testServer.Host.Services.GetService(typeof(IPostHistoryService)) as IPostHistoryService; var model = new ApiPostHistoryServerRequestModel(); model.SetProperties("B", DateTime.Parse("1/1/1988 12:00:00 AM"), 1, 1, "B", "B", "B", 1); CreateResponse <ApiPostHistoryServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.PostHistoryDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiPostHistoryServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public virtual async Task <IActionResult> Create([FromBody] ApiPostHistoryServerRequestModel model) { CreateResponse <ApiPostHistoryServerResponseModel> result = await this.PostHistoryService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/PostHistories/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
private async Task <ApiPostHistoryServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiPostHistoryServerRequestModel> patch) { var record = await this.PostHistoryService.Get(id); if (record == null) { return(null); } else { ApiPostHistoryServerRequestModel request = this.PostHistoryModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public void MapServerResponseToRequest() { var mapper = new ApiPostHistoryServerModelMapper(); var model = new ApiPostHistoryServerResponseModel(); model.SetProperties(1, "A", DateTime.Parse("1/1/1987 12:00:00 AM"), 1, 1, "A", "A", "A", 1); ApiPostHistoryServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.Comment.Should().Be("A"); response.CreationDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.PostHistoryTypeId.Should().Be(1); response.PostId.Should().Be(1); response.RevisionGUID.Should().Be("A"); response.Text.Should().Be("A"); response.UserDisplayName.Should().Be("A"); response.UserId.Should().Be(1); }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <IPostHistoryService, IPostHistoryRepository>(); var model = new ApiPostHistoryServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new PostHistoryService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.PostHistoryModelValidatorMock.Object, mock.DALMapperMockFactory.DALPostHistoryMapperMock); 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.PostHistoryModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <PostHistoryDeletedNotification>(), It.IsAny <CancellationToken>())); }
public void CreatePatch() { var mapper = new ApiPostHistoryServerModelMapper(); var model = new ApiPostHistoryServerRequestModel(); model.SetProperties("A", DateTime.Parse("1/1/1987 12:00:00 AM"), 1, 1, "A", "A", "A", 1); JsonPatchDocument <ApiPostHistoryServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiPostHistoryServerRequestModel(); patch.ApplyTo(response); response.Comment.Should().Be("A"); response.CreationDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.PostHistoryTypeId.Should().Be(1); response.PostId.Should().Be(1); response.RevisionGUID.Should().Be("A"); response.Text.Should().Be("A"); response.UserDisplayName.Should().Be("A"); response.UserId.Should().Be(1); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiPostHistoryServerRequestModel model) { ApiPostHistoryServerRequestModel request = await this.PatchModel(id, this.PostHistoryModelMapper.CreatePatch(model)) as ApiPostHistoryServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiPostHistoryServerResponseModel> result = await this.PostHistoryService.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 <IPostHistoryService, IPostHistoryRepository>(); var model = new ApiPostHistoryServerRequestModel(); var validatorMock = new Mock <IApiPostHistoryServerRequestModelValidator>(); 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 PostHistoryService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALPostHistoryMapperMock); 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 <PostHistoryDeletedNotification>(), It.IsAny <CancellationToken>()), Times.Never()); }
public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiPostHistoryServerRequestModel> patch) { ApiPostHistoryServerResponseModel record = await this.PostHistoryService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiPostHistoryServerRequestModel model = await this.PatchModel(id, patch) as ApiPostHistoryServerRequestModel; UpdateResponse <ApiPostHistoryServerResponseModel> result = await this.PostHistoryService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }