public async void Update_ErrorsOccurred_ShouldReturnErrorResponse() { var mock = new ServiceMockFacade <IVoteTypeService, IVoteTypeRepository>(); var model = new ApiVoteTypeServerRequestModel(); var validatorMock = new Mock <IApiVoteTypeServerRequestModelValidator>(); validatorMock.Setup(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiVoteTypeServerRequestModel>())).Returns(Task.FromResult(new ValidationResult(new List <ValidationFailure>() { new ValidationFailure("text", "test") }))); mock.RepositoryMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult(new VoteType())); var service = new VoteTypeService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALVoteTypeMapperMock, mock.DALMapperMockFactory.DALVoteMapperMock); UpdateResponse <ApiVoteTypeServerResponseModel> response = await service.Update(default(int), model); response.Should().NotBeNull(); response.Success.Should().BeFalse(); validatorMock.Verify(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiVoteTypeServerRequestModel>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <VoteTypeUpdatedNotification>(), It.IsAny <CancellationToken>()), Times.Never()); }
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; IVoteTypeService service = testServer.Host.Services.GetService(typeof(IVoteTypeService)) as IVoteTypeService; var model = new ApiVoteTypeServerRequestModel(); model.SetProperties("B"); CreateResponse <ApiVoteTypeServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.VoteTypeDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiVoteTypeServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public void MapServerResponseToRequest() { var mapper = new ApiVoteTypeServerModelMapper(); var model = new ApiVoteTypeServerResponseModel(); model.SetProperties(1, "A"); ApiVoteTypeServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.Name.Should().Be("A"); }
public void CreatePatch() { var mapper = new ApiVoteTypeServerModelMapper(); var model = new ApiVoteTypeServerRequestModel(); model.SetProperties("A"); JsonPatchDocument <ApiVoteTypeServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiVoteTypeServerRequestModel(); patch.ApplyTo(response); response.Name.Should().Be("A"); }
public virtual async Task <IActionResult> Create([FromBody] ApiVoteTypeServerRequestModel model) { CreateResponse <ApiVoteTypeServerResponseModel> result = await this.VoteTypeService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/VoteTypes/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
private async Task <ApiVoteTypeServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiVoteTypeServerRequestModel> patch) { var record = await this.VoteTypeService.Get(id); if (record == null) { return(null); } else { ApiVoteTypeServerRequestModel request = this.VoteTypeModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <IVoteTypeService, IVoteTypeRepository>(); var model = new ApiVoteTypeServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new VoteTypeService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.VoteTypeModelValidatorMock.Object, mock.DALMapperMockFactory.DALVoteTypeMapperMock, mock.DALMapperMockFactory.DALVoteMapperMock); 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.VoteTypeModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <VoteTypeDeletedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiVoteTypeServerRequestModel model) { ApiVoteTypeServerRequestModel request = await this.PatchModel(id, this.VoteTypeModelMapper.CreatePatch(model)) as ApiVoteTypeServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiVoteTypeServerResponseModel> result = await this.VoteTypeService.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 <ApiVoteTypeServerRequestModel> patch) { ApiVoteTypeServerResponseModel record = await this.VoteTypeService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiVoteTypeServerRequestModel model = await this.PatchModel(id, patch) as ApiVoteTypeServerRequestModel; UpdateResponse <ApiVoteTypeServerResponseModel> result = await this.VoteTypeService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }