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; IFollowerService service = testServer.Host.Services.GetService(typeof(IFollowerService)) as IFollowerService; var model = new ApiFollowerServerRequestModel(); model.SetProperties("B", DateTime.Parse("1/1/1988 12:00:00 AM"), 1, 1, "B", "B"); CreateResponse <ApiFollowerServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.FollowerDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiFollowerServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public virtual async Task <IActionResult> Create([FromBody] ApiFollowerServerRequestModel model) { CreateResponse <ApiFollowerServerResponseModel> result = await this.FollowerService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Followers/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
private async Task <ApiFollowerServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiFollowerServerRequestModel> patch) { var record = await this.FollowerService.Get(id); if (record == null) { return(null); } else { ApiFollowerServerRequestModel request = this.FollowerModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public void MapServerResponseToRequest() { var mapper = new ApiFollowerServerModelMapper(); var model = new ApiFollowerServerResponseModel(); model.SetProperties(1, "A", DateTime.Parse("1/1/1987 12:00:00 AM"), 1, 1, "A", "A"); ApiFollowerServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.Blocked.Should().Be("A"); response.DateFollowed.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.FollowedUserId.Should().Be(1); response.FollowingUserId.Should().Be(1); response.FollowRequestStatu.Should().Be("A"); response.Muted.Should().Be("A"); }
public void CreatePatch() { var mapper = new ApiFollowerServerModelMapper(); var model = new ApiFollowerServerRequestModel(); model.SetProperties("A", DateTime.Parse("1/1/1987 12:00:00 AM"), 1, 1, "A", "A"); JsonPatchDocument <ApiFollowerServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiFollowerServerRequestModel(); patch.ApplyTo(response); response.Blocked.Should().Be("A"); response.DateFollowed.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.FollowedUserId.Should().Be(1); response.FollowingUserId.Should().Be(1); response.FollowRequestStatu.Should().Be("A"); response.Muted.Should().Be("A"); }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <IFollowerService, IFollowerRepository>(); var model = new ApiFollowerServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new FollowerService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.FollowerModelValidatorMock.Object, mock.DALMapperMockFactory.DALFollowerMapperMock); 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.FollowerModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <FollowerDeletedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiFollowerServerRequestModel model) { ApiFollowerServerRequestModel request = await this.PatchModel(id, this.FollowerModelMapper.CreatePatch(model)) as ApiFollowerServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiFollowerServerResponseModel> result = await this.FollowerService.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 <IFollowerService, IFollowerRepository>(); var model = new ApiFollowerServerRequestModel(); var validatorMock = new Mock <IApiFollowerServerRequestModelValidator>(); 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 FollowerService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALFollowerMapperMock); 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 <FollowerDeletedNotification>(), It.IsAny <CancellationToken>()), Times.Never()); }
public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiFollowerServerRequestModel> patch) { ApiFollowerServerResponseModel record = await this.FollowerService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiFollowerServerRequestModel model = await this.PatchModel(id, patch) as ApiFollowerServerRequestModel; UpdateResponse <ApiFollowerServerResponseModel> result = await this.FollowerService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }