public async void Update_ErrorsOccurred_ShouldReturnErrorResponse() { var mock = new ServiceMockFacade <IBucketService, IBucketRepository>(); var model = new ApiBucketServerRequestModel(); var validatorMock = new Mock <IApiBucketServerRequestModelValidator>(); validatorMock.Setup(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiBucketServerRequestModel>())).Returns(Task.FromResult(new ValidationResult(new List <ValidationFailure>() { new ValidationFailure("text", "test") }))); mock.RepositoryMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult(new Bucket())); var service = new BucketService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALBucketMapperMock, mock.DALMapperMockFactory.DALFileMapperMock); UpdateResponse <ApiBucketServerResponseModel> response = await service.Update(default(int), model); response.Should().NotBeNull(); response.Success.Should().BeFalse(); validatorMock.Verify(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiBucketServerRequestModel>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <BucketUpdatedNotification>(), It.IsAny <CancellationToken>()), Times.Never()); }
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; IBucketService service = testServer.Host.Services.GetService(typeof(IBucketService)) as IBucketService; var model = new ApiBucketServerRequestModel(); model.SetProperties(Guid.Parse("3842cac4-b9a0-8223-0dcc-509a6f75849b"), "B"); CreateResponse <ApiBucketServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.BucketDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiBucketServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public void MapServerResponseToRequest() { var mapper = new ApiBucketServerModelMapper(); var model = new ApiBucketServerResponseModel(); model.SetProperties(1, Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da"), "A"); ApiBucketServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.ExternalId.Should().Be(Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da")); response.Name.Should().Be("A"); }
public virtual async Task <IActionResult> Create([FromBody] ApiBucketServerRequestModel model) { CreateResponse <ApiBucketServerResponseModel> result = await this.BucketService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Buckets/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
public void CreatePatch() { var mapper = new ApiBucketServerModelMapper(); var model = new ApiBucketServerRequestModel(); model.SetProperties(Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da"), "A"); JsonPatchDocument <ApiBucketServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiBucketServerRequestModel(); patch.ApplyTo(response); response.ExternalId.Should().Be(Guid.Parse("8420cdcf-d595-ef65-66e7-dff9f98764da")); response.Name.Should().Be("A"); }
private async Task <ApiBucketServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiBucketServerRequestModel> patch) { var record = await this.BucketService.Get(id); if (record == null) { return(null); } else { ApiBucketServerRequestModel request = this.BucketModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <IBucketService, IBucketRepository>(); var model = new ApiBucketServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new BucketService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.BucketModelValidatorMock.Object, mock.DALMapperMockFactory.DALBucketMapperMock, 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.BucketModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <BucketDeletedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiBucketServerRequestModel model) { ApiBucketServerRequestModel request = await this.PatchModel(id, this.BucketModelMapper.CreatePatch(model)) as ApiBucketServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiBucketServerResponseModel> result = await this.BucketService.Update(id, request); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }
public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiBucketServerRequestModel> patch) { ApiBucketServerResponseModel record = await this.BucketService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiBucketServerRequestModel model = await this.PatchModel(id, patch) as ApiBucketServerRequestModel; UpdateResponse <ApiBucketServerResponseModel> result = await this.BucketService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }