public async void Delete_ErrorsOccurred_ShouldReturnErrorResponse() { var mock = new ServiceMockFacade <IVehicleService, IVehicleRepository>(); var model = new ApiVehicleServerRequestModel(); var validatorMock = new Mock <IApiVehicleServerRequestModelValidator>(); 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 VehicleService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALVehicleMapperMock, mock.DALMapperMockFactory.DALVehicleCapabilitiesMapperMock, mock.DALMapperMockFactory.DALVehicleOfficerMapperMock); 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 <VehicleDeletedNotification>(), 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; IVehicleService service = testServer.Host.Services.GetService(typeof(IVehicleService)) as IVehicleService; var model = new ApiVehicleServerRequestModel(); model.SetProperties("B"); CreateResponse <ApiVehicleServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.VehicleDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiVehicleServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public void MapServerResponseToRequest() { var mapper = new ApiVehicleServerModelMapper(); var model = new ApiVehicleServerResponseModel(); model.SetProperties(1, "A"); ApiVehicleServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.Name.Should().Be("A"); }
public void CreatePatch() { var mapper = new ApiVehicleServerModelMapper(); var model = new ApiVehicleServerRequestModel(); model.SetProperties("A"); JsonPatchDocument <ApiVehicleServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiVehicleServerRequestModel(); patch.ApplyTo(response); response.Name.Should().Be("A"); }
public virtual async Task <IActionResult> Create([FromBody] ApiVehicleServerRequestModel model) { CreateResponse <ApiVehicleServerResponseModel> result = await this.VehicleService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Vehicles/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
private async Task <ApiVehicleServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiVehicleServerRequestModel> patch) { var record = await this.VehicleService.Get(id); if (record == null) { return(null); } else { ApiVehicleServerRequestModel request = this.VehicleModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiVehicleServerRequestModel model) { ApiVehicleServerRequestModel request = await this.PatchModel(id, this.VehicleModelMapper.CreatePatch(model)) as ApiVehicleServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiVehicleServerResponseModel> result = await this.VehicleService.Update(id, request); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <IVehicleService, IVehicleRepository>(); var model = new ApiVehicleServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new VehicleService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.VehicleModelValidatorMock.Object, mock.DALMapperMockFactory.DALVehicleMapperMock, mock.DALMapperMockFactory.DALVehicleCapabilitiesMapperMock, mock.DALMapperMockFactory.DALVehicleOfficerMapperMock); 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.VehicleModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <VehicleDeletedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiVehicleServerRequestModel> patch) { ApiVehicleServerResponseModel record = await this.VehicleService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiVehicleServerRequestModel model = await this.PatchModel(id, patch) as ApiVehicleServerRequestModel; UpdateResponse <ApiVehicleServerResponseModel> result = await this.VehicleService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }