public async void Update_ErrorsOccurred_ShouldReturnErrorResponse() { var mock = new ServiceMockFacade <ICityService, ICityRepository>(); var model = new ApiCityServerRequestModel(); var validatorMock = new Mock <IApiCityServerRequestModelValidator>(); validatorMock.Setup(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiCityServerRequestModel>())).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 City())); var service = new CityService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALCityMapperMock, mock.DALMapperMockFactory.DALEventMapperMock); UpdateResponse <ApiCityServerResponseModel> response = await service.Update(default(int), model); response.Should().NotBeNull(); response.Success.Should().BeFalse(); validatorMock.Verify(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiCityServerRequestModel>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <CityUpdatedNotification>(), 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; ICityService service = testServer.Host.Services.GetService(typeof(ICityService)) as ICityService; var model = new ApiCityServerRequestModel(); model.SetProperties("B", 1); CreateResponse <ApiCityServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.CityDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiCityServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public void MapServerResponseToRequest() { var mapper = new ApiCityServerModelMapper(); var model = new ApiCityServerResponseModel(); model.SetProperties(1, "A", 1); ApiCityServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.Name.Should().Be("A"); response.ProvinceId.Should().Be(1); }
public virtual async Task <IActionResult> Create([FromBody] ApiCityServerRequestModel model) { CreateResponse <ApiCityServerResponseModel> result = await this.CityService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Cities/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
public void CreatePatch() { var mapper = new ApiCityServerModelMapper(); var model = new ApiCityServerRequestModel(); model.SetProperties("A", 1); JsonPatchDocument <ApiCityServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiCityServerRequestModel(); patch.ApplyTo(response); response.Name.Should().Be("A"); response.ProvinceId.Should().Be(1); }
private async Task <ApiCityServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiCityServerRequestModel> patch) { var record = await this.CityService.Get(id); if (record == null) { return(null); } else { ApiCityServerRequestModel request = this.CityModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <ICityService, ICityRepository>(); var model = new ApiCityServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new CityService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.CityModelValidatorMock.Object, mock.DALMapperMockFactory.DALCityMapperMock, mock.DALMapperMockFactory.DALEventMapperMock); 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.CityModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <CityDeletedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiCityServerRequestModel model) { ApiCityServerRequestModel request = await this.PatchModel(id, this.CityModelMapper.CreatePatch(model)) as ApiCityServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiCityServerResponseModel> result = await this.CityService.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 <ApiCityServerRequestModel> patch) { ApiCityServerResponseModel record = await this.CityService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiCityServerRequestModel model = await this.PatchModel(id, patch) as ApiCityServerRequestModel; UpdateResponse <ApiCityServerResponseModel> result = await this.CityService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }