public async void Update_ErrorsOccurred_ShouldReturnErrorResponse() { var mock = new ServiceMockFacade <IPaymentTypeService, IPaymentTypeRepository>(); var model = new ApiPaymentTypeServerRequestModel(); var validatorMock = new Mock <IApiPaymentTypeServerRequestModelValidator>(); validatorMock.Setup(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiPaymentTypeServerRequestModel>())).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 PaymentType())); var service = new PaymentTypeService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALPaymentTypeMapperMock, mock.DALMapperMockFactory.DALSaleMapperMock); UpdateResponse <ApiPaymentTypeServerResponseModel> response = await service.Update(default(int), model); response.Should().NotBeNull(); response.Success.Should().BeFalse(); validatorMock.Verify(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiPaymentTypeServerRequestModel>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <PaymentTypeUpdatedNotification>(), 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; IPaymentTypeService service = testServer.Host.Services.GetService(typeof(IPaymentTypeService)) as IPaymentTypeService; var model = new ApiPaymentTypeServerRequestModel(); model.SetProperties("B"); CreateResponse <ApiPaymentTypeServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.PaymentTypeDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiPaymentTypeServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public void MapServerResponseToRequest() { var mapper = new ApiPaymentTypeServerModelMapper(); var model = new ApiPaymentTypeServerResponseModel(); model.SetProperties(1, "A"); ApiPaymentTypeServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.Name.Should().Be("A"); }
public void CreatePatch() { var mapper = new ApiPaymentTypeServerModelMapper(); var model = new ApiPaymentTypeServerRequestModel(); model.SetProperties("A"); JsonPatchDocument <ApiPaymentTypeServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiPaymentTypeServerRequestModel(); patch.ApplyTo(response); response.Name.Should().Be("A"); }
public virtual async Task <IActionResult> Create([FromBody] ApiPaymentTypeServerRequestModel model) { CreateResponse <ApiPaymentTypeServerResponseModel> result = await this.PaymentTypeService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/PaymentTypes/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
private async Task <ApiPaymentTypeServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiPaymentTypeServerRequestModel> patch) { var record = await this.PaymentTypeService.Get(id); if (record == null) { return(null); } else { ApiPaymentTypeServerRequestModel request = this.PaymentTypeModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <IPaymentTypeService, IPaymentTypeRepository>(); var model = new ApiPaymentTypeServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new PaymentTypeService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.PaymentTypeModelValidatorMock.Object, mock.DALMapperMockFactory.DALPaymentTypeMapperMock, mock.DALMapperMockFactory.DALSaleMapperMock); 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.PaymentTypeModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <PaymentTypeDeletedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiPaymentTypeServerRequestModel model) { ApiPaymentTypeServerRequestModel request = await this.PatchModel(id, this.PaymentTypeModelMapper.CreatePatch(model)) as ApiPaymentTypeServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiPaymentTypeServerResponseModel> result = await this.PaymentTypeService.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 <ApiPaymentTypeServerRequestModel> patch) { ApiPaymentTypeServerResponseModel record = await this.PaymentTypeService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiPaymentTypeServerRequestModel model = await this.PatchModel(id, patch) as ApiPaymentTypeServerRequestModel; UpdateResponse <ApiPaymentTypeServerResponseModel> result = await this.PaymentTypeService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }