Ejemplo n.º 1
0
        public static void MetadataApiFactory_CreateMetadataApi_Creates_Instance()
        {
            // Arrange
            Mock <MetadataApiFactory> mock = new Mock <MetadataApiFactory>();

            mock.CallBase = true;
            mock.Setup((p) => p.GetConfigSetting("UserName")).Returns("MyUserName");
            mock.Setup((p) => p.GetConfigSetting("Password")).Returns("MyPassword");

            Uri expectedUri;

            if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("QAS_ElectronicUpdates_ServiceUri")))
            {
                expectedUri = new Uri(Environment.GetEnvironmentVariable("QAS_ElectronicUpdates_ServiceUri"));
            }
            else
            {
                expectedUri = new Uri("https://ws.updates.qas.com/metadata/V1/");
            }

            MetadataApiFactory target = mock.Object;

            // Act
            IMetadataApi result = target.CreateMetadataApi();

            // Assert
            Assert.NotNull(result);
            Assert.IsType(typeof(MetadataApi), result);
            Assert.Equal(expectedUri, result.ServiceUri);
            Assert.Equal("MyUserName", result.UserName);
        }
Ejemplo n.º 2
0
        public static void MetadataApiFactory_GetAppSetting_Reads_Settings_Correctly_From_Environment_Variable()
        {
            // Arrange
            Environment.SetEnvironmentVariable("QAS_ElectronicUpdates_foo", "bar");

            // Act and Assert
            Assert.Equal("bar", MetadataApiFactory.GetAppSetting("foo"));
        }
Ejemplo n.º 3
0
        public static void MetadataApiFactory_CreateMetadataApi_Throws_If_No_User_Name_Configured()
        {
            // Arrange
            Mock <MetadataApiFactory> mock = new Mock <MetadataApiFactory>();

            mock.CallBase = true;
            mock.Setup((p) => p.GetConfigSetting("UserName")).Returns(string.Empty);
            mock.Setup((p) => p.GetConfigSetting("Password")).Returns("MyPassword");

            MetadataApiFactory target = mock.Object;

            // Act and Assert
            Assert.Throws <ConfigurationErrorsException>(() => target.CreateMetadataApi());
        }
Ejemplo n.º 4
0
        public static void MetadataApiFactory_CreateMetadataApi_Creates_Instance_If_Invalid_Service_Uri_Configured()
        {
            // Arrange
            Mock <MetadataApiFactory> mock = new Mock <MetadataApiFactory>();

            mock.CallBase = true;
            mock.Setup((p) => p.GetConfigSetting("ServiceUri")).Returns("NotAUri");
            mock.Setup((p) => p.GetConfigSetting("UserName")).Returns("MyUserName");
            mock.Setup((p) => p.GetConfigSetting("Password")).Returns("MyPassword");

            MetadataApiFactory target = mock.Object;

            // Act
            IMetadataApi result = target.CreateMetadataApi();

            // Assert
            Assert.NotNull(result);
            Assert.IsType(typeof(MetadataApi), result);
            Assert.Equal(new Uri("https://ws.updates.qas.com/metadata/V1/"), result.ServiceUri);
            Assert.Equal("MyUserName", result.UserName);
        }
        private static MetadataApi CreateAuthorizedService()
        {
            MetadataApiFactory factory = new MetadataApiFactory();

            return(factory.CreateMetadataApi() as MetadataApi);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Downloads the available data files from the QAS Electronic Updates Metadata REST API as an asynchronous operation.
        /// </summary>
        /// <returns>
        /// A <see cref="Task"/> representing the asynchronous operation to download any data files.
        /// </returns>
        internal static async Task MainAsync()
        {
            PrintBanner();

            // Get the configuration settings for downloading files
            string downloadRootPath      = MetadataApiFactory.GetAppSetting("DownloadRootPath");
            string verifyDownloadsString = MetadataApiFactory.GetAppSetting("ValidateDownloads");

            bool verifyDownloads;

            if (!bool.TryParse(verifyDownloadsString, out verifyDownloads))
            {
                verifyDownloads = true;
            }

            if (string.IsNullOrEmpty(downloadRootPath))
            {
                downloadRootPath = "QASData";
            }

            downloadRootPath = Path.GetFullPath(downloadRootPath);

            // Create the service implementation
            IMetadataApiFactory factory = new MetadataApiFactory();
            IMetadataApi        service = factory.CreateMetadataApi();

            Console.WriteLine("QAS Electronic Updates Metadata REST API: {0}", service.ServiceUri);
            Console.WriteLine();
            Console.WriteLine("User Name: {0}", service.UserName);
            Console.WriteLine();

            // Query the packages available to the account
            AvailablePackagesReply response = await service.GetAvailablePackagesAsync();

            Console.WriteLine("Available Package Groups:");
            Console.WriteLine();

            // Enumerate the package groups and list their packages and files
            if (response.PackageGroups != null && response.PackageGroups.Count > 0)
            {
                using (CancellationTokenSource tokenSource = new CancellationTokenSource())
                {
                    // Cancel the tasks if Ctrl+C is entered to the console
                    Console.CancelKeyPress += (sender, e) =>
                    {
                        if (!tokenSource.IsCancellationRequested)
                        {
                            tokenSource.Cancel();
                        }

                        e.Cancel = true;
                    };

                    try
                    {
                        Stopwatch stopwatch = Stopwatch.StartNew();

                        // Create a file store in which to cache information about which files
                        // have already been downloaded from the Metadata API service.
                        using (IFileStore fileStore = new LocalFileStore())
                        {
                            foreach (PackageGroup group in response.PackageGroups)
                            {
                                Console.WriteLine("Group Name: {0} ({1})", group.PackageGroupCode, group.Vintage);
                                Console.WriteLine();
                                Console.WriteLine("Packages:");
                                Console.WriteLine();

                                foreach (Package package in group.Packages)
                                {
                                    Console.WriteLine("Package Name: {0}", package.PackageCode);
                                    Console.WriteLine();
                                    Console.WriteLine("Files:");
                                    Console.WriteLine();

                                    foreach (DataFile file in package.Files)
                                    {
                                        if (fileStore.ContainsFile(file.MD5Hash))
                                        {
                                            // We already have this file, download not required
                                            Console.WriteLine("File with hash '{0}' already downloaded.", file.MD5Hash);
                                        }
                                        else
                                        {
                                            // Download the file
                                            await DownloadFileAsync(
                                                service,
                                                fileStore,
                                                group,
                                                file,
                                                downloadRootPath,
                                                verifyDownloads,
                                                tokenSource.Token);
                                        }
                                    }

                                    Console.WriteLine();
                                }

                                Console.WriteLine();
                            }
                        }

                        stopwatch.Stop();
                        Console.WriteLine("Downloaded data in {0:hh\\:mm\\:ss}.", stopwatch.Elapsed);
                    }
                    catch (OperationCanceledException ex)
                    {
                        // Only an error if not cancelled by the user
                        if (ex.CancellationToken != tokenSource.Token)
                        {
                            throw;
                        }

                        Console.WriteLine("File download cancelled by user.");
                    }

                    Console.WriteLine();
                }
            }
        }
Ejemplo n.º 7
0
 public static void MetadataApiFactory_GetAppSetting_Reads_Setting_Value_Correctly(string name, string value)
 {
     // Act and Assert
     Assert.Equal(value, MetadataApiFactory.GetAppSetting(name));
 }