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; ICustomerService service = testServer.Host.Services.GetService(typeof(ICustomerService)) as ICustomerService; var model = new ApiCustomerServerRequestModel(); model.SetProperties("B", "B", "B", "B"); CreateResponse <ApiCustomerServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.CustomerDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiCustomerServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public async virtual Task <IActionResult> Get(int id) { ApiCustomerServerResponseModel response = await this.CustomerService.Get(id); if (response == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { return(this.Ok(response)); } }
public void MapServerResponseToRequest() { var mapper = new ApiCustomerServerModelMapper(); var model = new ApiCustomerServerResponseModel(); model.SetProperties(1, "A", "A", "A", "A"); ApiCustomerServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.Email.Should().Be("A"); response.FirstName.Should().Be("A"); response.LastName.Should().Be("A"); response.Phone.Should().Be("A"); }
public async void Get_ShouldReturnNullBecauseRecordNotFound() { var mock = new ServiceMockFacade <ICustomerService, ICustomerRepository>(); mock.RepositoryMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult <Customer>(null)); var service = new CustomerService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.CustomerModelValidatorMock.Object, mock.DALMapperMockFactory.DALCustomerMapperMock); ApiCustomerServerResponseModel response = await service.Get(default(int)); response.Should().BeNull(); mock.RepositoryMock.Verify(x => x.Get(It.IsAny <int>())); }
public async void Create_Errors() { CustomerControllerMockFacade mock = new CustomerControllerMockFacade(); var mockResponse = new Mock <CreateResponse <ApiCustomerServerResponseModel> >(null as ApiCustomerServerResponseModel); var mockRecord = new ApiCustomerServerResponseModel(); mockResponse.SetupGet(x => x.Success).Returns(false); mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiCustomerServerRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiCustomerServerResponseModel> >(mockResponse.Object)); CustomerController controller = new CustomerController(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 ApiCustomerServerRequestModel()); response.Should().BeOfType <ObjectResult>(); (response as ObjectResult).StatusCode.Should().Be((int)HttpStatusCode.UnprocessableEntity); mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiCustomerServerRequestModel>())); }
public async void All_Exists() { CustomerControllerMockFacade mock = new CustomerControllerMockFacade(); var record = new ApiCustomerServerResponseModel(); var records = new List <ApiCustomerServerResponseModel>(); records.Add(record); mock.ServiceMock.Setup(x => x.All(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())).Returns(Task.FromResult(records)); CustomerController controller = new CustomerController(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 <ApiCustomerServerResponseModel>; 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 ApiCustomerServerModelMapper(); ApplicationDbContext context = testServer.Host.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext; ICustomerService service = testServer.Host.Services.GetService(typeof(ICustomerService)) as ICustomerService; ApiCustomerServerResponseModel model = await service.Get(1); ApiCustomerClientRequestModel request = mapper.MapServerResponseToClientRequest(model); request.SetProperties("B", "B", "B", "B", "B"); UpdateResponse <ApiCustomerClientResponseModel> updateResponse = await client.CustomerUpdateAsync(model.Id, request); context.Entry(context.Set <Customer>().ToList()[0]).Reload(); updateResponse.Record.Should().NotBeNull(); updateResponse.Success.Should().BeTrue(); updateResponse.Record.Id.Should().Be(1); context.Set <Customer>().ToList()[0].Email.Should().Be("B"); context.Set <Customer>().ToList()[0].FirstName.Should().Be("B"); context.Set <Customer>().ToList()[0].LastName.Should().Be("B"); context.Set <Customer>().ToList()[0].Notes.Should().Be("B"); context.Set <Customer>().ToList()[0].Phone.Should().Be("B"); updateResponse.Record.Id.Should().Be(1); updateResponse.Record.Email.Should().Be("B"); updateResponse.Record.FirstName.Should().Be("B"); updateResponse.Record.LastName.Should().Be("B"); updateResponse.Record.Notes.Should().Be("B"); updateResponse.Record.Phone.Should().Be("B"); }
public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiCustomerServerRequestModel> patch) { ApiCustomerServerResponseModel record = await this.CustomerService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiCustomerServerRequestModel model = await this.PatchModel(id, patch) as ApiCustomerServerRequestModel; UpdateResponse <ApiCustomerServerResponseModel> result = await this.CustomerService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }