public void NewValidationRulesShouldBeInTheRuleSetExceptBaselinedExceptionRules() { var validationRules = new Dictionary <object, string>(); var items = typeof(ValidationRules).GetFields().Select(f => new KeyValuePair <object, string>(f.GetValue(null), f.Name)); foreach (var item in items) { validationRules.Add(item.Key, item.Value); } var unFoundValidationRules = new List <object>(); foreach (var ruleSet in ValidationRuleSet.GetEdmModelRuleSet(new Version(4, 0))) { if (validationRules.ContainsKey(ruleSet)) { validationRules.Remove(ruleSet); } else { unFoundValidationRules.Add(validationRules); } } unFoundValidationRules.Should().HaveCount(0); // The 4 remaining rules are deprecated: // ComplexTypeMustContainProperties // OnlyEntityTypesCanBeOpen // ComplexTypeInvalidPolymorphicComplexType // ComplexTypeInvalidAbstractComplexType validationRules.ToList().Should().HaveCount(4); }
public void NewValidationRulesShouldBeInTheRuleSetExceptBaselinedExceptionRules() { var validationRules = new Dictionary <object, string>(); var items = typeof(ValidationRules).GetFields().Where(f => f.Name != "NavigationPropertyEntityMustNotIndirectlyContainItself" && f.Name != "EntityTypeKeyMissingOnEntityType" && f.Name != "VocabularyAnnotationTargetAllowedApplyToElement") .Select(f => new KeyValuePair <object, string>(f.GetValue(null), f.Name)); foreach (var item in items) { validationRules.Add(item.Key, item.Value); } var unFoundValidationRules = new List <object>(); foreach (var ruleSet in ValidationRuleSet.GetEdmModelRuleSet(new Version(4, 0))) { if (validationRules.ContainsKey(ruleSet)) { validationRules.Remove(ruleSet); } else { unFoundValidationRules.Add(validationRules); } } unFoundValidationRules.Should().HaveCount(0); validationRules.ToList().Should().HaveCount(0); }
public void NewValidationRulesShouldBeInTheRuleSetExceptBaselinedExceptionRules() { var validationRules = new Dictionary <object, string>(); var items = typeof(ValidationRules).GetFields().Select(f => new KeyValuePair <object, string>(f.GetValue(null), f.Name)); foreach (var item in items) { validationRules.Add(item.Key, item.Value); } var unFoundValidationRules = new List <object>(); foreach (var ruleSet in ValidationRuleSet.GetEdmModelRuleSet(new Version(4, 0))) { if (validationRules.ContainsKey(ruleSet)) { validationRules.Remove(ruleSet); } else { unFoundValidationRules.Add(validationRules); } } unFoundValidationRules.Should().HaveCount(0); validationRules.ToList().Should().HaveCount(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 = CsdlReader.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 = CsdlReader.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)); } }
public void TestEdmExpressionKindInterfaceCriticalKindValueUnexpectedOnlyModel() { var expectedErrors = new EdmLibTestErrors() { { null, null, EdmErrorCode.InterfaceCriticalKindValueUnexpected } }; var model = InterfaceCriticalModelBuilder.EdmExpressionKindInterfaceCriticalKindValueUnexpectedOnlyModel(); this.ValidateUsingEdmValidator(model, expectedErrors); this.ValidateUsingEdmValidator(model, EdmConstants.EdmVersion4, expectedErrors); this.ValidateUsingEdmValidator(model, ValidationRuleSet.GetEdmModelRuleSet(EdmConstants.EdmVersion4), expectedErrors); }
public void TestInterfaceCriticalPropertyValueMustNotBeNullUsingOperationParameterDeclaredTypeModel() { var expectedErrors = new EdmLibTestErrors() { { null, null, EdmErrorCode.InterfaceCriticalPropertyValueMustNotBeNull } }; var model = InterfaceCriticalModelBuilder.InterfaceCriticalPropertyValueMustNotBeNullUsingOperationParameterDeclaredTypeModel(); this.ValidateUsingEdmValidator(model, expectedErrors); this.ValidateUsingEdmValidator(model, EdmConstants.EdmVersion4, expectedErrors); this.ValidateUsingEdmValidator(model, ValidationRuleSet.GetEdmModelRuleSet(EdmConstants.EdmVersion4), expectedErrors); }
public void TestInterfaceCriticalKindValueMismatchOnlyUsingEnumTypeReferenceModel() { var expectedErrors = new EdmLibTestErrors() { { null, null, EdmErrorCode.InterfaceCriticalKindValueMismatch } }; var model = InterfaceCriticalModelBuilder.InterfaceCriticalKindValueMismatchOnlyUsingEnumTypeReferenceModel(); this.ValidateUsingEdmValidator(model, expectedErrors); this.ValidateUsingEdmValidator(model, EdmConstants.EdmVersion4, expectedErrors); this.ValidateUsingEdmValidator(model, ValidationRuleSet.GetEdmModelRuleSet(EdmConstants.EdmVersion4), expectedErrors); }
public void TestInterfaceCriticalEnumerableMustNotHaveNullElementsOnlyModel() { var expectedErrors = new EdmLibTestErrors() { { null, null, EdmErrorCode.InterfaceCriticalEnumerableMustNotHaveNullElements } }; var model = InterfaceCriticalModelBuilder.InterfaceCriticalEnumerableMustNotHaveNullElementsOnlyModel(); this.ValidateUsingEdmValidator(model, expectedErrors); this.ValidateUsingEdmValidator(model, EdmConstants.EdmVersion4, expectedErrors); this.ValidateUsingEdmValidator(model, ValidationRuleSet.GetEdmModelRuleSet(EdmConstants.EdmVersion4), expectedErrors); }
public void TestInterfaceCriticalPropertyValueMustNotBeNullCsdl() { var expectedErrors = new EdmLibTestErrors() { { null, null, EdmErrorCode.InterfaceCriticalPropertyValueMustNotBeNull } }; var csdls = InterfaceCriticalModelBuilder.InterfaceCriticalPropertyValueMustNotBeNullOnlyCsdl(); var model = this.GetParserResult(csdls); this.ValidateUsingEdmValidator(model, expectedErrors); this.ValidateUsingEdmValidator(model, EdmConstants.EdmVersion4, expectedErrors); this.ValidateUsingEdmValidator(model, ValidationRuleSet.GetEdmModelRuleSet(EdmConstants.EdmVersion4), expectedErrors); }
public void TestInterfaceCriticalNavigationPartnerInvalidOnlyCsdl() { var expectedErrors = new EdmLibTestErrors() { { null, null, EdmErrorCode.InterfaceCriticalNavigationPartnerInvalid }, { null, null, EdmErrorCode.BadUnresolvedNavigationPropertyPath } }; var csdls = InterfaceCriticalModelBuilder.InterfaceCriticalNavigationPartnerInvalidOnlyCsdl(); var model = this.GetParserResult(csdls); this.ValidateUsingEdmValidator(model, expectedErrors); this.ValidateUsingEdmValidator(model, ValidationRuleSet.GetEdmModelRuleSet(EdmConstants.EdmVersion4), expectedErrors); this.ValidateUsingEdmValidator(model, EdmConstants.EdmVersion4, expectedErrors); }
internal static void ValidateModel(IEdmModel model, Version edmSchemaVersion) { IEnumerable <EdmError> enumerable; ValidationRuleSet ruleSet = new ValidationRuleSet(ValidationRuleSet.GetEdmModelRuleSet(edmSchemaVersion).Except <ValidationRule>(excludedSchemaValidationRules).Concat <ValidationRule>(additionalSchemaValidationRules)); model.Validate(ruleSet, out enumerable); if ((enumerable != null) && enumerable.Any <EdmError>()) { StringBuilder builder = new StringBuilder(); foreach (EdmError error in enumerable) { builder.AppendLine(error.ToString()); } throw new DataServiceException(500, System.Data.Services.Strings.MetadataSerializer_ModelValidationErrors(builder.ToString())); } }
public void TestAllInterfaceCriticalModel() { var expectedErrors = new EdmLibTestErrors() { { null, null, EdmErrorCode.InterfaceCriticalEnumerableMustNotHaveNullElements }, { null, null, EdmErrorCode.InterfaceCriticalKindValueMismatch }, { null, null, EdmErrorCode.InterfaceCriticalKindValueUnexpected }, { null, null, EdmErrorCode.InterfaceCriticalNavigationPartnerInvalid }, { null, null, EdmErrorCode.InterfaceCriticalPropertyValueMustNotBeNull }, { null, null, EdmErrorCode.InterfaceCriticalPropertyValueMustNotBeNull }, }; var model = InterfaceCriticalModelBuilder.AllInterfaceCriticalModel(); this.ValidateUsingEdmValidator(model, expectedErrors); this.ValidateUsingEdmValidator(model, EdmConstants.EdmVersion4, expectedErrors); this.ValidateUsingEdmValidator(model, ValidationRuleSet.GetEdmModelRuleSet(EdmConstants.EdmVersion4), expectedErrors); }
/// <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())); } }
protected void VerifySemanticValidation(IEdmModel testModel, EdmVersion edmVersion, IEnumerable <EdmError> expectedErrors) { VerifySemanticValidation(testModel, ValidationRuleSet.GetEdmModelRuleSet(toProductVersionlookup[edmVersion]), expectedErrors); }
public void ValidateOutOfLineAnnotationWithValidTargetsInReferencedModel() { // Test that annotation targets which live in a referenced model don't cause a validation failure const string rawModelCsdl = @" <Schema Namespace=""ReferencedNS"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <Term Name=""SomeValueTerm"" Type=""Edm.String"" /> <EntityContainer Name=""SomeContainer""> <EntitySet Name=""SomeEntitySet"" EntityType=""ReferencedNS.SomeEntityType"" /> <ActionImport Name=""SomeFunctionImport"" Action=""ReferencedNS.SomeFunction"" /> </EntityContainer> <EntityType Name=""SomeEntityType""> <Key> <PropertyRef Name=""ID"" /> </Key> <Property Name=""ID"" Type=""Edm.String"" Nullable=""false"" /> </EntityType> <Action Name=""SomeFunction""><ReturnType Type=""Edm.Int32""/> <Parameter Name=""SomeFunctionImportParameter"" Type=""Edm.String"" /> </Action> </Schema>"; const string annotationModelCsdl = @" <Schema Namespace=""AnnotationNS"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <Term Name=""CommonValueTerm"" Type=""Edm.String"" /> <Annotations Target=""ReferencedNS.SomeContainer""> <Annotation Term=""AnnotationNS.CommonValueTerm"" String=""Hello world!"" /> </Annotations> <Annotations Target=""ReferencedNS.SomeContainer/SomeEntitySet""> <Annotation Term=""AnnotationNS.CommonValueTerm"" String=""Hello world!"" /> </Annotations> <Annotations Target=""ReferencedNS.SomeEntityType""> <Annotation Term=""AnnotationNS.CommonValueTerm"" String=""Hello world!"" /> </Annotations> <Annotations Target=""ReferencedNS.SomeValueTerm""> <Annotation Term=""AnnotationNS.CommonValueTerm"" String=""Hello world!"" /> </Annotations> <Annotations Target=""ReferencedNS.SomeFunction(Edm.String)""> <Annotation Term=""AnnotationNS.CommonValueTerm"" String=""Hello world!"" /> </Annotations> <Annotations Target=""ReferencedNS.SomeContainer/SomeFunctionImport""> <Annotation Term=""AnnotationNS.CommonValueTerm"" String=""Hello world!"" /> </Annotations> <Annotations Target=""ReferencedNS.SomeFunction(Edm.String)/SomeFunctionImportParameter""> <Annotation Term=""AnnotationNS.CommonValueTerm"" String=""Hello world!"" /> </Annotations> </Schema>"; var rawModel = this.GetParserResult(new List <string> { rawModelCsdl }); var modelWithAnnotations = this.GetParserResult(new List <string> { annotationModelCsdl }, rawModel); Assert.AreEqual(7, modelWithAnnotations.VocabularyAnnotations.Count(), "Invalid count of annotation."); var ruleSet = ValidationRuleSet.GetEdmModelRuleSet(Microsoft.OData.Edm.Library.EdmConstants.EdmVersion4); IEnumerable <EdmError> actualErrors = null; var validationResult = modelWithAnnotations.Validate(ruleSet, out actualErrors); Assert.IsTrue(validationResult, "Expected no validation errors from annotation with targets in an external model."); }
public void ValidateUnresolvedInlineAnnotationTargetsWithUnresolvedTypes() { // Test that unresolved annotation term types load and raise appropriate validation failures const string csdl = @" <Schema Namespace=""NS"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <Term Name=""CommonValueTerm"" Type=""Edm.String"" /> <EntityContainer Name=""SomeContainer""> <EntitySet Name=""SomeEntitySet"" EntityType=""NS.SomeEntityType""> <Annotation Term=""RefNS.UnknownValueTerm""> <Record Type=""RefNs.UnknownType""> <PropertyValue Property=""Property"" String=""PropertyValue"" /> </Record> </Annotation> </EntitySet> <ActionImport Name=""SomeFunctionImport"" Action=""NS.SomeFunction"" > <Annotation Term=""RefNS.UnknownValueTerm""> <Record Type=""RefNs.UnknownType""> <PropertyValue Property=""Property"" String=""PropertyValue"" /> </Record> </Annotation> </ActionImport> <Annotation Term=""NS.CommonValueTerm"" String=""Hello world!""> <Annotation Term=""RefNS.UnknownValueTerm""> <Record Type=""RefNs.UnknownType""> <PropertyValue Property=""Property"" String=""PropertyValue"" /> </Record> </Annotation> </Annotation> <Annotation Term=""RefNS.UnknownValueTerm""> <Record Type=""RefNs.UnknownType""> <PropertyValue Property=""Property"" String=""PropertyValue"" /> </Record> </Annotation> </EntityContainer> <EntityType Name=""SomeEntityType""> <Key> <PropertyRef Name=""ID"" /> </Key> <Property Name=""ID"" Type=""Edm.String"" Nullable=""false""> <Annotation Term=""RefNS.UnknownValueTerm""> <Record Type=""RefNs.UnknownType""> <PropertyValue Property=""Property"" String=""PropertyValue"" /> </Record> </Annotation> </Property> <Annotation Term=""RefNS.UnknownValueTerm""> <Record Type=""RefNs.UnknownType""> <PropertyValue Property=""Property"" String=""PropertyValue"" /> </Record> </Annotation> </EntityType> <Action Name=""SomeFunction""><ReturnType Type=""Edm.Int32""/> <Parameter Name=""SomeFunctionImportParameter"" Type=""Edm.String"" > <Annotation Term=""RefNS.UnknownValueTerm""> <Record Type=""RefNs.UnknownType""> <PropertyValue Property=""Property"" String=""PropertyValue"" /> </Record> </Annotation> </Parameter> <Annotation Term=""RefNS.UnknownValueTerm""> <Record Type=""RefNs.UnknownType""> <PropertyValue Property=""Property"" String=""PropertyValue"" /> </Record> </Annotation> </Action> </Schema>"; var model = this.GetParserResult(new List <string> { csdl }); // Note: we don't currently appear to support annotations on annotations, which is why the count // is 8 when in fact there are 9 in the payload. Assert.AreEqual(8, model.VocabularyAnnotations.Count(), "Invalid count of annotation."); var ruleSet = ValidationRuleSet.GetEdmModelRuleSet(Microsoft.OData.Edm.EdmConstants.EdmVersion4); IEnumerable <EdmError> actualErrors = null; var validationResult = model.Validate(ruleSet, out actualErrors); Assert.AreEqual(7, actualErrors.Count(), "Expected errors for unresolved types."); }