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 ApiDeviceActionServerModelMapper(); ApplicationDbContext context = testServer.Host.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext; IDeviceActionService service = testServer.Host.Services.GetService(typeof(IDeviceActionService)) as IDeviceActionService; ApiDeviceActionServerResponseModel model = await service.Get(1); ApiDeviceActionClientRequestModel request = mapper.MapServerResponseToClientRequest(model); request.SetProperties("B", 1, "B"); UpdateResponse <ApiDeviceActionClientResponseModel> updateResponse = await client.DeviceActionUpdateAsync(model.Id, request); context.Entry(context.Set <DeviceAction>().ToList()[0]).Reload(); updateResponse.Record.Should().NotBeNull(); updateResponse.Success.Should().BeTrue(); updateResponse.Record.Id.Should().Be(1); context.Set <DeviceAction>().ToList()[0].Action.Should().Be("B"); context.Set <DeviceAction>().ToList()[0].DeviceId.Should().Be(1); context.Set <DeviceAction>().ToList()[0].Name.Should().Be("B"); updateResponse.Record.Id.Should().Be(1); updateResponse.Record.Action.Should().Be("B"); updateResponse.Record.DeviceId.Should().Be(1); updateResponse.Record.Name.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; IDeviceActionService service = testServer.Host.Services.GetService(typeof(IDeviceActionService)) as IDeviceActionService; var model = new ApiDeviceActionServerRequestModel(); model.SetProperties("B", 1, "B"); CreateResponse <ApiDeviceActionServerResponseModel> createdResponse = await service.Create(model); createdResponse.Success.Should().BeTrue(); ActionResponse deleteResult = await client.DeviceActionDeleteAsync(2); deleteResult.Success.Should().BeTrue(); ApiDeviceActionServerResponseModel verifyResponse = await service.Get(2); verifyResponse.Should().BeNull(); }
public void MapServerResponseToRequest() { var mapper = new ApiDeviceActionServerModelMapper(); var model = new ApiDeviceActionServerResponseModel(); model.SetProperties(1, "A", 1, "A"); ApiDeviceActionServerRequestModel response = mapper.MapServerResponseToRequest(model); response.Should().NotBeNull(); response.Action.Should().Be("A"); response.DeviceId.Should().Be(1); response.Name.Should().Be("A"); }
public async virtual Task <IActionResult> Get(int id) { ApiDeviceActionServerResponseModel response = await this.DeviceActionService.Get(id); if (response == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { return(this.Ok(response)); } }
public async void Get_ShouldReturnNullBecauseRecordNotFound() { var mock = new ServiceMockFacade <IDeviceActionService, IDeviceActionRepository>(); mock.RepositoryMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult <DeviceAction>(null)); var service = new DeviceActionService(mock.LoggerMock.Object, mock.MediatorMock.Object, mock.RepositoryMock.Object, mock.ModelValidatorMockFactory.DeviceActionModelValidatorMock.Object, mock.DALMapperMockFactory.DALDeviceActionMapperMock); ApiDeviceActionServerResponseModel response = await service.Get(default(int)); response.Should().BeNull(); mock.RepositoryMock.Verify(x => x.Get(It.IsAny <int>())); }
public async void Create_Errors() { DeviceActionControllerMockFacade mock = new DeviceActionControllerMockFacade(); var mockResponse = new Mock <CreateResponse <ApiDeviceActionServerResponseModel> >(null as ApiDeviceActionServerResponseModel); var mockRecord = new ApiDeviceActionServerResponseModel(); mockResponse.SetupGet(x => x.Success).Returns(false); mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiDeviceActionServerRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiDeviceActionServerResponseModel> >(mockResponse.Object)); DeviceActionController controller = new DeviceActionController(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 ApiDeviceActionServerRequestModel()); response.Should().BeOfType <ObjectResult>(); (response as ObjectResult).StatusCode.Should().Be((int)HttpStatusCode.UnprocessableEntity); mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiDeviceActionServerRequestModel>())); }
public async void All_Exists() { DeviceActionControllerMockFacade mock = new DeviceActionControllerMockFacade(); var record = new ApiDeviceActionServerResponseModel(); var records = new List <ApiDeviceActionServerResponseModel>(); records.Add(record); mock.ServiceMock.Setup(x => x.All(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())).Returns(Task.FromResult(records)); DeviceActionController controller = new DeviceActionController(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 <ApiDeviceActionServerResponseModel>; 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 <ApiDeviceActionServerRequestModel> patch) { ApiDeviceActionServerResponseModel record = await this.DeviceActionService.Get(id); if (record == null) { return(this.StatusCode(StatusCodes.Status404NotFound)); } else { ApiDeviceActionServerRequestModel model = await this.PatchModel(id, patch) as ApiDeviceActionServerRequestModel; UpdateResponse <ApiDeviceActionServerResponseModel> result = await this.DeviceActionService.Update(id, model); if (result.Success) { return(this.Ok(result)); } else { return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result)); } } }