示例#1
0
        public async void FindMatchingDevice_byDeviceType_AndroidAsync()
        {
            // Arrange
            var requestedDevice = new RequestedDevices
            {
                DeviceType = DeviceType.Android
            };

            var mockHttp = new MockHttpMessageHandler();

            mockHttp.When(HttpMethod.Get, $"{_httpLocalhost}/api/v1/device").Respond("application/json",
                                                                                     JsonConvert.SerializeObject(new List <Device> {
                _device1, _device2
            }));

            // Act
            var deviceUtils = new DeviceUtils(Logger, _externalProcesses);
            var restClient  = new RestClient(_config.Object, new HttpClient(mockHttp), Logger);
            var result      = await deviceUtils.FindMatchingDevice(requestedDevice, restClient);

            // Assert
            var viewResult = Assert.IsType <Device>(result);

            Assert.Equal(DeviceType.Android, viewResult.Type);
            Assert.True(viewResult.Available);
        }
示例#2
0
        /// <inheritdoc />
        /// <summary>
        /// Finds the matching device.
        /// </summary>
        /// <returns>The matching device.</returns>
        /// <param name="requestedDevice">Requested device.</param>
        /// <param name="restClient">Rest client.</param>
        public async Task <Device> FindMatchingDevice(RequestedDevices requestedDevice, IRestClient restClient)
        {
            if (requestedDevice.DeviceId != null)
            {
                return(await restClient.GetDevice(requestedDevice.DeviceId));
            }

            if (requestedDevice.DeviceType != DeviceType.Unspecified)
            {
                var allDevices = await restClient.GetDevices();

                var matchingDevices =
                    allDevices.Where((dev) => dev.Type == requestedDevice.DeviceType).ToList();
                return(SelectRandomDevice(matchingDevices));
            }

            if (!string.IsNullOrEmpty(requestedDevice.DeviceName))
            {
                var allDevices = await restClient.GetDevices();

                var matchingDevices =
                    allDevices.Where((dev) => dev.Name == requestedDevice.DeviceName).ToList();
                return(SelectRandomDevice(matchingDevices));
            }

            if (requestedDevice.Properties.Any())
            {
                var allDevices = await restClient.GetDevices();

                var matchingDevices = new List <Device>();

                foreach (var device in allDevices)
                {
                    var match = false;
                    foreach (var requestedDeviceProperty in requestedDevice.Properties)
                    {
                        if (requestedDeviceProperty.Value.Contains('*') || requestedDeviceProperty.Value.Contains('?'))
                        {
                            match = false;
                            foreach (var prop in device.Properties)
                            {
                                if (prop.Key == requestedDeviceProperty.Key)
                                {
                                    var regex = WildCardToRegular(requestedDeviceProperty.Value);
                                    if (Regex.IsMatch(prop.Value, regex))
                                    {
                                        match = true;
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            match = device.Properties.Any(prop =>
                                                          prop.Key == requestedDeviceProperty.Key && prop.Value == requestedDeviceProperty.Value);
                        }
                    }

                    if (match)
                    {
                        matchingDevices.Add(device);
                    }
                }

                return(SelectRandomDevice(matchingDevices));
            }

            return(null);
        }