public async Task TestCheckServicesStatusAsyncSinglePage()
        {
            var responses = new[]
            {
                new ListServicesResponse
                {
                    Services = new List <ManagedService>
                    {
                        new ManagedService {
                            ServiceName = Service1
                        },
                        new ManagedService {
                            ServiceName = Service3
                        }
                    }
                }
            };
            ServiceManagementService service = GetMockedService(
                (ServiceManagementService s) => s.Services,
                t => t.List(),
                responses);

            var dataSource    = new ServiceManagementDataSource(service, ProjectName);
            var actualResults = (await dataSource.CheckServicesStatusAsync(new[] { Service1, Service2, Service3 })).ToList();

            var expectedResults = new[]
            {
                new ServiceStatus(Service1, true),
                new ServiceStatus(Service2, false),
                new ServiceStatus(Service3, true)
            };

            Assert.AreEqual(expectedResults.Length, actualResults.Count);
            CollectionAssert.AreEqual(expectedResults, actualResults);
        }
        /// <summary>
        /// This method will enable the list of services given.
        /// </summary>
        /// <param name="serviceNames">The list of services to enable.</param>
        /// <returns>A task that will be completed once the operation finishes.</returns>
        public async Task EnableServicesAsync(IEnumerable <string> serviceNames)
        {
            ServiceManagementDataSource dataSource = _dataSource.Value;

            if (dataSource == null)
            {
                return;
            }

            try
            {
                await ProgressDialogWindow.PromptUser(
                    dataSource.EnableAllServicesAsync(serviceNames),
                    new ProgressDialogWindow.Options
                {
                    Title         = Resources.ApiManagerEnableServicesTitle,
                    Message       = Resources.ApiManagerEnableServicesProgressMessage,
                    IsCancellable = false
                });
            }
            catch (DataSourceException ex)
            {
                UserPromptUtils.ErrorPrompt(
                    message: Resources.ApiManagerEnableServicesErrorMessage,
                    title: Resources.UiErrorCaption,
                    errorDetails: ex.Message);
            }
        }
        public void BeforeEach()
        {
            _serviceMock      = new Mock <ServiceManagementService>();
            _servicesResource = _serviceMock.Resource(s => s.Services);

            _objectUnderTest = new ServiceManagementDataSource(_serviceMock.Object, ProjectName);
        }
        /// <summary>
        /// This method will check that all given services are enabled and if not will prompt the user to enable the
        /// necessary services.
        /// </summary>
        /// <param name="serviceNames">The services to check.</param>
        /// <param name="prompt">The prompt to use in the prompt dialog to ask the user for permission to enable the services.</param>
        /// <returns>A task that will be true if all services where enabled, false if the user cancelled or if the operation failed.</returns>
        public async Task <bool> EnsureAllServicesEnabledAsync(
            IEnumerable <string> serviceNames,
            string prompt)
        {
            ServiceManagementDataSource dataSource = _dataSource.Value;

            if (dataSource == null)
            {
                return(false);
            }

            try
            {
                // Check all services in parallel.
                IList <string> servicesToEnable = (await dataSource.CheckServicesStatusAsync(serviceNames))
                                                  .Where(x => !x.Enabled)
                                                  .Select(x => x.Name)
                                                  .ToList();
                if (servicesToEnable.Count == 0)
                {
                    Debug.WriteLine("All the services are already enabled.");
                    return(true);
                }

                // Need to enable the services, prompt the user.
                Debug.WriteLine($"Need to enable the services: {string.Join(",", servicesToEnable)}.");
                if (!UserPromptUtils.ActionPrompt(
                        prompt: prompt,
                        title: Resources.ApiManagerEnableServicesTitle,
                        actionCaption: Resources.UiEnableButtonCaption))
                {
                    return(false);
                }

                // Enable all services in parallel.
                await ProgressDialogWindow.PromptUser(
                    dataSource.EnableAllServicesAsync(servicesToEnable),
                    new ProgressDialogWindow.Options
                {
                    Title         = Resources.ApiManagerEnableServicesTitle,
                    Message       = Resources.ApiManagerEnableServicesProgressMessage,
                    IsCancellable = false
                });

                return(true);
            }
            catch (DataSourceException ex)
            {
                UserPromptUtils.ErrorPrompt(
                    message: Resources.ApiManagerEnableServicesErrorMessage,
                    title: Resources.UiErrorCaption,
                    errorDetails: ex.Message);
                return(false);
            }
        }
        /// <summary>
        /// This method will check that all of the given service names are enabled.
        /// </summary>
        /// <param name="serviceNames">The list of services to check.</param>
        /// <returns>A task that will be true if all services are enabled, false otherwise.</returns>
        public async Task <bool> AreServicesEnabledAsync(IList <string> serviceNames)
        {
            if (serviceNames == null || serviceNames.Count == 0)
            {
                return(true);
            }

            ServiceManagementDataSource dataSource = _dataSource.Value;

            if (dataSource == null)
            {
                return(false);
            }

            IEnumerable <ServiceStatus> serviceStatus = await dataSource.CheckServicesStatusAsync(serviceNames);

            return(serviceStatus.All(x => x.Enabled));
        }