public void ValueTerm_Record_OnEntityType()
        {
            this.SetupModels();

            IEdmEntityType person = this.baseModel.FindEntityType("NS1.Person");
            IEdmValueTerm termStringValue = this.longDefinitionModel.FindValueTerm("bar.StringValue");
            var record = new EdmRecordExpression(
                new EdmPropertyConstructor("p1", new EdmStringConstant("s1")),
                new EdmPropertyConstructor("p2", new EdmIntegerConstant(2)));

            this.CreateAndAttachValueAnnotation(person, termStringValue, record);

            string expectedCsdl =
@"<Schema Namespace=""NS1"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <EntityType Name=""Person"">
        <Key>
            <PropertyRef Name=""Name"" />
        </Key>
        <Property Name=""Name"" Type=""Edm.String"" Nullable=""false"" />
        <Property Name=""Birthday"" Type=""Edm.DateTimeOffset"" Nullable=""false"" />
    </EntityType>
    <Annotations Target=""NS1.Person"">
        <Annotation Term=""bar.StringValue"">
            <Record>
                <PropertyValue Property=""p1"" String=""s1"" />
                <PropertyValue Property=""p2"" Int=""2"" />
            </Record>
        </Annotation>
    </Annotations>
</Schema>";
            this.SerializeAndVerifyAgainst(this.baseModel, expectedCsdl);
        }
        public static void SetSearchRestrictionsCapabilitiesAnnotation(this EdmModel model, IEdmEntitySet entitySet, bool searchable, CapabilitiesSearchExpressions unsupported)
        {
            if (model == null) throw new ArgumentNullException("model");
            if (entitySet == null) throw new ArgumentNullException("entitySet");

            var target = entitySet;
            var term = SearchRestrictionsTerm;
            var name = new EdmEnumTypeReference(SearchExpressionsType, false).ToStringLiteral((long)unsupported);
            var properties = new IEdmPropertyConstructor[]
            {
                new EdmPropertyConstructor("Searchable", new EdmBooleanConstant(searchable)),
                new EdmPropertyConstructor("UnsupportedExpressions", new EdmEnumMemberReferenceExpression(SearchExpressionsType.Members.Single(m => m.Name == name))),
            };
            var record = new EdmRecordExpression(properties);

            var annotation = new EdmAnnotation(target, term, record);
            annotation.SetSerializationLocation(model, entitySet.ToSerializationLocation());
            model.AddVocabularyAnnotation(annotation);
        }
        private static void SetCapabilitiesAnnotation(this EdmModel model, IEdmVocabularyAnnotatable target, IEdmValueTerm term, bool value, IEnumerable<IEdmNavigationProperty> navigationProperties, string name1, string name2)
        {
            if (navigationProperties == null)
            {
                navigationProperties = new IEdmNavigationProperty[0];
            }

            var properties = new IEdmPropertyConstructor[]
            {
                new EdmPropertyConstructor(name1, new EdmBooleanConstant(value)),
                new EdmPropertyConstructor(name2, new EdmCollectionExpression(navigationProperties.Select(p => new EdmNavigationPropertyPathExpression(p.Name)))),
            };
            var record = new EdmRecordExpression(properties);

            var annotation = new EdmAnnotation(target, term, record);
            annotation.SetSerializationLocation(model, target.ToSerializationLocation());
            model.AddVocabularyAnnotation(annotation);
        }
        public static IEdmModel CastResultTrueEvaluationModel()
        {
            var model = new EdmModel();

            var address = new EdmComplexType("NS", "Address");
            address.AddStructuralProperty("StreetNumber", EdmCoreModel.Instance.GetInt32(true));
            address.AddStructuralProperty("StreetName", EdmCoreModel.Instance.GetString(true));
            model.AddElement(address);

            var friend = new EdmEntityType("NS", "Friend");
            var friendName = friend.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(false));
            friend.AddKeys(friendName);
            var friendAddress = friend.AddStructuralProperty("Address", new EdmComplexTypeReference(address, true));
            model.AddElement(friend);

            var addressRecord = new EdmRecordExpression(new EdmPropertyConstructor[] { 
                new EdmPropertyConstructor("StreetNumber", new EdmIntegerConstant(3)), 
                new EdmPropertyConstructor("StreetName", new EdmStringConstant("에O詰 갂คำŚёæ")) 
            });

            var friendAddressCast = new EdmCastExpression(addressRecord, new EdmComplexTypeReference(address, true));

            var friendTerm = new EdmTerm("NS", "FriendTerm", new EdmEntityTypeReference(friend, true));
            model.AddElement(friendTerm);

            var valueAnnotation = new EdmAnnotation(
                friend,
                friendTerm,
                new EdmRecordExpression(
                    new EdmPropertyConstructor(friendName.Name, new EdmStringConstant("foo")),
                    new EdmPropertyConstructor(friendAddress.Name, friendAddressCast)));

            valueAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.OutOfLine);
            model.AddVocabularyAnnotation(valueAnnotation);

            return model;
        }
        public static void SetNavigationRestrictionsCapabilitiesAnnotation(this EdmModel model, IEdmEntitySet entitySet, CapabilitiesNavigationType type, IEnumerable<Tuple<IEdmNavigationProperty, CapabilitiesNavigationType>> properties)
        {
            if (model == null) throw new ArgumentNullException("model");
            if (entitySet == null) throw new ArgumentNullException("entitySet");

            if (properties == null)
            {
                properties = new Tuple<IEdmNavigationProperty, CapabilitiesNavigationType>[0];
            }

            var target = entitySet;
            var term = NavigationRestrictionsTerm;
            // handle type
            var typeLiteral = new EdmEnumTypeReference(NavigationTypeType, false).ToStringLiteral((long)type);
            // handle properties
            var propertiesExpression = properties.Select(p =>
            {
                var name = new EdmEnumTypeReference(NavigationTypeType, false).ToStringLiteral((long)p.Item2);
                return new EdmRecordExpression(new IEdmPropertyConstructor[]
                {
                    new EdmPropertyConstructor("NavigationProperty", new EdmNavigationPropertyPathExpression(p.Item1.Name)),
                    new EdmPropertyConstructor("Navigability", new EdmEnumMemberReferenceExpression(NavigationTypeType.Members.Single(m => m.Name == name))),
                });
            });

            var record = new EdmRecordExpression(new IEdmPropertyConstructor[]
            {
                new EdmPropertyConstructor("Navigability", new EdmEnumMemberReferenceExpression(NavigationTypeType.Members.Single(m => m.Name == typeLiteral))),
                new EdmPropertyConstructor("RestrictedProperties", new EdmCollectionExpression(propertiesExpression))
            });

            var annotation = new EdmAnnotation(target, term, record);
            annotation.SetSerializationLocation(model, entitySet.ToSerializationLocation());
            model.AddVocabularyAnnotation(annotation);
        }
        private static void SetCoreChangeTrackingAnnotation(this EdmModel model, EdmEntitySet entitySet, IEdmStructuralProperty[] filterableProperties, IEdmNavigationProperty[] expandableProperties)
        {
            IEdmModel termModel = ReadTermModel("CoreCapabilities.csdl");
            IEdmValueTerm changeTracking = termModel.FindDeclaredValueTerm("Core.ChangeTracking");
            var exp = new EdmRecordExpression(
                new EdmPropertyConstructor("Supported", new EdmBooleanConstant(true)),

                new EdmPropertyConstructor("FilterableProperties", new EdmCollectionExpression(filterableProperties.Select(p => new EdmPropertyPathExpression(p.Name)))),
                new EdmPropertyConstructor("ExpandableProperties", new EdmCollectionExpression(expandableProperties.Select(p => new EdmPropertyPathExpression(p.Name)))));

            EdmAnnotation annotation = new EdmAnnotation(entitySet, changeTracking, exp);
            annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
            model.AddVocabularyAnnotation(annotation);
        }
        private static void SetVocabularyAnnotation(this EdmModel model, IEdmVocabularyAnnotatable target,
            IList<IEdmPropertyConstructor> properties, string qualifiedName)
        {
            Contract.Assert(model != null);
            Contract.Assert(target != null);

            IEdmValueTerm term = model.FindValueTerm(qualifiedName);
            if (term != null)
            {
                IEdmRecordExpression record = new EdmRecordExpression(properties);
                EdmAnnotation annotation = new EdmAnnotation(target, term, record);
                annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
                model.SetVocabularyAnnotation(annotation);
            }
        }
        public void EdmRecordExpression()
        {
            var e = new EdmRecordExpression(EdmCoreModel.Instance.GetBoolean(true).AsStructured(),
                new EdmPropertyConstructor("p1", new EdmStringConstant("qwerty")),
                new EdmPropertyConstructor("p2", new EdmStringConstant("qwerty2")));
            Assert.AreEqual(EdmExpressionKind.Record, e.ExpressionKind, "e.ExpressionKind");
            Assert.AreEqual("Edm.Boolean", e.DeclaredType.FullName(), "e.DeclaredType");
            Assert.IsTrue(e.IsBad(), "e is bad because it has a bad declared type");
            Assert.AreEqual(1, e.Errors().Count(), "Expression has errors");

            e = new EdmRecordExpression();
            Assert.IsNull(e.DeclaredType, "e.DeclaredType");
            Assert.AreEqual(0, e.Properties.Count(), "e.Properties.Count()");
            Assert.IsFalse(e.IsBad(), "e is good");
            Assert.AreEqual(0, e.Errors().Count(), "Expression has no errors");

            e = new EdmRecordExpression(new EdmEntityTypeReference(new EdmEntityType("", ""), false),
                new EdmPropertyConstructor("p1", new EdmStringConstant("qwerty")),
                new EdmPropertyConstructor("p2", new EdmStringConstant("qwerty2")));
            Assert.IsFalse(e.IsBad(), "e is good");
            Assert.AreEqual(0, e.Errors().Count(), "Expression has no errors");

            e = new EdmRecordExpression((IEdmStructuredTypeReference)null);
            Assert.IsNull(e.DeclaredType, "e.DeclaredType");
            Assert.AreEqual(0, e.Properties.Count(), "e.Properties.Count()");
            Assert.IsFalse(e.IsBad(), "e is good");
            Assert.AreEqual(0, e.Errors().Count(), "Expression has no errors");

            this.VerifyThrowsException(typeof(ArgumentNullException), () => new EdmPropertyConstructor(null, new EdmStringConstant("qwerty")));
            this.VerifyThrowsException(typeof(ArgumentNullException), () => new EdmPropertyConstructor("p1", null));
        }
 public IEdmExpression ConvertToStockExpression(IEdmExpression edmExpression, EdmModel stockModel)
 {
     IEdmExpression result = null;
     switch (edmExpression.ExpressionKind)
     {
         case EdmExpressionKind.Null:
             result = EdmNullExpression.Instance;
             break;
         case EdmExpressionKind.StringConstant:
             var tempString = (IEdmStringConstantExpression)edmExpression;
             result = new EdmStringConstant(tempString.Type != null ? this.ConvertToStockTypeReference(tempString.Type, stockModel).AsString() : null, tempString.Value);
             break;
         case EdmExpressionKind.IntegerConstant:
             var tempInteger = (IEdmIntegerConstantExpression)edmExpression;
             result = new EdmIntegerConstant(tempInteger.Type != null ? this.ConvertToStockTypeReference(tempInteger.Type, stockModel).AsPrimitive() : null, tempInteger.Value);
             break;
         case EdmExpressionKind.Record:
             var tempRecord = (IEdmRecordExpression)edmExpression;
             result = new EdmRecordExpression(
                 tempRecord.DeclaredType == null ? null : this.ConvertToStockTypeReference(tempRecord.DeclaredType, stockModel).AsStructured(),
                 tempRecord.Properties.Select(edmProperty => 
                     (IEdmPropertyConstructor)new EdmPropertyConstructor(edmProperty.Name, this.ConvertToStockExpression(edmProperty.Value, stockModel))));
             break;
         case EdmExpressionKind.Collection:
             var tempCollection = (IEdmCollectionExpression)edmExpression;
             result = new EdmCollectionExpression(tempCollection.Elements.Select(element => this.ConvertToStockExpression(element, stockModel)));
             break;
         default:
             throw new NotImplementedException();
     }
     return result;
 }
        public void ConstructibleVocabularyAddingValueAnnotationToNewElement()
        {
            EdmModel model = VocabularyTestModelBuilder.InlineAnnotationSimpleModel();

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

            var container = model.FindEntityContainer("Container") as EdmEntityContainer;
            Assert.IsNotNull(container, "Invalid entity container name.");

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

            EdmEntitySet carSet = container.AddEntitySet("CarSet", carType);

            var person = model.FindEntityType("AnnotationNamespace.Person") as EdmEntityType;
            Assert.IsNotNull(person, "Invalid entity type.");

            EdmTerm personTerm = new EdmTerm("AnnotationNamespace", "PersonTerm", new EdmEntityTypeReference(person, true));
            EdmRecordExpression recordOfPerson = new EdmRecordExpression(
                new EdmPropertyConstructor("Id", new EdmIntegerConstant(22)),
                new EdmPropertyConstructor("Name", new EdmStringConstant("Johnny")));

            EdmAnnotation valueAnnotation = new EdmAnnotation(
                carSet,
                personTerm,
                recordOfPerson);

            valueAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
            model.AddVocabularyAnnotation(valueAnnotation);

            vocabularyAnnotations = model.VocabularyAnnotations;
            Assert.AreEqual(2, vocabularyAnnotations.Count(), "Invalid vocabulary annotation count.");

            List<PropertyValue> listOfNames = new List<PropertyValue>   { 
                                                                            new PropertyValue("Id", "22"), 
                                                                            new PropertyValue("Name", "Johnny")
                                                                        };

            var valueAnnotationFound = this.CheckForValueAnnotation(vocabularyAnnotations, EdmExpressionKind.Record, listOfNames);
            Assert.IsTrue(valueAnnotationFound, "Annotation can't be found.");

            var containerCarSet = container.EntitySets().Where(x => x.Name.Equals("CarSet")).SingleOrDefault() as EdmEntitySet;
            Assert.IsNotNull(containerCarSet, "Entity set did not get added to container properly.");

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

            valueAnnotationFound = this.CheckForValueAnnotation(carSetVocabularAnnotation, EdmExpressionKind.Record, listOfNames);
            Assert.IsTrue(valueAnnotationFound, "Annotation can't be found.");
        }
        public void ConstructibleVocabularyAddingOutOfLineValueAnnotationToExistingElement()
        {
            EdmModel model = VocabularyTestModelBuilder.InlineAnnotationSimpleModel();

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

            var container = model.FindEntityContainer("Container") as EdmEntityContainer;
            Assert.IsNotNull(container, "Invalid entity container name.");

            var person = model.FindEntityType("AnnotationNamespace.Person") as EdmEntityType;
            Assert.IsNotNull(person, "Invalid entity type.");

            EdmTerm personTerm = new EdmTerm("AnnotationNamespace", "PersonTerm", new EdmEntityTypeReference(person, true));
            EdmRecordExpression recordOfPerson = new EdmRecordExpression(
                new EdmPropertyConstructor("Id", new EdmIntegerConstant(22)),
                new EdmPropertyConstructor("Name", new EdmStringConstant("Johnny")));

            EdmAnnotation valueAnnotation = new EdmAnnotation(
                container,
                personTerm,
                recordOfPerson);
            model.AddVocabularyAnnotation(valueAnnotation);

            vocabularyAnnotations = model.VocabularyAnnotations;
            Assert.AreEqual(2, vocabularyAnnotations.Count(), "Invalid vocabulary annotation count.");

            List<PropertyValue> listOfNames = new List<PropertyValue>   { 
                                                                            new PropertyValue("Id", "22"), 
                                                                            new PropertyValue("Name", "Johnny")
                                                                        };

            var valueAnnotationFound = this.CheckForValueAnnotation(vocabularyAnnotations, EdmExpressionKind.Record, listOfNames);
            Assert.IsTrue(valueAnnotationFound, "Annotation can't be found.");

            var containerVocabularyAnnotations = container.VocabularyAnnotations(model);
            valueAnnotationFound = this.CheckForValueAnnotation(containerVocabularyAnnotations, EdmExpressionKind.Record, listOfNames);
            Assert.IsTrue(valueAnnotationFound, "Annotation can't be found.");
        }
        public static IEdmModel ValueAnnotationWithCollectionComplexTypeModel()
        {
            var model = new EdmModel();

            var person = new EdmComplexType("NS", "Person");
            var name = person.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(true));
            model.AddElement(person);

            var friendNames = new EdmTerm("NS", "FriendNames", EdmCoreModel.GetCollection(new EdmComplexTypeReference(person, true)));
            model.AddElement(friendNames);

            var billGatesRecord = new EdmRecordExpression(new EdmPropertyConstructor("Name", new EdmStringConstant("Bill Gates")));
            var steveBRecord = new EdmRecordExpression(new EdmPropertyConstructor("Name", new EdmStringConstant("Steve B")));
            var annotationValue = new EdmCollectionExpression(billGatesRecord, steveBRecord);

            var valueAnnotation = new EdmAnnotation(
                person,
                friendNames,
                annotationValue);
            model.AddVocabularyAnnotation(valueAnnotation);

            return model;
        }
        public static IEdmModel ValueAnnotationComplexTypeWithFewerPropertiesModel()
        {
            var model = new EdmModel();

            var address = new EdmComplexType("NS", "Address");
            address.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(true));
            model.AddElement(address);

            var person = new EdmComplexType("NS", "Person");
            person.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false));
            person.AddStructuralProperty("Address", new EdmComplexTypeReference(address, true));
            person.AddStructuralProperty("FriendNames", EdmCoreModel.GetCollection(EdmCoreModel.Instance.GetString(true)));
            model.AddElement(person);

            var personInfoTerm = new EdmTerm("NS", "PersonInfo", new EdmComplexTypeReference(person, true));
            model.AddElement(personInfoTerm);

            var addressRecord = new EdmRecordExpression(new EdmPropertyConstructor("Name", new EdmStringConstant("Microsoft Way")));

            var valueAnnotationRecord = new EdmRecordExpression(
                new EdmPropertyConstructor("Id", new EdmIntegerConstant(7)),
                new EdmPropertyConstructor("Address", addressRecord));
            
            var valueAnnotation = new EdmAnnotation(
                personInfoTerm,
                personInfoTerm,
                valueAnnotationRecord);

            valueAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.OutOfLine);
            model.AddVocabularyAnnotation(valueAnnotation);

            return model;
        }
        public static IEdmModel SimpleValueAnnotationWithComplexTypeModel()
        {
            var model = new EdmModel();

            var address = new EdmComplexType("ßÆœÇèÒöæ", "नुसौस्वागूूम");
            address.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(true));
            model.AddElement(address);

            var person = new EdmComplexType("ßÆœÇèÒöæ", "Person");
            person.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false));
            person.AddStructuralProperty("Address", new EdmComplexTypeReference(address, true));
            person.AddStructuralProperty("öøãçšŰŽ", EdmCoreModel.GetCollection(EdmCoreModel.Instance.GetString(true)));
            model.AddElement(person);

            var personInfoTerm = new EdmTerm("ßÆœÇèÒöæ", "PersonInfo", new EdmComplexTypeReference(person, true));
            model.AddElement(personInfoTerm);

            var addressRecord = new EdmRecordExpression(new EdmPropertyConstructor("Name", new EdmStringConstant("Microsoft Way")));

            var friendNamesRecord = new EdmCollectionExpression(new EdmStringConstant("伯唯堯帯作停捜桜噂構申表アイウ¥¥"), new EdmStringConstant("bar"));

            var valueAnnotationRecord = new EdmRecordExpression(
                new EdmPropertyConstructor("Id", new EdmIntegerConstant(7)),
                new EdmPropertyConstructor("Address", addressRecord),
                new EdmPropertyConstructor("öøãçšŰŽ", friendNamesRecord));

            var valueAnnotation = new EdmAnnotation(
                personInfoTerm,
                personInfoTerm,
                valueAnnotationRecord);

            valueAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.OutOfLine);
            model.AddVocabularyAnnotation(valueAnnotation);

            return model;
        }