コード例 #1
0
        /// <summary>
        /// Creates a new instance of <see cref="IMetadataApi"/>.
        /// </summary>
        /// <returns>
        /// The created instance of <see cref="IMetadataApi"/>.
        /// </returns>
        /// <exception cref="ConfigurationErrorsException">
        /// The application is not configured correctly to create an instance of <see cref="IMetadataApi"/>.
        /// </exception>
        public virtual IMetadataApi CreateMetadataApi()
        {
            // Get the token to use to connect to the QAS Electronic Updates Metadata REST API
            string token = GetConfigSetting("token");

            if (string.IsNullOrEmpty(token))
            {
                throw new InvalidOperationException("The Electronic Updates authentication token has not been configured.");
            }

            // Has the REST API endpoint URI been overridden?
            string serviceUrl = GetConfigSetting("serviceUri");

            Uri serviceUri;

            if (!Uri.TryCreate(serviceUrl, UriKind.Absolute, out serviceUri))
            {
                serviceUri = new Uri("https://ws.updates.qas.com/metadata/V2/");
            }

            // Create the service implementation
            IMetadataApi service = new MetadataApi(serviceUri);

            // Set the credentials to use to authenticate with the service
            service.SetToken(token);

            return(service);
        }
コード例 #2
0
        public static async Task MetadataApi_GetAvailablePackagesAsync_Throws_MetadataApiException_If_Error_Occurs()
        {
            // Arrange
            Uri         serviceUri = new Uri("https://ws.updates.qas.com/metadata/V2/");
            MetadataApi target     = new MetadataApi(serviceUri);

            // Act and Assert - Should throw as no credentials configured
            await Assert.ThrowsAsync <MetadataApiException>(() => target.GetAvailablePackagesAsync());
        }
コード例 #3
0
        public static async Task MetadataApi_GetDownloadUriAsync_Throws_MetadataApiException_If_Error_Occurs()
        {
            // Arrange
            Uri         serviceUri = new Uri("https://ws.updates.qas.com/metadata/V2/");
            MetadataApi target     = new MetadataApi(serviceUri);

            string fileName    = "MyFile.txt";
            string fileHash    = "7039d49e15fd4e164e2c07fe76fd61a2";
            long?  startAtByte = null;
            long?  endAtByte   = null;

            // Act and Assert - Should throw as no credentials configured
            await Assert.ThrowsAsync <MetadataApiException>(() => target.GetDownloadUriAsync(fileName, fileHash, startAtByte, endAtByte));
        }
コード例 #4
0
        public static async Task MetadataApi_GetAvailablePackagesAsync_Returns_Available_Packages()
        {
            // Arrange
            MetadataApi target = CreateAuthorizedService();

            // Act
            List <PackageGroup> result = await target.GetAvailablePackagesAsync();

            // Assert
            Assert.NotNull(result);
            Assert.NotEqual(0, result.Count);
            Assert.DoesNotContain(null, result);

            Assert.All(
                result,
                (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);
                    });
                });
            });
        }
コード例 #5
0
        public static async Task MetadataApi_GetDownloadUriAsync_Returns_File_Download_Uri_Which_Downloads_Correct_File()
        {
            // Arrange
            MetadataApi target = CreateAuthorizedService();

            List <PackageGroup> packages = await target.GetAvailablePackagesAsync();

            DataFile dataFile = packages
                                .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 (HttpClient client = new HttpClient())
                {
                    using (var request = new HttpRequestMessage(HttpMethod.Get, result))
                    {
                        using (
                            Stream contentStream = await(await client.SendAsync(request)).Content.ReadAsStreamAsync(),
                            stream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None, 4096, true))
                        {
                            await contentStream.CopyToAsync(stream);
                        }
                    }
                }

                Assert.True(File.Exists(tempPath));

                byte[] hash;

                using (Stream stream = File.OpenRead(tempPath))
                {
                    Assert.Equal(dataFile.Size, stream.Length);

                    using (HashAlgorithm algorithm = MD5.Create())
                    {
                        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);
                }
            }
        }