示例#1
0
        public async Task TestCircularImportWithMoniker()
        {
            var corpus = TestHelper.GetLocalCorpus("", "");
            var folder = corpus.Storage.FetchRootFolder("local");

            var docA = new CdmDocumentDefinition(corpus.Ctx, "A.cdm.json");

            folder.Documents.Add(docA);
            docA.Imports.Add("B.cdm.json", "moniker");

            var docB = new CdmDocumentDefinition(corpus.Ctx, "B.cdm.json");

            folder.Documents.Add(docB);
            docB.Imports.Add("C.cdm.json");

            var docC = new CdmDocumentDefinition(corpus.Ctx, "C.cdm.json");

            folder.Documents.Add(docC);
            docC.Imports.Add("B.cdm.json");

            // forces docB to be indexed first.
            await docB.IndexIfNeeded(new ResolveOptions(), true);

            await docA.IndexIfNeeded(new ResolveOptions(), true);

            // should contain A, B and C.
            Assert.AreEqual(3, docA.ImportPriorities.ImportPriority.Count);

            Assert.IsFalse(docA.ImportPriorities.hasCircularImport);

            // docB and docC should have the hasCircularImport set to true.
            Assert.IsTrue(docB.ImportPriorities.hasCircularImport);
            Assert.IsTrue(docC.ImportPriorities.hasCircularImport);
        }
示例#2
0
        public async Task TestMonikeredImportIsNotAdded()
        {
            var corpus = TestHelper.GetLocalCorpus("", "");
            var folder = corpus.Storage.FetchRootFolder("local");

            var docA = new CdmDocumentDefinition(corpus.Ctx, "A.cdm.json");

            folder.Documents.Add(docA);
            docA.Imports.Add("B.cdm.json", "moniker");

            var docB = new CdmDocumentDefinition(corpus.Ctx, "B.cdm.json");

            folder.Documents.Add(docB);
            docB.Imports.Add("C.cdm.json");

            var docC = new CdmDocumentDefinition(corpus.Ctx, "C.cdm.json");

            folder.Documents.Add(docC);

            // forces docB to be indexed first, so the priorityList will be read from the cache this time.
            await docB.IndexIfNeeded(new ResolveOptions(docB), true);

            await docA.IndexIfNeeded(new ResolveOptions(docA), true);

            // should only contain docA and docC, docB should be excluded.
            Assert.AreEqual(2, docA.ImportPriorities.ImportPriority.Count);

            Assert.IsFalse(docA.ImportPriorities.hasCircularImport);
            Assert.IsFalse(docB.ImportPriorities.hasCircularImport);
            Assert.IsFalse(docC.ImportPriorities.hasCircularImport);
        }
示例#3
0
        public void TestZeroMinimumCardinality()
        {
            string testName = "TestZeroMinimumCardinality";

            CdmCorpusDefinition corpus = TestHelper.GetLocalCorpus(testsSubpath, testName);

            corpus.SetEventCallback(new EventCallback
            {
                Invoke = (CdmStatusLevel statusLevel, string message) =>
                {
                    Assert.Fail(message);
                }
            }, CdmStatusLevel.Warning);

            // Create Local Root Folder
            CdmFolderDefinition localRoot = corpus.Storage.FetchRootFolder("local");

            // Create Manifest
            CdmManifestDefinition manifest = corpus.MakeObject <CdmManifestDefinition>(CdmObjectType.ManifestDef, "default");

            localRoot.Documents.Add(manifest, "default.manifest.cdm.json");

            string entityName = "TestEntity";

            // Create Entity
            CdmEntityDefinition entity = corpus.MakeObject <CdmEntityDefinition>(CdmObjectType.EntityDef, entityName);

            entity.ExtendsEntity = corpus.MakeRef <CdmEntityReference>(CdmObjectType.EntityRef, "CdmEntity", true);

            // Create Entity Document
            CdmDocumentDefinition document = corpus.MakeObject <CdmDocumentDefinition>(CdmObjectType.DocumentDef, $"{entityName}.cdm.json", false);

            document.Definitions.Add(entity);
            localRoot.Documents.Add(document, document.Name);
            manifest.Entities.Add(entity);

            string attributeName     = "testAttribute";
            string attributeDataType = "string";
            string attributePurpose  = "hasA";

            // Create Type Attribute
            CdmTypeAttributeDefinition attribute = corpus.MakeObject <CdmTypeAttributeDefinition>(CdmObjectType.TypeAttributeDef, nameOrRef: attributeName, simpleNameRef: false);

            attribute.DataType    = corpus.MakeRef <CdmDataTypeReference>(CdmObjectType.DataTypeRef, refObj: attributeDataType, simpleNameRef: true);
            attribute.Purpose     = corpus.MakeRef <CdmPurposeReference>(CdmObjectType.PurposeRef, refObj: attributePurpose, simpleNameRef: true);
            attribute.DisplayName = attributeName;

            if (entity != null)
            {
                entity.Attributes.Add(attribute);
            }

            attribute.Cardinality = new CardinalitySettings(attribute)
            {
                Minimum = "0",
                Maximum = "*"
            };

            Assert.IsTrue(attribute.IsNullable == true);
        }
示例#4
0
        /// <summary>
        /// Create a simple entity called 'TestSource' with a single attribute
        /// </summary>
        /// <param name="entityName"></param>
        /// <param name="attributesParams"></param>
        /// <returns></returns>
        public CdmEntityDefinition CreateBasicEntity(string entityName, List <TypeAttributeParam> attributesParams)
        {
            CdmEntityDefinition entity = Corpus.MakeObject <CdmEntityDefinition>(CdmObjectType.EntityDef, entityName);

            foreach (TypeAttributeParam attributesParam in attributesParams)
            {
                CdmTypeAttributeDefinition attribute = Corpus.MakeObject <CdmTypeAttributeDefinition>(CdmObjectType.TypeAttributeDef, nameOrRef: attributesParam.AttributeName, simpleNameRef: false);
                attribute.DataType    = Corpus.MakeRef <CdmDataTypeReference>(CdmObjectType.DataTypeRef, refObj: attributesParam.AttributeDataType, simpleNameRef: true);
                attribute.Purpose     = Corpus.MakeRef <CdmPurposeReference>(CdmObjectType.PurposeRef, refObj: attributesParam.AttributePurpose, simpleNameRef: true);
                attribute.DisplayName = attributesParam.AttributeName;

                entity.Attributes.Add(attribute);
            }

            CdmDocumentDefinition entityDoc = Corpus.MakeObject <CdmDocumentDefinition>(CdmObjectType.DocumentDef, $"{entityName}.cdm.json", false);

            entityDoc.Imports.Add(AllImportsDocName);
            entityDoc.Definitions.Add(entity);

            LocalStorageRoot.Documents.Add(entityDoc, entityDoc.Name);
            DefaultManifest.Entities.Add(entity);
            AllImports.Imports.Add(entity.InDocument.Name);

            return(entity);
        }
示例#5
0
        /// <summary>
        /// Create a simple entity called 'TestSource' with a single attribute
        /// </summary>
        /// <param name="corpus"></param>
        /// <param name="manifestDefault"></param>
        /// <param name="localRoot"></param>
        /// <returns></returns>
        private CdmEntityDefinition CreateEntityTestSource(CdmCorpusDefinition corpus, CdmManifestDefinition manifestDefault, CdmFolderDefinition localRoot)
        {
            string entityName = "TestSource";

            CdmEntityDefinition entityTestSource = corpus.MakeObject <CdmEntityDefinition>(CdmObjectType.EntityDef, entityName);
            {
                string attributeName = "TestAttribute";

                CdmTypeAttributeDefinition entityTestAttribute = corpus.MakeObject <CdmTypeAttributeDefinition>(CdmObjectType.TypeAttributeDef, nameOrRef: attributeName, simpleNameRef: false);
                entityTestAttribute.DataType    = corpus.MakeRef <CdmDataTypeReference>(CdmObjectType.DataTypeRef, refObj: "string", simpleNameRef: true);
                entityTestAttribute.Purpose     = corpus.MakeRef <CdmPurposeReference>(CdmObjectType.PurposeRef, refObj: "hasA", simpleNameRef: true);
                entityTestAttribute.DisplayName = attributeName;

                entityTestSource.Attributes.Add(entityTestAttribute);
            }

            CdmDocumentDefinition entityTestSourceDoc = corpus.MakeObject <CdmDocumentDefinition>(CdmObjectType.DocumentDef, $"{entityName}.cdm.json", false);

            entityTestSourceDoc.Imports.Add(FoundationJsonPath);
            entityTestSourceDoc.Definitions.Add(entityTestSource);

            localRoot.Documents.Add(entityTestSourceDoc, entityTestSourceDoc.Name);
            manifestDefault.Entities.Add(entityTestSource);

            return(entityTestSource);
        }
示例#6
0
        public async Task TestLoadingSameMissingImportsAsync()
        {
            var localAdapter = this.CreateStorageAdapterForTest("TestLoadingSameMissingImportsAsync");
            var cdmCorpus    = this.CreateTestCorpus(localAdapter);

            var resOpt = new ResolveOptions()
            {
                StrictValidation = true
            };

            CdmDocumentDefinition mainDoc = await cdmCorpus.FetchObjectAsync <CdmDocumentDefinition>("mainEntity.cdm.json", null, resOpt);

            Assert.IsNotNull(mainDoc);
            Assert.AreEqual(2, mainDoc.Imports.Count);

            // make sure imports loaded correctly, despite them missing imports
            CdmDocumentDefinition firstImport  = mainDoc.Imports[0].Document;
            CdmDocumentDefinition secondImport = mainDoc.Imports[1].Document;

            Assert.AreEqual(1, firstImport.Imports.Count);
            Assert.IsNull(firstImport.Imports[0].Document);

            Assert.AreEqual(1, secondImport.Imports.Count);
            Assert.IsNull(firstImport.Imports[0].Document);
        }
示例#7
0
 public TableAnnotationProcessor(CdmCorpusDefinition corpus, CdmReferenceResolver resolver, CdmDocumentDefinition entityDocument, CdmEntityDefinition entityDefinition)
 {
     this.corpus           = corpus;
     this.resolver         = resolver;
     this.entityDocument   = entityDocument;
     this.entityDefinition = entityDefinition;
 }
示例#8
0
        /// <summary>
        /// Tries to fetch the document with expected fileName.
        /// Caches results in <see cref="CachedDefDocs"/>.
        /// </summary>
        /// <param name="ctx">The context</param>
        /// <param name="fileName">The name of the file that needs to be retrieved.</param>
        /// <returns>The content of the definition file with the expected fileName, or null if no such file was found.</returns>
        private static async Task <CdmDocumentDefinition> FetchDefDoc(CdmCorpusContext ctx, string fileName)
        {
            // Since the CachedDefDocs is a static property and there might be multiple corpus running,
            // we need to make sure that each corpus will have its own cached def document.
            // This is achieved by adding the context as part of the key to the document.
            var key = Tuple.Create(ctx, fileName);

            if (CachedDefDocs.ContainsKey(key))
            {
                return(CachedDefDocs[key]);
            }

            string    path     = $"/extensions/{fileName}";
            var       absPath  = ctx.Corpus.Storage.CreateAbsoluteCorpusPath(path, ctx.Corpus.Storage.FetchRootFolder("cdm"));
            CdmObject document = await ctx.Corpus.FetchObjectAsync <CdmObject>(absPath);

            if (document != null)
            {
                CdmDocumentDefinition extensionDoc = document as CdmDocumentDefinition;

                // Needs to lock the dictionary since it is a static property and there might be multiple corpus running on the same environment.
                lock (CachedDefDocs)
                {
                    CachedDefDocs.Add(key, extensionDoc);
                }
                return(extensionDoc);
            }
            return(null);
        }
示例#9
0
        public async Task TestLoadingSameImportsAsync()
        {
            var localAdapter = this.CreateStorageAdapterForTest("TestLoadingSameImportsAsync");
            var cdmCorpus    = this.CreateTestCorpus(localAdapter);

            var resOpt = new ResolveOptions()
            {
                StrictValidation = true
            };

            CdmDocumentDefinition mainDoc = await cdmCorpus.FetchObjectAsync <CdmDocumentDefinition>("mainEntity.cdm.json", null, resOpt);

            Assert.IsNotNull(mainDoc);
            Assert.AreEqual(2, mainDoc.Imports.Count);

            CdmDocumentDefinition firstImport  = mainDoc.Imports[0].Document;
            CdmDocumentDefinition secondImport = mainDoc.Imports[1].Document;

            // since these two imports are loaded asynchronously, we need to make sure that
            // the import that they share (targetImport) was loaded, and that the
            // targetImport doc is attached to both of these import objects
            Assert.AreEqual(1, firstImport.Imports.Count);
            Assert.IsNotNull(firstImport.Imports[0].Document);
            Assert.AreEqual(1, secondImport.Imports.Count);
            Assert.IsNotNull(secondImport.Imports[0].Document);
        }
示例#10
0
        public void TestManifestAddListOfEntityDeclarations()
        {
            var cdmCorpus = new CdmCorpusDefinition();

            cdmCorpus.Storage.DefaultNamespace = "local";
            cdmCorpus.Storage.Mount("local", new LocalAdapter("CdmCorpus/LocalPath"));

            var ctx = cdmCorpus.Ctx;

            var cdmDocument = new CdmDocumentDefinition(ctx, "NameOfDocument");
            var collection  = new CdmEntityCollection(ctx, cdmDocument);

            var entityList = new List <CdmEntityDefinition>();

            for (int i = 0; i < 2; i++)
            {
                var entity = new CdmEntityDefinition(cdmCorpus.Ctx, $"entityName_{i}", null);
                CdmCollectionHelperFunctions.CreateDocumentForEntity(cdmCorpus, entity);
                entityList.Add(entity);
            }

            Assert.AreEqual(0, collection.Count);

            collection.AddRange(entityList);

            Assert.AreEqual(2, collection.Count);

            for (int i = 0; i < 2; i++)
            {
                Assert.AreEqual($"entityName_{i}", collection[i].EntityName);
            }
        }
示例#11
0
        public void TestCdmCollectionRemoveAt()
        {
            var cdmCorpus = new CdmCorpusDefinition();

            cdmCorpus.Storage.DefaultNamespace = "local";
            cdmCorpus.Storage.Mount("local", new LocalAdapter("CdmCorpus/LocalPath"));

            var ctx = cdmCorpus.Ctx;

            var cdmDocument = new CdmDocumentDefinition(ctx, "NameOfDocument");
            var collection  = new CdmCollection <CdmAttributeContext>(ctx, cdmDocument, Enums.CdmObjectType.AttributeContextDef);

            var addedDocument = collection.Add("nameOfNewDocument");

            var addedDocument2 = collection.Add("otherDocument");

            Assert.AreEqual(2, collection.Count);

            collection.RemoveAt(0);
            Assert.AreEqual(1, collection.Count);
            Assert.AreEqual(addedDocument2, collection[0]);
            collection.RemoveAt(1);
            Assert.AreEqual(1, collection.Count);
            Assert.AreEqual(addedDocument2, collection[0]);
            collection.RemoveAt(0);
            Assert.AreEqual(0, collection.Count);
        }
示例#12
0
        public void TestCdmCollectionRemoveMethod()
        {
            var cdmCorpus = new CdmCorpusDefinition();

            cdmCorpus.Storage.DefaultNamespace = "local";
            cdmCorpus.Storage.Mount("local", new LocalAdapter("CdmCorpus/LocalPath"));

            var ctx = cdmCorpus.Ctx;

            var cdmDocument = new CdmDocumentDefinition(ctx, "NameOfDocument");
            var collection  = new CdmCollection <CdmAttributeContext>(ctx, cdmDocument, Enums.CdmObjectType.AttributeContextDef);

            var addedDocument = collection.Add("nameOfNewDocument");

            var addedDocument2 = collection.Add("otherDocument");

            Assert.AreEqual(2, collection.Count);

            bool removed = collection.Remove(addedDocument);

            Assert.IsTrue(removed);

            // try to remove a second time.
            removed = collection.Remove(addedDocument);
            Assert.IsFalse(removed);
            Assert.AreEqual(1, collection.Count);
        }
示例#13
0
        /// <summary>
        /// Create an entity 'TestEntityAttributeProjection' that contains an entity attribute with a projection as a source entity
        /// </summary>
        /// <param name="corpus"></param>
        /// <param name="manifestDefault"></param>
        /// <param name="localRoot"></param>
        /// <returns></returns>
        private CdmEntityDefinition CreateEntityTestEntityAttributeProjection(CdmCorpusDefinition corpus, CdmManifestDefinition manifestDefault, CdmFolderDefinition localRoot)
        {
            string entityName = "TestEntityAttributeProjection";

            CdmEntityReference inlineProjectionEntityRef = corpus.MakeObject <CdmEntityReference>(CdmObjectType.EntityRef, null);

            inlineProjectionEntityRef.ExplicitReference = CreateProjection(corpus);

            CdmEntityDefinition entityTestEntityAttributeProjection = corpus.MakeObject <CdmEntityDefinition>(CdmObjectType.EntityDef, entityName);
            {
                string attributeName = "TestAttribute";

                CdmEntityAttributeDefinition entityTestEntityAttribute = corpus.MakeObject <CdmEntityAttributeDefinition>(CdmObjectType.EntityAttributeDef, nameOrRef: attributeName, simpleNameRef: false);
                entityTestEntityAttribute.Entity = inlineProjectionEntityRef;

                entityTestEntityAttributeProjection.Attributes.Add(entityTestEntityAttribute);
            }

            CdmDocumentDefinition entityTestEntityAttributeProjectionDoc = corpus.MakeObject <CdmDocumentDefinition>(CdmObjectType.DocumentDef, $"{entityName}.cdm.json", false);

            entityTestEntityAttributeProjectionDoc.Imports.Add(FoundationJsonPath);
            entityTestEntityAttributeProjectionDoc.Imports.Add("TestSource.cdm.json");
            entityTestEntityAttributeProjectionDoc.Definitions.Add(entityTestEntityAttributeProjection);

            localRoot.Documents.Add(entityTestEntityAttributeProjectionDoc, entityTestEntityAttributeProjectionDoc.Name);
            manifestDefault.Entities.Add(entityTestEntityAttributeProjection);

            return(entityTestEntityAttributeProjection);
        }
示例#14
0
        public void TestDocumentCollectionAddRange()
        {
            var manifest = CdmCollectionHelperFunctions.GenerateManifest();
            var folder   = new CdmFolderDefinition(manifest.Ctx, "Folder");

            folder.Corpus     = manifest.Ctx.Corpus;
            folder.FolderPath = "FolderPath/";
            folder.Namespace  = "Namespace";

            Assert.AreEqual(0, folder.Documents.Count);

            var document  = new CdmDocumentDefinition(manifest.Ctx, "DocumentName");
            var document2 = new CdmDocumentDefinition(manifest.Ctx, "DocumentName2");

            var documentList = new List <CdmDocumentDefinition> {
                document, document2
            };

            folder.Documents.AddRange(documentList);
            Assert.AreEqual(2, folder.Documents.Count);
            Assert.AreEqual(document, folder.Documents[0]);
            Assert.AreEqual(document2, folder.Documents[1]);

            Assert.AreEqual("DocumentName", document.Name);
            Assert.AreEqual("FolderPath/", document.FolderPath);
            Assert.AreEqual(folder, document.Owner);
            Assert.AreEqual("Namespace", document.Namespace);
            Assert.IsTrue(document.NeedsIndexing);

            Assert.AreEqual("DocumentName2", document2.Name);
            Assert.AreEqual("FolderPath/", document2.FolderPath);
            Assert.AreEqual(folder, document2.Owner);
            Assert.AreEqual("Namespace", document2.Namespace);
            Assert.IsTrue(document2.NeedsIndexing);
        }
示例#15
0
 public CdmEntityGenerator(CdmCorpusDefinition corpus, CdmReferenceResolver resolver, CdmDocumentDefinition entityDocument, string version)
 {
     this.corpus         = corpus;
     this.resolver       = resolver;
     this.entityDocument = entityDocument;
     this.version        = version;
 }
示例#16
0
        public async Task TestImportsRelativePath()
        {
            // the corpus path in the imports are relative to the document where it was defined.
            // when saving in model.json the documents are flattened to the manifest level
            // so it is necessary to recalculate the path to be relative to the manifest.
            var corpus = this.GetLocalCorpus("notImportantLocation");
            var folder = corpus.Storage.FetchRootFolder("local");

            var manifest          = new CdmManifestDefinition(corpus.Ctx, "manifest");
            var entityDeclaration = manifest.Entities.Add("EntityName", "EntityName/EntityName.cdm.json/EntityName");

            folder.Documents.Add(manifest);

            var entityFolder = folder.ChildFolders.Add("EntityName");

            var document = new CdmDocumentDefinition(corpus.Ctx, "EntityName.cdm.json");

            document.Imports.Add("subfolder/EntityName.cdm.json");
            document.Definitions.Add("EntityName");
            entityFolder.Documents.Add(document);

            var subFolder = entityFolder.ChildFolders.Add("subfolder");

            subFolder.Documents.Add("EntityName.cdm.json");

            var data = await ManifestPersistence.ToData(manifest, null, null);

            Assert.AreEqual(1, data.Entities.Count);
            var imports = data.Entities[0]["cdm:imports"].ToObject <List <Import> >();

            Assert.AreEqual(1, imports.Count);
            Assert.AreEqual("EntityName/subfolder/EntityName.cdm.json", imports[0].CorpusPath);
        }
示例#17
0
        public async Task TestLoadingAlreadyPresentImportsAsync()
        {
            var localAdapter = this.CreateStorageAdapterForTest("TestLoadingAlreadyPresentImportsAsync");
            var cdmCorpus    = this.CreateTestCorpus(localAdapter);

            // load the first doc
            CdmDocumentDefinition mainDoc = await cdmCorpus.FetchObjectAsync <CdmDocumentDefinition>("mainEntity.cdm.json");

            Assert.IsNotNull(mainDoc);
            Assert.AreEqual(1, mainDoc.Imports.Count);

            CdmDocumentDefinition importDoc = mainDoc.Imports[0].Doc;

            Assert.IsNotNull(importDoc);

            // now load the second doc, which uses the same import
            // the import should not be loaded again, it should be the same object
            CdmDocumentDefinition secondDoc = await cdmCorpus.FetchObjectAsync <CdmDocumentDefinition>("secondEntity.cdm.json");

            Assert.IsNotNull(secondDoc);
            Assert.AreEqual(1, secondDoc.Imports.Count);

            CdmDocumentDefinition secondImportDoc = mainDoc.Imports[0].Doc;

            Assert.IsNotNull(secondImportDoc);

            Assert.AreSame(importDoc, secondImportDoc);
        }
示例#18
0
        public async Task TestLoadingAlreadyPresentImportsAsync()
        {
            var cdmCorpus = TestHelper.GetLocalCorpus(testsSubpath, nameof(TestLoadingAlreadyPresentImportsAsync));

            var resOpt = new ResolveOptions()
            {
                ImportsLoadStrategy = ImportsLoadStrategy.Load
            };

            // load the first doc
            CdmDocumentDefinition mainDoc = await cdmCorpus.FetchObjectAsync <CdmDocumentDefinition>("mainEntity.cdm.json", null, resOpt);

            Assert.IsNotNull(mainDoc);
            Assert.AreEqual(1, mainDoc.Imports.Count);

            CdmDocumentDefinition importDoc = mainDoc.Imports[0].Document;

            Assert.IsNotNull(importDoc);

            // now load the second doc, which uses the same import
            // the import should not be loaded again, it should be the same object
            CdmDocumentDefinition secondDoc = await cdmCorpus.FetchObjectAsync <CdmDocumentDefinition>("secondEntity.cdm.json", null, resOpt);

            Assert.IsNotNull(secondDoc);
            Assert.AreEqual(1, secondDoc.Imports.Count);

            CdmDocumentDefinition secondImportDoc = mainDoc.Imports[0].Document;

            Assert.IsNotNull(secondImportDoc);

            Assert.AreSame(importDoc, secondImportDoc);
        }
示例#19
0
        public async Task TestLoadingSameImportsAsync()
        {
            var cdmCorpus = TestHelper.GetLocalCorpus(testsSubpath, nameof(TestLoadingSameImportsAsync));

            var resOpt = new ResolveOptions()
            {
                ImportsLoadStrategy = ImportsLoadStrategy.Load
            };

            CdmDocumentDefinition mainDoc = await cdmCorpus.FetchObjectAsync <CdmDocumentDefinition>("mainEntity.cdm.json", null, resOpt);

            Assert.IsNotNull(mainDoc);
            Assert.AreEqual(2, mainDoc.Imports.Count);

            CdmDocumentDefinition firstImport  = mainDoc.Imports[0].Document;
            CdmDocumentDefinition secondImport = mainDoc.Imports[1].Document;

            // since these two imports are loaded asynchronously, we need to make sure that
            // the import that they share (targetImport) was loaded, and that the
            // targetImport doc is attached to both of these import objects
            Assert.AreEqual(1, firstImport.Imports.Count);
            Assert.IsNotNull(firstImport.Imports[0].Document);
            Assert.AreEqual(1, secondImport.Imports.Count);
            Assert.IsNotNull(secondImport.Imports[0].Document);
        }
示例#20
0
        public async Task TestLoadingSameMissingImportsAsync()
        {
            var expectedLogCodes = new HashSet <CdmLogCode> {
                CdmLogCode.ErrPersistFileReadFailure, CdmLogCode.WarnResolveImportFailed, CdmLogCode.WarnDocImportNotLoaded
            };
            var cdmCorpus = TestHelper.GetLocalCorpus(testsSubpath, nameof(TestLoadingSameMissingImportsAsync), expectedCodes: expectedLogCodes);

            var resOpt = new ResolveOptions()
            {
                ImportsLoadStrategy = ImportsLoadStrategy.Load
            };

            CdmDocumentDefinition mainDoc = await cdmCorpus.FetchObjectAsync <CdmDocumentDefinition>("mainEntity.cdm.json", null, resOpt);

            Assert.IsNotNull(mainDoc);
            Assert.AreEqual(2, mainDoc.Imports.Count);

            // make sure imports loaded correctly, despite them missing imports
            CdmDocumentDefinition firstImport  = mainDoc.Imports[0].Document;
            CdmDocumentDefinition secondImport = mainDoc.Imports[1].Document;

            Assert.AreEqual(1, firstImport.Imports.Count);
            Assert.IsNull(firstImport.Imports[0].Document);

            Assert.AreEqual(1, secondImport.Imports.Count);
            Assert.IsNull(firstImport.Imports[0].Document);
        }
示例#21
0
        public async Task TestResolvedManifestImport()
        {
            var corpus = TestHelper.GetLocalCorpus(testsSubpath, nameof(TestResolvedManifestImport));

            // Make sure that we are not picking up the default namespace while testing.
            corpus.Storage.DefaultNamespace = "remote";

            var documentName = "localImport.cdm.json";
            var localFolder  = corpus.Storage.FetchRootFolder("local");

            // Create a manifest that imports a document on the same folder.
            var manifest = new CdmManifestDefinition(corpus.Ctx, "default");

            manifest.Imports.Add(documentName);
            localFolder.Documents.Add(manifest);

            var document = new CdmDocumentDefinition(corpus.Ctx, documentName);

            localFolder.Documents.Add(document);

            // Resolve the manifest into a different folder.
            var resolvedManifest = await manifest.CreateResolvedManifestAsync("output:/default.manifest.cdm.json", null);

            // Checks if the import path on the resolved manifest points to the original location.
            Assert.AreEqual(1, resolvedManifest.Imports.Count);
            Assert.AreEqual($"local:/{documentName}", resolvedManifest.Imports[0].CorpusPath);
        }
示例#22
0
        public void TestDocumentCollectionInsert()
        {
            var manifest = CdmCollectionHelperFunctions.GenerateManifest();
            var folder   = new CdmFolderDefinition(manifest.Ctx, "Folder");

            folder.InDocument = manifest;
            folder.Corpus     = manifest.Ctx.Corpus;
            folder.FolderPath = "FolderPath/";
            folder.Namespace  = "Namespace";
            var document = new CdmDocumentDefinition(manifest.Ctx, "DocumentName");

            var doc1 = folder.Documents.Add("doc1");
            var doc2 = folder.Documents.Add("doc2");

            manifest.IsDirty = false;

            folder.Documents.Insert(2, document);
            Assert.IsTrue(manifest.IsDirty);
            Assert.AreEqual(3, folder.Documents.Count);
            Assert.AreEqual(doc1, folder.Documents[0]);
            Assert.AreEqual(doc2, folder.Documents[1]);
            Assert.AreEqual(document, folder.Documents[2]);

            Assert.AreEqual("FolderPath/", document.FolderPath);
            Assert.AreEqual(folder, document.Owner);
            Assert.AreEqual("Namespace", document.Namespace);
            Assert.IsTrue(document.NeedsIndexing);
            Assert.AreEqual(folder, document.Owner);
            Assert.IsTrue(folder.DocumentLookup.ContainsKey(document.Name));
            Assert.IsTrue(manifest.Ctx.Corpus.documentLibrary.Contains(Tuple.Create(folder, document)));

            // reinsert same name doc
            folder.Documents.Insert(2, document);
            Assert.AreEqual(3, folder.Documents.Count);
        }
示例#23
0
        public void TestDocumentCollectionAdd()
        {
            var manifest = CdmCollectionHelperFunctions.GenerateManifest();
            var folder   = new CdmFolderDefinition(manifest.Ctx, "Folder");

            folder.Corpus     = manifest.Ctx.Corpus;
            folder.FolderPath = "FolderPath/";
            folder.Namespace  = "Namespace";
            var document = new CdmDocumentDefinition(manifest.Ctx, "DocumentName");

            Assert.AreEqual(0, folder.Documents.Count);
            var addedDocument = folder.Documents.Add(document);

            Assert.AreEqual(1, folder.Documents.Count);
            Assert.AreEqual(document, folder.Documents[0]);
            Assert.AreEqual(document, addedDocument);
            Assert.AreEqual("FolderPath/", document.FolderPath);
            Assert.AreEqual(folder, document.Owner);
            Assert.AreEqual("Namespace", document.Namespace);
            Assert.IsTrue(document.NeedsIndexing);

            var doc = folder.Documents.Add(document);

            Assert.IsNull(doc);
        }
示例#24
0
        public void TestCdmCollectionAddMethod()
        {
            var cdmCorpus = new CdmCorpusDefinition();

            cdmCorpus.Storage.DefaultNamespace = "local";
            cdmCorpus.Storage.Mount("local", new LocalAdapter("CdmCorpus/LocalPath"));

            var ctx = cdmCorpus.Ctx;

            var cdmDocument = new CdmDocumentDefinition(ctx, "NameOfDocument");
            var collection  = new CdmCollection <CdmAttributeContext>(ctx, cdmDocument, Enums.CdmObjectType.AttributeContextDef);

            var addedAttributeContext = collection.Add("nameOfNewAttribute");

            Assert.AreEqual(1, collection.Count);
            Assert.AreEqual("nameOfNewAttribute", collection[0].Name);
            Assert.AreEqual(cdmDocument, collection[0].Owner);
            Assert.AreEqual(ctx, collection[0].Ctx);

            Assert.AreEqual(collection[0], addedAttributeContext);

            var attributeContext = new CdmAttributeContext(ctx, "NameOfAttributeContext");
            var addedAttribute   = collection.Add(attributeContext);

            Assert.AreEqual(2, collection.Count);
            Assert.AreEqual(attributeContext, addedAttribute);
            Assert.AreEqual(attributeContext, collection[1]);
            Assert.AreEqual(cdmDocument, attributeContext.Owner);
        }
示例#25
0
        /// <summary>
        /// For all the definitions (name, type) we have found for extensions, search in CDM Standard Schema for definition files.
        /// If we find a definition in a CDM Standard Schema file, we add that file to importsList.
        /// At the same time, the found definition is removed from extensionTraitDefList.
        /// When this function returns, extensionTraitDefList only contains definitions that are not present in any CDM Standard Schema file,
        /// and a list of CDM Standard Schema files with relevant definitions is returned.
        /// </summary>
        /// <param name="ctx"> The context</param>
        /// <param name="extensionTraitDefList"> The list of all definitions for all found extensions. Function modifies this list by removing definitions found in CDM Standard Schema files.</param>
        /// <returns> A list of CDM Standard Schema files to import.</returns>
        public static async Task <List <CdmImport> > StandardImportDetection(CdmCorpusContext ctx, CdmCollection <CdmTraitDefinition> extensionTraitDefList)
        {
            List <CdmImport> importsList = new List <CdmImport>();

            // have to go from end to start because I might remove elements
            for (int traitIndex = extensionTraitDefList.Count - 1; traitIndex >= 0; traitIndex--)
            {
                CdmTraitDefinition extensionTraitDef = extensionTraitDefList[traitIndex];
                if (!TraitDefIsExtension(extensionTraitDef))
                {
                    Logger.Error(nameof(ExtensionHelper), ctx, $"Invalid extension trait name {extensionTraitDef.TraitName}, expected prefix {ExtensionTraitNamePrefix}.");

                    return(null);
                }

                string[] extensionBreakdown = RemoveExtensionTraitNamePrefix(extensionTraitDef.TraitName).Split(':');
                if (extensionBreakdown.Length > 1)
                {
                    string extensionName  = extensionBreakdown[0];
                    string fileName       = $"{extensionName}.extension.cdm.json";
                    string fileCorpusPath = $"cdm:/extensions/{fileName}";
                    CdmDocumentDefinition extensionDoc = await FetchDefDoc(ctx, fileName);

                    // If no document was found for that extensionName, the trait does not have a document with it's definition.
                    // Trait will be kept in extensionTraitDefList (a document with its definition will be created locally)
                    if (extensionDoc == null)
                    {
                        continue;
                    }

                    // There is a document with extensionName, now we search for the trait in the document.
                    // If we find it, we remove the trait from extensionTraitDefList and add the document to imports.
                    CdmTraitDefinition matchingTrait = extensionDoc.Definitions.AllItems.Find(
                        (definition) => definition.ObjectType == CdmObjectType.TraitDef && definition.GetName() == extensionTraitDef.TraitName)
                                                       as CdmTraitDefinition;
                    if (matchingTrait != null)
                    {
                        List <CdmParameterDefinition> parameterList = matchingTrait.Parameters.AllItems;
                        if (
                            extensionTraitDef.Parameters.AllItems.TrueForAll(
                                (CdmParameterDefinition extensionParameter) =>
                                parameterList.Exists(
                                    (CdmParameterDefinition defParameter) => defParameter.Name == extensionParameter.Name)
                                )
                            )
                        {
                            extensionTraitDefList.Remove(extensionTraitDefList[traitIndex]);
                            if (!importsList.Exists((CdmImport importDoc) => importDoc.CorpusPath == fileCorpusPath))
                            {
                                CdmImport importObject = ctx.Corpus.MakeObject <CdmImport>(CdmObjectType.Import);
                                importObject.CorpusPath = fileCorpusPath;
                                importsList.Add(importObject);
                            }
                        }
                    }
                }
            }
            return(importsList);
        }
示例#26
0
        /// <summary>
        /// Create and initialize _allImports file
        /// </summary>
        /// <returns></returns>
        public CdmDocumentDefinition CreateAndInitializeAllImportsFile()
        {
            CdmDocumentDefinition allImportsDoc = new CdmDocumentDefinition(Corpus.Ctx, AllImportsName);

            CdmDocumentDefinition allImportsDocDef = LocalStorageRoot.Documents.Add(allImportsDoc, AllImportsDocName);

            allImportsDocDef.Imports.Add(FoundationJsonPath);

            return(allImportsDocDef);
        }
示例#27
0
 public static DocumentContent ToData(CdmDocumentDefinition instance, ResolveOptions resOpt, CopyOptions options)
 {
     return(new DocumentContent
     {
         Schema = instance.Schema,
         JsonSchemaSemanticVersion = instance.JsonSchemaSemanticVersion,
         Imports = Utils.ListCopyData <Import>(resOpt, instance.Imports, options),
         Definitions = Utils.ListCopyData(resOpt, instance.Definitions, options)
     });
 }
示例#28
0
        }                                         // moniker that was found on the ref

        /// <summary>
        /// Creates a new instance of Resolve Options using most common parameters.
        /// </summary>
        /// <param name="cdmDocument">Document to use as point of reference when resolving relative paths and symbol names.</param>
        public ResolveOptions(CdmDocumentDefinition cdmDocument)
        {
            WrtDoc = cdmDocument;
            // avoid one to many relationship nesting and to use foreign keys for many to one refs.
            Directives = new AttributeResolutionDirectiveSet(new HashSet <string>()
            {
                "normalized", "referenceOnly"
            });
            SymbolRefSet = new SymbolSet();
        }
示例#29
0
 /// <summary>
 /// Creates a new instance of Resolve Options using most common parameters.
 /// </summary>
 /// <param name="cdmDocument">Document to use as point of reference when resolving relative paths and symbol names.</param>
 /// <param name="Directives">Directives to use when resolving attributes</param>
 public ResolveOptions(CdmDocumentDefinition cdmDocument, AttributeResolutionDirectiveSet Directives = null)
     : this()
 {
     WrtDoc = cdmDocument;
     // provided or default to 'avoid one to many relationship nesting and to use foreign keys for many to one refs'. this is for back compat with behavior before the corpus has a default directive property
     this.Directives = Directives != null?Directives.Copy() : new AttributeResolutionDirectiveSet(new HashSet <string>()
     {
         "normalized", "referenceOnly"
     });
 }
示例#30
0
        public async Task TestDeeperCircularImportWithMoniker()
        {
            var corpus = TestHelper.GetLocalCorpus("", "");
            var folder = corpus.Storage.FetchRootFolder("local");

            var docA = new CdmDocumentDefinition(corpus.Ctx, "A.cdm.json");

            folder.Documents.Add(docA);
            docA.Imports.Add("B.cdm.json");

            var docB = new CdmDocumentDefinition(corpus.Ctx, "B.cdm.json");

            folder.Documents.Add(docB);
            docB.Imports.Add("C.cdm.json", "moniker");

            var docC = new CdmDocumentDefinition(corpus.Ctx, "C.cdm.json");

            folder.Documents.Add(docC);
            docC.Imports.Add("D.cdm.json");

            var docD = new CdmDocumentDefinition(corpus.Ctx, "D.cdm.json");

            folder.Documents.Add(docD);
            docD.Imports.Add("C.cdm.json");

            // indexIfNeeded will internally call prioritizeImports on every document.
            await docA.IndexIfNeeded(new ResolveOptions(), true);

            Assert.AreEqual(4, docA.ImportPriorities.ImportPriority.Count);
            AssertImportInfo(docA.ImportPriorities.ImportPriority[docA], 0, false);
            AssertImportInfo(docA.ImportPriorities.ImportPriority[docB], 1, false);
            AssertImportInfo(docA.ImportPriorities.ImportPriority[docD], 2, false);
            AssertImportInfo(docA.ImportPriorities.ImportPriority[docC], 3, false);

            // reset the importsPriorities.
            MarkDocumentsToIndex(folder.Documents);

            // force docC to be indexed first, so the priorityList will be read from the cache this time.
            await docC.IndexIfNeeded(new ResolveOptions(), true);

            await docA.IndexIfNeeded(new ResolveOptions(), true);

            Assert.AreEqual(4, docA.ImportPriorities.ImportPriority.Count);

            // indexes the rest of the documents.
            await docB.IndexIfNeeded(new ResolveOptions(), true);

            await docD.IndexIfNeeded(new ResolveOptions(), true);

            Assert.IsFalse(docA.ImportPriorities.hasCircularImport);
            Assert.IsFalse(docB.ImportPriorities.hasCircularImport);
            Assert.IsTrue(docC.ImportPriorities.hasCircularImport);
            Assert.IsTrue(docD.ImportPriorities.hasCircularImport);
        }