public void Annotations_On_NonEntityType_Should_Be_Generated()
        {
            var model = new StubEdmModel();

            var entity = new StubEdmEntityType("NS1", "Person");
            var vt = new StubValueTerm("NS1", "MyValueTerm") { Type = EdmCoreModel.Instance.GetString(true) };
            var va1 = new StubValueAnnotation() { Term = vt, Value = new StubStringConstantExpression("Great!!!") };
            entity.AddVocabularyAnnotation(va1);
            model.Add(entity);

            var entitySet = new StubEdmEntitySet("personSet", null);
            var va2 = new StubValueAnnotation() { Term = vt, Value = new StubStringConstantExpression("Aha!!!") };
            entitySet.AddVocabularyAnnotation(va2);

            var container = new StubEdmEntityContainer("NS1", "myContainer") { entitySet };
            var va3 = new StubValueAnnotation() { Term = vt, Value = new StubStringConstantExpression("Huh??") };
            container.AddVocabularyAnnotation(va3);
            model.Add(container);

            XElement result = this.generator.GenerateApplicationCsdl(EdmVersion.V40, model);

            string expected = @"
<Schema Namespace='Application.NS1' xmlns='http://docs.oasis-open.org/odata/ns/edm'>
  <Annotations Target='NS1.Person'>
    <Annotation Term='NS1.MyValueTerm' String='Great!!!' />
  </Annotations>
  <Annotations Target='NS1.myContainer'>
    <Annotation Term='NS1.MyValueTerm' String='Huh??' />
  </Annotations>
  <Annotations Target='NS1.myContainer/personSet'>
    <Annotation Term='NS1.MyValueTerm' String='Aha!!!' />
  </Annotations>
</Schema>";
            AssertHelper.AssertXElementEquals(expected, result);
        }
        public void Simple_ValueAnnotation_Should_Be_Generated()
        {
            var model = new StubEdmModel();

            var entity = new StubEdmEntityType("NS1", "Person");

            var vt = new StubValueTerm("NS1", "MyValueTerm") { Type = EdmCoreModel.Instance.GetString(true) };
            var va1 = new StubValueAnnotation() { Term = vt, Value = new StubStringConstantExpression("Great!!!") };
            var va2 = new StubValueAnnotation() { Term = vt, Qualifier = "phone", Value = new StubStringConstantExpression("Fabulous!!!") };
            entity.AddVocabularyAnnotation(va1);
            entity.AddVocabularyAnnotation(va2);

            model.Add(entity);

            XElement result = this.generator.GenerateApplicationCsdl(EdmVersion.V40, model);

            string expected = @"
<Schema Namespace='Application.NS1' xmlns='http://docs.oasis-open.org/odata/ns/edm'>
  <Annotations Target='NS1.Person'>
    <Annotation Term='NS1.MyValueTerm' String='Great!!!' />
    <Annotation Term='NS1.MyValueTerm' Qualifier='phone' String='Fabulous!!!' />
  </Annotations>
</Schema>";
            AssertHelper.AssertXElementEquals(expected, result);
        }
        public static IEdmModel SimpleValueAnnotation()
        {
            StubEdmModel model = new StubEdmModel();
            CreateVocabularyDefinitions(model);

            var person = new StubEdmEntityType("NS1", "Person") 
            { 
                new StubEdmStructuralProperty("Id") { Type = EdmCoreModel.Instance.GetInt16(false) },
                new StubEdmStructuralProperty("FirstName") { Type = EdmCoreModel.Instance.GetString(false) },
                new StubEdmStructuralProperty("LastName") { Type = EdmCoreModel.Instance.GetString(false) },
            };

            var titleValueTerm = model.FindValueTerm("NS1.Title");
            var titleValueAnnotation = new StubValueAnnotation() { Term = titleValueTerm, Value = new StubStringConstantExpression("Sir") };
            person.AddVocabularyAnnotation(titleValueAnnotation);
            model.Add(person);

            return model;
        }
        public void AttachValueTermAnnotation()
        {
            var entityType = new StubEdmEntityType("NS1", "Person");

            var valueTerm = new StubValueTerm("", "FullName") { Type = EdmCoreModel.Instance.GetString(false) };

            var valueAnnotation = new StubValueAnnotation()
            {
                Term = valueTerm,
                Value = new StubStringConstantExpression("Forever Young"),
            };

            entityType.AddVocabularyAnnotation(valueAnnotation);

            Assert.AreEqual(1, entityType.InlineVocabularyAnnotations.Count(), "annotation count");

            var actual = entityType.InlineVocabularyAnnotations.Single() as IEdmValueAnnotation;

            Assert.AreEqual("", actual.Term.Namespace, "namespace");
            Assert.AreEqual("FullName", actual.Term.Name, "name");
            Assert.IsTrue(((IEdmValueTerm)actual.Term).Type.IsString() , "value term type is string");
            Assert.AreEqual("Forever Young", ((IEdmStringConstantExpression)actual.Value).Value, "annotation value");
        }
        private StubEdmModel CreateApplicationModel()
        {
            var model = new StubEdmModel();

            var valueTerm = new StubValueTerm("", "foo") { Type = EdmCoreModel.Instance.GetInt32(true) };
            model.Add(valueTerm);

            var barValueTerm = this.definitionModel.SchemaElements.OfType<IEdmValueTerm>().FirstOrDefault(t => t.Name == "bar");
            var valueAnnotation = new StubValueAnnotation() { Term = barValueTerm, Value = new StubStringConstantExpression("zzz") };
            valueTerm.AddVocabularyAnnotation(valueAnnotation);

            return model;
        }
        public void ValueTerm_Annotation_Value_Not_Match_Should_Error()
        {
            var barValueTerm = this.definitionModel.SchemaElements.OfType<IEdmValueTerm>().FirstOrDefault(t => t.Name == "bar");
            var valueAnnotation = new StubValueAnnotation() { Term = barValueTerm, Value = new StubStringConstantExpression("_not_exist_") };

            var target = this.actualModel.SchemaElements.OfType<StubValueTerm>().First();
            target.RemoveAnnotationsForTerm(barValueTerm);
            target.AddVocabularyAnnotation(valueAnnotation);

            var errors = this.comparer.CompareModels(this.expectedModel, this.actualModel);
            this.DumpErrors(errors);

            Assert.AreNotEqual(0, errors.Count, "Should have error!");
            Assert.IsTrue(errors.Any(e => e.Contains("Value expression mismatch")));
        }
        public void ValueTerm_Annotation_Count_Not_Match_Should_Error()
        {
            var bazValueTerm = this.definitionModel.SchemaElements.OfType<IEdmValueTerm>().FirstOrDefault(t => t.Name == "baz");
            var valueAnnotation = new StubValueAnnotation() { Term = bazValueTerm, Value = new StubStringConstantExpression("zzz") };
            this.actualModel.SchemaElements.OfType<StubValueTerm>().First().AddVocabularyAnnotation(valueAnnotation);

            var errors = this.comparer.CompareModels(this.expectedModel, this.actualModel);
            this.DumpErrors(errors);

            Assert.AreNotEqual(0, errors.Count, "Should have error!");
            Assert.IsTrue(errors.Any(e => e.Contains("Wrong TermAnnotations count")));
        }
        public static IEdmModel ValueAnnotationWithRecord()
        {
            StubEdmModel model = new StubEdmModel();
            CreateVocabularyDefinitions(model);

            var person = new StubEdmEntityType("NS1", "Person");
            model.Add(person);

            var reviewValueTerm = new StubValueTerm("NS1", "hReviewTerm");
            reviewValueTerm.Type = new StubEdmTypeReference() { Definition = model.FindType("NS1.hReviewEntity"), IsNullable = true };
            model.Add(reviewValueTerm);

            var recordExpression = new StubRecordExpression();
            recordExpression.AddProperty("Name", new StubStringConstantExpression("Young"));
            recordExpression.AddProperty("IdType", new StubStringConstantExpression("Driver License"));
            var reviewValueAnnotation = new StubValueAnnotation()
            {
                Term = reviewValueTerm,
                Value = recordExpression
            };

            person.AddVocabularyAnnotation(reviewValueAnnotation);

            return model;
        }
        public static IEdmModel StructuredValueAnnotation()
        {
            StubEdmModel model = new StubEdmModel();
            CreateVocabularyDefinitions(model);

            var person = new StubEdmEntityType("NS1", "Person");
            model.Add(person);

            var reviewValueTerm = new StubValueTerm("NS1", "hReviewTerm");
            reviewValueTerm.Type = new StubEdmTypeReference() { Definition = model.FindType("NS1.hReviewEntity"), IsNullable = true };
            model.Add(reviewValueTerm);

            var reviewValueAnnotation = new StubValueAnnotation()
            {
                Term = reviewValueTerm,
                Value = new StubStringConstantExpression("this should be Record"),
            };

            person.AddVocabularyAnnotation(reviewValueAnnotation);

            return model;
        }
        public static IEdmModel SimpleValueAnnotationOnContainerAndEntitySet()
        {
            StubEdmModel model = new StubEdmModel();
            CreateVocabularyDefinitions(model);

            var person = new StubEdmEntityType("NS1", "Person");
            var container = new StubEdmEntityContainer("NS1", "Container");
            var personSet = new StubEdmEntitySet("PersonSet", container) { Type = new EdmCollectionType(new EdmEntityTypeReference(person, false)) };
            container.Add(personSet);
            model.Add(container);
            model.Add(person);

            var titleValueTerm = model.FindValueTerm("NS1.Title");
            var titleValueAnnotation = new StubValueAnnotation() { Term = titleValueTerm, Value = new StubStringConstantExpression("Sir") };

            container.AddVocabularyAnnotation(titleValueAnnotation);
            personSet.AddVocabularyAnnotation(titleValueAnnotation);

            return model;
        }
        public static IEdmModel MultipleValueAnnotations()
        {
            StubEdmModel model = new StubEdmModel();
            CreateVocabularyDefinitions(model);

            var person = new StubEdmEntityType("NS1", "Person") 
            { 
                new StubEdmStructuralProperty("Id") { Type = EdmCoreModel.Instance.GetInt16(false) },
                new StubEdmStructuralProperty("FirstName") { Type = EdmCoreModel.Instance.GetString(false) },
                new StubEdmStructuralProperty("LastName") { Type = EdmCoreModel.Instance.GetString(false) },
            };

            var titleValueTerm = model.FindValueTerm("NS1.Title");
            var titleValueAnnotation = new StubValueAnnotation() { Term = titleValueTerm, Value = new StubStringConstantExpression("Sir") };
            person.AddVocabularyAnnotation(titleValueAnnotation);

            var displaySizeValueTerm = model.FindValueTerm("NS1.DisplaySize");
            var displaySizeValueAnnotation = new StubValueAnnotation() { Term = displaySizeValueTerm, Value = new StubStringConstantExpression("1024") };
            person.AddVocabularyAnnotation(displaySizeValueAnnotation);
            model.Add(person);

            var container = new StubEdmEntityContainer("NS1", "Container");
            var personSet = new StubEdmEntitySet("PersonSet", container) { Type = new EdmCollectionType(new EdmEntityTypeReference(person, false)) };
            personSet.AddVocabularyAnnotation(titleValueAnnotation);
            container.Add(personSet);
            model.Add(container);

            return model;
        }
        public static IEdmModel SimpleValueAnnotationConfict()
        {
            StubEdmModel model = new StubEdmModel();
            CreateVocabularyDefinitions(model);

            var person = new StubEdmEntityType("NS1", "Person");

            var titleValueTerm = model.FindValueTerm("NS1.Title");
            var titleValueAnnotation = new StubValueAnnotation() { Term = titleValueTerm, Value = new StubStringConstantExpression("Sir") };
            person.AddVocabularyAnnotation(titleValueAnnotation);
            var titleValueAnnotation2 = new StubValueAnnotation() { Term = titleValueTerm, Value = new StubStringConstantExpression("Senor") };
            person.AddVocabularyAnnotation(titleValueAnnotation2);

            model.Add(person);

            return model;
        }