Exemple #1
0
        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);
        }
Exemple #2
0
        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));
        }
Exemple #3
0
        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)));
        }
Exemple #4
0
        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));
        }
Exemple #5
0
        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);
        }
Exemple #6
0
        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));
        }