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; IRateService service = testServer.Host.Services.GetService(typeof(IRateService)) as IRateService; var model = new ApiRateServerRequestModel(); model.SetProperties(2m, 1, 1); CreateResponse <ApiRateServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.RateDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiRateServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public void MapServerResponseToRequest() { var mapper = new ApiRateServerModelMapper(); var model = new ApiRateServerResponseModel(); model.SetProperties(1, 1m, 1, 1); ApiRateServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.AmountPerMinute.Should().Be(1m); response.TeacherId.Should().Be(1); response.TeacherSkillId.Should().Be(1); }
public virtual async Task <IActionResult> Create([FromBody] ApiRateServerRequestModel model) { CreateResponse <ApiRateServerResponseModel> result = await this.RateService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Rates/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
public void CreatePatch() { var mapper = new ApiRateServerModelMapper(); var model = new ApiRateServerRequestModel(); model.SetProperties(1m, 1, 1); JsonPatchDocument <ApiRateServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiRateServerRequestModel(); patch.ApplyTo(response); response.AmountPerMinute.Should().Be(1m); response.TeacherId.Should().Be(1); response.TeacherSkillId.Should().Be(1); }
private async Task <ApiRateServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiRateServerRequestModel> patch) { var record = await this.RateService.Get(id); if (record == null) { return(null); } else { ApiRateServerRequestModel request = this.RateModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <IRateService, IRateRepository>(); var model = new ApiRateServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new RateService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.RateModelValidatorMock.Object, mock.DALMapperMockFactory.DALRateMapperMock); 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.RateModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <RateDeletedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiRateServerRequestModel model) { ApiRateServerRequestModel request = await this.PatchModel(id, this.RateModelMapper.CreatePatch(model)) as ApiRateServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiRateServerResponseModel> result = await this.RateService.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 <IRateService, IRateRepository>(); var model = new ApiRateServerRequestModel(); var validatorMock = new Mock <IApiRateServerRequestModelValidator>(); 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 RateService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALRateMapperMock); 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 <RateDeletedNotification>(), It.IsAny <CancellationToken>()), Times.Never()); }
public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiRateServerRequestModel> patch) { ApiRateServerResponseModel record = await this.RateService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiRateServerRequestModel model = await this.PatchModel(id, patch) as ApiRateServerRequestModel; UpdateResponse <ApiRateServerResponseModel> result = await this.RateService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }