From 6d8e6556f3509df649b86661b1f0c7838a19ad4b Mon Sep 17 00:00:00 2001 From: "NORTHAMERICA\\haoqihuang" Date: Fri, 2 Jul 2021 14:25:33 -0700 Subject: [PATCH] use linked rules instead of single dictionary rule, modify two origin rules file, and fix the tests. --- .../InvalidTestSchemaErrorContent.json | 21 +---- .../test/TreeSchemaValidatorTests.cs | 39 +++++---- Forge.TreeWalker/Forge.TreeWalker.csproj | 80 +++++++++---------- .../ForgeSchemaDictionaryValidationRules.json | 14 +--- .../contracts/ForgeSchemaValidationRules.json | 2 +- Forge.TreeWalker/src/ForgeSchemaValidator.cs | 2 +- 6 files changed, 71 insertions(+), 87 deletions(-) diff --git a/Forge.TreeWalker.UnitTests/test/InvalidTestSchemas/InvalidTestSchemaErrorContent.json b/Forge.TreeWalker.UnitTests/test/InvalidTestSchemas/InvalidTestSchemaErrorContent.json index a4f4095..f99e7ae 100644 --- a/Forge.TreeWalker.UnitTests/test/InvalidTestSchemas/InvalidTestSchemaErrorContent.json +++ b/Forge.TreeWalker.UnitTests/test/InvalidTestSchemas/InvalidTestSchemaErrorContent.json @@ -1,27 +1,10 @@ { "Tree": { "Root": { - "Type": "Action", - "Actions": { - "Root_CollectDiagnosticsAction": { - "Action": "CollectDiagnosticsAction", - "Input": { - "Command": "TheCommand" - } - } - }, - "ChildSelector": [ - { - "Child": "LeafNodeSummaryTest", - "Lable": "Lable" - } - ] - }, - "LeafNodeSummaryTest": { "Type": "Leaf", "Actions": { - "LeafNodeSummaryTest_LeafNodeSummaryAction": { - "Action": "NoInputAction" + "LeafActionWithoutRequiredActionInput": { + "Action": "LeafNodeSummaryAction" } } } diff --git a/Forge.TreeWalker.UnitTests/test/TreeSchemaValidatorTests.cs b/Forge.TreeWalker.UnitTests/test/TreeSchemaValidatorTests.cs index e313c21..51f3704 100644 --- a/Forge.TreeWalker.UnitTests/test/TreeSchemaValidatorTests.cs +++ b/Forge.TreeWalker.UnitTests/test/TreeSchemaValidatorTests.cs @@ -29,6 +29,7 @@ namespace Microsoft.Forge.TreeWalker.UnitTests private string schemaDirectoryPath; private string stringRules; private string rulesForDictionary; + private JSchema linkedRulesForDictionary; private JSchema jschemaRules; private string invalidSchemaDirectoryPath; private string invalidSchemaWithErrorContent; @@ -41,6 +42,7 @@ namespace Microsoft.Forge.TreeWalker.UnitTests schemaDirectoryPath = Path.Combine(Environment.CurrentDirectory, "test\\ExampleSchemas"); stringRules = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "contracts\\ForgeSchemaValidationRules.json")); rulesForDictionary = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "contracts\\ForgeSchemaDictionaryValidationRules.json")); + linkedRulesForDictionary = ForgeSchemaValidator.GetLinkedJSchemaRules(rulesForDictionary, stringRules, "//ForgeSchemaValidationRules.json"); jschemaRules = JSchema.Parse(stringRules); forgeTreeAsString = File.ReadAllText(forgeTreeFromPath); forgeTreeDictionaryAsString = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "test\\ExampleSchemas\\SubroutineSchema.json")); @@ -58,8 +60,9 @@ namespace Microsoft.Forge.TreeWalker.UnitTests { try { - JSchema linkedRules = ForgeSchemaValidator.GetLinkedJSchemaRules(stringRules, stringRules, "//ForgeSchemaValidationRules.json"); - ForgeSchemaValidator.ValidateSchemaAsForgeTree(treeSchema, linkedRules, out IList errorList); + JSchema linkedRules = ForgeSchemaValidator.GetLinkedJSchemaRules(rulesForDictionary, stringRules, "//ForgeSchemaValidationRules.json"); + bool res = ForgeSchemaValidator.ValidateSchemaAsForgeTreeDictionary(treeSchemas, linkedRules, true, out IList errorList); + Assert.IsTrue(res); } catch (Exception ex) { @@ -71,7 +74,7 @@ namespace Microsoft.Forge.TreeWalker.UnitTests public void Test_ValidateSchemaAsForgeTree_WithRulesAsString() { bool res = ForgeSchemaValidator.ValidateSchemaAsForgeTree(treeSchema, stringRules, out IList errorList); - Assert.AreEqual(true, res); + Assert.IsTrue(res); Assert.AreEqual(0, errorList.Count); } @@ -79,7 +82,7 @@ namespace Microsoft.Forge.TreeWalker.UnitTests public void Test_ValidateSchemaAsForgeTree_WithRulesAsJSchema() { bool res = ForgeSchemaValidator.ValidateSchemaAsForgeTree(treeSchema, jschemaRules, out IList errorList); - Assert.AreEqual(true, res); + Assert.IsTrue(res); Assert.AreEqual(0, errorList.Count); } @@ -87,15 +90,15 @@ namespace Microsoft.Forge.TreeWalker.UnitTests public void Test_ValidateSchemaAsForgeTreeDictionary_WithoutValidateAsDictionary() { bool res = ForgeSchemaValidator.ValidateSchemaAsForgeTreeDictionary(treeSchemas, stringRules, false, out IList errorList); - Assert.AreEqual(true, res); + Assert.IsTrue(res); Assert.AreEqual(0, errorList.Count); } [TestMethod] public void Test_ValidateSchemaAsForgeTreeDictionary_WithValidateAsDictionary() { - bool res = ForgeSchemaValidator.ValidateSchemaAsForgeTreeDictionary(treeSchemas, rulesForDictionary, true, out IList errorList); - Assert.AreEqual(true, res); + bool res = ForgeSchemaValidator.ValidateSchemaAsForgeTreeDictionary(treeSchemas, linkedRulesForDictionary, true, out IList errorList); + Assert.IsTrue(res); Assert.AreEqual(0, errorList.Count); } @@ -103,15 +106,15 @@ namespace Microsoft.Forge.TreeWalker.UnitTests public void Test_ValidateSchemaAsString_WithForgeTree() { bool res = ForgeSchemaValidator.ValidateSchemaAsString(forgeTreeAsString, stringRules, false, out IList errorList); - Assert.AreEqual(true, res); + Assert.IsTrue(res); Assert.AreEqual(0, errorList.Count); } [TestMethod] public void Test_ValidateSchemaAsString_WithValidateAsDictionary() { - bool res = ForgeSchemaValidator.ValidateSchemaAsString(forgeTreeDictionaryAsString, rulesForDictionary, true, out IList errorList); - Assert.AreEqual(true, res); + bool res = ForgeSchemaValidator.ValidateSchemaAsString(forgeTreeDictionaryAsString, linkedRulesForDictionary, true, out IList errorList); + Assert.IsTrue(res); Assert.AreEqual(0, errorList.Count); } @@ -119,7 +122,7 @@ namespace Microsoft.Forge.TreeWalker.UnitTests public void Test_ValidateSchemaFromPath_WithoutValidateAsDictionary() { bool res = ForgeSchemaValidator.ValidateSchemaFromPath(forgeTreeFromPath, stringRules, false, out IList errorList); - Assert.AreEqual(true, res); + Assert.IsTrue(res); Assert.AreEqual(0, errorList.Count); } @@ -127,14 +130,14 @@ namespace Microsoft.Forge.TreeWalker.UnitTests public void Test_ValidateSchemasFromDirectory_WithValidateAsSeparateFiles() { bool res = ForgeSchemaValidator.ValidateSchemaFromDirectory(schemaDirectoryPath, stringRules, false, out IList errorList); - Assert.AreEqual(true, res); + Assert.IsTrue(res); Assert.AreEqual(0, errorList.Count); } [TestMethod] public void Test_GetLinkedJSchemaRules_WithInvalidUrl__Fail() { - Assert.ThrowsException(() => ForgeSchemaValidator.GetLinkedJSchemaRules(stringRules, stringRules, "//")); + Assert.ThrowsException(() => ForgeSchemaValidator.GetLinkedJSchemaRules(rulesForDictionary, stringRules, "//nameImadeup.json")); } [TestMethod] @@ -158,9 +161,15 @@ namespace Microsoft.Forge.TreeWalker.UnitTests } [TestMethod] - public void Test_ValidateSchemaFromDirectory_WithValidateAsDictionary_DirectoryContainsForgeTrees_Fail() + public void Test_ValidateSchemaFromDirectory_WithValidateAsDictionary_DirectoryContainsForgeTree_Fail() { - Assert.ThrowsException(() => ForgeSchemaValidator.ValidateSchemaFromDirectory(schemaDirectoryPath, rulesForDictionary, true, out IList errorList)); + Assert.ThrowsException(() => ForgeSchemaValidator.ValidateSchemaFromDirectory(schemaDirectoryPath, linkedRulesForDictionary, true, out IList errorList)); + } + + [TestMethod] + public void Test_ValidateSchemaAsForgeTreeDictionary_WithRulesUnlinked_Fail() + { + Assert.ThrowsException(() => ForgeSchemaValidator.ValidateSchemaAsForgeTreeDictionary(treeSchemas, rulesForDictionary, true, out IList errorList)); } } } diff --git a/Forge.TreeWalker/Forge.TreeWalker.csproj b/Forge.TreeWalker/Forge.TreeWalker.csproj index 25478fe..6a5b740 100644 --- a/Forge.TreeWalker/Forge.TreeWalker.csproj +++ b/Forge.TreeWalker/Forge.TreeWalker.csproj @@ -1,42 +1,42 @@  - - Microsoft.Forge.TreeWalker - Microsoft.Forge.TreeWalker - netstandard2.0;net462 - true - true - Microsoft - © Microsoft Corporation. All rights reserved. - A Generic Low-Code Framework Built on a Config-Driven Tree Walker. - LICENSE.txt - https://github.com/microsoft/Forge - https://github.com/microsoft/Forge - true - bin\Release - true - Forge;TreeWalker;Roslyn;async;dynamic;generic;workflow engine;decision tree;config;stateful;low-code;tree visualization;workflow framework;JSON - - - - all - - - - - - - - - - - PreserveNewest - true - true - - - PreserveNewest - true - true - - + + Microsoft.Forge.TreeWalker + Microsoft.Forge.TreeWalker + netstandard2.0;net462 + true + true + Microsoft + © Microsoft Corporation. All rights reserved. + A Generic Low-Code Framework Built on a Config-Driven Tree Walker. + LICENSE.txt + https://github.com/microsoft/Forge + https://github.com/microsoft/Forge + true + bin\Release + true + Forge;TreeWalker;Roslyn;async;dynamic;generic;workflow engine;decision tree;config;stateful;low-code;tree visualization;workflow framework;JSON + + + + all + + + + + + + + + + + PreserveNewest + true + true + + + PreserveNewest + true + true + + \ No newline at end of file diff --git a/Forge.TreeWalker/contracts/ForgeSchemaDictionaryValidationRules.json b/Forge.TreeWalker/contracts/ForgeSchemaDictionaryValidationRules.json index a2ce082..64690fd 100644 --- a/Forge.TreeWalker/contracts/ForgeSchemaDictionaryValidationRules.json +++ b/Forge.TreeWalker/contracts/ForgeSchemaDictionaryValidationRules.json @@ -8,24 +8,16 @@ "additionalProperties": false, "definitions": { "ForgeTreeDefinition": { - "additionalProperties": false, + "type": "object", "properties": { "Tree": { - "$ref": "#/definitions/TreeDefinition" + "$ref": "file://ForgeSchemaValidationRules.json#/definitions/TreeDefinition" }, "RootTreeNodeKey": { "type": "string" } }, - "type": "object" - }, - "TreeDefinition": { - "type": "object", - "patternProperties": { - ".*?": { - "$dynamicRef": "file://ForgeSchemaValidationRules.json#/definitions/TreeDefinition" - } - } + "additionalProperties": false } } } \ No newline at end of file diff --git a/Forge.TreeWalker/contracts/ForgeSchemaValidationRules.json b/Forge.TreeWalker/contracts/ForgeSchemaValidationRules.json index 29f1647..3af7616 100644 --- a/Forge.TreeWalker/contracts/ForgeSchemaValidationRules.json +++ b/Forge.TreeWalker/contracts/ForgeSchemaValidationRules.json @@ -175,7 +175,7 @@ } }, "additionalProperties": false, - "required": [ "Label", "Child" ] + "required": [ "Child" ] }, "RetryPolicy": { "type": "object", diff --git a/Forge.TreeWalker/src/ForgeSchemaValidator.cs b/Forge.TreeWalker/src/ForgeSchemaValidator.cs index 98f4a4d..08a6b06 100644 --- a/Forge.TreeWalker/src/ForgeSchemaValidator.cs +++ b/Forge.TreeWalker/src/ForgeSchemaValidator.cs @@ -30,7 +30,7 @@ namespace Microsoft.Forge.TreeWalker /// The parent rules to absorb the child rules /// The address of childRules /// The result of schema combination. - public static JSchema GetLinkedJSchemaRules(string childRules, string parentRules, string referenceUri) + public static JSchema GetLinkedJSchemaRules(string parentRules, string childRules, string referenceUri) { JSchemaPreloadedResolver resolver = new JSchemaPreloadedResolver(); resolver.Add(new Uri(referenceUri), childRules);