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; IFileService service = testServer.Host.Services.GetService(typeof(IFileService)) as IFileService; var model = new ApiFileServerRequestModel(); model.SetProperties(1, DateTime.Parse("1/1/1988 12:00:00 AM"), "B", DateTime.Parse("1/1/1988 12:00:00 AM"), "B", Guid.Parse("3842cac4-b9a0-8223-0dcc-509a6f75849b"), 2m, 1, "B", "B", "B"); CreateResponse <ApiFileServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.FileDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiFileServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public virtual async Task <IActionResult> Create([FromBody] ApiFileServerRequestModel model) { CreateResponse <ApiFileServerResponseModel> result = await this.FileService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Files/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
private async Task <ApiFileServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiFileServerRequestModel> patch) { var record = await this.FileService.Get(id); if (record == null) { return(null); } else { ApiFileServerRequestModel request = this.FileModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <IFileService, IFileRepository>(); var model = new ApiFileServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new FileService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.FileModelValidatorMock.Object, mock.DALMapperMockFactory.DALFileMapperMock); 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.FileModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <FileDeletedNotification>(), It.IsAny <CancellationToken>())); }
public void MapServerResponseToRequest() { var mapper = new ApiFileServerModelMapper(); var model = new ApiFileServerResponseModel(); model.SetProperties(1, 1, DateTime.Parse("1/1/1987 12:00:00 AM"), "A", DateTime.Parse("1/1/1987 12:00:00 AM"), "A", Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da"), 1m, 1, "A", "A", "A"); ApiFileServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.BucketId.Should().Be(1); response.DateCreated.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.Description.Should().Be("A"); response.Expiration.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.Extension.Should().Be("A"); response.ExternalId.Should().Be(Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da")); response.FileSizeInByte.Should().Be(1m); response.FileTypeId.Should().Be(1); response.Location.Should().Be("A"); response.PrivateKey.Should().Be("A"); response.PublicKey.Should().Be("A"); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiFileServerRequestModel model) { ApiFileServerRequestModel request = await this.PatchModel(id, this.FileModelMapper.CreatePatch(model)) as ApiFileServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiFileServerResponseModel> result = await this.FileService.Update(id, request); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }
public void CreatePatch() { var mapper = new ApiFileServerModelMapper(); var model = new ApiFileServerRequestModel(); model.SetProperties(1, DateTime.Parse("1/1/1987 12:00:00 AM"), "A", DateTime.Parse("1/1/1987 12:00:00 AM"), "A", Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da"), 1m, 1, "A", "A", "A"); JsonPatchDocument <ApiFileServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiFileServerRequestModel(); patch.ApplyTo(response); response.BucketId.Should().Be(1); response.DateCreated.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.Description.Should().Be("A"); response.Expiration.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.Extension.Should().Be("A"); response.ExternalId.Should().Be(Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da")); response.FileSizeInByte.Should().Be(1m); response.FileTypeId.Should().Be(1); response.Location.Should().Be("A"); response.PrivateKey.Should().Be("A"); response.PublicKey.Should().Be("A"); }
public async void Delete_ErrorsOccurred_ShouldReturnErrorResponse() { var mock = new ServiceMockFacade <IFileService, IFileRepository>(); var model = new ApiFileServerRequestModel(); var validatorMock = new Mock <IApiFileServerRequestModelValidator>(); 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 FileService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALFileMapperMock); 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 <FileDeletedNotification>(), It.IsAny <CancellationToken>()), Times.Never()); }
public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiFileServerRequestModel> patch) { ApiFileServerResponseModel record = await this.FileService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiFileServerRequestModel model = await this.PatchModel(id, patch) as ApiFileServerRequestModel; UpdateResponse <ApiFileServerResponseModel> result = await this.FileService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }