public void EdmCollectionExpression()
        {
            var e = new EdmCollectionExpression(
                new EdmLabeledExpression("l1", new EdmStringConstant("qwerty")),
                new EdmLabeledExpression("l2", new EdmStringConstant("qwerty2")));

            Assert.IsFalse(e.IsBad(), "Expression not bad.");
            Assert.AreEqual(EdmExpressionKind.Collection, e.ExpressionKind, "e.ExpressionKind");
            Assert.AreEqual(2, e.Elements.Count(), "e.Elements.Count()");
            Assert.AreEqual("l2", ((EdmLabeledExpression)e.Elements.ElementAt(1)).Name, "((EdmLabeledElement)e.Elements.ElementAt(1)).Label");
            var l1 = e.Elements.First();

            e = new EdmCollectionExpression();
            Assert.IsNull(e.DeclaredType, "e.DeclaredType");
            Assert.AreEqual(0, e.Elements.Count(), "e.Elements.Count()");
            Assert.IsFalse(e.IsBad(), "Expression not bad.");
            Assert.AreEqual(0, e.Errors().Count(), "Expression has no errors");

            try
            {
                new EdmCollectionExpression((IEdmLabeledExpression[])null);
                Assert.Fail("exception expected");
            }
            catch (ArgumentNullException)
            {
            }
        }
        public void ValueTerm_Collection_OnEntityType()
        {
            this.SetupModels();

            IEdmEntityType person          = this.baseModel.FindEntityType("NS1.Person");
            IEdmValueTerm  termStringValue = this.longDefinitionModel.FindValueTerm("bar.StringValue");
            var            collection      = new EdmCollectionExpression(new EdmStringConstant("s1"), new EdmLabeledExpression("xyz", new EdmIntegerConstant(2)));

            this.CreateAndAttachValueAnnotation(person, termStringValue, collection);

            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"">
            <Collection>
                <String>s1</String>
                <LabeledElement Name=""xyz"">
                    <Int>2</Int>
                </LabeledElement>
            </Collection>
        </Annotation>
    </Annotations>
</Schema>";

            this.SerializeAndVerifyAgainst(this.baseModel, expectedCsdl);
        }
示例#3
0
        private static void AddOptimisticConcurrencyAnnotation(this EdmModel model, IEdmVocabularyAnnotatable target,
                                                               NavigationSourceConfiguration navigationSourceConfiguration, EdmTypeMap edmTypeMap)
        {
            EntityTypeConfiguration entityTypeConfig = navigationSourceConfiguration.EntityType;

            IEnumerable <StructuralPropertyConfiguration> concurrencyPropertyies =
                entityTypeConfig.Properties.OfType <StructuralPropertyConfiguration>().Where(property => property.ConcurrencyToken);

            IList <IEdmStructuralProperty> edmProperties = new List <IEdmStructuralProperty>();

            foreach (StructuralPropertyConfiguration property in concurrencyPropertyies)
            {
                IEdmProperty value;
                if (edmTypeMap.EdmProperties.TryGetValue(property.PropertyInfo, out value))
                {
                    var item = value as IEdmStructuralProperty;
                    if (item != null)
                    {
                        edmProperties.Add(item);
                    }
                }
            }

            if (edmProperties.Any())
            {
                IEdmCollectionExpression collectionExpression = new EdmCollectionExpression(edmProperties.Select(p => new EdmPropertyPathExpression(p.Name)).ToArray());
                IEdmTerm term = Microsoft.OData.Edm.Vocabularies.V1.CoreVocabularyModel.ConcurrencyTerm;
                EdmVocabularyAnnotation annotation = new EdmVocabularyAnnotation(target, term, collectionExpression);
                annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
                model.SetVocabularyAnnotation(annotation);
            }
        }
        private static void AddOptimisticConcurrencyAnnotation(this EdmModel model, IEdmVocabularyAnnotatable target,
                                                               EntityTypeConfiguration entityTypeConfig, EdmTypeMap edmTypeMap)
        {
            IEnumerable <StructuralPropertyConfiguration> concurrencyPropertyies =
                entityTypeConfig.Properties.OfType <StructuralPropertyConfiguration>().Where(property => property.ConcurrencyToken);

            IList <IEdmStructuralProperty> edmProperties = new List <IEdmStructuralProperty>();

            foreach (StructuralPropertyConfiguration property in concurrencyPropertyies)
            {
                IEdmProperty value;
                if (edmTypeMap.EdmProperties.TryGetValue(property.PropertyInfo, out value))
                {
                    var item = value as IEdmStructuralProperty;
                    if (item != null)
                    {
                        edmProperties.Add(item);
                    }
                }
            }

            if (edmProperties.Any())
            {
                // todo: fix SetOptimisticConcurrencyAnnotation to support setting concurrency annotations on singletons
                // https://github.com/OData/odata.net/issues/770
                // model.SetOptimisticConcurrencyAnnotation(target, edmProperties);

                IEdmCollectionExpression collectionExpression = new EdmCollectionExpression(edmProperties.Select(p => new EdmPropertyPathExpression(p.Name)).ToArray());
                IEdmValueTerm            term = Microsoft.OData.Edm.Vocabularies.V1.CoreVocabularyModel.ConcurrencyTerm;

                EdmAnnotation annotation = new EdmAnnotation(target, term, collectionExpression);
                annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
                model.SetVocabularyAnnotation(annotation);
            }
        }
        private static void SetDerivedTypeAnnotation(EdmModel model, IEdmVocabularyAnnotatable target, params string[] derivedTypes)
        {
            IEdmTerm term = ValidationVocabularyModel.DerivedTypeConstraintTerm;
            var      collectionExpression = new EdmCollectionExpression(derivedTypes.Select(d => new EdmStringConstant(d)));
            EdmVocabularyAnnotation valueAnnotationOnProperty = new EdmVocabularyAnnotation(target, term, collectionExpression);

            valueAnnotationOnProperty.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
            model.AddVocabularyAnnotation(valueAnnotationOnProperty);
        }
示例#6
0
        public void ExceptionThrowForInvalidPropertyPath()
        {
            EdmModel model = new EdmModel();

            EdmEntityType personType = new EdmEntityType("MyNs", "Person", null, false, false, true);

            personType.AddKeys(personType.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32));
            personType.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(isNullable: true));

            var container = new EdmEntityContainer("MyNs", "Container");

            model.AddElement(personType);
            container.AddEntitySet("People", personType);
            model.AddElement(container);
            IEdmEntitySet peopleSet = model.FindDeclaredEntitySet("People");

            IEdmPathExpression nameExpression = new EdmPropertyPathExpression("NameName");

            IEdmCollectionExpression collection = new EdmCollectionExpression(new[] { nameExpression });
            IEdmValueTerm            term       = null;

            foreach (var referencedModel in model.ReferencedModels)
            {
                term = referencedModel.FindDeclaredValueTerm("Org.OData.Core.V1.OptimisticConcurrencyControl");

                if (term != null)
                {
                    break;
                }
            }

            Assert.IsNotNull(term);

            EdmAnnotation valueAnnotationOnEntitySet = new EdmAnnotation(peopleSet, term, collection);

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

            ODataEntry entry = new ODataEntry
            {
                Properties = new[]
                {
                    new ODataProperty {
                        Name = "ID", Value = 123
                    },
                    new ODataProperty {
                        Name = "Name", Value = "lucy"
                    },
                }
            };

            Action action = () => GetWriterOutputForContentTypeAndKnobValue(entry, model, peopleSet, personType);

            action.ShouldThrow <ODataException>().WithMessage(ErrorStrings.EdmValueUtils_PropertyDoesntExist("MyNs.Person", "NameName"));
        }
示例#7
0
        private static void SetCapabilitiesAnnotation(this EdmModel model, IEdmVocabularyAnnotatable target, IEdmValueTerm term, IEnumerable <string> values)
        {
            if (values == null)
            {
                values = new string[0];
            }

            var expression = new EdmCollectionExpression(values.Select(function => new EdmStringConstant(function)));
            var annotation = new EdmAnnotation(target, term, expression);

            annotation.SetSerializationLocation(model, target.ToSerializationLocation());
            model.AddVocabularyAnnotation(annotation);
        }
示例#8
0
        public static IEdmModel GetEdmModel()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.EntitySet <StreamCustomer>("StreamCustomers");
            EdmModel model = builder.GetEdmModel() as EdmModel;

            IEdmEntityType streamCustomerType = model.SchemaElements.OfType <IEdmEntityType>().FirstOrDefault(c => c.Name == "StreamCustomer");

            Assert.NotNull(streamCustomerType);
            IEdmProperty photoProperty = streamCustomerType.FindProperty("Photo");

            EdmStringConstant       strConstant1         = new EdmStringConstant("application/javascript");
            EdmStringConstant       strConstant2         = new EdmStringConstant("image/png");
            EdmCollectionExpression collectionExpression = new EdmCollectionExpression(strConstant1, strConstant2);
            EdmVocabularyAnnotation annotation           = new EdmVocabularyAnnotation(photoProperty, CoreVocabularyModel.AcceptableMediaTypesTerm, collectionExpression);

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

            return(model);
        }
示例#9
0
        public void GetAcceptableMediaTypes_Works_Target()
        {
            // Arrange
            EdmModel      model  = new EdmModel();
            EdmEntityType entity = new EdmEntityType("NS", "entity");

            model.AddElement(entity);

            // Act
            IList <string> mediaTypes = model.GetAcceptableMediaTypes(entity);

            // Assert
            Assert.Null(mediaTypes);

            // Act
            EdmCollectionExpression collectionExp = new EdmCollectionExpression(
                new EdmStringConstant("application/octet-stream"),
                new EdmStringConstant("text/plain"));

            var annotation = new EdmVocabularyAnnotation(entity, CoreVocabularyModel.AcceptableMediaTypesTerm, collectionExp);

            annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
            model.SetVocabularyAnnotation(annotation);

            // Act
            mediaTypes = model.GetAcceptableMediaTypes(entity);

            // Assert
            Assert.Collection(mediaTypes,
                              e =>
            {
                Assert.Equal("application/octet-stream", e);
            },
                              e =>
            {
                Assert.Equal("text/plain", e);
            });
        }
        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 ConfigurationAnnotation(EdmModel model, string propertyName = null)
        {
            Model = model;
            var type = Model.GetEdmType(typeof(TEntity)) as EdmEntityType;
            IEdmVocabularyAnnotatable target = type;

            if (propertyName != null)
            {
                target = type.Properties().SingleOrDefault(p => p.Name == propertyName);
                if (target == null)
                {
                    Valid = false;
                }
            }

            if (Valid)
            {
                var coll       = new EdmCollectionExpression(ChildExpressions);
                var annotation = new EdmVocabularyAnnotation(target, AnnotationManagerBase.IqlConfigurationTerm, coll);
                annotation.SetSerializationLocation(Model, target.ToSerializationLocation());
                Model.AddVocabularyAnnotation(annotation);
            }
        }
示例#12
0
        public ODataJsonLightPropertySerializerTests()
        {
            // Initialize open EntityType: EntityType.
            EdmModel edmModel = new EdmModel();

            myInt32 = new EdmTypeDefinition("TestNamespace", "MyInt32", EdmPrimitiveTypeKind.Int32);
            EdmTypeDefinitionReference myInt32Reference = new EdmTypeDefinitionReference(myInt32, true);

            edmModel.AddElement(myInt32);

            myString = new EdmTypeDefinition("TestNamespace", "MyString", EdmPrimitiveTypeKind.String);
            EdmTypeDefinitionReference myStringReference = new EdmTypeDefinitionReference(myString, true);

            edmModel.AddElement(myString);

            EdmEntityType edmEntityType = new EdmEntityType("TestNamespace", "EntityType", baseType: null, isAbstract: false, isOpen: true);

            edmEntityType.AddStructuralProperty("DeclaredProperty", EdmPrimitiveTypeKind.Guid);
            edmEntityType.AddStructuralProperty("DeclaredGeometryProperty", EdmPrimitiveTypeKind.Geometry);
            edmEntityType.AddStructuralProperty("DeclaredSingleProperty", EdmPrimitiveTypeKind.Single);
            edmEntityType.AddStructuralProperty("DeclaredDoubleProperty", EdmPrimitiveTypeKind.Double);
            edmEntityType.AddStructuralProperty("MyInt32Property", myInt32Reference);
            edmEntityType.AddStructuralProperty("MyStringProperty", myStringReference);
            edmEntityType.AddStructuralProperty("TimeOfDayProperty", EdmPrimitiveTypeKind.TimeOfDay);
            edmEntityType.AddStructuralProperty("DateProperty", EdmPrimitiveTypeKind.Date);
            edmEntityType.AddStructuralProperty("PrimitiveProperty", EdmPrimitiveTypeKind.PrimitiveType);

            // add derived type constraint property.
            var derivedTypeConstrictionProperty = edmEntityType.AddStructuralProperty("PrimitiveConstraintProperty", EdmPrimitiveTypeKind.PrimitiveType);
            var term = ValidationVocabularyModel.DerivedTypeConstraintTerm;
            IEdmStringConstantExpression stringConstant1 = new EdmStringConstant("Edm.Int32");
            IEdmStringConstantExpression stringConstant2 = new EdmStringConstant("Edm.Boolean");
            var collectionExpression = new EdmCollectionExpression(new[] { stringConstant1, stringConstant2 });
            EdmVocabularyAnnotation valueAnnotationOnProperty = new EdmVocabularyAnnotation(derivedTypeConstrictionProperty, term, collectionExpression);

            valueAnnotationOnProperty.SetSerializationLocation(edmModel, EdmVocabularyAnnotationSerializationLocation.Inline);
            edmModel.AddVocabularyAnnotation(valueAnnotationOnProperty);

            edmModel.AddElement(edmEntityType);

            // Initialize ComplexType: Address, HomeAddress, and OpenAddress
            this.addressType = new EdmComplexType("TestNamespace", "Address", baseType: null, isAbstract: false, isOpen: false);
            this.addressType.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            this.derivedAddressType = new EdmComplexType("TestNamespace", "HomeAddress", baseType: this.addressType, isAbstract: false, isOpen: false);
            this.derivedAddressType.AddStructuralProperty("FamilyName", EdmPrimitiveTypeKind.String);

            this.openAddressType = new EdmComplexType("TestNamespace", "OpenAddress", baseType: null, isAbstract: false, isOpen: true);
            this.openAddressType.AddStructuralProperty("CountryRegion", EdmPrimitiveTypeKind.String);

            edmModel.AddElement(this.addressType);
            edmModel.AddElement(this.derivedAddressType);
            edmModel.AddElement(this.openAddressType);

            this.model            = TestUtils.WrapReferencedModelsToMainModel(edmModel);
            this.entityType       = edmEntityType;
            this.declaredProperty = new ODataProperty {
                Name = "DeclaredProperty", Value = Guid.Empty
            };
            this.undeclaredProperty = new ODataProperty {
                Name = "UndeclaredProperty", Value = DateTimeOffset.MinValue
            };
            this.declaredGeometryProperty = new ODataProperty {
                Name = "DeclaredGeometryProperty", Value = GeometryPoint.Create(0.0, 0.0)
            };
            this.declaredPropertyTimeOfDay = new ODataProperty {
                Name = "TimeOfDayProperty", Value = new TimeOfDay(1, 30, 5, 123)
            };
            this.declaredPropertyDate = new ODataProperty {
                Name = "DateProperty", Value = new Date(2014, 9, 17)
            };

            this.declaredPropertyAddress = new ODataProperty()
            {
                Name  = "AddressProperty",
                Value = new ODataResourceValue
                {
                    TypeName   = "TestNamespace.Address",
                    Properties = new ODataProperty[] { new ODataProperty {
                                                           Name = "City", Value = "Shanghai"
                                                       } }
                }
            };
            this.declaredPropertyHomeAddress = new ODataProperty()
            {
                Name  = "HomeAddressProperty",
                Value = new ODataResourceValue
                {
                    TypeName   = "TestNamespace.HomeAddress",
                    Properties = new ODataProperty[]
                    {
                        new ODataProperty {
                            Name = "FamilyName", Value = "Green"
                        },
                        new ODataProperty {
                            Name = "City", Value = "Shanghai"
                        }
                    }
                }
            };
            this.declaredPropertyAddressWithInstanceAnnotation = new ODataProperty()
            {
                Name  = "AddressProperty",
                Value = new ODataResourceValue
                {
                    TypeName   = "TestNamespace.Address",
                    Properties = new ODataProperty[]
                    {
                        new ODataProperty {
                            Name = "City", Value = "Shanghai"
                        }
                    },
                    InstanceAnnotations = new Collection <ODataInstanceAnnotation>
                    {
                        new ODataInstanceAnnotation("Is.ReadOnly", new ODataPrimitiveValue(true))
                    }
                }
            };
            this.declaredPropertyHomeAddressWithInstanceAnnotations = new ODataProperty()
            {
                Name  = "HomeAddressProperty",
                Value = new ODataResourceValue
                {
                    TypeName   = "TestNamespace.HomeAddress",
                    Properties = new ODataProperty[]
                    {
                        new ODataProperty
                        {
                            Name  = "FamilyName",
                            Value = "Green",
                            InstanceAnnotations = new Collection <ODataInstanceAnnotation>
                            {
                                new ODataInstanceAnnotation("FamilyName.annotation", new ODataPrimitiveValue(true))
                            }
                        },
                        new ODataProperty
                        {
                            Name  = "City",
                            Value = "Shanghai",
                            InstanceAnnotations = new Collection <ODataInstanceAnnotation>
                            {
                                new ODataInstanceAnnotation("City.annotation1", new ODataPrimitiveValue(true)),
                                new ODataInstanceAnnotation("City.annotation2", new ODataPrimitiveValue(123))
                            }
                        }
                    },
                    InstanceAnnotations = new Collection <ODataInstanceAnnotation>
                    {
                        new ODataInstanceAnnotation("Is.AutoComputable", new ODataPrimitiveValue(true)),
                        new ODataInstanceAnnotation("Is.ReadOnly", new ODataPrimitiveValue(false))
                    }
                }
            };

            this.declaredPropertyCountryRegion = new ODataProperty()
            {
                Name = "CountryRegion", Value = "China"
            };
            this.declaredPropertyCountryRegionWithInstanceAnnotation = new ODataProperty()
            {
                Name  = "CountryRegion",
                Value = "China",
                InstanceAnnotations = new Collection <ODataInstanceAnnotation>
                {
                    new ODataInstanceAnnotation("Is.ReadOnly", new ODataPrimitiveValue(true))
                }
            };
            this.undeclaredPropertyCity = new ODataProperty()
            {
                Name = "City", Value = "Shanghai"
            };

            this.declaredPropertyMyInt32 = new ODataProperty()
            {
                Name = "MyInt32Property", Value = 12345
            };
            this.declaredPropertyMyInt32WithInstanceAnnotations = new ODataProperty()
            {
                Name  = "MyInt32Property",
                Value = 12345,
                InstanceAnnotations = new Collection <ODataInstanceAnnotation>
                {
                    new ODataInstanceAnnotation("Is.AutoComputable", new ODataPrimitiveValue(true)),
                    new ODataInstanceAnnotation("Is.ReadOnly", new ODataPrimitiveValue(false))
                }
            };
            this.declaredPropertyMyString = new ODataProperty()
            {
                Name = "MyStringProperty", Value = "abcde"
            };
        }