/// <summary> /// Executes one rule to validate the current interop request contecxt /// </summary> /// <param name="context">the current interop request context</param> /// <param name="rule">the rule to be validated</param> /// <returns>TestResult object of the validation</returns> /// <exception cref="Exception">Throws various exception when rule engine encounters unrecoverable errors like bad dynamic rules</exception> public static TestResult ExecuteRule(ServiceContext context, Rule rule) { if (context == null) { throw new ArgumentNullException("context"); } if (rule == null) { throw new ArgumentNullException("rule"); } bool passed = true; TestResult result; TestResult dummy; //Set the test service's current verified rule if (DataService.serviceInstance != null) { DataService.serviceInstance.SwitchRule(rule.Name); } if (rule.Condition == null || rule.Condition.Verify(context, out dummy)) { passed = rule.Action.Verify(context, out result); } else { result = new TestResult(); } result.JobId = context.JobId; result.SetProperties(rule, passed); return result; }
/// <summary> /// Converts a semantic rule extension to Interop Rule object /// </summary> /// <param name="extension">semantic rule extension</param> /// <returns>The Interop Rule object</returns> private static Rule CreateRule(ExtensionRule extension) { Rule rule = new Rule(); rule.Name = extension.Name; rule.Category = extension.Category; rule.Description = extension.Description; rule.SpecificationSection = extension.SpecificationSection; rule.V4SpecificationSection = extension.V4SpecificationSection; rule.V4Specification = extension.V4Specification; rule.HelpLink = extension.HelpLink; rule.ErrorMessage = extension.ErrorMessage; rule.RequirementLevel = extension.RequirementLevel; rule.Aspect = extension.Aspect; rule.PayloadType = extension.PayloadType; rule.IsMediaLinkEntry = extension.IsMediaLinkEntry; rule.Projection = extension.Projection; rule.PayloadFormat = extension.PayloadFormat; rule.Version = extension.Version; rule.RequireMetadata = extension.RequireMetadata; rule.RequireServiceDocument = extension.RequireServiceDocument; rule.Offline = extension.IsOfflineContext; rule.OdataMetadataType = extension.OdataMetadataType; rule.LevelType = extension.LevelType; rule.ResourceType = extension.ResourceType; rule.DependencyType = extension.DependencyType; if (extension.DependencyType.HasValue && extension.DependencyType.Value == ConformanceDependencyType.Dependency && extension.DependencyInfo != null) { rule.DependencyInfo = new ConformanceRuleDependencyInfo(extension.DependencyInfo.CheckType, extension.DependencyInfo.RuleRelationship, extension.DependencyInfo.BindingRules); } rule.Action = new ExtensionVerifier(extension.Verify); rule.Condition = null; return rule; }
/// <summary> /// Sets properties properly based on execution result of specified rule /// </summary> /// <param name="rule">the rule validated</param> /// <param name="passed">whether the rule passed or not</param> public void SetProperties(Rule rule, bool passed) { if (rule == null) { throw new ArgumentNullException("rule"); } if (rule.DependencyType.HasValue) // For dependency and skip rule, set the default test result { if (rule.DependencyType.Value == ConformanceDependencyType.Dependency) { this.Classification = Constants.ClassificationPending; } else if (rule.DependencyType.Value == ConformanceDependencyType.Skip) { this.Classification = Constants.ClassificationSkip; } } if (string.IsNullOrEmpty(this.Classification)) { if (passed) { this.Classification = Constants.ClassificationSuccess; } else { switch (rule.RequirementLevel) { case RequirementLevel.Must: case RequirementLevel.MustNot: this.Classification = Constants.ClassificationError; break; case RequirementLevel.Should: case RequirementLevel.ShouldNot: this.Classification = Constants.ClassificationWarning; break; case RequirementLevel.May: case RequirementLevel.Recommended: this.Classification = Constants.ClassificationRecommendation; break; default: this.Classification = Constants.ClassificationWarning; break; } this.Description = rule.ErrorMessage; } } this.RuleName = rule.Name; this.Category = rule.Category; this.HelpLink = rule.HelpLink; this.SpecificationSection = rule.SpecificationSection; this.V4SpecificationSection = rule.V4SpecificationSection; this.V4Specification = rule.V4Specification; this.RequirementLevel = rule.RequirementLevel; this.Target = rule.PayloadType.HasValue ? rule.PayloadType.Value.ToString() : null; this.Description = rule.Description; this.ErrorMessage = rule.ErrorMessage; this.Version = rule.Version.HasValue ? rule.Version.Value.ToString() : null; }
/// <summary> /// Creates an aborted test result from rule definition and job ID /// </summary> /// <param name="rule">The rule this aborted result is about</param> /// <param name="jobId">The job this aborted validation is part of</param> /// <returns>TestResult object for aborted validation</returns> public static TestResult CreateAbortedResult(Rule rule, Guid jobId) { TestResult result = new TestResult(); result.JobId = jobId; result.SetProperties(rule, true); result.Classification = Constants.ClassificationAborted; return result; }
/// <summary> /// Gets one rule as defined by the xml node /// </summary> /// <param name="x">Xml node which defines the rule</param> /// <returns>Rule as defined by xml node</returns> private static Rule CreateRule(XElement x) { try { string name = x.GetAttributeValue("id"); string specificationSection = x.GetAttributeValue("specificationsection"); string v4specificationSection = x.GetAttributeValue("v4specificationsection"); string v4specification = x.GetAttributeValue("v4specification"); string requirementLevel = x.GetAttributeValue("requirementlevel"); string category = x.GetAttributeValue("category"); string useCase = x.GetAttributeValue("usecase"); string respVersion = x.GetAttributeValue("version"); string target = x.GetAttributeValue("target"); string format = x.GetAttributeValue("format"); string mle = x.GetAttributeValue("mle"); string projection = x.GetAttributeValue("projection"); string metadata = x.GetAttributeValue("metadata"); string serviceDocument = x.GetAttributeValue("svcdoc"); string description = x.GetFirstSubElementValue("description"); string errorMessage = x.GetFirstSubElementValue("errormessage"); string offline = x.GetAttributeValue("offline"); string odatametadatatype = x.GetAttributeValue("odatametadatatype"); string conformancetype = x.GetAttributeValue("conformancetype"); string helpLink = (x.Element("helplink") != null && x.Element("helplink").Element("a") != null) ? x.Element("helplink").Element("a").GetAttributeValue("href") : null; if (string.IsNullOrEmpty(category) || string.IsNullOrEmpty(name)) { return null; } else { Rule rule = new Rule(); rule.Name = name; rule.SpecificationSection = specificationSection; rule.V4SpecificationSection = v4specificationSection; rule.V4Specification = v4specification; rule.HelpLink = helpLink; rule.Category = category; rule.PayloadType = target.ToNullable<PayloadType>(); if (!string.IsNullOrEmpty(mle) && rule.PayloadType.HasValue && rule.PayloadType.Value == PayloadType.Entry) { rule.IsMediaLinkEntry = mle.ToNullableBool(); } if (!string.IsNullOrEmpty(projection) && rule.PayloadType.HasValue && (rule.PayloadType.Value == PayloadType.Entry || rule.PayloadType.Value == PayloadType.Feed)) { rule.Projection = projection.ToNullableBool(); } rule.RequireMetadata = metadata.ToNullableBool(); rule.RequireServiceDocument = serviceDocument.ToNullableBool(); rule.Description = description; rule.ErrorMessage = errorMessage; rule.RequirementLevel = (RequirementLevel)Enum.Parse(typeof(RequirementLevel), requirementLevel, true); rule.Aspect = useCase; rule.Version = respVersion.ToNullable<ODataVersion>(); rule.Offline = offline.ToNullableBool(); rule.PayloadFormat = format.ToNullable<PayloadFormat>(); if (!string.IsNullOrEmpty(odatametadatatype)) { rule.OdataMetadataType = (ODataMetadataType)Enum.Parse(typeof(ODataMetadataType), odatametadatatype, true); } SetRuleCondition(ref rule, x.Element("condition")); SetRuleAction(ref rule, x.Element("action")); return rule; } } catch (ArgumentException) { return null; } }
/// <summary> /// Sets condition property of the specified rule from the xml node /// </summary> /// <param name="rule">The specified rule to have its condition property be set</param> /// <param name="condition">xml node containing the condition verification</param> private static void SetRuleCondition(ref Rule rule, XElement condition) { if (condition != null) { rule.Condition = VerifierFactory.Create(condition); } }
/// <summary> /// Sets action property of the specified rule from the xml node /// </summary> /// <param name="rule">The specified rule to have its action property be set</param> /// <param name="action">xml node containing the action verification</param> private static void SetRuleAction(ref Rule rule, XElement action) { rule.Action = VerifierFactory.Create(action); }
/// <summary> /// Gets one rule as defined by the xml node /// </summary> /// <param name="x">Xml node which defines the rule</param> /// <returns>Rule as defined by xml node</returns> private static Rule CreateRule(XElement x) { try { string name = x.GetAttributeValue("id"); string specificationSection = x.GetAttributeValue("specificationsection"); string v4specificationSection = x.GetAttributeValue("v4specificationsection"); string v4specification = x.GetAttributeValue("v4specification"); string requirementLevel = x.GetAttributeValue("requirementlevel"); string category = x.GetAttributeValue("category"); string useCase = x.GetAttributeValue("usecase"); string respVersion = x.GetAttributeValue("version"); string target = x.GetAttributeValue("target"); string format = x.GetAttributeValue("format"); string mle = x.GetAttributeValue("mle"); string projection = x.GetAttributeValue("projection"); string metadata = x.GetAttributeValue("metadata"); string serviceDocument = x.GetAttributeValue("svcdoc"); string description = x.GetFirstSubElementValue("description"); string errorMessage = x.GetFirstSubElementValue("errormessage"); string offline = x.GetAttributeValue("offline"); string odatametadatatype = x.GetAttributeValue("odatametadatatype"); string conformancetype = x.GetAttributeValue("conformancetype"); string helpLink = (x.Element("helplink") != null && x.Element("helplink").Element("a") != null) ? x.Element("helplink").Element("a").GetAttributeValue("href") : null; if (string.IsNullOrEmpty(category) || string.IsNullOrEmpty(name)) { return(null); } else { Rule rule = new Rule(); rule.Name = name; rule.SpecificationSection = specificationSection; rule.V4SpecificationSection = v4specificationSection; rule.V4Specification = v4specification; rule.HelpLink = helpLink; rule.Category = category; rule.PayloadType = target.ToNullable <PayloadType>(); if (!string.IsNullOrEmpty(mle) && rule.PayloadType.HasValue && rule.PayloadType.Value == PayloadType.Entry) { rule.IsMediaLinkEntry = mle.ToNullableBool(); } if (!string.IsNullOrEmpty(projection) && rule.PayloadType.HasValue && (rule.PayloadType.Value == PayloadType.Entry || rule.PayloadType.Value == PayloadType.Feed)) { rule.Projection = projection.ToNullableBool(); } rule.RequireMetadata = metadata.ToNullableBool(); rule.RequireServiceDocument = serviceDocument.ToNullableBool(); rule.Description = description; rule.ErrorMessage = errorMessage; rule.RequirementLevel = (RequirementLevel)Enum.Parse(typeof(RequirementLevel), requirementLevel, true); rule.Aspect = useCase; rule.Version = respVersion.ToNullable <ODataVersion>(); rule.Offline = offline.ToNullableBool(); rule.PayloadFormat = format.ToNullable <PayloadFormat>(); if (!string.IsNullOrEmpty(odatametadatatype)) { rule.OdataMetadataType = (ODataMetadataType)Enum.Parse(typeof(ODataMetadataType), odatametadatatype, true); } SetRuleCondition(ref rule, x.Element("condition")); SetRuleAction(ref rule, x.Element("action")); return(rule); } } catch (ArgumentException) { return(null); } }