public async void Update_ErrorsOccurred_ShouldReturnErrorResponse() { var mock = new ServiceMockFacade <IPersonTypeService, IPersonTypeRepository>(); var model = new ApiPersonTypeServerRequestModel(); var validatorMock = new Mock <IApiPersonTypeServerRequestModelValidator>(); validatorMock.Setup(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiPersonTypeServerRequestModel>())).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 PersonType())); var service = new PersonTypeService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALPersonTypeMapperMock, mock.DALMapperMockFactory.DALCallPersonMapperMock); UpdateResponse <ApiPersonTypeServerResponseModel> response = await service.Update(default(int), model); response.Should().NotBeNull(); response.Success.Should().BeFalse(); validatorMock.Verify(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiPersonTypeServerRequestModel>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <PersonTypeUpdatedNotification>(), 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; IPersonTypeService service = testServer.Host.Services.GetService(typeof(IPersonTypeService)) as IPersonTypeService; var model = new ApiPersonTypeServerRequestModel(); model.SetProperties("B"); CreateResponse <ApiPersonTypeServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.PersonTypeDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiPersonTypeServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public void MapServerResponseToRequest() { var mapper = new ApiPersonTypeServerModelMapper(); var model = new ApiPersonTypeServerResponseModel(); model.SetProperties(1, "A"); ApiPersonTypeServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.Name.Should().Be("A"); }
public void CreatePatch() { var mapper = new ApiPersonTypeServerModelMapper(); var model = new ApiPersonTypeServerRequestModel(); model.SetProperties("A"); JsonPatchDocument <ApiPersonTypeServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiPersonTypeServerRequestModel(); patch.ApplyTo(response); response.Name.Should().Be("A"); }
public virtual async Task <IActionResult> Create([FromBody] ApiPersonTypeServerRequestModel model) { CreateResponse <ApiPersonTypeServerResponseModel> result = await this.PersonTypeService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/PersonTypes/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
private async Task <ApiPersonTypeServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiPersonTypeServerRequestModel> patch) { var record = await this.PersonTypeService.Get(id); if (record == null) { return(null); } else { ApiPersonTypeServerRequestModel request = this.PersonTypeModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <IPersonTypeService, IPersonTypeRepository>(); var model = new ApiPersonTypeServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new PersonTypeService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.PersonTypeModelValidatorMock.Object, mock.DALMapperMockFactory.DALPersonTypeMapperMock, mock.DALMapperMockFactory.DALCallPersonMapperMock); 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.PersonTypeModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <PersonTypeDeletedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiPersonTypeServerRequestModel model) { ApiPersonTypeServerRequestModel request = await this.PatchModel(id, this.PersonTypeModelMapper.CreatePatch(model)) as ApiPersonTypeServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiPersonTypeServerResponseModel> result = await this.PersonTypeService.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 <ApiPersonTypeServerRequestModel> patch) { ApiPersonTypeServerResponseModel record = await this.PersonTypeService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiPersonTypeServerRequestModel model = await this.PatchModel(id, patch) as ApiPersonTypeServerRequestModel; UpdateResponse <ApiPersonTypeServerResponseModel> result = await this.PersonTypeService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }