public void MetadataApiFactory_CreateMetadataApi_Creates_Instance()
        {
            var configuration = new ConfigurationBuilder()
                                .AddInMemoryCollection(inMemoryConfig)
                                .Build();

            // Arrange
            MetadataApiFactory target = new MetadataApiFactory(configuration);

            Uri expectedUri;

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

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

            // Assert
            Assert.NotNull(result);
            Assert.IsType(typeof(MetadataApi), result);
            Assert.Equal(expectedUri, result.ServiceUri);
            Assert.Equal("AuthToken", result.Token);
        }
        public void MetadataApiFactory_CreateMetadataApi_Throws_If_No_Token_Configured()
        {
            // Arrange
            var configuration = new ConfigurationBuilder()
                                .AddInMemoryCollection(inMemoryConfigEmpty)
                                .Build();

            var target = new MetadataApiFactory(configuration);

            // Act and Assert
            Assert.Throws <InvalidOperationException>(() => target.CreateMetadataApi());
        }
        public static void MetadataApiFactory_GetAppSetting_Reads_Settings_Correctly_From_Environment_Variable()
        {
            // Arrange
            var configuration = new ConfigurationBuilder()
                                .AddInMemoryCollection(new Dictionary <string, string> {
                { "appSettings:token", "AuthToken" }
            })
                                .Build();

            MetadataApiFactory target = new MetadataApiFactory(configuration);

            Environment.SetEnvironmentVariable("EDQ_ElectronicUpdates_foo", "bar");

            // Act and Assert
            Assert.Equal("bar", target.GetConfigSetting("foo"));
        }
        public void MetadataApiFactory_CreateMetadataApi_Creates_Instance_If_Invalid_Service_Uri_Configured()
        {
            // Arrange
            var configuration = new ConfigurationBuilder()
                                .AddInMemoryCollection(inMemoryConfig)
                                .Build();

            // Arrange
            MetadataApiFactory target = new MetadataApiFactory(configuration);

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

            // Assert
            Assert.NotNull(result);
            Assert.IsType(typeof(MetadataApi), result);
            Assert.Equal(new Uri("https://ws.updates.qas.com/metadata/V2/"), result.ServiceUri);
            Assert.Equal("AuthToken", result.Token);
        }
Exemple #5
0
        private static MetadataApi CreateAuthorizedService()
        {
            MetadataApiFactory factory = new MetadataApiFactory(Program.GetConfiguration());

            return(factory.CreateMetadataApi() as MetadataApi);
        }
Exemple #6
0
        /// <summary>
        /// Downloads the available data files from the 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>
        public static async Task MainAsync()
        {
            PrintBanner();

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

            // Get the configuration settings for downloading files
            string downloadRootPath      = factory.GetConfigSetting("downloadRootPath");
            string verifyDownloadsString = factory.GetConfigSetting("validateDownloads");

            bool verifyDownloads;

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

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

            downloadRootPath = Path.GetFullPath(downloadRootPath);

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

            // Query the packages available to the account
            List <PackageGroup> response = await service.GetAvailablePackagesAsync();

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

            // Enumerate the package groups and list their packages and files
            if (response != null && response.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)
                            {
                                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();
                }
            }
        }