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; ITweetService service = testServer.Host.Services.GetService(typeof(ITweetService)) as ITweetService; var model = new ApiTweetServerRequestModel(); model.SetProperties("B", DateTime.Parse("1/1/1988 12:00:00 AM"), 1, TimeSpan.Parse("02:00:00"), 1); CreateResponse <ApiTweetServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.TweetDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiTweetServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public async void Delete_ErrorsOccurred_ShouldReturnErrorResponse() { var mock = new ServiceMockFacade <ITweetService, ITweetRepository>(); var model = new ApiTweetServerRequestModel(); var validatorMock = new Mock <IApiTweetServerRequestModelValidator>(); 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 TweetService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALTweetMapperMock, mock.DALMapperMockFactory.DALQuoteTweetMapperMock, mock.DALMapperMockFactory.DALRetweetMapperMock); 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 <TweetDeletedNotification>(), It.IsAny <CancellationToken>()), Times.Never()); }
public virtual async Task <IActionResult> Create([FromBody] ApiTweetServerRequestModel model) { CreateResponse <ApiTweetServerResponseModel> result = await this.TweetService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Tweets/{result.Record.TweetId}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
public void MapServerResponseToRequest() { var mapper = new ApiTweetServerModelMapper(); var model = new ApiTweetServerResponseModel(); model.SetProperties(1, "A", DateTime.Parse("1/1/1987 12:00:00 AM"), 1, TimeSpan.Parse("01:00:00"), 1); ApiTweetServerRequestModel 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.LocationId.Should().Be(1); response.Time.Should().Be(TimeSpan.Parse("01:00:00")); response.UserUserId.Should().Be(1); }
private async Task <ApiTweetServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiTweetServerRequestModel> patch) { var record = await this.TweetService.Get(id); if (record == null) { return(null); } else { ApiTweetServerRequestModel request = this.TweetModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public void CreatePatch() { var mapper = new ApiTweetServerModelMapper(); var model = new ApiTweetServerRequestModel(); model.SetProperties("A", DateTime.Parse("1/1/1987 12:00:00 AM"), 1, TimeSpan.Parse("01:00:00"), 1); JsonPatchDocument <ApiTweetServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiTweetServerRequestModel(); patch.ApplyTo(response); response.Content.Should().Be("A"); response.Date.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.LocationId.Should().Be(1); response.Time.Should().Be(TimeSpan.Parse("01:00:00")); response.UserUserId.Should().Be(1); }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <ITweetService, ITweetRepository>(); var model = new ApiTweetServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new TweetService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.TweetModelValidatorMock.Object, mock.DALMapperMockFactory.DALTweetMapperMock, mock.DALMapperMockFactory.DALQuoteTweetMapperMock, mock.DALMapperMockFactory.DALRetweetMapperMock); 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.TweetModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <TweetDeletedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiTweetServerRequestModel model) { ApiTweetServerRequestModel request = await this.PatchModel(id, this.TweetModelMapper.CreatePatch(model)) as ApiTweetServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiTweetServerResponseModel> result = await this.TweetService.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 <ApiTweetServerRequestModel> patch) { ApiTweetServerResponseModel record = await this.TweetService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiTweetServerRequestModel model = await this.PatchModel(id, patch) as ApiTweetServerRequestModel; UpdateResponse <ApiTweetServerResponseModel> result = await this.TweetService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }