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; IVoteService service = testServer.Host.Services.GetService(typeof(IVoteService)) as IVoteService; var model = new ApiVoteServerRequestModel(); model.SetProperties(2, DateTime.Parse("1/1/1988 12:00:00 AM"), 1, 1, 1); CreateResponse <ApiVoteServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.VoteDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiVoteServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public virtual async Task <IActionResult> Create([FromBody] ApiVoteServerRequestModel model) { CreateResponse <ApiVoteServerResponseModel> result = await this.VoteService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Votes/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
private async Task <ApiVoteServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiVoteServerRequestModel> patch) { var record = await this.VoteService.Get(id); if (record == null) { return(null); } else { ApiVoteServerRequestModel request = this.VoteModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public void MapServerResponseToRequest() { var mapper = new ApiVoteServerModelMapper(); var model = new ApiVoteServerResponseModel(); model.SetProperties(1, 1, DateTime.Parse("1/1/1987 12:00:00 AM"), 1, 1, 1); ApiVoteServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.BountyAmount.Should().Be(1); response.CreationDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.PostId.Should().Be(1); response.UserId.Should().Be(1); response.VoteTypeId.Should().Be(1); }
public void CreatePatch() { var mapper = new ApiVoteServerModelMapper(); var model = new ApiVoteServerRequestModel(); model.SetProperties(1, DateTime.Parse("1/1/1987 12:00:00 AM"), 1, 1, 1); JsonPatchDocument <ApiVoteServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiVoteServerRequestModel(); patch.ApplyTo(response); response.BountyAmount.Should().Be(1); response.CreationDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.PostId.Should().Be(1); response.UserId.Should().Be(1); response.VoteTypeId.Should().Be(1); }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <IVoteService, IVoteRepository>(); var model = new ApiVoteServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new VoteService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.VoteModelValidatorMock.Object, mock.DALMapperMockFactory.DALVoteMapperMock); 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.VoteModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <VoteDeletedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiVoteServerRequestModel model) { ApiVoteServerRequestModel request = await this.PatchModel(id, this.VoteModelMapper.CreatePatch(model)) as ApiVoteServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiVoteServerResponseModel> result = await this.VoteService.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 <IVoteService, IVoteRepository>(); var model = new ApiVoteServerRequestModel(); var validatorMock = new Mock <IApiVoteServerRequestModelValidator>(); 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 VoteService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALVoteMapperMock); 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 <VoteDeletedNotification>(), It.IsAny <CancellationToken>()), Times.Never()); }
public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiVoteServerRequestModel> patch) { ApiVoteServerResponseModel record = await this.VoteService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiVoteServerRequestModel model = await this.PatchModel(id, patch) as ApiVoteServerRequestModel; UpdateResponse <ApiVoteServerResponseModel> result = await this.VoteService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }