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; IQuoteTweetService service = testServer.Host.Services.GetService(typeof(IQuoteTweetService)) as IQuoteTweetService; var model = new ApiQuoteTweetServerRequestModel(); model.SetProperties("B", DateTime.Parse("1/1/1988 12:00:00 AM"), 1, 1, TimeSpan.Parse("02:00:00")); CreateResponse <ApiQuoteTweetServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.QuoteTweetDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiQuoteTweetServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public virtual async Task <IActionResult> Create([FromBody] ApiQuoteTweetServerRequestModel model) { CreateResponse <ApiQuoteTweetServerResponseModel> result = await this.QuoteTweetService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/QuoteTweets/{result.Record.QuoteTweetId}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
private async Task <ApiQuoteTweetServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiQuoteTweetServerRequestModel> patch) { var record = await this.QuoteTweetService.Get(id); if (record == null) { return(null); } else { ApiQuoteTweetServerRequestModel request = this.QuoteTweetModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public void MapServerResponseToRequest() { var mapper = new ApiQuoteTweetServerModelMapper(); var model = new ApiQuoteTweetServerResponseModel(); model.SetProperties(1, "A", DateTime.Parse("1/1/1987 12:00:00 AM"), 1, 1, TimeSpan.Parse("01:00:00")); ApiQuoteTweetServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.Content.Should().Be("A"); response.Date.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.RetweeterUserId.Should().Be(1); response.SourceTweetId.Should().Be(1); response.Time.Should().Be(TimeSpan.Parse("01:00:00")); }
public void CreatePatch() { var mapper = new ApiQuoteTweetServerModelMapper(); var model = new ApiQuoteTweetServerRequestModel(); model.SetProperties("A", DateTime.Parse("1/1/1987 12:00:00 AM"), 1, 1, TimeSpan.Parse("01:00:00")); JsonPatchDocument <ApiQuoteTweetServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiQuoteTweetServerRequestModel(); patch.ApplyTo(response); response.Content.Should().Be("A"); response.Date.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.RetweeterUserId.Should().Be(1); response.SourceTweetId.Should().Be(1); response.Time.Should().Be(TimeSpan.Parse("01:00:00")); }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <IQuoteTweetService, IQuoteTweetRepository>(); var model = new ApiQuoteTweetServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new QuoteTweetService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.QuoteTweetModelValidatorMock.Object, mock.DALMapperMockFactory.DALQuoteTweetMapperMock); 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.QuoteTweetModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <QuoteTweetDeletedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiQuoteTweetServerRequestModel model) { ApiQuoteTweetServerRequestModel request = await this.PatchModel(id, this.QuoteTweetModelMapper.CreatePatch(model)) as ApiQuoteTweetServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiQuoteTweetServerResponseModel> result = await this.QuoteTweetService.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 <IQuoteTweetService, IQuoteTweetRepository>(); var model = new ApiQuoteTweetServerRequestModel(); var validatorMock = new Mock <IApiQuoteTweetServerRequestModelValidator>(); 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 QuoteTweetService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALQuoteTweetMapperMock); 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 <QuoteTweetDeletedNotification>(), It.IsAny <CancellationToken>()), Times.Never()); }
public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiQuoteTweetServerRequestModel> patch) { ApiQuoteTweetServerResponseModel record = await this.QuoteTweetService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiQuoteTweetServerRequestModel model = await this.PatchModel(id, patch) as ApiQuoteTweetServerRequestModel; UpdateResponse <ApiQuoteTweetServerResponseModel> result = await this.QuoteTweetService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }