Esempio n. 1
0
        protected void VerifySemanticValidation(IEdmModel testModel, ValidationRuleSet ruleset, IEnumerable <EdmError> expectedErrors)
        {
            // Compare the actual errors of the test models to the expected errors.
            IEnumerable <EdmError> actualErrors = null;
            var validationResult = testModel.Validate(ruleset, out actualErrors);

            Assert.IsTrue(actualErrors.Any() ? !validationResult : validationResult, "The return value of the Validate method does not match the reported validation errors.");
            this.CompareErrors(actualErrors, expectedErrors);

            // Compare the round-tripped immutable model through the CSDL serialized from the original test model against the expected errors.
            Func <EdmVersion> GetEdmVersionFromRuleSet = () =>
            {
                EdmVersion result = EdmVersion.Latest;
                foreach (var edmVersion in new EdmVersion[] { EdmVersion.V40 })
                {
                    var versionRuleSet = ValidationRuleSet.GetEdmModelRuleSet(toProductVersionlookup[edmVersion]);
                    if (versionRuleSet.Count() == ruleset.Count() && !ruleset.Except(versionRuleSet).Any())
                    {
                        result = edmVersion;
                        break;
                    }
                }
                return(result);
            };

            IEnumerable <EdmError> serializationErrors;
            var serializedCsdls = this.GetSerializerResult(testModel, GetEdmVersionFromRuleSet(), out serializationErrors).Select(n => XElement.Parse(n));

            if (!serializedCsdls.Any())
            {
                Assert.AreNotEqual(0, serializationErrors.Count(), "Empty models should have associated errors");
                return;
            }

            if (!actualErrors.Any())
            {
                // if the original test model is valid, the round-tripped model should be well-formed and valid.
                IEnumerable <EdmError> parserErrors = null;
                IEdmModel roundtrippedModel         = null;
                var       isWellformed = SchemaReader.TryParse(serializedCsdls.Select(e => e.CreateReader()), out roundtrippedModel, out parserErrors);
                Assert.IsTrue(isWellformed && !parserErrors.Any(), "The model from valid CSDLs should be generated back to well-formed CSDLs.");

                IEnumerable <EdmError> validationErrors = null;
                var isValid = roundtrippedModel.Validate(out validationErrors);
                Assert.IsTrue(!validationErrors.Any() && isValid, "The model from valid CSDLs should be generated back to valid CSDLs.");
            }
            else
            {
                // if the originl test model is not valid, the serializer should still generate CSDLs that parser can handle, but the round trip-ability is not guarantted.
                IEnumerable <EdmError> parserErrors = null;
                IEdmModel roundtrippedModel         = null;
                var       isWellformed = SchemaReader.TryParse(serializedCsdls.Select(e => e.CreateReader()), out roundtrippedModel, out parserErrors);
                Assert.IsTrue(isWellformed, "The parser cannot handle the CSDL that the serializer generated:" + Environment.NewLine + String.Join(Environment.NewLine, parserErrors));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Validates the given <paramref name="model"/>.
        /// </summary>
        /// <param name="model">The <see cref="IEdmModel"/> to check.</param>
        /// <param name="edmSchemaVersion">The EDM version to be used.</param>
        internal static void ValidateModel(IEdmModel model, Version edmSchemaVersion)
        {
            ValidationRuleSet ruleSet = ValidationRuleSet.GetEdmModelRuleSet(edmSchemaVersion);
            ValidationRuleSet ruleSetWithoutTypeNameChecks = new ValidationRuleSet(
                ruleSet.Except(excludedSchemaValidationRules).Concat(additionalSchemaValidationRules));

            IEnumerable <EdmError> validationErrors;

            model.Validate(ruleSetWithoutTypeNameChecks, out validationErrors);

            if (validationErrors != null && validationErrors.Any())
            {
                StringBuilder builder = new StringBuilder();
                foreach (EdmError validationError in validationErrors)
                {
                    builder.AppendLine(validationError.ToString());
                }

                throw new DataServiceException(500, Microsoft.OData.Service.Strings.MetadataSerializer_ModelValidationErrors(builder.ToString()));
            }
        }