public async void Update_ErrorsOccurred_ShouldReturnErrorResponse() { var mock = new ServiceMockFacade <IDestinationService, IDestinationRepository>(); var model = new ApiDestinationServerRequestModel(); var validatorMock = new Mock <IApiDestinationServerRequestModelValidator>(); validatorMock.Setup(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiDestinationServerRequestModel>())).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 Destination())); var service = new DestinationService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALDestinationMapperMock, mock.DALMapperMockFactory.DALPipelineStepDestinationMapperMock); UpdateResponse <ApiDestinationServerResponseModel> response = await service.Update(default(int), model); response.Should().NotBeNull(); response.Success.Should().BeFalse(); validatorMock.Verify(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiDestinationServerRequestModel>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <DestinationUpdatedNotification>(), 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; IDestinationService service = testServer.Host.Services.GetService(typeof(IDestinationService)) as IDestinationService; var model = new ApiDestinationServerRequestModel(); model.SetProperties(1, "B", 2); CreateResponse <ApiDestinationServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.DestinationDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiDestinationServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public virtual async Task <IActionResult> Create([FromBody] ApiDestinationServerRequestModel model) { CreateResponse <ApiDestinationServerResponseModel> result = await this.DestinationService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Destinations/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
public void MapServerResponseToRequest() { var mapper = new ApiDestinationServerModelMapper(); var model = new ApiDestinationServerResponseModel(); model.SetProperties(1, 1, "A", 1); ApiDestinationServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.CountryId.Should().Be(1); response.Name.Should().Be("A"); response.Order.Should().Be(1); }
private async Task <ApiDestinationServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiDestinationServerRequestModel> patch) { var record = await this.DestinationService.Get(id); if (record == null) { return(null); } else { ApiDestinationServerRequestModel request = this.DestinationModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public void CreatePatch() { var mapper = new ApiDestinationServerModelMapper(); var model = new ApiDestinationServerRequestModel(); model.SetProperties(1, "A", 1); JsonPatchDocument <ApiDestinationServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiDestinationServerRequestModel(); patch.ApplyTo(response); response.CountryId.Should().Be(1); response.Name.Should().Be("A"); response.Order.Should().Be(1); }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <IDestinationService, IDestinationRepository>(); var model = new ApiDestinationServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new DestinationService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.DestinationModelValidatorMock.Object, mock.DALMapperMockFactory.DALDestinationMapperMock, mock.DALMapperMockFactory.DALPipelineStepDestinationMapperMock); 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.DestinationModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <DestinationDeletedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiDestinationServerRequestModel model) { ApiDestinationServerRequestModel request = await this.PatchModel(id, this.DestinationModelMapper.CreatePatch(model)) as ApiDestinationServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiDestinationServerResponseModel> result = await this.DestinationService.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 <ApiDestinationServerRequestModel> patch) { ApiDestinationServerResponseModel record = await this.DestinationService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiDestinationServerRequestModel model = await this.PatchModel(id, patch) as ApiDestinationServerRequestModel; UpdateResponse <ApiDestinationServerResponseModel> result = await this.DestinationService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }