public void TestAnnotationsWithModelReferencesAnnotationsInTheModel()
        {
            var vocabulary = new FunctionalUtilities.ModelWithRemovableElements <EdmModel>(CreateModel());

            vocabulary.RemoveElement(vocabulary.EntityContainer);
            vocabulary.RemoveElement(vocabulary.FindEntityType("NS1.Customer"));
            IEnumerable <EdmError> errors;

            Assert.IsTrue(vocabulary.Validate(out errors), "validate vocabulary");

            var model = new FunctionalUtilities.ModelWithRemovableElements <EdmModel>(CreateModel());

            model.RemoveElement(model.FindTerm("NS1.Title"));
            model.RemoveElement(model.FindEntityType("NS1.Person"));
            model.WrappedModel.AddReferencedModel(vocabulary);

            var vterm    = vocabulary.FindTerm("NS1.Title");
            var tterm    = vocabulary.FindEntityType("NS1.Person");
            var customer = model.FindEntityType("NS1.Customer");

            var vannotation = new EdmVocabularyAnnotation(
                customer,
                vterm,
                "q1",
                new EdmStringConstant("Hello world!"));

            model.WrappedModel.AddVocabularyAnnotation(vannotation);

            var sw = new StringWriter();
            var w  = XmlWriter.Create(sw, new XmlWriterSettings()
            {
                Indent = true
            });

            model.TryWriteSchema(w, out errors);
            w.Close();
            Assert.AreEqual(
                @"<?xml version=""1.0"" encoding=""utf-16""?>
<Schema Namespace=""NS1"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
  <EntityType Name=""Customer"">
    <Key>
      <PropertyRef Name=""CustomerID"" />
    </Key>
    <Property Name=""CustomerID"" Type=""Edm.String"" Nullable=""false"" />
  </EntityType>
  <EntityContainer Name=""Container"">
    <EntitySet Name=""Customers"" EntityType=""NS1.Customer"" />
  </EntityContainer>
  <Annotations Target=""NS1.Customer"">
    <Annotation Term=""NS1.Title"" Qualifier=""q1"" String=""Hello world!"" />
  </Annotations>
</Schema>", sw.ToString(), "model.WriteCsdl(w)");
        }
        public void ReferencedModelUnresolvedTermValidationTest()
        {
            var mainModel = new FunctionalUtilities.ModelWithRemovableElements<EdmModel>(ValidationTestModelBuilder.ReferenceBasicTestMainModel() as EdmModel);
            var referencedModel = ValidationTestModelBuilder.ReferenceBasicTestReferencedModel();

            var valueAnnotation = new EdmAnnotation(
                mainModel.FindEntityType("NS1.Customer"),
                referencedModel.FindValueTerm("NS1.Title"),
                "q1",
                new EdmStringConstant("Hello world!"));
            mainModel.WrappedModel.AddVocabularyAnnotation(valueAnnotation);

            var expectedErrors = new EdmLibTestErrors()
            {
                { null, null, EdmErrorCode.BadUnresolvedTerm},
            };
            IEnumerable<EdmError> edmErrors;
            mainModel.Validate(out edmErrors);
            this.CompareErrors(edmErrors, expectedErrors);

            mainModel.WrappedModel.AddReferencedModel(referencedModel);
            expectedErrors = null;
            mainModel.Validate(out edmErrors);
            this.CompareErrors(edmErrors, expectedErrors);

            mainModel.RemoveReference(referencedModel);
            expectedErrors = new EdmLibTestErrors()
            {
                { null, null, EdmErrorCode.BadUnresolvedTerm},
            };
            mainModel.Validate(out edmErrors);
            this.CompareErrors(edmErrors, expectedErrors);
        }
        public void CreateSimpleTermAnnotation()
        {
            var model = new FunctionalUtilities.ModelWithRemovableElements <EdmModel>(CreateModel());

            var term     = model.FindTerm("NS1.Title");
            var customer = model.FindEntityType("NS1.Customer");

            var annotation = new EdmVocabularyAnnotation(
                customer,
                term,
                "q1",
                new EdmStringConstant("Hello world!"));

            model.WrappedModel.AddVocabularyAnnotation(annotation);

            var annotation2 = new EdmVocabularyAnnotation(
                customer,
                term,
                "q2",
                new EdmStringConstant("Hello world2!"));

            model.WrappedModel.AddVocabularyAnnotation(annotation2);

            var annotations = customer.VocabularyAnnotations(model).ToList();

            Assert.AreEqual(2, annotations.Count, "customer.VocabularyAnnotations(model).Count");
            Assert.IsTrue(annotations.Contains(annotation), "annotations.Contains(annotation)");
            Assert.IsTrue(annotations.Contains(annotation2), "annotations.Contains(annotation)");

            IEnumerable <EdmError> errors;

            Assert.IsTrue(model.Validate(out errors), "validate");

            var sw = new StringWriter();
            var w  = XmlWriter.Create(sw, new XmlWriterSettings()
            {
                Indent = true
            });

            model.TryWriteSchema(w, out errors);
            w.Close();
            Assert.AreEqual(
                @"<?xml version=""1.0"" encoding=""utf-16""?>
<Schema Namespace=""NS1"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
  <EntityType Name=""Customer"">
    <Key>
      <PropertyRef Name=""CustomerID"" />
    </Key>
    <Property Name=""CustomerID"" Type=""Edm.String"" Nullable=""false"" />
  </EntityType>
  <Term Name=""Title"" Type=""Edm.String"" />
  <EntityType Name=""Person"">
    <Key>
      <PropertyRef Name=""ID"" />
    </Key>
    <Property Name=""ID"" Type=""Edm.String"" Nullable=""false"" />
  </EntityType>
  <EntityContainer Name=""Container"">
    <EntitySet Name=""Customers"" EntityType=""NS1.Customer"" />
  </EntityContainer>
  <Annotations Target=""NS1.Customer"">
    <Annotation Term=""NS1.Title"" Qualifier=""q1"" String=""Hello world!"" />
    <Annotation Term=""NS1.Title"" Qualifier=""q2"" String=""Hello world2!"" />
  </Annotations>
</Schema>", sw.ToString(), "model.WriteCsdl(w)");

            model.RemoveVocabularyAnnotation(annotation);
            Assert.IsTrue(model.Validate(out errors), "validate2");
            sw = new StringWriter();
            w  = XmlWriter.Create(sw, new XmlWriterSettings()
            {
                Indent = true
            });
            model.TryWriteSchema(w, out errors);
            w.Close();
            Assert.AreEqual(
                @"<?xml version=""1.0"" encoding=""utf-16""?>
<Schema Namespace=""NS1"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
  <EntityType Name=""Customer"">
    <Key>
      <PropertyRef Name=""CustomerID"" />
    </Key>
    <Property Name=""CustomerID"" Type=""Edm.String"" Nullable=""false"" />
  </EntityType>
  <Term Name=""Title"" Type=""Edm.String"" />
  <EntityType Name=""Person"">
    <Key>
      <PropertyRef Name=""ID"" />
    </Key>
    <Property Name=""ID"" Type=""Edm.String"" Nullable=""false"" />
  </EntityType>
  <EntityContainer Name=""Container"">
    <EntitySet Name=""Customers"" EntityType=""NS1.Customer"" />
  </EntityContainer>
  <Annotations Target=""NS1.Customer"">
    <Annotation Term=""NS1.Title"" Qualifier=""q2"" String=""Hello world2!"" />
  </Annotations>
</Schema>", sw.ToString(), "model.WriteCsdl(w) 2");
        }
        public void ConstructibleVocabularyRemovingValueAnnotationToNewElement()
        {
            var model = new FunctionalUtilities.ModelWithRemovableElements<EdmModel>(VocabularyTestModelBuilder.InlineAnnotationSimpleModel());
            Assert.AreEqual(1, model.VocabularyAnnotations.Count(), "Invalid vocabulary annotation count.");

            var carType = model.FindEntityType("DefaultNamespace.Car");
            Assert.IsNotNull(carType, "Invalid entity type.");
            Assert.AreEqual(0, carType.VocabularyAnnotations(model).Count(), "Invalid vocabulary annotation count.");

            EdmTerm hiddenName = new EdmTerm("AnnotationNamespace", "HiddenName", EdmCoreModel.Instance.GetString(true));
            model.WrappedModel.AddElement(hiddenName);

            EdmAnnotation valueAnnotation = new EdmAnnotation(
                carType,
                hiddenName,
                new EdmStringConstant("Gray"));

            model.WrappedModel.AddVocabularyAnnotation(valueAnnotation);
            Assert.AreEqual(2, model.VocabularyAnnotations.Count(), "Invalid vocabulary annotation count.");
            Assert.AreEqual(1, carType.VocabularyAnnotations(model).Count(), "Invalid vocabulary annotation count.");

            var valueAnnotationFound = this.CheckForValueAnnotation(carType.VocabularyAnnotations(model), EdmExpressionKind.StringConstant, new List<PropertyValue> { new PropertyValue("Gray") });
            Assert.IsTrue(valueAnnotationFound, "Value annotation cannot be found.");

            model.RemoveVocabularyAnnotation(valueAnnotation);
            Assert.AreEqual(1, model.VocabularyAnnotations.Count(), "Invalid vocabulary annotation count.");
            Assert.AreEqual(0, carType.VocabularyAnnotations(model).Count(), "Invalid vocabulary annotation count.");
        }
        public void ConstructibleVocabularyRemovingAnnotationToExistElement()
        {
            var model = new FunctionalUtilities.ModelWithRemovableElements<EdmModel>(VocabularyTestModelBuilder.InlineAnnotationSimpleModel());
            Assert.AreEqual(1, model.VocabularyAnnotations.Count(), "Invalid vocabulary annotation count.");

            var carType = model.FindEntityType("DefaultNamespace.Car");
            Assert.IsNotNull(carType, "Invalid entity type.");
            var carWheels = carType.FindProperty("Wheels");
            Assert.IsNotNull(carWheels, "Invalid entity type property.");

            var carWheelsVocabularyAnnotations = carWheels.VocabularyAnnotations(model);
            Assert.AreEqual(1, carWheelsVocabularyAnnotations.Count(), "Invalid vocabulary annotation count.");

            var valueAnnotation = carWheelsVocabularyAnnotations.ElementAt(0);
            model.RemoveVocabularyAnnotation(valueAnnotation);
            Assert.AreEqual(0, carWheelsVocabularyAnnotations.Count(), "Invalid vocabulary annotation count.");
            Assert.AreEqual(0, model.VocabularyAnnotations.Count(), "Invalid vocabulary annotation count.");
        }
        public void CreateSimpleValueTermAnnotation()
        {
            var model = new FunctionalUtilities.ModelWithRemovableElements<EdmModel>(CreateModel());

            var term = model.FindValueTerm("NS1.Title");
            var customer = model.FindEntityType("NS1.Customer");

            var annotation = new EdmAnnotation(
                customer,
                term,
                "q1",
                new EdmStringConstant("Hello world!"));
            model.WrappedModel.AddVocabularyAnnotation(annotation);

            var annotation2 = new EdmAnnotation(
                customer,
                term,
                "q2",
                new EdmStringConstant("Hello world2!"));
            model.WrappedModel.AddVocabularyAnnotation(annotation2);

            var annotations = customer.VocabularyAnnotations(model).ToList();
            Assert.AreEqual(2, annotations.Count, "customer.VocabularyAnnotations(model).Count");
            Assert.IsTrue(annotations.Contains(annotation), "annotations.Contains(annotation)");
            Assert.IsTrue(annotations.Contains(annotation2), "annotations.Contains(annotation)");

            IEnumerable<EdmError> errors;
            Assert.IsTrue(model.Validate(out errors), "validate");

            var sw = new StringWriter();
            var w = XmlWriter.Create(sw, new XmlWriterSettings() { Indent = true });
            model.TryWriteCsdl(w, out errors);
            w.Close();
            Assert.AreEqual(
@"<?xml version=""1.0"" encoding=""utf-16""?>
<Schema Namespace=""NS1"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
  <EntityType Name=""Customer"">
    <Key>
      <PropertyRef Name=""CustomerID"" />
    </Key>
    <Property Name=""CustomerID"" Type=""Edm.String"" Nullable=""false"" />
  </EntityType>
  <Term Name=""Title"" Type=""Edm.String"" />
  <EntityType Name=""Person"">
    <Key>
      <PropertyRef Name=""ID"" />
    </Key>
    <Property Name=""ID"" Type=""Edm.String"" Nullable=""false"" />
  </EntityType>
  <EntityContainer Name=""Container"">
    <EntitySet Name=""Customers"" EntityType=""NS1.Customer"" />
  </EntityContainer>
  <Annotations Target=""NS1.Customer"">
    <Annotation Term=""NS1.Title"" Qualifier=""q1"" String=""Hello world!"" />
    <Annotation Term=""NS1.Title"" Qualifier=""q2"" String=""Hello world2!"" />
  </Annotations>
</Schema>", sw.ToString(), "model.WriteCsdl(w)");

            model.RemoveVocabularyAnnotation(annotation);
            Assert.IsTrue(model.Validate(out errors), "validate2");
            sw = new StringWriter();
            w = XmlWriter.Create(sw, new XmlWriterSettings() { Indent = true });
            model.TryWriteCsdl(w, out errors);
            w.Close();
            Assert.AreEqual(
@"<?xml version=""1.0"" encoding=""utf-16""?>
<Schema Namespace=""NS1"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
  <EntityType Name=""Customer"">
    <Key>
      <PropertyRef Name=""CustomerID"" />
    </Key>
    <Property Name=""CustomerID"" Type=""Edm.String"" Nullable=""false"" />
  </EntityType>
  <Term Name=""Title"" Type=""Edm.String"" />
  <EntityType Name=""Person"">
    <Key>
      <PropertyRef Name=""ID"" />
    </Key>
    <Property Name=""ID"" Type=""Edm.String"" Nullable=""false"" />
  </EntityType>
  <EntityContainer Name=""Container"">
    <EntitySet Name=""Customers"" EntityType=""NS1.Customer"" />
  </EntityContainer>
  <Annotations Target=""NS1.Customer"">
    <Annotation Term=""NS1.Title"" Qualifier=""q2"" String=""Hello world2!"" />
  </Annotations>
</Schema>", sw.ToString(), "model.WriteCsdl(w) 2");
        }
        public void TestAnnotationsWithModelReferencesAnnotationsInTheModel()
        {
            var vocabulary = new FunctionalUtilities.ModelWithRemovableElements<EdmModel>(CreateModel());
            vocabulary.RemoveElement(vocabulary.EntityContainer);
            vocabulary.RemoveElement(vocabulary.FindEntityType("NS1.Customer"));
            IEnumerable<EdmError> errors;
            Assert.IsTrue(vocabulary.Validate(out errors), "validate vocabulary");

            var model = new FunctionalUtilities.ModelWithRemovableElements<EdmModel>(CreateModel());
            model.RemoveElement(model.FindValueTerm("NS1.Title"));
            model.RemoveElement(model.FindEntityType("NS1.Person"));
            model.WrappedModel.AddReferencedModel(vocabulary);

            var vterm = vocabulary.FindValueTerm("NS1.Title");
            var tterm = vocabulary.FindEntityType("NS1.Person");
            var customer = model.FindEntityType("NS1.Customer");

            var vannotation = new EdmAnnotation(
                customer,
                vterm,
                "q1",
                new EdmStringConstant("Hello world!"));
            model.WrappedModel.AddVocabularyAnnotation(vannotation);

            var sw = new StringWriter();
            var w = XmlWriter.Create(sw, new XmlWriterSettings() { Indent = true });
            model.TryWriteCsdl(w, out errors);
            w.Close();
            Assert.AreEqual(
@"<?xml version=""1.0"" encoding=""utf-16""?>
<Schema Namespace=""NS1"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
  <EntityType Name=""Customer"">
    <Key>
      <PropertyRef Name=""CustomerID"" />
    </Key>
    <Property Name=""CustomerID"" Type=""Edm.String"" Nullable=""false"" />
  </EntityType>
  <EntityContainer Name=""Container"">
    <EntitySet Name=""Customers"" EntityType=""NS1.Customer"" />
  </EntityContainer>
  <Annotations Target=""NS1.Customer"">
    <Annotation Term=""NS1.Title"" Qualifier=""q1"" String=""Hello world!"" />
  </Annotations>
</Schema>", sw.ToString(), "model.WriteCsdl(w)");
        }