public virtual async void TestUpdate() { var builder = new WebHostBuilder() .UseEnvironment("Production") .UseStartup <TestStartup>(); TestServer testServer = new TestServer(builder); var client = new ApiClient(testServer.CreateClient()); client.SetBearerToken(JWTTestHelper.GenerateBearerToken()); var mapper = new ApiLinkTypeServerModelMapper(); ApplicationDbContext context = testServer.Host.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext; ILinkTypeService service = testServer.Host.Services.GetService(typeof(ILinkTypeService)) as ILinkTypeService; ApiLinkTypeServerResponseModel model = await service.Get(1); ApiLinkTypeClientRequestModel request = mapper.MapServerResponseToClientRequest(model); request.SetProperties("B"); UpdateResponse <ApiLinkTypeClientResponseModel> updateResponse = await client.LinkTypeUpdateAsync(model.Id, request); context.Entry(context.Set <LinkType>().ToList()[0]).Reload(); updateResponse.Record.Should().NotBeNull(); updateResponse.Success.Should().BeTrue(); updateResponse.Record.Id.Should().Be(1); context.Set <LinkType>().ToList()[0].RwType.Should().Be("B"); updateResponse.Record.Id.Should().Be(1); updateResponse.Record.RwType.Should().Be("B"); }
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 async virtual Task <IActionResult> Get(int id) { ApiLinkTypeServerResponseModel response = await this.LinkTypeService.Get(id); if (response == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { return(this.Ok(response)); } }
public async void Get_ShouldReturnNullBecauseRecordNotFound() { var mock = new ServiceMockFacade <ILinkTypeService, ILinkTypeRepository>(); mock.RepositoryMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult <LinkType>(null)); var service = new LinkTypeService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.LinkTypeModelValidatorMock.Object, mock.DALMapperMockFactory.DALLinkTypeMapperMock, mock.DALMapperMockFactory.DALPostLinkMapperMock); ApiLinkTypeServerResponseModel response = await service.Get(default(int)); response.Should().BeNull(); mock.RepositoryMock.Verify(x => x.Get(It.IsAny <int>())); }
public async void Create_Errors() { LinkTypeControllerMockFacade mock = new LinkTypeControllerMockFacade(); var mockResponse = new Mock <CreateResponse <ApiLinkTypeServerResponseModel> >(null as ApiLinkTypeServerResponseModel); var mockRecord = new ApiLinkTypeServerResponseModel(); mockResponse.SetupGet(x => x.Success).Returns(false); mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiLinkTypeServerRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiLinkTypeServerResponseModel> >(mockResponse.Object)); LinkTypeController controller = new LinkTypeController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); IActionResult response = await controller.Create(new ApiLinkTypeServerRequestModel()); response.Should().BeOfType <ObjectResult>(); (response as ObjectResult).StatusCode.Should().Be((int)HttpStatusCode.UnprocessableEntity); mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiLinkTypeServerRequestModel>())); }
public async void All_Exists() { LinkTypeControllerMockFacade mock = new LinkTypeControllerMockFacade(); var record = new ApiLinkTypeServerResponseModel(); var records = new List <ApiLinkTypeServerResponseModel>(); records.Add(record); mock.ServiceMock.Setup(x => x.All(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())).Returns(Task.FromResult(records)); LinkTypeController controller = new LinkTypeController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); IActionResult response = await controller.All(1000, 0, string.Empty); response.Should().BeOfType <OkObjectResult>(); (response as OkObjectResult).StatusCode.Should().Be((int)HttpStatusCode.OK); var items = (response as OkObjectResult).Value as List <ApiLinkTypeServerResponseModel>; items.Count.Should().Be(1); mock.ServiceMock.Verify(x => x.All(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())); }
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)); } } }