public static async Task MetadataApi_GetDownloadUriAsync_Returns_File_Download_Uri_Which_Downloads_Correct_File() { // Arrange MetadataApi target = CreateAuthorizedService(); AvailablePackagesReply packages = await target.GetAvailablePackagesAsync(); DataFile dataFile = packages.PackageGroups .SelectMany((p) => p.Packages) .First() .Files .First(); // Act Uri result = await target.GetDownloadUriAsync(dataFile.FileName, dataFile.MD5Hash); // Assert Assert.NotNull(result); Assert.True(result.IsAbsoluteUri); string tempPath = Path.Combine(Path.GetTempPath(), dataFile.FileName); try { using (WebClient client = new WebClient()) { await client.DownloadFileTaskAsync(result, tempPath); } Assert.True(File.Exists(tempPath)); byte[] hash; using (Stream stream = File.OpenRead(tempPath)) { Assert.Equal(dataFile.Size, stream.Length); using (HashAlgorithm algorithm = HashAlgorithm.Create("MD5")) { hash = algorithm.ComputeHash(stream); } } string hashString = string.Concat(hash.Select((p) => p.ToString("x2", CultureInfo.InvariantCulture))); Assert.Equal(dataFile.MD5Hash, hashString); } finally { if (File.Exists(tempPath)) { File.Delete(tempPath); } } }
public static async Task MetadataApi_GetAvailablePackagesAsync_Returns_Available_Packages() { // Arrange MetadataApi target = CreateAuthorizedService(); // Act AvailablePackagesReply result = await target.GetAvailablePackagesAsync(); // Assert Assert.NotNull(result); Assert.NotNull(result.PackageGroups); Assert.NotEqual(0, result.PackageGroups.Count); Assert.DoesNotContain(null, result.PackageGroups); Assert.All( result.PackageGroups, (group) => { Assert.False(string.IsNullOrEmpty(group.PackageGroupCode)); Assert.False(string.IsNullOrEmpty(group.Vintage)); Assert.NotNull(group.Packages); Assert.NotEqual(0, group.Packages.Count); Assert.DoesNotContain(null, group.Packages); Assert.All( group.Packages, (package) => { Assert.False(string.IsNullOrEmpty(package.PackageCode)); Assert.NotNull(package.Files); Assert.NotEqual(0, package.Files.Count); Assert.DoesNotContain(null, package.Files); Assert.All( package.Files, (file) => { Assert.False(string.IsNullOrEmpty(file.FileName)); Assert.False(string.IsNullOrEmpty(file.MD5Hash)); Assert.True(file.Size > 0L); }); }); }); }
/// <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(); } } }