public async Task FetchLocalRepository(bool fetchExpanded)
        {
            // Casing is irrelevant for fetchers as they grab content based on file-path
            // which will always be lowercase. Casing IS important for the resolve flow
            // and is covered by tests there.
            string targetDtmi = "dtmi:com:example:temperaturecontroller;1";

            ResolverClientOptions options = fetchExpanded ?
                                            new ResolverClientOptions(DependencyResolutionOption.TryFromExpanded) :
                                            new ResolverClientOptions();

            string            expectedPath = DtmiConventions.DtmiToQualifiedPath(targetDtmi, _localUri.AbsolutePath, fetchExpanded);
            LocalModelFetcher localFetcher = new LocalModelFetcher(_logger.Object, options);
            string            fetcherPath  = localFetcher.GetPath(targetDtmi, _localUri, fetchExpanded);

            Assert.AreEqual(fetcherPath, expectedPath);

            // Resolution correctness is covered in ResolverIntegrationTests
            FetchResult fetchResult = await localFetcher.FetchAsync(targetDtmi, _localUri);

            Assert.True(!string.IsNullOrEmpty(fetchResult.Definition));
            Assert.True(!string.IsNullOrEmpty(fetchResult.Path));
            Assert.AreEqual(fetchResult.FromExpanded, fetchExpanded);

            _logger.ValidateLog(StandardStrings.FetchingContent(fetcherPath), LogLevel.Trace, Times.Once());
        }
        public void FetchLocalRepositoryModelDoesNotExist()
        {
            string            targetDtmi   = "dtmi:com:example:thermojax;1";
            LocalModelFetcher localFetcher = new LocalModelFetcher(_logger.Object, new ResolverClientOptions());

            Assert.ThrowsAsync <FileNotFoundException>(async() => await localFetcher.FetchAsync(targetDtmi, _localUri));

            string expectedModelPath = DtmiConventions.DtmiToQualifiedPath(targetDtmi, _localUri.AbsolutePath);

            _logger.ValidateLog(StandardStrings.ErrorAccessLocalRepositoryModel(expectedModelPath), LogLevel.Warning, Times.Once());
        }
        public static string EnsureValidModelFilePath(string modelFilePath, string modelContent, string repository)
        {
            if (IsRelativePath(repository))
            {
                repository = Path.GetFullPath(repository);
            }

            string rootId             = new Parsing(null).GetRootId(modelContent);
            string modelPath          = Path.GetFullPath(DtmiConventions.DtmiToQualifiedPath(rootId, repository));
            Uri    targetModelPathUri = new Uri(modelPath);
            Uri    modelFilePathUri   = new Uri(modelFilePath);

            if (targetModelPathUri.AbsolutePath != modelFilePathUri.AbsolutePath)
            {
                return(Path.GetFullPath(targetModelPathUri.AbsolutePath));
            }

            return(null);
        }
Ejemplo n.º 4
0
        public async static Task ImportAsync(string modelContent, DirectoryInfo repository)
        {
            string rootId     = new Parsing(null).GetRootId(modelContent);
            string createPath = DtmiConventions.DtmiToQualifiedPath(rootId, repository.FullName);

            Outputs.WriteOut($"- Importing model \"{rootId}\"...");
            if (File.Exists(createPath))
            {
                Outputs.WriteOut(
                    $"Skipping \"{rootId}\". Model file already exists in repository.",
                    ConsoleColor.DarkCyan);
                return;
            }

            UTF8Encoding utf8WithoutBom = new UTF8Encoding(false);

            (new FileInfo(createPath)).Directory.Create();
            await File.WriteAllTextAsync(createPath, modelContent, utf8WithoutBom);
        }
        // [TestCase(true)] - TODO: Uncomment when consistent remote repo available.
        public async Task FetchRemoteRepository(bool fetchExpanded)
        {
            string targetDtmi = "dtmi:com:example:temperaturecontroller;1";

            RemoteModelFetcher remoteFetcher = new RemoteModelFetcher(_logger.Object, new ResolverClientOptions());
            string             expectedPath  = DtmiConventions.DtmiToQualifiedPath(targetDtmi, _remoteUri.AbsoluteUri, fetchExpanded);
            string             fetcherPath   = remoteFetcher.GetPath(targetDtmi, _remoteUri, fetchExpanded);

            Assert.AreEqual(fetcherPath, expectedPath);

            // Resolution correctness is covered in ResolverIntegrationTests
            FetchResult fetchResult = await remoteFetcher.FetchAsync(targetDtmi, _remoteUri);

            Assert.True(!string.IsNullOrEmpty(fetchResult.Definition));
            Assert.True(!string.IsNullOrEmpty(fetchResult.Path));
            Assert.AreEqual(fetchResult.FromExpanded, fetchExpanded);

            _logger.ValidateLog($"{StandardStrings.FetchingContent(fetcherPath)}", LogLevel.Trace, Times.Once());
        }
Ejemplo n.º 6
0
        public void DtmiToQualifiedPath(string dtmi, string repository, string expectedPath)
        {
            if (string.IsNullOrEmpty(expectedPath))
            {
                Action act = () => DtmiConventions.DtmiToQualifiedPath(dtmi, repository);
                act.Should().Throw <ArgumentException>().WithMessage(string.Format(StandardStrings.InvalidDtmiFormat, dtmi));
                return;
            }

            string modelPath = DtmiConventions.DtmiToQualifiedPath(dtmi, repository);

            modelPath.Should().Be(expectedPath);

            string expectedExpandedPath = expectedPath.Replace(
                ModelsRepositoryConstants.JsonFileExtension, ModelsRepositoryConstants.ExpandedJsonFileExtension);
            string expandedModelPath = DtmiConventions.DtmiToQualifiedPath(dtmi, repository, true);

            expandedModelPath.Should().Be(expectedExpandedPath);
        }
Ejemplo n.º 7
0
        public void DtmiToQualifiedPath(string dtmi, string expectedPath, string repository)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                repository = repository.Replace("\\", "/");
            }

            if (string.IsNullOrEmpty(expectedPath))
            {
                ArgumentException re = Assert.Throws<ArgumentException>(() => DtmiConventions.DtmiToQualifiedPath(dtmi, repository));
                Assert.AreEqual(re.Message, StandardStrings.InvalidDtmiFormat(dtmi));
                return;
            }

            string modelPath = DtmiConventions.DtmiToQualifiedPath(dtmi, repository);
            Assert.AreEqual($"{repository}/{expectedPath}", modelPath);

            string expandedModelPath = DtmiConventions.DtmiToQualifiedPath(dtmi, repository, true);
            Assert.AreEqual($"{repository}/{expectedPath.Replace(".json", ".expanded.json")}", expandedModelPath);
        }
        private static FileInfo ImportModel(JsonElement modelItem, string fileName, DirectoryInfo repository, ILogger logger)
        {
            //Do DTMI verification
            var rootId = Validations.GetRootId(modelItem, fileName);

            if (!ResolverClient.IsValidDtmi(rootId.GetString()))
            {
                throw new InvalidDTMIException(rootId);
            }
            if (!Validations.ValidateDTMIs(modelItem, fileName, logger))
            {
                throw new InvalidDTMIException(fileName);
            }

            // write file to repository location
            var newPath = DtmiConventions.DtmiToQualifiedPath(rootId.GetString(), repository.FullName);

            // TODO: consistent paths. Use global arg formatters.
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                newPath = newPath.Replace("\\", "/");
            }

            if (!File.Exists(newPath))
            {
                CheckCreateDirectory(newPath);
                logger.LogTrace($"Writing new file to '{newPath}'. ");
                File.WriteAllText(newPath, modelItem.ToString(), Encoding.UTF8);
            }
            else
            {
                throw new IOException($"File '{newPath}' already exists. Please remove prior to execution.");
            }

            //return file info
            return(new FileInfo(newPath));
        }
        public void DtmiToQualifiedPath(string dtmi, string expectedPath, string repository)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                repository = repository.Replace("\\", "/");
            }

            if (string.IsNullOrEmpty(expectedPath))
            {
                Action act = () => DtmiConventions.DtmiToQualifiedPath(dtmi, repository);
                act.Should().Throw <ArgumentException>().WithMessage(string.Format(StandardStrings.InvalidDtmiFormat, dtmi));
                return;
            }

            string modelPath = DtmiConventions.DtmiToQualifiedPath(dtmi, repository);

            modelPath.Should().Be($"{repository}/{expectedPath}");

            string expandedModelPath = DtmiConventions.DtmiToQualifiedPath(dtmi, repository, true);

            expandedModelPath
            .Should()
            .Be($"{repository}/{expectedPath.Replace(ModelsRepositoryConstants.JsonFileExtension, ModelsRepositoryConstants.ExpandedJsonFileExtension)}");
        }
Ejemplo n.º 10
0
        public static void GetDtmiToQualifiedPath()
        {
            #region Snippet:ModelsRepositorySamplesDtmiConventionsGetDtmiToQualifiedPath

            // This snippet shows obtaining a fully qualified path to a model file.

            // Local repository example
            string localRepository         = "/path/to/repository";
            string fullyQualifiedModelPath =
                DtmiConventions.DtmiToQualifiedPath("dtmi:com:example:Thermostat;1", localRepository);

            // Prints '/path/to/repository/dtmi/com/example/thermostat-1.json'
            Console.WriteLine(fullyQualifiedModelPath);

            // Remote repository example
            string remoteRepository = "https://contoso.com/models";
            fullyQualifiedModelPath =
                DtmiConventions.DtmiToQualifiedPath("dtmi:com:example:Thermostat;1", remoteRepository);

            // Prints 'https://contoso.com/models/dtmi/com/example/thermostat-1.json'
            Console.WriteLine(fullyQualifiedModelPath);

            #endregion Snippet:ModelsRepositorySamplesDtmiConventionsGetDtmiToQualifiedPath
        }
Ejemplo n.º 11
0
        private static string GetPath(string dtmi, Uri repositoryUri, bool expanded = false)
        {
            string registryPath = repositoryUri.AbsolutePath;

            return(DtmiConventions.DtmiToQualifiedPath(dtmi, registryPath, expanded));
        }
Ejemplo n.º 12
0
        private static string GetPath(string dtmi, Uri repositoryUri, bool expanded = false)
        {
            string absoluteUri = repositoryUri.AbsoluteUri;

            return(DtmiConventions.DtmiToQualifiedPath(dtmi, absoluteUri, expanded));
        }