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; IBadgeService service = testServer.Host.Services.GetService(typeof(IBadgeService)) as IBadgeService; var model = new ApiBadgeServerRequestModel(); model.SetProperties(DateTime.Parse("1/1/1988 12:00:00 AM"), "B", 1); CreateResponse <ApiBadgeServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.BadgeDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiBadgeServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public virtual async Task <IActionResult> Create([FromBody] ApiBadgeServerRequestModel model) { CreateResponse <ApiBadgeServerResponseModel> result = await this.BadgeService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Badges/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
public void MapServerResponseToRequest() { var mapper = new ApiBadgeServerModelMapper(); var model = new ApiBadgeServerResponseModel(); model.SetProperties(1, DateTime.Parse("1/1/1987 12:00:00 AM"), "A", 1); ApiBadgeServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.Date.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.Name.Should().Be("A"); response.UserId.Should().Be(1); }
private async Task <ApiBadgeServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiBadgeServerRequestModel> patch) { var record = await this.BadgeService.Get(id); if (record == null) { return(null); } else { ApiBadgeServerRequestModel request = this.BadgeModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public void CreatePatch() { var mapper = new ApiBadgeServerModelMapper(); var model = new ApiBadgeServerRequestModel(); model.SetProperties(DateTime.Parse("1/1/1987 12:00:00 AM"), "A", 1); JsonPatchDocument <ApiBadgeServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiBadgeServerRequestModel(); patch.ApplyTo(response); response.Date.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.Name.Should().Be("A"); response.UserId.Should().Be(1); }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <IBadgeService, IBadgeRepository>(); var model = new ApiBadgeServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new BadgeService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.BadgeModelValidatorMock.Object, mock.DALMapperMockFactory.DALBadgeMapperMock); 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.BadgeModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <BadgeDeletedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiBadgeServerRequestModel model) { ApiBadgeServerRequestModel request = await this.PatchModel(id, this.BadgeModelMapper.CreatePatch(model)) as ApiBadgeServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiBadgeServerResponseModel> result = await this.BadgeService.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 <IBadgeService, IBadgeRepository>(); var model = new ApiBadgeServerRequestModel(); var validatorMock = new Mock <IApiBadgeServerRequestModelValidator>(); 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 BadgeService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALBadgeMapperMock); 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 <BadgeDeletedNotification>(), It.IsAny <CancellationToken>()), Times.Never()); }
public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiBadgeServerRequestModel> patch) { ApiBadgeServerResponseModel record = await this.BadgeService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiBadgeServerRequestModel model = await this.PatchModel(id, patch) as ApiBadgeServerRequestModel; UpdateResponse <ApiBadgeServerResponseModel> result = await this.BadgeService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }