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; INoteService service = testServer.Host.Services.GetService(typeof(INoteService)) as INoteService; var model = new ApiNoteServerRequestModel(); model.SetProperties(1, DateTime.Parse("1/1/1988 12:00:00 AM"), "B", 1); CreateResponse <ApiNoteServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.NoteDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiNoteServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public async virtual Task <IActionResult> Get(int id) { ApiNoteServerResponseModel response = await this.NoteService.Get(id); if (response == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { return(this.Ok(response)); } }
public void MapServerResponseToRequest() { var mapper = new ApiNoteServerModelMapper(); var model = new ApiNoteServerResponseModel(); model.SetProperties(1, 1, DateTime.Parse("1/1/1987 12:00:00 AM"), "A", 1); ApiNoteServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.CallId.Should().Be(1); response.DateCreated.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM")); response.NoteText.Should().Be("A"); response.OfficerId.Should().Be(1); }
public async void Get_ShouldReturnNullBecauseRecordNotFound() { var mock = new ServiceMockFacade <INoteService, INoteRepository>(); mock.RepositoryMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult <Note>(null)); var service = new NoteService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.NoteModelValidatorMock.Object, mock.DALMapperMockFactory.DALNoteMapperMock); ApiNoteServerResponseModel response = await service.Get(default(int)); response.Should().BeNull(); mock.RepositoryMock.Verify(x => x.Get(It.IsAny <int>())); }
public async void Create_Errors() { NoteControllerMockFacade mock = new NoteControllerMockFacade(); var mockResponse = new Mock <CreateResponse <ApiNoteServerResponseModel> >(null as ApiNoteServerResponseModel); var mockRecord = new ApiNoteServerResponseModel(); mockResponse.SetupGet(x => x.Success).Returns(false); mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiNoteServerRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiNoteServerResponseModel> >(mockResponse.Object)); NoteController controller = new NoteController(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 ApiNoteServerRequestModel()); response.Should().BeOfType <ObjectResult>(); (response as ObjectResult).StatusCode.Should().Be((int)HttpStatusCode.UnprocessableEntity); mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiNoteServerRequestModel>())); }
public async void All_Exists() { NoteControllerMockFacade mock = new NoteControllerMockFacade(); var record = new ApiNoteServerResponseModel(); var records = new List <ApiNoteServerResponseModel>(); records.Add(record); mock.ServiceMock.Setup(x => x.All(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())).Returns(Task.FromResult(records)); NoteController controller = new NoteController(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 <ApiNoteServerResponseModel>; 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 ApiNoteServerModelMapper(); ApplicationDbContext context = testServer.Host.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext; INoteService service = testServer.Host.Services.GetService(typeof(INoteService)) as INoteService; ApiNoteServerResponseModel model = await service.Get(1); ApiNoteClientRequestModel request = mapper.MapServerResponseToClientRequest(model); request.SetProperties(1, DateTime.Parse("1/1/1988 12:00:00 AM"), "B", 1); UpdateResponse <ApiNoteClientResponseModel> updateResponse = await client.NoteUpdateAsync(model.Id, request); context.Entry(context.Set <Note>().ToList()[0]).Reload(); updateResponse.Record.Should().NotBeNull(); updateResponse.Success.Should().BeTrue(); updateResponse.Record.Id.Should().Be(1); context.Set <Note>().ToList()[0].CallId.Should().Be(1); context.Set <Note>().ToList()[0].DateCreated.Should().Be(DateTime.Parse("1/1/1988 12:00:00 AM")); context.Set <Note>().ToList()[0].NoteText.Should().Be("B"); context.Set <Note>().ToList()[0].OfficerId.Should().Be(1); updateResponse.Record.Id.Should().Be(1); updateResponse.Record.CallId.Should().Be(1); updateResponse.Record.DateCreated.Should().Be(DateTime.Parse("1/1/1988 12:00:00 AM")); updateResponse.Record.NoteText.Should().Be("B"); updateResponse.Record.OfficerId.Should().Be(1); }
public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiNoteServerRequestModel> patch) { ApiNoteServerResponseModel record = await this.NoteService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiNoteServerRequestModel model = await this.PatchModel(id, patch) as ApiNoteServerRequestModel; UpdateResponse <ApiNoteServerResponseModel> result = await this.NoteService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }