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; ICustomerCommunicationService service = testServer.Host.Services.GetService(typeof(ICustomerCommunicationService)) as ICustomerCommunicationService; var model = new ApiCustomerCommunicationServerRequestModel(); model.SetProperties(1, DateTime.Parse("1/1/1988 12:00:00 AM"), 1, "B"); CreateResponse <ApiCustomerCommunicationServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.CustomerCommunicationDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiCustomerCommunicationServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public virtual async Task <IActionResult> Create([FromBody] ApiCustomerCommunicationServerRequestModel model) { CreateResponse <ApiCustomerCommunicationServerResponseModel> result = await this.CustomerCommunicationService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/CustomerCommunications/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
public void MapServerResponseToRequest() { var mapper = new ApiCustomerCommunicationServerModelMapper(); var model = new ApiCustomerCommunicationServerResponseModel(); model.SetProperties(1, 1, DateTime.Parse("1/1/1987 12:00:00 AM"), 1, "A"); ApiCustomerCommunicationServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.CustomerId.Should().Be(1); response.DateCreated.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.EmployeeId.Should().Be(1); response.Notes.Should().Be("A"); }
private async Task <ApiCustomerCommunicationServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiCustomerCommunicationServerRequestModel> patch) { var record = await this.CustomerCommunicationService.Get(id); if (record == null) { return(null); } else { ApiCustomerCommunicationServerRequestModel request = this.CustomerCommunicationModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public void CreatePatch() { var mapper = new ApiCustomerCommunicationServerModelMapper(); var model = new ApiCustomerCommunicationServerRequestModel(); model.SetProperties(1, DateTime.Parse("1/1/1987 12:00:00 AM"), 1, "A"); JsonPatchDocument <ApiCustomerCommunicationServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiCustomerCommunicationServerRequestModel(); patch.ApplyTo(response); response.CustomerId.Should().Be(1); response.DateCreated.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.EmployeeId.Should().Be(1); response.Notes.Should().Be("A"); }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <ICustomerCommunicationService, ICustomerCommunicationRepository>(); var model = new ApiCustomerCommunicationServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new CustomerCommunicationService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.CustomerCommunicationModelValidatorMock.Object, mock.DALMapperMockFactory.DALCustomerCommunicationMapperMock); 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.CustomerCommunicationModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <CustomerCommunicationDeletedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiCustomerCommunicationServerRequestModel model) { ApiCustomerCommunicationServerRequestModel request = await this.PatchModel(id, this.CustomerCommunicationModelMapper.CreatePatch(model)) as ApiCustomerCommunicationServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiCustomerCommunicationServerResponseModel> result = await this.CustomerCommunicationService.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 <ICustomerCommunicationService, ICustomerCommunicationRepository>(); var model = new ApiCustomerCommunicationServerRequestModel(); var validatorMock = new Mock <IApiCustomerCommunicationServerRequestModelValidator>(); 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 CustomerCommunicationService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALCustomerCommunicationMapperMock); 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 <CustomerCommunicationDeletedNotification>(), It.IsAny <CancellationToken>()), Times.Never()); }
public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiCustomerCommunicationServerRequestModel> patch) { ApiCustomerCommunicationServerResponseModel record = await this.CustomerCommunicationService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiCustomerCommunicationServerRequestModel model = await this.PatchModel(id, patch) as ApiCustomerCommunicationServerRequestModel; UpdateResponse <ApiCustomerCommunicationServerResponseModel> result = await this.CustomerCommunicationService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }