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; ITagService service = testServer.Host.Services.GetService(typeof(ITagService)) as ITagService; var model = new ApiTagServerRequestModel(); model.SetProperties(2, 1, "B", 1); CreateResponse <ApiTagServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.TagDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiTagServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public async virtual Task <IActionResult> Get(int id) { ApiTagServerResponseModel response = await this.TagService.Get(id); if (response == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { return(this.Ok(response)); } }
public void MapServerResponseToRequest() { var mapper = new ApiTagServerModelMapper(); var model = new ApiTagServerResponseModel(); model.SetProperties(1, 1, 1, "A", 1); ApiTagServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.Count.Should().Be(1); response.ExcerptPostId.Should().Be(1); response.TagName.Should().Be("A"); response.WikiPostId.Should().Be(1); }
public async void Get_ShouldReturnNullBecauseRecordNotFound() { var mock = new ServiceMockFacade <ITagService, ITagRepository>(); mock.RepositoryMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult <Tag>(null)); var service = new TagService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.TagModelValidatorMock.Object, mock.DALMapperMockFactory.DALTagMapperMock); ApiTagServerResponseModel response = await service.Get(default(int)); response.Should().BeNull(); mock.RepositoryMock.Verify(x => x.Get(It.IsAny <int>())); }
public async void Create_Errors() { TagControllerMockFacade mock = new TagControllerMockFacade(); var mockResponse = new Mock <CreateResponse <ApiTagServerResponseModel> >(null as ApiTagServerResponseModel); var mockRecord = new ApiTagServerResponseModel(); mockResponse.SetupGet(x => x.Success).Returns(false); mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiTagServerRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiTagServerResponseModel> >(mockResponse.Object)); TagController controller = new TagController(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 ApiTagServerRequestModel()); response.Should().BeOfType <ObjectResult>(); (response as ObjectResult).StatusCode.Should().Be((int)HttpStatusCode.UnprocessableEntity); mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiTagServerRequestModel>())); }
public async void All_Exists() { TagControllerMockFacade mock = new TagControllerMockFacade(); var record = new ApiTagServerResponseModel(); var records = new List <ApiTagServerResponseModel>(); records.Add(record); mock.ServiceMock.Setup(x => x.All(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())).Returns(Task.FromResult(records)); TagController controller = new TagController(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 <ApiTagServerResponseModel>; items.Count.Should().Be(1); mock.ServiceMock.Verify(x => x.All(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())); }
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 ApiTagServerModelMapper(); ApplicationDbContext context = testServer.Host.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext; ITagService service = testServer.Host.Services.GetService(typeof(ITagService)) as ITagService; ApiTagServerResponseModel model = await service.Get(1); ApiTagClientRequestModel request = mapper.MapServerResponseToClientRequest(model); request.SetProperties(2, 1, "B", 1); UpdateResponse <ApiTagClientResponseModel> updateResponse = await client.TagUpdateAsync(model.Id, request); context.Entry(context.Set <Tag>().ToList()[0]).Reload(); updateResponse.Record.Should().NotBeNull(); updateResponse.Success.Should().BeTrue(); updateResponse.Record.Id.Should().Be(1); context.Set <Tag>().ToList()[0].Count.Should().Be(2); context.Set <Tag>().ToList()[0].ExcerptPostId.Should().Be(1); context.Set <Tag>().ToList()[0].TagName.Should().Be("B"); context.Set <Tag>().ToList()[0].WikiPostId.Should().Be(1); updateResponse.Record.Id.Should().Be(1); updateResponse.Record.Count.Should().Be(2); updateResponse.Record.ExcerptPostId.Should().Be(1); updateResponse.Record.TagName.Should().Be("B"); updateResponse.Record.WikiPostId.Should().Be(1); }
public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiTagServerRequestModel> patch) { ApiTagServerResponseModel record = await this.TagService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiTagServerRequestModel model = await this.PatchModel(id, patch) as ApiTagServerRequestModel; UpdateResponse <ApiTagServerResponseModel> result = await this.TagService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }