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);
        }
Example #2
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);
        }
Example #3
0
        public IoTDeviceRestfulDataEnricher(ITelemetryClient telemetryClient, IConfiguration config, ILogger <IoTDeviceRestfulDataEnricher> logger)
        {
            this.telemetryClient = telemetryClient;
            this.config          = config;
            this.logger          = logger;

            client = RestService.For <IMetadataApi>(config.GetValue <string>("REST_SERVICE_URL"));
        }
        /// <summary>
        /// Gets the download <see cref="Uri"/> for the specified file as an asynchronous operation.
        /// </summary>
        /// <param name="value">The <see cref="IMetadataApi"/> to get the download URI.</param>
        /// <param name="fileName">The name of the file to download.</param>
        /// <param name="fileHash">The hash of the file to download.</param>
        /// <returns>
        /// A <see cref="Task{T}"/> containing the <see cref="Uri"/> to download the file specified by
        /// <paramref name="fileName"/> and <paramref name="fileHash"/> from as an asynchronous operation.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="value"/> is <see langword="null"/>.
        /// </exception>
        public static async Task <Uri> GetDownloadUriAsync(this IMetadataApi value, string fileName, string fileHash)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            return(await value.GetDownloadUriAsync(fileName, fileHash, null, null));
        }
Example #5
0
 public MongoCollectionInitializer(IDocumentStorageManager documentStorageManager,
                                   IJsonObjectSerializer jsonObjectSerializer,
                                   IMetadataApi metadataApi,
                                   ILog log) : base(1)
 {
     _documentStorageManager = documentStorageManager;
     _jsonObjectSerializer   = jsonObjectSerializer;
     _metadataApi            = metadataApi;
     _log = log;
 }
        public static async Task IMetadataApiExtensions_GetDownloadUriAsync_Throws_If_Value_Is_Null()
        {
            // Arrange
            IMetadataApi value = null;

            string fileName = "MyFileName.dts";
            string fileHash = "58b653e3762e8048995e00024a512c53";

            // Act and Assert
            await Assert.ThrowsAsync <ArgumentNullException>("value", () => value.GetDownloadUriAsync(fileName, fileHash));
        }
Example #7
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, methods: new string[] { "GET", "PUT", "OPTIONS" })] HttpRequestMessage req,
            TraceWriter log,
            [Inject(typeof(IMetadataApi))] IMetadataApi metadataApi)
        {
            var httpMethods = new Dictionary <string, Func <HttpRequestMessage, TraceWriter, Task <HttpResponseMessage> > >
            {
                { "GET", async(r, l) => await metadataApi.Get(r, l) },
                { "PUT", async(r, l) => await metadataApi.Put(r, l) },
            };

            var response = httpMethods.ContainsKey(req.Method.Method) ? await httpMethods[req.Method.Method](req, log)
                : req.CreateResponse(req.Method.Method == "OPTIONS" ? HttpStatusCode.OK : HttpStatusCode.NotFound);

            AddCORSHeader(req, response, "GET, PUT, OPTIONS");

            return(response);
        }
        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);
        }
Example #9
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);
        }
        public static async Task IMetadataApiExtensions_GetDownloadUriAsync_Calls_Interface_Method_With_No_File_Range()
        {
            // Arrange
            string fileName = "MyFileName.dts";
            string fileHash = "58b653e3762e8048995e00024a512c53";

            Uri uri = new Uri("https://dn.updates.qas.com/MyFile.dts");

            Mock <IMetadataApi> mock = new Mock <IMetadataApi>();

            mock.Setup((p) => p.GetDownloadUriAsync(fileName, fileHash, null, null))
            .ReturnsAsync(uri)
            .Verifiable();

            IMetadataApi value = mock.Object;

            // Act and Assert
            Uri result = await value.GetDownloadUriAsync(fileName, fileHash);

            mock.Verify();
            Assert.Same(uri, result);
        }
        /// <summary>
        ///     Конструктор
        /// </summary>
        /// <param name="metadataApi">Провайдер метаданных сервисной части</param>
        /// <param name="schema">Схема данных документа, к которому выполняется запрос</param>
        public QueryCriteriaAnalyzer(IMetadataApi metadataApi, dynamic schema)
        {
            var metadataIterator = new SchemaIterator(metadataApi)
            {
                OnObjectProperty = schemaObject =>
                {
                    if (schemaObject.IsResolve)
                    {
                        _resolveProperties.Add(schemaObject);
                    }
                },
                OnArrayProperty = schemaObject =>
                {
                    if (schemaObject.IsDocumentArray)
                    {
                        _resolveProperties.Add(schemaObject);
                    }
                }
            };

            metadataIterator.ProcessSchema(schema);
        }
Example #12
0
 public SchemaIterator(IMetadataApi metadataApi)
 {
     _metadataApi = metadataApi;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ExtensionObjectSerializer"/> class.
 /// </summary>
 /// <param name="metadataApi">The metadata API.</param>
 public ExtensionObjectSerializer(IMetadataApi metadataApi)
 {
     this.metadataApi = metadataApi;
 }
        private static void AssertFieldMetadata(IMetadataApi metadataApi, Guid objectMetadataId)
        {
            IFieldMetadata fieldMetadata = metadataApi.GetField(objectMetadataId, "Name");
            Assert.AreEqual("Name", fieldMetadata.Name);
            Assert.AreEqual(FieldType.String, fieldMetadata.Type);
            Assert.AreEqual(1, fieldMetadata.Ordinal);
            Assert.IsTrue(fieldMetadata.IsRequired);

            fieldMetadata = metadataApi.GetField(objectMetadataId, "Birthday");
            Assert.AreEqual("Birthday", fieldMetadata.Name);
            Assert.AreEqual(FieldType.DateTime, fieldMetadata.Type);
            Assert.AreEqual(2, fieldMetadata.Ordinal);
            Assert.IsFalse(fieldMetadata.IsRequired);

            fieldMetadata = metadataApi.GetField(objectMetadataId, "Level");
            Assert.AreEqual("Level", fieldMetadata.Name);
            Assert.AreEqual(FieldType.Integer, fieldMetadata.Type);
            Assert.AreEqual(3, fieldMetadata.Ordinal);
            Assert.IsTrue(fieldMetadata.IsRequired);

            fieldMetadata = metadataApi.GetField(objectMetadataId, "Salary");
            Assert.AreEqual("Salary", fieldMetadata.Name);
            Assert.AreEqual(FieldType.Decimal, fieldMetadata.Type);
            Assert.AreEqual(4, fieldMetadata.Ordinal);
            Assert.IsTrue(fieldMetadata.IsRequired);

            fieldMetadata = metadataApi.GetField(objectMetadataId, "Position");
            Assert.AreEqual("Position", fieldMetadata.Name);
            Assert.AreEqual(FieldType.Hierarchy, fieldMetadata.Type);
            Assert.AreEqual(5, fieldMetadata.Ordinal);
            Assert.IsTrue(fieldMetadata.IsRequired);

            HierarchyFieldMetadata hierarchyFieldMetadata = fieldMetadata as HierarchyFieldMetadata;
            Assert.AreEqual(1, hierarchyFieldMetadata.Node.Length);
            Assert.AreEqual("Director", hierarchyFieldMetadata.Node[0].Name);

            fieldMetadata = metadataApi.GetField(objectMetadataId, "HuKou");
            Assert.AreEqual("HuKou", fieldMetadata.Name);
            Assert.AreEqual(FieldType.Enumeration, fieldMetadata.Type);
            Assert.AreEqual(6, fieldMetadata.Ordinal);
            Assert.IsFalse(fieldMetadata.IsRequired);

            IEnumerable<IFieldMetadata> allFieldMetadata = metadataApi.GetFields(objectMetadataId);
            Assert.AreEqual(6, allFieldMetadata.Count());
        }
 public MetadataApiHttpService(IMetadataApi metadataApi)
 {
     _metadataApi = metadataApi;
 }
Example #16
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();
                }
            }
        }
Example #17
0
        /// <summary>
        /// Downloads the specified file from the specified package group as an asynchronous operation.
        /// </summary>
        /// <param name="service">The Metadata API to use to get the download URI.</param>
        /// <param name="fileStore">The file store to register the download with if successfully downloaded.</param>
        /// <param name="group">The package group to download the file from.</param>
        /// <param name="file">The data file to download.</param>
        /// <param name="downloadRootPath">The root path to download data to.</param>
        /// <param name="verifyDownloads">Whether to verify file downloads.</param>
        /// <param name="cancellationToken">The cancellation token to use to cancel any downloads.</param>
        /// <returns>
        /// A <see cref="Task"/> representing the asynchronous operation.
        /// </returns>
        private static async Task DownloadFileAsync(
            IMetadataApi service,
            IFileStore fileStore,
            PackageGroup group,
            DataFile file,
            string downloadRootPath,
            bool verifyDownloads,
            CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

            Console.WriteLine("File name: {0}", file.FileName);
            Console.WriteLine("File hash: {0}", file.MD5Hash);
            Console.WriteLine("File size: {0:N0}", file.Size);

            // Query the URIs to download the file from
            Uri uri = await service.GetDownloadUriAsync(file.FileName, file.MD5Hash);

            if (uri == null)
            {
                Console.WriteLine("File '{0}' is not available for download at this time.", file.FileName);
                Console.WriteLine();
                return;
            }

            Console.WriteLine("File URI: {0}", uri);
            Console.WriteLine();

            // Create the path to the directory to download the file to
            string directoryPath = Path.Combine(
                downloadRootPath,
                group.PackageGroupCode,
                group.Vintage);

            string filePath = Path.GetFullPath(Path.Combine(directoryPath, file.FileName));

            // Create the directory if it doesn't already exist
            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            // Download the file
            Console.WriteLine("Downloading '{0}' ({1}) to '{2}'...", file.FileName, file.MD5Hash, filePath);

            using (HttpClient client = new HttpClient())
            {
                using (HttpResponseMessage response = await client.GetAsync(uri, cancellationToken))
                {
                    response.EnsureSuccessStatusCode();

                    using (Stream stream = await response.Content.ReadAsStreamAsync())
                    {
                        using (Stream fileStream = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
                        {
                            // Ensure any existing file is overwritten
                            fileStream.SetLength(0L);

                            await stream.CopyToAsync(fileStream, 4096, cancellationToken);
                        }
                    }
                }
            }

            // Validate the download is correct, if configured.
            // Don't register the file in the file store as the file download became corrupted somehow.
            if (!verifyDownloads || VerifyDownload(filePath, file.MD5Hash))
            {
                // Register the file with the file store so further invocations
                // of the application don't unnecessarily download the file again
                fileStore.RegisterFile(file.MD5Hash, filePath);
            }
        }
Example #18
0
 public PrintViewApi(IMetadataApi metadataApi, IPrintViewBuilder printViewBuilder)
 {
     _metadataApi      = metadataApi;
     _printViewBuilder = printViewBuilder;
 }