public async void Update_ErrorsOccurred_ShouldReturnErrorResponse() { var mock = new ServiceMockFacade <IOffCapabilityService, IOffCapabilityRepository>(); var model = new ApiOffCapabilityServerRequestModel(); var validatorMock = new Mock <IApiOffCapabilityServerRequestModelValidator>(); validatorMock.Setup(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiOffCapabilityServerRequestModel>())).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 OffCapability())); var service = new OffCapabilityService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALOffCapabilityMapperMock, mock.DALMapperMockFactory.DALOfficerCapabilitiesMapperMock); UpdateResponse <ApiOffCapabilityServerResponseModel> response = await service.Update(default(int), model); response.Should().NotBeNull(); response.Success.Should().BeFalse(); validatorMock.Verify(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiOffCapabilityServerRequestModel>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <OffCapabilityUpdatedNotification>(), 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; IOffCapabilityService service = testServer.Host.Services.GetService(typeof(IOffCapabilityService)) as IOffCapabilityService; var model = new ApiOffCapabilityServerRequestModel(); model.SetProperties("B"); CreateResponse <ApiOffCapabilityServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.OffCapabilityDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiOffCapabilityServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public void MapServerResponseToRequest() { var mapper = new ApiOffCapabilityServerModelMapper(); var model = new ApiOffCapabilityServerResponseModel(); model.SetProperties(1, "A"); ApiOffCapabilityServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.Name.Should().Be("A"); }
public void CreatePatch() { var mapper = new ApiOffCapabilityServerModelMapper(); var model = new ApiOffCapabilityServerRequestModel(); model.SetProperties("A"); JsonPatchDocument <ApiOffCapabilityServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiOffCapabilityServerRequestModel(); patch.ApplyTo(response); response.Name.Should().Be("A"); }
public virtual async Task <IActionResult> Create([FromBody] ApiOffCapabilityServerRequestModel model) { CreateResponse <ApiOffCapabilityServerResponseModel> result = await this.OffCapabilityService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/OffCapabilities/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
private async Task <ApiOffCapabilityServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiOffCapabilityServerRequestModel> patch) { var record = await this.OffCapabilityService.Get(id); if (record == null) { return(null); } else { ApiOffCapabilityServerRequestModel request = this.OffCapabilityModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <IOffCapabilityService, IOffCapabilityRepository>(); var model = new ApiOffCapabilityServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new OffCapabilityService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.OffCapabilityModelValidatorMock.Object, mock.DALMapperMockFactory.DALOffCapabilityMapperMock, mock.DALMapperMockFactory.DALOfficerCapabilitiesMapperMock); 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.OffCapabilityModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <OffCapabilityDeletedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiOffCapabilityServerRequestModel model) { ApiOffCapabilityServerRequestModel request = await this.PatchModel(id, this.OffCapabilityModelMapper.CreatePatch(model)) as ApiOffCapabilityServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiOffCapabilityServerResponseModel> result = await this.OffCapabilityService.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 <ApiOffCapabilityServerRequestModel> patch) { ApiOffCapabilityServerResponseModel record = await this.OffCapabilityService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiOffCapabilityServerRequestModel model = await this.PatchModel(id, patch) as ApiOffCapabilityServerRequestModel; UpdateResponse <ApiOffCapabilityServerResponseModel> result = await this.OffCapabilityService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }