public async void Update_ErrorsOccurred_ShouldReturnErrorResponse() { var mock = new ServiceMockFacade <ILinkTypeService, ILinkTypeRepository>(); var model = new ApiLinkTypeServerRequestModel(); var validatorMock = new Mock <IApiLinkTypeServerRequestModelValidator>(); validatorMock.Setup(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiLinkTypeServerRequestModel>())).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 LinkType())); var service = new LinkTypeService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, validatorMock.Object, mock.DALMapperMockFactory.DALLinkTypeMapperMock, mock.DALMapperMockFactory.DALPostLinkMapperMock); UpdateResponse <ApiLinkTypeServerResponseModel> response = await service.Update(default(int), model); response.Should().NotBeNull(); response.Success.Should().BeFalse(); validatorMock.Verify(x => x.ValidateUpdateAsync(It.IsAny <int>(), It.IsAny <ApiLinkTypeServerRequestModel>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <LinkTypeUpdatedNotification>(), 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; ILinkTypeService service = testServer.Host.Services.GetService(typeof(ILinkTypeService)) as ILinkTypeService; var model = new ApiLinkTypeServerRequestModel(); model.SetProperties("B"); CreateResponse <ApiLinkTypeServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.LinkTypeDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiLinkTypeServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public void MapServerResponseToRequest() { var mapper = new ApiLinkTypeServerModelMapper(); var model = new ApiLinkTypeServerResponseModel(); model.SetProperties(1, "A"); ApiLinkTypeServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.RwType.Should().Be("A"); }
public virtual async Task <IActionResult> Create([FromBody] ApiLinkTypeServerRequestModel model) { CreateResponse <ApiLinkTypeServerResponseModel> result = await this.LinkTypeService.Create(model); if (result.Success) { return(this.Created($"{this.Settings.ExternalBaseUrl}/api/LinkTypes/{result.Record.Id}", result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } }
public void CreatePatch() { var mapper = new ApiLinkTypeServerModelMapper(); var model = new ApiLinkTypeServerRequestModel(); model.SetProperties("A"); JsonPatchDocument <ApiLinkTypeServerRequestModel> patch = mapper.CreatePatch(model); var response = new ApiLinkTypeServerRequestModel(); patch.ApplyTo(response); response.RwType.Should().Be("A"); }
private async Task <ApiLinkTypeServerRequestModel> PatchModel(int id, JsonPatchDocument <ApiLinkTypeServerRequestModel> patch) { var record = await this.LinkTypeService.Get(id); if (record == null) { return(null); } else { ApiLinkTypeServerRequestModel request = this.LinkTypeModelMapper.MapServerResponseToRequest(record); patch.ApplyTo(request); return(request); } }
public async void Delete_NoErrorsOccurred_ShouldReturnResponse() { var mock = new ServiceMockFacade <ILinkTypeService, ILinkTypeRepository>(); var model = new ApiLinkTypeServerRequestModel(); mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask); var service = new LinkTypeService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.LinkTypeModelValidatorMock.Object, mock.DALMapperMockFactory.DALLinkTypeMapperMock, mock.DALMapperMockFactory.DALPostLinkMapperMock); 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.LinkTypeModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>())); mock.MediatorMock.Verify(x => x.Publish(It.IsAny <LinkTypeDeletedNotification>(), It.IsAny <CancellationToken>())); }
public virtual async Task <IActionResult> Update(int id, [FromBody] ApiLinkTypeServerRequestModel model) { ApiLinkTypeServerRequestModel request = await this.PatchModel(id, this.LinkTypeModelMapper.CreatePatch(model)) as ApiLinkTypeServerRequestModel; if (request == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { UpdateResponse <ApiLinkTypeServerResponseModel> result = await this.LinkTypeService.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 <ApiLinkTypeServerRequestModel> patch) { ApiLinkTypeServerResponseModel record = await this.LinkTypeService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiLinkTypeServerRequestModel model = await this.PatchModel(id, patch) as ApiLinkTypeServerRequestModel; UpdateResponse <ApiLinkTypeServerResponseModel> result = await this.LinkTypeService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }