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; IDirectTweetService service = testServer.Host.Services.GetService(typeof(IDirectTweetService)) as IDirectTweetService; var model = new ApiDirectTweetServerRequestModel(); model.SetProperties("B", DateTime.Parse("1/1/1988 12:00:00 AM"), 1, TimeSpan.Parse("02:00:00")); CreateResponse <ApiDirectTweetServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.DirectTweetDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiDirectTweetServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public async virtual Task <IActionResult> Get(int id) { ApiDirectTweetServerResponseModel response = await this.DirectTweetService.Get(id); if (response == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { return(this.Ok(response)); } }
public void MapServerResponseToRequest() { var mapper = new ApiDirectTweetServerModelMapper(); var model = new ApiDirectTweetServerResponseModel(); model.SetProperties(1, "A", DateTime.Parse("1/1/1987 12:00:00 AM"), 1, TimeSpan.Parse("01:00:00")); ApiDirectTweetServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.Content.Should().Be("A"); response.Date.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.TaggedUserId.Should().Be(1); response.Time.Should().Be(TimeSpan.Parse("01:00:00")); }
public async void Get_ShouldReturnNullBecauseRecordNotFound() { var mock = new ServiceMockFacade <IDirectTweetService, IDirectTweetRepository>(); mock.RepositoryMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult <DirectTweet>(null)); var service = new DirectTweetService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.DirectTweetModelValidatorMock.Object, mock.DALMapperMockFactory.DALDirectTweetMapperMock); ApiDirectTweetServerResponseModel response = await service.Get(default(int)); response.Should().BeNull(); mock.RepositoryMock.Verify(x => x.Get(It.IsAny <int>())); }
public async void Create_Errors() { DirectTweetControllerMockFacade mock = new DirectTweetControllerMockFacade(); var mockResponse = new Mock <CreateResponse <ApiDirectTweetServerResponseModel> >(null as ApiDirectTweetServerResponseModel); var mockRecord = new ApiDirectTweetServerResponseModel(); mockResponse.SetupGet(x => x.Success).Returns(false); mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiDirectTweetServerRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiDirectTweetServerResponseModel> >(mockResponse.Object)); DirectTweetController controller = new DirectTweetController(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 ApiDirectTweetServerRequestModel()); response.Should().BeOfType <ObjectResult>(); (response as ObjectResult).StatusCode.Should().Be((int)HttpStatusCode.UnprocessableEntity); mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiDirectTweetServerRequestModel>())); }
public async void All_Exists() { DirectTweetControllerMockFacade mock = new DirectTweetControllerMockFacade(); var record = new ApiDirectTweetServerResponseModel(); var records = new List <ApiDirectTweetServerResponseModel>(); records.Add(record); mock.ServiceMock.Setup(x => x.All(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())).Returns(Task.FromResult(records)); DirectTweetController controller = new DirectTweetController(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 <ApiDirectTweetServerResponseModel>; 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 ApiDirectTweetServerModelMapper(); ApplicationDbContext context = testServer.Host.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext; IDirectTweetService service = testServer.Host.Services.GetService(typeof(IDirectTweetService)) as IDirectTweetService; ApiDirectTweetServerResponseModel model = await service.Get(1); ApiDirectTweetClientRequestModel request = mapper.MapServerResponseToClientRequest(model); request.SetProperties("B", DateTime.Parse("1/1/1988 12:00:00 AM"), 1, TimeSpan.Parse("02:00:00")); UpdateResponse <ApiDirectTweetClientResponseModel> updateResponse = await client.DirectTweetUpdateAsync(model.TweetId, request); context.Entry(context.Set <DirectTweet>().ToList()[0]).Reload(); updateResponse.Record.Should().NotBeNull(); updateResponse.Success.Should().BeTrue(); updateResponse.Record.TweetId.Should().Be(1); context.Set <DirectTweet>().ToList()[0].Content.Should().Be("B"); context.Set <DirectTweet>().ToList()[0].Date.Should().Be(DateTime.Parse("1/1/1988 12:00:00 AM")); context.Set <DirectTweet>().ToList()[0].TaggedUserId.Should().Be(1); context.Set <DirectTweet>().ToList()[0].Time.Should().Be(TimeSpan.Parse("02:00:00")); updateResponse.Record.TweetId.Should().Be(1); updateResponse.Record.Content.Should().Be("B"); updateResponse.Record.Date.Should().Be(DateTime.Parse("1/1/1988 12:00:00 AM")); updateResponse.Record.TaggedUserId.Should().Be(1); updateResponse.Record.Time.Should().Be(TimeSpan.Parse("02:00:00")); }
public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiDirectTweetServerRequestModel> patch) { ApiDirectTweetServerResponseModel record = await this.DirectTweetService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiDirectTweetServerRequestModel model = await this.PatchModel(id, patch) as ApiDirectTweetServerRequestModel; UpdateResponse <ApiDirectTweetServerResponseModel> result = await this.DirectTweetService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }