public void Should_return_ok_with_count_equal_to_one_when_single_device_exists() { var parameters = new DevicesParametersModel(); //Arrange _deviceApiService.Setup(x => x.GetDevicesCount(null, null, String.Empty)).Returns(1); //Act var result = _devicesController.GetDevicesCount(parameters).GetAwaiter().GetResult(); //Assert Assert.IsInstanceOf <OkObjectResult>(result); var okObjectResult = result as OkObjectResult; Assert.NotNull(okObjectResult); Assert.IsInstanceOf <DevicesCountRootObject>(okObjectResult.Value); var rootObject = okObjectResult.Value as DevicesCountRootObject; Assert.NotNull(rootObject); rootObject.Count.ShouldEqual(1); }
public async Task <IActionResult> GetDevices(DevicesParametersModel parameters) { if (parameters.Limit < Configurations.MinLimit || parameters.Limit > Configurations.MaxLimit) { return(await Error(HttpStatusCode.BadRequest, "limit", "Invalid limit parameter")); } if (parameters.Page < Configurations.DefaultPageValue) { return(await Error(HttpStatusCode.BadRequest, "page", "Invalid request parameters")); } var devices = _deviceApiService.GetDevices(parameters.Ids, parameters.Limit, parameters.Page, parameters.SinceId, parameters.CreatedAtMin, parameters.CreatedAtMax, parameters.Status) .Where(d => TenantMappingService.Authorize(d)); IList <DeviceDto> devicesAsDtos = devices.Select(device => _dtoHelper.PrepareDeviceDto(device)).ToList(); var devicesRootObject = new DevicesRootObject { Devices = devicesAsDtos }; var json = JsonFieldsSerializer.Serialize(devicesRootObject, parameters.Fields); return(new RawJsonActionResult(json)); }
public async Task <IActionResult> GetDevicesCount(DevicesParametersModel parameters) { var devicesCount = _deviceApiService.GetDevicesCount(parameters.CreatedAtMin, parameters.CreatedAtMax, parameters.Status); var devicesCountRootObject = new DevicesCountRootObject() { Count = devicesCount }; return(await Task.FromResult <IActionResult>(Ok(devicesCountRootObject))); }
public void Should_call_serializer_with_no_devices_when_no_devices_exist() { //Arrange var returnedDevicesList = new List <Device>(); var parameters = new DevicesParametersModel(); _deviceApiService.Setup(x => x.GetDevices(parameters.Ids, parameters.Limit, parameters.Page, parameters.SinceId, parameters.CreatedAtMin, parameters.CreatedAtMax, parameters.Status)).Returns(returnedDevicesList); //Act _devicesController.GetDevices(parameters).GetAwaiter().GetResult(); //Assert _jsonFieldsSerializer.Verify(x => x.Serialize(It.Is <DevicesRootObject>(d => d.Devices.Count == returnedDevicesList.Count), It.IsIn(parameters.Fields), null)); }
public void Should_return_bad_request_when_invalid_page_passed(int invalidPage) { //Arrange var returnedDevicesList = new List <Device>(); var parameters = new DevicesParametersModel { Page = invalidPage }; _deviceApiService.Setup(x => x.GetDevices(parameters.Ids, parameters.Limit, parameters.Page, parameters.SinceId, parameters.CreatedAtMin, parameters.CreatedAtMax, parameters.Status)).Returns(returnedDevicesList); _jsonFieldsSerializer.Setup(x => x.Serialize(It.IsAny <DevicesRootObject>(), It.IsAny <string>(), null)) .Returns(String.Empty); //Act var result = _devicesController.GetDevices(parameters).GetAwaiter().GetResult(); var statusCode = ActionResultExecutor.ExecuteResult(result); //Assert Assert.AreEqual(HttpStatusCode.BadRequest, statusCode); }
public void Should_call_service_with_same_parameters_when_valid_parameters_passed() { //Arrange var returnedDevicesList = new List <Device>(); var parameters = new DevicesParametersModel { SinceId = Configurations.DefaultSinceId + 1, // some different than default since id CreatedAtMin = DateTime.Now, CreatedAtMax = DateTime.Now, Page = Configurations.DefaultPageValue + 1, // some different than default page Limit = Configurations.MinLimit + 1, // some different than default limit }; _deviceApiService.Setup(x => x.GetDevices(null, parameters.Limit, parameters.Page, parameters.SinceId, parameters.CreatedAtMin, parameters.CreatedAtMax, string.Empty)).Returns(returnedDevicesList); //Act _devicesController.GetDevices(parameters).GetAwaiter().GetResult(); //Assert _deviceApiService.Verify(x => x.GetDevices(null, parameters.Limit, parameters.Page, parameters.SinceId, parameters.CreatedAtMin, parameters.CreatedAtMax, string.Empty)); }