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; IRetweetService service = testServer.Host.Services.GetService(typeof(IRetweetService)) as IRetweetService; var model = new ApiRetweetServerRequestModel(); model.SetProperties(DateTime.Parse("1/1/1988 12:00:00 AM"), 1, TimeSpan.Parse("02:00:00"), 1); CreateResponse <ApiRetweetServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.RetweetDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiRetweetServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public virtual async Task <IActionResult> Create([FromBody] ApiRetweetServerRequestModel model) { CreateResponse <ApiRetweetServerResponseModel> result = await this.RetweetService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Retweets/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
public void MapServerResponseToRequest() { var mapper = new ApiRetweetServerModelMapper(); var model = new ApiRetweetServerResponseModel(); model.SetProperties(1, DateTime.Parse("1/1/1987 12:00:00 AM"), 1, TimeSpan.Parse("01:00:00"), 1); ApiRetweetServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.Date.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.RetwitterUserId.Should().Be(1); response.Time.Should().Be(TimeSpan.Parse("01:00:00")); response.TweetTweetId.Should().Be(1); }
private async Task <ApiRetweetServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiRetweetServerRequestModel> patch) { var record = await this.RetweetService.Get(id); if (record == null) { return(null); } else { ApiRetweetServerRequestModel request = this.RetweetModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public void CreatePatch() { var mapper = new ApiRetweetServerModelMapper(); var model = new ApiRetweetServerRequestModel(); model.SetProperties(DateTime.Parse("1/1/1987 12:00:00 AM"), 1, TimeSpan.Parse("01:00:00"), 1); JsonPatchDocument <ApiRetweetServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiRetweetServerRequestModel(); patch.ApplyTo(response); response.Date.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.RetwitterUserId.Should().Be(1); response.Time.Should().Be(TimeSpan.Parse("01:00:00")); response.TweetTweetId.Should().Be(1); }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <IRetweetService, IRetweetRepository>(); var model = new ApiRetweetServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new RetweetService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.RetweetModelValidatorMock.Object, 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.RetweetModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <RetweetDeletedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiRetweetServerRequestModel model) { ApiRetweetServerRequestModel request = await this.PatchModel(id, this.RetweetModelMapper.CreatePatch(model)) as ApiRetweetServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiRetweetServerResponseModel> result = await this.RetweetService.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 <IRetweetService, IRetweetRepository>(); var model = new ApiRetweetServerRequestModel(); var validatorMock = new Mock <IApiRetweetServerRequestModelValidator>(); 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 RetweetService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, 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 <RetweetDeletedNotification>(), It.IsAny <CancellationToken>()), Times.Never()); }
public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiRetweetServerRequestModel> patch) { ApiRetweetServerResponseModel record = await this.RetweetService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiRetweetServerRequestModel model = await this.PatchModel(id, patch) as ApiRetweetServerRequestModel; UpdateResponse <ApiRetweetServerResponseModel> result = await this.RetweetService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }