public async Task TestLoadingInvalidModelJsonName()
        {
            var testName      = "TestLoadingInvalidModelJsonName";
            var testInputPath = TestHelper.GetInputFolderPath(testsSubpath, testName);

            CdmCorpusDefinition corpus = new CdmCorpusDefinition();

            corpus.SetEventCallback(new EventCallback {
                Invoke = CommonDataModelLoader.ConsoleStatusReport
            }, CdmStatusLevel.Warning);
            corpus.Storage.Mount("local", new LocalAdapter(testInputPath));
            corpus.Storage.DefaultNamespace = "local";

            // We are trying to load a file with an invalid name, so FetchObjectAsync() should just return null.
            var invalidModelJson = await corpus.FetchObjectAsync <CdmManifestDefinition>("test.model.json");

            Assert.IsNull(invalidModelJson);
        }
Exemple #2
0
        public async Task TestResolveWithExtended()
        {
            CdmCorpusDefinition cdmCorpus = TestHelper.GetLocalCorpus(testsSubpath, "TestResolveWithExtended");

            cdmCorpus.SetEventCallback(new EventCallback {
                Invoke = (CdmStatusLevel statusLevel, string message) =>
                {
                    if (message.Contains("unable to resolve the reference"))
                    {
                        Assert.Fail();
                    }
                }
            }, CdmStatusLevel.Warning);

            CdmEntityDefinition ent = await cdmCorpus.FetchObjectAsync <CdmEntityDefinition>("local:/sub/Account.cdm.json/Account");

            await ent.CreateResolvedEntityAsync("Account_");
        }
Exemple #3
0
        public async Task TestRunSequentiallyAndSourceInput()
        {
            string testName            = "TestRunSequentiallyAndSourceInput";
            string entityName          = "NewPerson";
            CdmCorpusDefinition corpus = ProjectionTestUtils.GetLocalCorpus(testsSubpath, testName);

            CdmEntityDefinition entity = await corpus.FetchObjectAsync <CdmEntityDefinition>($"local:/{entityName}.cdm.json/{entityName}");

            CdmEntityDefinition resolvedEntity = await ProjectionTestUtils.GetResolvedEntity(corpus, entity, new List <string> {
            });

            // Original set of attributes: ["name", "age", "address", "phoneNumber", "email"]
            // Replace "age" with "ageFK" and "address" with "addressFK" as foreign keys, followed by a add count attribute.
            Assert.AreEqual(3, resolvedEntity.Attributes.Count);
            Assert.AreEqual("ageFK", (resolvedEntity.Attributes[0] as CdmTypeAttributeDefinition).Name);
            Assert.AreEqual("addressFK", (resolvedEntity.Attributes[1] as CdmTypeAttributeDefinition).Name);
            Assert.AreEqual("countAttribute", (resolvedEntity.Attributes[2] as CdmTypeAttributeDefinition).Name);
        }
Exemple #4
0
        public async Task TestMaxDepthOnPolymorphicEntity()
        {
            string testName   = "TestMaxDepthOnPolymorphicEntity";
            string entityName = "A";

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

            CdmEntityDefinition entity = await corpus.FetchObjectAsync <CdmEntityDefinition>($"{entityName}.cdm.json/{entityName}");

            ResolveOptions resOpt = new ResolveOptions(entity)
            {
                MaxDepth = 1
            };
            CdmEntityDefinition resEntity = await entity.CreateResolvedEntityAsync($"resolved-{entityName}", resOpt);

            Assert.IsNotNull(resEntity);
            Assert.AreEqual(4, resEntity.Attributes.Count);
        }
Exemple #5
0
        public async Task TestLoadingConfigAndTryingToFetchManifest()
        {
            var testInputPath = TestHelper.GetInputFolderPath(testsSubpath, "TestLoadingConfigAndTryingToFetchManifest");

            // Create a corpus to load the config.
            var cdmCorpus = this.GetLocalCorpus(testInputPath);

            var config = await cdmCorpus.Storage.FetchAdapter("local").ReadAsync("/config.json");

            var differentCorpus = new CdmCorpusDefinition();

            var unrecognizedAdapters = differentCorpus.Storage.MountFromConfig(config, true);

            var cdmManifest = await differentCorpus.FetchObjectAsync <CdmManifestDefinition>("model.json", cdmCorpus.Storage.FetchRootFolder("local"));

            Assert.IsNotNull(cdmManifest);
            Assert.AreEqual(1, unrecognizedAdapters.Count);
        }
Exemple #6
0
        public void TestLoadingBlankName()
        {
            var corpus = new CdmCorpusDefinition();

            var argumentData = new JObject()
            {
                ["name"]  = " ",
                ["value"] = 0
            };

            var argument = ArgumentPersistence.FromData(corpus.Ctx, argumentData);

            Assert.AreEqual(0, argument.Value);

            var argumentToData = ArgumentPersistence.ToData(argument, null, null);

            Assert.AreEqual(0, argumentToData);
        }
Exemple #7
0
        private CdmCorpusDefinition InitializeClientWithUserDatabase()
        {
            CdmCorpusDefinition corpus = TestHelper.GetLocalCorpus(testsSubpath, "TestTelemetryKustoClient");

            // TODO: need to investigate why only Java not failing if using event callback from GetLocalCorpus()
            // set callback to receive error and warning logs.
            corpus.SetEventCallback(new EventCallback
            {
                Invoke = (level, message) =>
                {
                    // Do nothing
                }
            }, CdmStatusLevel.Progress);


            string tenantId = Environment.GetEnvironmentVariable("KUSTO_TENANTID");
            string clientId = Environment.GetEnvironmentVariable("KUSTO_CLIENTID");
            string secret   = Environment.GetEnvironmentVariable("KUSTO_SECRET");

            string clusterName  = Environment.GetEnvironmentVariable("KUSTO_CLUSTER");
            string databaseName = Environment.GetEnvironmentVariable("KUSTO_DATABASE");
            string infoTable    = Environment.GetEnvironmentVariable("KUSTO_INFOTABLE");
            string warningTable = Environment.GetEnvironmentVariable("KUSTO_WARNINGTABLE");
            string errorTable   = Environment.GetEnvironmentVariable("KUSTO_ERRORTABLE");

            Assert.IsFalse(string.IsNullOrEmpty(tenantId), "KUSTO_TENANTID not set");
            Assert.IsFalse(string.IsNullOrEmpty(clientId), "KUSTO_CLIENTID not set");
            Assert.IsFalse(string.IsNullOrEmpty(secret), "KUSTO_SECRET not set");

            Assert.IsFalse(string.IsNullOrEmpty(clusterName), "KUSTO_CLUSTER not set");
            Assert.IsFalse(string.IsNullOrEmpty(databaseName), "KUSTO_DATABASE not set");
            Assert.IsFalse(string.IsNullOrEmpty(infoTable), "KUSTO_INFOTABLE not set");
            Assert.IsFalse(string.IsNullOrEmpty(warningTable), "KUSTO_WARNINGTABLE not set");
            Assert.IsFalse(string.IsNullOrEmpty(errorTable), "KUSTO_ERRORTABLE not set");

            TelemetryConfig kustoConfig = new TelemetryConfig(tenantId, clientId, secret,
                                                              clusterName, databaseName, infoTable, warningTable, errorTable, EnvironmentType.DEV, removeUserContent: false);

            corpus.TelemetryClient = new TelemetryKustoClient(corpus.Ctx, kustoConfig);

            corpus.AppId = "CDM Integration Test";

            return(corpus);
        }
Exemple #8
0
        private static async Task SaveCDMDocuments(string nameSpace, CdmCorpusDefinition cdmCorpus)
        {
            Console.WriteLine("Make placeholder manifest");
            // Make the temp manifest and add it to the root of the local documents in the corpus
            CdmManifestDefinition manifestAbstract = cdmCorpus.MakeObject <CdmManifestDefinition>(CdmObjectType.ManifestDef, "tempAbstract");

            // Add the temp manifest to the root of the local documents in the corpus.
            var localRoot = cdmCorpus.Storage.FetchRootFolder(nameSpace);

            localRoot.Documents.Add(manifestAbstract);

            // Create two entities from scratch, and add some attributes, traits, properties, and relationships in between
            Console.WriteLine("Create net new entities");

            DTDLParser parser = new DTDLParser(dtdlRoot, "json");

            foreach (DTInterfaceInfo info in parser.DTDLInterfaces)
            {
                CreateCustomEntity(cdmCorpus, manifestAbstract, localRoot, info);
            }

            // Create the resolved version of everything in the root folder too
            Console.WriteLine("Resolve the placeholder");
            var manifestResolved = await manifestAbstract.CreateResolvedManifestAsync("default", null);

            // Add an import to the foundations doc so the traits about partitons will resolve nicely
            manifestResolved.Imports.Add("cdm:/foundations.cdm.json");

            Console.WriteLine($"Save the folders and partition data documents to {nameSpace} storage.");
            await SavePartitionDocuments(nameSpace, cdmCorpus, manifestResolved);

            Console.WriteLine($"Save the manifest, entity and resolved documents to {nameSpace} storage.");
            // Save all other files (resolved, manifest, entity etc.)

            var manifestFileName = $"{manifestResolved.ManifestName}.manifest.cdm.json";
            var manifestSaved    = await manifestResolved.SaveAsAsync(manifestFileName, true);

            LogSaveOutput(manifestSaved, manifestFileName);

            var modelFileName = "model.json";
            var modelSaved    = await manifestResolved.SaveAsAsync(modelFileName, true);

            LogSaveOutput(modelSaved, modelFileName);
        }
Exemple #9
0
        public async Task ResolveEntities()
        {
            var cdmCorpus = new CdmCorpusDefinition();

            var testInputPath = TestHelper.GetInputFolderPath(testsSubpath, "TestResolveEntities");

            cdmCorpus.RootPath = testInputPath;
            cdmCorpus.Storage.Mount("local", new LocalAdapter(testInputPath));
            cdmCorpus.Storage.DefaultNamespace = "local";
            var entities = await this.GetAllEntities(cdmCorpus);

            var entityResolutionTimes = new List <Tuple <string, long> >();

            foreach (var data in entities)
            {
                var entity     = data.Item1;
                var doc        = data.Item2;
                var directives = new AttributeResolutionDirectiveSet(new HashSet <string> {
                    "normalized", "referenceOnly"
                });
                var resOpt = new ResolveOptions {
                    WrtDoc = doc, Directives = directives
                };
                var watch = Stopwatch.StartNew();
                await entity.CreateResolvedEntityAsync($"{entity.GetName()}_", resOpt); watch.Stop();
                entityResolutionTimes.Add(Tuple.Create(entity.AtCorpusPath, watch.ElapsedMilliseconds));
            }

            entityResolutionTimes.Sort((lhs, rhs) =>
            {
                var diff = rhs.Item2 - lhs.Item2;
                return(diff == 0 ? 0 : diff < 0 ? -1 : 1);
            });

            foreach (var data in entityResolutionTimes)
            {
                Trace.WriteLine($"{data.Item1}:{data.Item2}");
            }

            Assert.Performance(1000, entityResolutionTimes[0].Item2);
            var total = entityResolutionTimes.Sum(data => data.Item2);

            Assert.Performance(5000, total);
        }
Exemple #10
0
        public void TestEntityAttributeSource()
        {
            CdmCorpusDefinition corpus = new CdmCorpusDefinition();
            int errorCount             = 0;

            corpus.SetEventCallback(new EventCallback()
            {
                Invoke = (level, message) =>
                {
                    errorCount++;
                }
            }, CdmStatusLevel.Error);
            CdmProjection projection       = new CdmProjection(corpus.Ctx);
            CdmEntityAttributeDefinition _ = new CdmEntityAttributeDefinition(corpus.Ctx, "attribute")
            {
                Entity = new CdmEntityReference(corpus.Ctx, projection, false)
            };

            // First case, a projection without source.
            projection.Validate();
            Assert.AreEqual(1, errorCount);
            errorCount = 0;

            // Second case, a projection with a nested projection.
            CdmProjection innerProjection = new CdmProjection(corpus.Ctx);

            projection.Source = new CdmEntityReference(corpus.Ctx, innerProjection, false);
            projection.Validate();
            innerProjection.Validate();
            Assert.AreEqual(1, errorCount);
            errorCount = 0;

            // Third case, a projection with an explicit entity definition.
            innerProjection.Source = new CdmEntityReference(corpus.Ctx, new CdmEntityDefinition(corpus.Ctx, "Entity"), false);
            projection.Validate();
            innerProjection.Validate();
            Assert.AreEqual(0, errorCount);

            // Third case, a projection with a named reference.
            innerProjection.Source = new CdmEntityReference(corpus.Ctx, "Entity", false);
            projection.Validate();
            innerProjection.Validate();
            Assert.AreEqual(0, errorCount);
        }
Exemple #11
0
        internal async Task <bool> IndexIfNeeded(ResolveOptions resOpt)
        {
            if (this.NeedsIndexing)
            {
                // make the corpus internal machinery pay attention to this document for this call
                CdmCorpusDefinition corpus = (this.Folder as CdmFolderDefinition).Corpus;

                ConcurrentDictionary <CdmDocumentDefinition, byte> docsJustAdded = new ConcurrentDictionary <CdmDocumentDefinition, byte>();
                ConcurrentDictionary <string, byte> docsNotFound = new ConcurrentDictionary <string, byte>();
                await corpus.ResolveImportsAsync(this, docsJustAdded, docsNotFound);

                // maintain actual current doc
                docsJustAdded[this] = 1;

                return(corpus.IndexDocuments(resOpt, this, docsJustAdded));
            }

            return(true);
        }
Exemple #12
0
        public async Task TestEntityAttributeProjUsingObjectModel()
        {
            CdmCorpusDefinition corpus = TestHelper.GetLocalCorpus(testsSubpath, "TestEntityAttributeProjUsingObjectModel");

            corpus.Storage.Mount("local", new LocalAdapter(TestHelper.GetActualOutputFolderPath(testsSubpath, "TestEntityAttributeProjUsingObjectModel")));
            CdmFolderDefinition localRoot = corpus.Storage.FetchRootFolder("local");

            // Create an entity
            CdmEntityDefinition entity = ProjectionTestUtils.CreateEntity(corpus, localRoot);

            // Create a projection
            CdmProjection projection = ProjectionTestUtils.CreateProjection(corpus, localRoot);

            // Create an ExcludeAttributes operation
            CdmOperationExcludeAttributes excludeAttrsOp = corpus.MakeObject <CdmOperationExcludeAttributes>(CdmObjectType.OperationExcludeAttributesDef);

            excludeAttrsOp.ExcludeAttributes = new List <string>()
            {
                "id", "date"
            };
            projection.Operations.Add(excludeAttrsOp);

            // Create an entity reference to hold this projection
            CdmEntityReference projectionEntityRef = corpus.MakeObject <CdmEntityReference>(CdmObjectType.EntityRef, null);

            projectionEntityRef.ExplicitReference = projection;

            // Create an entity attribute that contains this projection and add this to the entity
            CdmEntityAttributeDefinition entityAttribute = corpus.MakeObject <CdmEntityAttributeDefinition>(CdmObjectType.EntityAttributeDef, "TestEntityAttribute");

            entityAttribute.Entity = projectionEntityRef;
            entity.Attributes.Add(entityAttribute);

            // Resolve the entity
            CdmEntityDefinition resolvedEntity = await entity.CreateResolvedEntityAsync($"Resolved_{entity.EntityName}.cdm.json", null, localRoot);

            // Verify correctness of the resolved attributes after running the ExcludeAttributes operation
            // Original set of attributes: ["id", "name", "value", "date"]
            // Excluded attributes: ["id", "date"]
            Assert.AreEqual(2, resolvedEntity.Attributes.Count);
            Assert.AreEqual("name", (resolvedEntity.Attributes[0] as CdmTypeAttributeDefinition).Name);
            Assert.AreEqual("value", (resolvedEntity.Attributes[1] as CdmTypeAttributeDefinition).Name);
        }
Exemple #13
0
        public async Task TestSystemAndResourceAdapters()
        {
            var path = TestHelper.GetExpectedOutputFolderPath(testsSubpath, "TestSystemAndResourceAdapters");

            // Create a corpus to load the config.
            var cdmCorpus = this.GetLocalCorpus(path);

            var differentCorpus = new CdmCorpusDefinition();

            differentCorpus.Storage.Unmount("cdm");

            differentCorpus.Storage.DefaultNamespace = "local";

            var resultConfig = differentCorpus.Storage.FetchConfig();

            var outputConfig = await cdmCorpus.Storage.NamespaceAdapters["local"].ReadAsync("/config.json");

            Assert.AreEqual(outputConfig, resultConfig);
        }
Exemple #14
0
        /// <summary>
        /// Resolve the entities in the given manifest.
        /// </summary>
        /// <param name="testName">The name of the test. It is used to decide the path of input / output files. </param>
        /// <parameter name="manifestName">The name of the manifest to be used. </parameter>
        /// <returns> The resolved entities. </returns>
        private async Task <string> ResolveEnvironment(string testName, string manifestName)
        {
            var cdmCorpus = new CdmCorpusDefinition();

            cdmCorpus.SetEventCallback(new EventCallback {
                Invoke = CommonDataModelLoader.ConsoleStatusReport
            }, CdmStatusLevel.Warning);
            var testLocalAdapter = this.CreateStorageAdapterConfigForTest(testName);

            cdmCorpus.Storage.Mount("local", testLocalAdapter);

            var manifest = await cdmCorpus.FetchObjectAsync <CdmManifestDefinition>($"local:/{manifestName}.manifest.cdm.json");

            var directives = new AttributeResolutionDirectiveSet(new HashSet <string> {
                "normalized", "referenceOnly"
            });

            return(await ListAllResolved(cdmCorpus, directives, manifest, new StringSpewCatcher()));
        }
Exemple #15
0
        private async static Task <CdmManifestDefinition> LoadAndResolveManifest(CdmCorpusDefinition corpus, CdmManifestDefinition manifest, string renameSuffix)
        {
            Console.WriteLine("Resolving manifest " + manifest.ManifestName + " ...");
            CdmManifestDefinition resolvedManifest = await manifest.CreateResolvedManifestAsync(manifest.ManifestName + renameSuffix, "{n}-resolved.cdm.json");

            foreach (CdmManifestDeclarationDefinition subManifestDecl in manifest.SubManifests)
            {
                CdmManifestDefinition subManifest = await corpus.FetchObjectAsync <CdmManifestDefinition>(subManifestDecl.Definition, manifest);

                CdmManifestDefinition resolvedSubManifest = await LoadAndResolveManifest(corpus, subManifest, renameSuffix);

                CdmManifestDeclarationDefinition resolvedDecl = corpus.MakeObject <CdmManifestDeclarationDefinition>(CdmObjectType.ManifestDeclarationDef, resolvedSubManifest.ManifestName);
                resolvedDecl.Definition = corpus.Storage.CreateRelativeCorpusPath(resolvedSubManifest.AtCorpusPath, resolvedManifest);

                resolvedManifest.SubManifests.Add(resolvedDecl);
            }

            return(resolvedManifest);
        }
        /// <summary>
        /// Test that a document is fetched using the syms persistence class.
        /// </summary>
        internal async Task TestSymsFetchDocument(CdmCorpusDefinition corpus, CdmManifestDefinition manifestExpected)
        {
            foreach (var ent in manifestExpected.Entities)
            {
                var doc = await corpus.FetchObjectAsync <CdmDocumentDefinition>($"syms:/{manifestExpected.ManifestName}/{ent.EntityName}.cdm.json");

                Assert.IsNotNull(doc);
                Assert.IsTrue(string.Equals($"{ent.EntityName}.cdm.json", doc.Name));
                await doc.SaveAsAsync($"localActOutput:/{doc.Name}");

                var docLocal = await corpus.FetchObjectAsync <CdmDocumentDefinition>(doc.Name);

                await docLocal.SaveAsAsync($"localExpOutput:/{doc.Name}");

                var actualContent   = TestHelper.GetActualOutputFileContent(testsSubpath, nameof(TestSymsSavingAndFetchingDocument), doc.Name);
                var expectedContent = TestHelper.GetExpectedOutputFileContent(testsSubpath, nameof(TestSymsSavingAndFetchingDocument), doc.Name);
                TestHelper.AssertSameObjectWasSerialized(actualContent, expectedContent);
            }
        }
Exemple #17
0
        public async Task TestEntityProjection()
        {
            string testName   = "TestEntityProjection";
            string entityName = testName;

            CdmCorpusDefinition   corpus   = TestHelper.GetLocalCorpus(testsSubpath, testName);
            CdmManifestDefinition manifest = await corpus.FetchObjectAsync <CdmManifestDefinition>($"local:/default.manifest.cdm.json");

            string expectedOutputPath = TestHelper.GetExpectedOutputFolderPath(testsSubpath, testName);

            CdmEntityDefinition entTestEntityProjection = await corpus.FetchObjectAsync <CdmEntityDefinition>($"local:/{entityName}.cdm.json/{entityName}", manifest);

            Assert.IsNotNull(entTestEntityProjection);
            CdmEntityDefinition resolvedTestEntityProjection = await TestUtils.GetResolvedEntity(corpus, entTestEntityProjection, new List <string> {
            });

            Assert.IsNotNull(resolvedTestEntityProjection);
            AttributeContextUtil.ValidateAttributeContext(corpus, expectedOutputPath, entityName, resolvedTestEntityProjection);
        }
        public async Task TestEntityAttributeProjUsingObjectModel()
        {
            string testName               = "TestEntityAttributeProjUsingObjectModel";
            CdmCorpusDefinition corpus    = ProjectionTestUtils.GetCorpus(testName, testsSubpath);
            CdmFolderDefinition localRoot = corpus.Storage.FetchRootFolder("local");

            // Create an entity
            CdmEntityDefinition entity = ProjectionTestUtils.CreateEntity(corpus, localRoot);

            // Create a projection
            CdmProjection projection = ProjectionTestUtils.CreateProjection(corpus, localRoot);

            // Create an AddAttributeGroup operation
            CdmOperationAddAttributeGroup addAttGroupOp = corpus.MakeObject <CdmOperationAddAttributeGroup>(CdmObjectType.OperationAddAttributeGroupDef);

            addAttGroupOp.AttributeGroupName = "PersonAttributeGroup";
            projection.Operations.Add(addAttGroupOp);

            // Create an entity reference to hold this projection
            CdmEntityReference projectionEntityRef = corpus.MakeObject <CdmEntityReference>(CdmObjectType.EntityRef, null);

            projectionEntityRef.ExplicitReference = projection;

            // Create an entity attribute that contains this projection and add this to the entity
            CdmEntityAttributeDefinition entityAttribute = corpus.MakeObject <CdmEntityAttributeDefinition>(CdmObjectType.EntityAttributeDef, "TestEntityAttribute");

            entityAttribute.Entity = projectionEntityRef;
            entity.Attributes.Add(entityAttribute);

            // Resolve the entity.
            CdmEntityDefinition resolvedEntity = await entity.CreateResolvedEntityAsync($"Resolved_{entity.EntityName}.cdm.json", null, localRoot);

            // Verify correctness of the resolved attributes after running the AddAttributeGroup operation
            // Original set of attributes: ["id", "name", "value", "date"]
            CdmAttributeGroupDefinition attGroupDefinition = this.ValidateAttributeGroup(resolvedEntity.Attributes, "PersonAttributeGroup");

            Assert.AreEqual(4, attGroupDefinition.Members.Count);
            Assert.AreEqual("id", (attGroupDefinition.Members[0] as CdmTypeAttributeDefinition).Name);
            Assert.AreEqual("name", (attGroupDefinition.Members[1] as CdmTypeAttributeDefinition).Name);
            Assert.AreEqual("value", (attGroupDefinition.Members[2] as CdmTypeAttributeDefinition).Name);
            Assert.AreEqual("date", (attGroupDefinition.Members[3] as CdmTypeAttributeDefinition).Name);
        }
Exemple #19
0
        static async Task Main(string[] args)
        {
            // ------------------------------------------------------------------------------------------------------------
            // Instantiate a corpus. The corpus is the collection of all documents and folders created or discovered
            // while navigating objects and paths.

            var cdmCorpus = new CdmCorpusDefinition();

            // ------------------------------------------------------------------------------------------------------------
            // Configure storage adapters and mount them to the corpus.

            // We want our storage adapters to point at the local manifest location and at the example public standards.
            string pathFromExeToExampleRoot = "../../../../../../";

            // Storage adapter pointing to the target local manifest location.
            cdmCorpus.Storage.Mount("local", new LocalAdapter(pathFromExeToExampleRoot + "1-read-manifest/sample-data"));

            // 'local' is our default namespace.
            // Any paths that start navigating without a device tag (ex. 'cdm') will just default to the 'local' namepace.
            cdmCorpus.Storage.DefaultNamespace = "local";

            // Storage adapter pointing to the example public standards.
            // This is a fake 'cdm'; normally the Github adapter would be used to point at the real public standards.
            // Mount it as the 'cdm' device, not the default, so that we must use "cdm:<folder-path>" to get there.
            cdmCorpus.Storage.Mount("cdm", new LocalAdapter(pathFromExeToExampleRoot + "example-public-standards"));

            // Example how to mount to the ADLS:
            // cdmCorpus.Storage.Mount("adls",
            //    new ADLSAdapter(
            //      "<ACCOUNT-NAME>.dfs.core.windows.net", // Hostname.
            //      "/<FILESYSTEM-NAME>", // Root.
            //      "72f988bf-86f1-41af-91ab-2d7cd011db47",  // Tenant ID.
            //      "<CLIENT-ID>",  // Client ID.
            //      "<CLIENT-SECRET>" // Client secret.
            //    )
            // );

            // ------------------------------------------------------------------------------------------------------------
            // Open the default manifest file at the root.

            await ExploreManifest(cdmCorpus, "default.manifest.cdm.json");
        }
Exemple #20
0
        public async Task TestEntityProjUsingObjectModel()
        {
            CdmCorpusDefinition corpus = ProjectionTestUtils.GetLocalCorpus(testsSubpath, "TestEntityProjUsingObjectModel");

            corpus.Storage.Mount("local", new LocalAdapter(TestHelper.GetActualOutputFolderPath(testsSubpath, "TestEntityProjUsingObjectModel")));
            CdmFolderDefinition localRoot = corpus.Storage.FetchRootFolder("local");

            // Create an entity
            CdmEntityDefinition entity = ProjectionTestUtils.CreateEntity(corpus, localRoot);

            // Create a projection
            CdmProjection projection = ProjectionTestUtils.CreateProjection(corpus, localRoot);

            // Create an AddTypeAttribute operation
            CdmOperationAddTypeAttribute addTypeAttrOp = corpus.MakeObject <CdmOperationAddTypeAttribute>(CdmObjectType.OperationAddTypeAttributeDef);

            addTypeAttrOp.TypeAttribute          = corpus.MakeObject <CdmTypeAttributeDefinition>(CdmObjectType.TypeAttributeDef, "testType");
            addTypeAttrOp.TypeAttribute.DataType = corpus.MakeRef <CdmDataTypeReference>(CdmObjectType.DataTypeRef, "entityName", true);
            projection.Operations.Add(addTypeAttrOp);

            // Create an entity reference to hold this projection
            CdmEntityReference projectionEntityRef = corpus.MakeObject <CdmEntityReference>(CdmObjectType.EntityRef, null);

            projectionEntityRef.ExplicitReference = projection;

            // Set the entity's ExtendEntity to be the projection
            entity.ExtendsEntity = projectionEntityRef;

            // Resolve the entity
            CdmEntityDefinition resolvedEntity = await entity.CreateResolvedEntityAsync($"Resolved_{entity.EntityName}.cdm.json", null, localRoot);

            // Verify correctness of the resolved attributes after running the AddTypeAttribute operation
            // Original set of attributes: ["id", "name", "value", "date"]
            // Type attribute: "testType"
            Assert.AreEqual(5, resolvedEntity.Attributes.Count);
            Assert.AreEqual("id", (resolvedEntity.Attributes[0] as CdmTypeAttributeDefinition).Name);
            Assert.AreEqual("name", (resolvedEntity.Attributes[1] as CdmTypeAttributeDefinition).Name);
            Assert.AreEqual("value", (resolvedEntity.Attributes[2] as CdmTypeAttributeDefinition).Name);
            Assert.AreEqual("date", (resolvedEntity.Attributes[3] as CdmTypeAttributeDefinition).Name);
            Assert.AreEqual("testType", (resolvedEntity.Attributes[4] as CdmTypeAttributeDefinition).Name);
            Assert.AreEqual("is.linkedEntity.name", resolvedEntity.Attributes[4].AppliedTraits[4].NamedReference);
        }
Exemple #21
0
        public async Task TestReadingIsPrimaryKey()
        {
            var testInputPath          = TestHelper.GetInputFolderPath(testsSubpath, "TestReadingIsPrimaryKey");
            CdmCorpusDefinition corpus = new CdmCorpusDefinition();

            corpus.SetEventCallback(new EventCallback {
                Invoke = CommonDataModelLoader.ConsoleStatusReport
            }, CdmStatusLevel.Warning);
            corpus.Storage.Mount("local", new LocalAdapter(testInputPath));
            corpus.Storage.DefaultNamespace = "local";

            // Read from an unresolved entity schema.
            CdmEntityDefinition entity = await corpus.FetchObjectAsync <CdmEntityDefinition>("local:/TeamMembership.cdm.json/TeamMembership");

            CdmAttributeGroupReference  attributeGroupRef = (CdmAttributeGroupReference)entity.Attributes[0];
            CdmAttributeGroupDefinition attributeGroup    = (CdmAttributeGroupDefinition)attributeGroupRef.ExplicitReference;
            CdmTypeAttributeDefinition  typeAttribute     = (CdmTypeAttributeDefinition)attributeGroup.Members[0];

            Assert.IsTrue((bool)typeAttribute.IsPrimaryKey);

            // Check that the trait "is.identifiedBy" is created with the correct argument.
            CdmTraitReference isIdentifiedBy1 = typeAttribute.AppliedTraits[1];

            Assert.AreEqual("is.identifiedBy", isIdentifiedBy1.NamedReference);
            Assert.AreEqual("TeamMembership/(resolvedAttributes)/teamMembershipId", isIdentifiedBy1.Arguments[0].Value);

            // Read from a resolved entity schema.
            CdmEntityDefinition resolvedEntity = await corpus.FetchObjectAsync <CdmEntityDefinition>("local:/TeamMembership_Resolved.cdm.json/TeamMembership");

            CdmTypeAttributeDefinition resolvedTypeAttribute = (CdmTypeAttributeDefinition)resolvedEntity.Attributes[0];

            Assert.IsTrue((bool)resolvedTypeAttribute.IsPrimaryKey);

            // Check that the trait "is.identifiedBy" is created with the correct argument.
            CdmTraitReference isIdentifiedBy2 = resolvedTypeAttribute.AppliedTraits[6];

            Assert.AreEqual("is.identifiedBy", isIdentifiedBy2.NamedReference);

            CdmAttributeReference argumentValue = isIdentifiedBy2.Arguments[0].Value;

            Assert.AreEqual("TeamMembership/(resolvedAttributes)/teamMembershipId", argumentValue.NamedReference);
        }
Exemple #22
0
        /// <summary>
        /// Loads an entity, resolves it, and then validates the generated attribute contexts
        /// </summary>
        private async Task LoadEntityForResolutionOptionAndSave(string testName, string entityName, List <string> resOpts)
        {
            string expectedOutputPath = TestHelper.GetExpectedOutputFolderPath(testsSubpath, testName);
            string fileNameSuffix     = ProjectionTestUtils.GetResolutionOptionNameSuffix(resOpts);

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

            corpus.Storage.Mount("expected", new LocalAdapter(expectedOutputPath));

            CdmEntityDefinition entity = await corpus.FetchObjectAsync <CdmEntityDefinition>($"local:/{entityName}.cdm.json/{entityName}");

            Assert.IsNotNull(entity);
            CdmEntityDefinition resolvedEntity = await ProjectionTestUtils.GetResolvedEntity(corpus, entity, resOpts, true);

            Assert.IsNotNull(resolvedEntity);

            await ValidateResolvedAttributes(corpus, resolvedEntity, entityName, fileNameSuffix);

            await AttributeContextUtil.ValidateAttributeContext(corpus, expectedOutputPath, $"{entityName}{fileNameSuffix}", resolvedEntity);
        }
Exemple #23
0
        public async Task TestNonPolymorphicProj()
        {
            string testName   = "TestNonPolymorphicProj";
            string entityName = "NewPerson";

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

            CdmEntityDefinition entity = await corpus.FetchObjectAsync <CdmEntityDefinition>($"local:/{entityName}.cdm.json/{entityName}");

            CdmEntityDefinition resolvedEntity = await ProjectionTestUtils.GetResolvedEntity(corpus, entity, new List <string> {
            });

            // Original set of attributes: ["name", "age", "address", "phoneNumber", "email"]
            // Combined attributes ["phoneNumber", "email"] into "contactAt"
            Assert.AreEqual(4, resolvedEntity.Attributes.Count);
            Assert.AreEqual("name", (resolvedEntity.Attributes[0] as CdmTypeAttributeDefinition).Name);
            Assert.AreEqual("age", (resolvedEntity.Attributes[1] as CdmTypeAttributeDefinition).Name);
            Assert.AreEqual("address", (resolvedEntity.Attributes[2] as CdmTypeAttributeDefinition).Name);
            Assert.AreEqual("contactAt", (resolvedEntity.Attributes[3] as CdmTypeAttributeDefinition).Name);
        }
Exemple #24
0
        /// <summary>
        /// List the incoming and outgoing relationships
        /// </summary>
        /// <param name="corpus"></param>
        /// <param name="entity"></param>
        /// <param name="actualOutputFolder"></param>
        /// <param name="entityName"></param>
        /// <returns></returns>
        private static string ListRelationships(CdmCorpusDefinition corpus, CdmEntityDefinition entity, string actualOutputFolder, string entityName)
        {
            StringBuilder bldr = new StringBuilder();

            bldr.AppendLine($"Incoming Relationships For: {entity.EntityName}:");
            // Loop through all the relationships where other entities point to this entity.
            foreach (CdmE2ERelationship relationship in corpus.FetchIncomingRelationships(entity))
            {
                bldr.AppendLine(PrintRelationship(relationship));
            }

            Console.WriteLine($"Outgoing Relationships For: {entity.EntityName}:");
            // Now loop through all the relationships where this entity points to other entities.
            foreach (CdmE2ERelationship relationship in corpus.FetchOutgoingRelationships(entity))
            {
                bldr.AppendLine(PrintRelationship(relationship));
            }

            return(bldr.ToString());
        }
Exemple #25
0
        public async Task TestResolveTestCorpus()
        {
            Assert.IsTrue(Directory.Exists(Path.GetFullPath(SchemaDocsPath)), "SchemaDocsRoot not found!!!");

            var cdmCorpus = new CdmCorpusDefinition();

            cdmCorpus.SetEventCallback(new EventCallback {
                Invoke = CommonDataModelLoader.ConsoleStatusReport
            }, CdmStatusLevel.Warning);

            cdmCorpus.Storage.Mount("local", new LocalAdapter(SchemaDocsPath));
            var manifest = await cdmCorpus.FetchObjectAsync <CdmManifestDefinition>(TestHelper.CdmStandardSchemaPath) as CdmManifestDefinition;

            var directives = new AttributeResolutionDirectiveSet(new HashSet <string> {
                "normalized", "referenceOnly"
            });
            var allResolved = await ListAllResolved(cdmCorpus, directives, manifest, new StringSpewCatcher());

            Assert.IsTrue(!string.IsNullOrWhiteSpace(allResolved));
        }
Exemple #26
0
        public async Task TestReadingIsPrimaryKeyConstructedFromPurpose()
        {
            var testInputPath          = TestHelper.GetInputFolderPath(testsSubpath, "TestReadingIsPrimaryKeyConstructedFromPurpose");
            CdmCorpusDefinition corpus = new CdmCorpusDefinition();

            corpus.SetEventCallback(new EventCallback {
                Invoke = CommonDataModelLoader.ConsoleStatusReport
            }, CdmStatusLevel.Warning);
            corpus.Storage.Mount("local", new LocalAdapter(testInputPath));
            corpus.Storage.DefaultNamespace = "local";

            CdmEntityDefinition entity = await corpus.FetchObjectAsync <CdmEntityDefinition>("local:/TeamMembership.cdm.json/TeamMembership");

            CdmAttributeGroupReference  attributeGroupRef = (CdmAttributeGroupReference)entity.Attributes[0];
            CdmAttributeGroupDefinition attributeGroup    = (CdmAttributeGroupDefinition)attributeGroupRef.ExplicitReference;
            CdmTypeAttributeDefinition  typeAttribute     = (CdmTypeAttributeDefinition)attributeGroup.Members[0];

            Assert.AreEqual("identifiedBy", typeAttribute.Purpose.NamedReference);
            Assert.IsTrue((bool)typeAttribute.IsPrimaryKey);
        }
        public void TestResolutionGuidanceCopy()
        {
            var corpus             = new CdmCorpusDefinition();
            var resolutionGuidance = new CdmAttributeResolutionGuidance(corpus.Ctx)
            {
                expansion           = new CdmAttributeResolutionGuidance.Expansion(),
                entityByReference   = new CdmAttributeResolutionGuidance.CdmAttributeResolutionGuidance_EntityByReference(),
                selectsSubAttribute = new CdmAttributeResolutionGuidance.CdmAttributeResolutionGuidance_SelectsSubAttribute(),
                imposedDirectives   = new List <string>(),
                removedDirectives   = new List <string>()
            };

            var resolutionGuidanceCopy = resolutionGuidance.Copy() as CdmAttributeResolutionGuidance;

            Assert.IsFalse(Object.ReferenceEquals(resolutionGuidance.expansion, resolutionGuidanceCopy.expansion));
            Assert.IsFalse(Object.ReferenceEquals(resolutionGuidance.entityByReference, resolutionGuidanceCopy.entityByReference));
            Assert.IsFalse(Object.ReferenceEquals(resolutionGuidance.selectsSubAttribute, resolutionGuidanceCopy.selectsSubAttribute));
            Assert.IsFalse(Object.ReferenceEquals(resolutionGuidance.imposedDirectives, resolutionGuidanceCopy.imposedDirectives));
            Assert.IsFalse(Object.ReferenceEquals(resolutionGuidance.removedDirectives, resolutionGuidanceCopy.removedDirectives));
        }
        public async Task TestAddTypeAttrOnTypeAttrProj()
        {
            string testName            = nameof(TestAddTypeAttrOnTypeAttrProj);
            string entityName          = "Person";
            CdmCorpusDefinition corpus = TestHelper.GetLocalCorpus(testsSubpath, testName);

            foreach (List <string> resOpt in resOptsCombinations)
            {
                await ProjectionTestUtils.LoadEntityForResolutionOptionAndSave(corpus, testName, testsSubpath, entityName, resOpt);
            }

            CdmEntityDefinition entity = await corpus.FetchObjectAsync <CdmEntityDefinition>($"local:/{entityName}.cdm.json/{entityName}");

            CdmEntityDefinition resolvedEntity = await ProjectionTestUtils.GetResolvedEntity(corpus, entity, new List <string> {
            });

            Assert.AreEqual(2, resolvedEntity.Attributes.Count);
            Assert.AreEqual("newTerm", (resolvedEntity.Attributes[0] as CdmTypeAttributeDefinition).Name);
            Assert.AreEqual("FavoriteTerm", (resolvedEntity.Attributes[1] as CdmTypeAttributeDefinition).Name);
        }
Exemple #29
0
        public async Task TestConditionalProj()
        {
            string testName            = nameof(TestConditionalProj);
            string entityName          = "NewPerson";
            CdmCorpusDefinition corpus = TestHelper.GetLocalCorpus(testsSubpath, testName);

            corpus.Storage.Mount("traitGroup", new LocalAdapter(traitGroupFilePath));

            foreach (List <string> resOpt in resOptsCombinations)
            {
                await ProjectionTestUtils.LoadEntityForResolutionOptionAndSave(corpus, testName, testsSubpath, entityName, resOpt);
            }

            CdmEntityDefinition entity = await corpus.FetchObjectAsync <CdmEntityDefinition>($"local:/{entityName}.cdm.json/{entityName}");

            CdmEntityDefinition resolvedEntity = await ProjectionTestUtils.GetResolvedEntity(corpus, entity, new List <string> {
                "referenceOnly"
            });

            // Original set of attributes: ["name", "age", "address", "phoneNumber", "email"]
            // Condition not met, no traits are added
            Assert.AreEqual(5, resolvedEntity.Attributes.Count);
            ValidateTrait((CdmTypeAttributeDefinition)resolvedEntity.Attributes[0], "name", doesNotExist: true);
            ValidateTrait((CdmTypeAttributeDefinition)resolvedEntity.Attributes[1], "age", doesNotExist: true);
            ValidateTrait((CdmTypeAttributeDefinition)resolvedEntity.Attributes[2], "address", doesNotExist: true);
            ValidateTrait((CdmTypeAttributeDefinition)resolvedEntity.Attributes[3], "phoneNumber", doesNotExist: true);
            ValidateTrait((CdmTypeAttributeDefinition)resolvedEntity.Attributes[4], "email", doesNotExist: true);

            CdmEntityDefinition resolvedEntity2 = await ProjectionTestUtils.GetResolvedEntity(corpus, entity, new List <string> {
                "structured"
            });

            // Original set of attributes: ["name", "age", "address", "phoneNumber", "email"]
            // Condition met, new traits are added
            Assert.AreEqual(5, resolvedEntity2.Attributes.Count);
            ValidateTrait((CdmTypeAttributeDefinition)resolvedEntity2.Attributes[0], "name", true);
            ValidateTrait((CdmTypeAttributeDefinition)resolvedEntity2.Attributes[1], "age");
            ValidateTrait((CdmTypeAttributeDefinition)resolvedEntity2.Attributes[2], "address");
            ValidateTrait((CdmTypeAttributeDefinition)resolvedEntity2.Attributes[3], "phoneNumber");
            ValidateTrait((CdmTypeAttributeDefinition)resolvedEntity2.Attributes[4], "email");
        }
        public async Task TestConditionalProj()
        {
            string testName            = "TestConditionalProj";
            string entityName          = "NewPerson";
            CdmCorpusDefinition corpus = ProjectionTestUtils.GetCorpus(testName, testsSubpath);

            foreach (List <string> resOpt in resOptsCombinations)
            {
                await ProjectionTestUtils.LoadEntityForResolutionOptionAndSave(corpus, testName, testsSubpath, entityName, resOpt);
            }

            CdmEntityDefinition entity = await corpus.FetchObjectAsync <CdmEntityDefinition>($"local:/{entityName}.cdm.json/{entityName}");

            CdmEntityDefinition resolvedEntity = await ProjectionTestUtils.GetResolvedEntity(corpus, entity, new List <string> {
                "referenceOnly"
            });

            // Original set of attributes: ["name", "age", "address", "phoneNumber", "email"]
            // Condition not met, keep attributes in flat list
            Assert.AreEqual(5, resolvedEntity.Attributes.Count);
            Assert.AreEqual("name", (resolvedEntity.Attributes[0] as CdmTypeAttributeDefinition).Name);
            Assert.AreEqual("age", (resolvedEntity.Attributes[1] as CdmTypeAttributeDefinition).Name);
            Assert.AreEqual("address", (resolvedEntity.Attributes[2] as CdmTypeAttributeDefinition).Name);
            Assert.AreEqual("phoneNumber", (resolvedEntity.Attributes[3] as CdmTypeAttributeDefinition).Name);
            Assert.AreEqual("email", (resolvedEntity.Attributes[4] as CdmTypeAttributeDefinition).Name);

            CdmEntityDefinition resolvedEntity2 = await ProjectionTestUtils.GetResolvedEntity(corpus, entity, new List <string> {
                "structured"
            });

            // Original set of attributes: ["name", "age", "address", "phoneNumber", "email"]
            // Condition met, put all attributes in an attribute group
            CdmAttributeGroupDefinition attGroupDefinition = this.ValidateAttributeGroup(resolvedEntity2.Attributes, "PersonAttributeGroup");

            Assert.AreEqual(5, attGroupDefinition.Members.Count);
            Assert.AreEqual("name", (attGroupDefinition.Members[0] as CdmTypeAttributeDefinition).Name);
            Assert.AreEqual("age", (attGroupDefinition.Members[1] as CdmTypeAttributeDefinition).Name);
            Assert.AreEqual("address", (attGroupDefinition.Members[2] as CdmTypeAttributeDefinition).Name);
            Assert.AreEqual("phoneNumber", (attGroupDefinition.Members[3] as CdmTypeAttributeDefinition).Name);
            Assert.AreEqual("email", (attGroupDefinition.Members[4] as CdmTypeAttributeDefinition).Name);
        }