public static IEdmModel BuildEdmModel(ODataModelBuilder builder) { if (builder == null) { throw Error.ArgumentNull("builder"); } EdmModel model = new EdmModel(); EdmEntityContainer container = new EdmEntityContainer(builder.Namespace, builder.ContainerName); // add types and sets, building an index on the way. Dictionary<Type, IEdmType> edmTypeMap = model.AddTypes(builder.StructuralTypes, builder.EnumTypes); Dictionary<string, EdmEntitySet> edmEntitySetMap = model.AddEntitySets(builder, container, edmTypeMap); // add procedures model.AddProcedures(builder.Procedures, container, edmTypeMap, edmEntitySetMap); // finish up model.AddElement(container); // build the map from IEdmEntityType to IEdmFunctionImport model.SetAnnotationValue<BindableProcedureFinder>(model, new BindableProcedureFinder(model)); // set the data service version annotations. model.SetDataServiceVersion(builder.DataServiceVersion); model.SetMaxDataServiceVersion(builder.MaxDataServiceVersion); return model; }
public ODataSingletonDeserializerTest() { EdmModel model = new EdmModel(); var employeeType = new EdmEntityType("NS", "Employee"); employeeType.AddStructuralProperty("EmployeeId", EdmPrimitiveTypeKind.Int32); employeeType.AddStructuralProperty("EmployeeName", EdmPrimitiveTypeKind.String); model.AddElement(employeeType); EdmEntityContainer defaultContainer = new EdmEntityContainer("NS", "Default"); model.AddElement(defaultContainer); _singleton = new EdmSingleton(defaultContainer, "CEO", employeeType); defaultContainer.AddElement(_singleton); model.SetAnnotationValue<ClrTypeAnnotation>(employeeType, new ClrTypeAnnotation(typeof(EmployeeModel))); _edmModel = model; _edmContainer = defaultContainer; _readContext = new ODataDeserializerContext { Path = new ODataPath(new SingletonPathSegment(_singleton)), Model = _edmModel, ResourceType = typeof(EmployeeModel) }; _deserializerProvider = new DefaultODataDeserializerProvider(); }
private void SetImmediateAnnotations(IEdmElement edmAnnotatable, IEdmElement stockAnnotatable, IEdmModel edmModel, EdmModel stockModel) { IEnumerable<IEdmDirectValueAnnotation> annotations = edmModel.DirectValueAnnotations(edmAnnotatable); foreach (IEdmDirectValueAnnotation annotation in annotations) { var annotationValue = annotation.Value; string annotationNamespace = annotation.NamespaceUri; string annotationName = annotation.Name; stockModel.SetAnnotationValue(stockAnnotatable, annotationNamespace, annotationName, annotationValue); } }
public static EdmModel AnnotationWithoutChildrenModel() { EdmModel model = new EdmModel(); EdmComplexType complexType = new EdmComplexType("DefaultNamespace", "ComplexType"); complexType.AddStructuralProperty("Data", EdmCoreModel.Instance.GetString(true)); model.AddElement(complexType); XElement annotationElement = new XElement("{http://foo}Annotation"); var annotation = new EdmStringConstant(EdmCoreModel.Instance.GetString(false), annotationElement.ToString()); annotation.SetIsSerializedAsElement(model, true); model.SetAnnotationValue(complexType, "http://foo", "Annotation", annotation); return model; }
public static IEdmModel BuildEdmModel(ODataModelBuilder builder) { if (builder == null) { throw Error.ArgumentNull("builder"); } EdmModel model = new EdmModel(); EdmEntityContainer container = new EdmEntityContainer(builder.Namespace, builder.ContainerName); // add types and sets, building an index on the way. IEnumerable<IEdmTypeConfiguration> configTypes = builder.StructuralTypes.Concat<IEdmTypeConfiguration>(builder.EnumTypes); EdmTypeMap edmMap = EdmTypeBuilder.GetTypesAndProperties(configTypes); Dictionary<Type, IEdmType> edmTypeMap = model.AddTypes(edmMap); // Add EntitySets and build the mapping between the EdmEntitySet and the NavigationSourceConfiguration NavigationSourceAndAnnotations[] entitySets = container.AddEntitySetAndAnnotations(builder, edmTypeMap); // Add Singletons and build the mapping between the EdmSingleton and the NavigationSourceConfiguration NavigationSourceAndAnnotations[] singletons = container.AddSingletonAndAnnotations(builder, edmTypeMap); // Merge EntitySets and Singletons together IEnumerable<NavigationSourceAndAnnotations> navigationSources = entitySets.Concat(singletons); // Build the navigation source map IDictionary<string, EdmNavigationSource> navigationSourceMap = model.GetNavigationSourceMap(builder, edmTypeMap, navigationSources); // Add the core vocabulary annotations model.AddCoreVocabularyAnnotations(entitySets, edmMap); // Add the capabilities vocabulary annotations model.AddCapabilitiesVocabularyAnnotations(entitySets, edmMap); // add procedures model.AddProcedures(builder.Procedures, container, edmTypeMap, navigationSourceMap); // finish up model.AddElement(container); // build the map from IEdmEntityType to IEdmFunctionImport model.SetAnnotationValue<BindableProcedureFinder>(model, new BindableProcedureFinder(model)); return model; }
private static EdmModel SetNullAnnotationNameModel() { EdmModel model = new EdmModel(); EdmEnumType spicy = new EdmEnumType("DefaultNamespace", "Spicy"); model.AddElement(spicy); XElement annotationElement = new XElement("{http://foo}Annotation", new XElement("{http://foo1}Child", "1" ) ); var annotation = new EdmStringConstant(EdmCoreModel.Instance.GetString(false), annotationElement.ToString()); annotation.SetIsSerializedAsElement(model, true); model.SetAnnotationValue(spicy, "http://foo", null, annotation); return model; }
public void AnnotationNameWhichIsNotSimpleIdentifierShouldPassValidation() { var model = new EdmModel(); model.SetEdmVersion(Microsoft.OData.Edm.Library.EdmConstants.EdmVersion4); var fredFlintstone = new EdmComplexType("Flintstones", "Fred"); fredFlintstone.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(false)); model.AddElement(fredFlintstone); // set the annotation name to a value which is valid XML, but not a valid SimpleIdentifier. model.SetAnnotationValue(fredFlintstone, "sap", "content-version", new EdmStringConstant(EdmCoreModel.Instance.GetString(false), "hello")); Assert.AreEqual(1, model.DirectValueAnnotations(fredFlintstone).Count(), "Wrong # of Annotations on {0}.", fredFlintstone); IEnumerable<EdmError> errors; model.Validate(out errors); Assert.AreEqual(0, errors.Count(), "Model expected to be valid."); }
public static EdmModel OperationImportParameterWithAnnotationModel() { EdmModel model = new EdmModel(); EdmEntityContainer container = new EdmEntityContainer("Default", "Container"); EdmAction simpleOperationAction = new EdmAction("Default", "SimpleFunction", EdmCoreModel.Instance.GetInt32(false)); EdmOperationParameter simpleOperationName = new EdmOperationParameter(simpleOperationAction, "Name", EdmCoreModel.Instance.GetString(true)); simpleOperationAction.AddParameter(simpleOperationName); model.AddElement(simpleOperationAction); EdmOperationImport simpleOperation = new EdmActionImport(container, "SimpleFunction", simpleOperationAction); container.AddElement(simpleOperation); model.AddElement(container); var annotationElement = new XElement("{http://foo}Annotation", 1); var annotation = new EdmStringConstant(EdmCoreModel.Instance.GetString(false), annotationElement.ToString()); annotation.SetIsSerializedAsElement(model, true); model.SetAnnotationValue(simpleOperationName, "http://foo", "Annotation", annotation); return model; }
public void TestingDirectValueAnnotationsWithVariousConstantTypes() { EdmModel model = new EdmModel(); EdmEntityContainer container = new EdmEntityContainer("TestModel", "DefaultContainer"); model.SetAnnotationValue(container, "http://foo.bar.com", "foo", new EdmBooleanConstant(EdmCoreModel.Instance.GetBoolean(false), true)); model.SetAnnotationValue(container, "http://foo.bar.com", "bar", new EdmFloatingConstant(EdmCoreModel.Instance.GetDouble(false), 3.14)); model.SetAnnotationValue(container, "http://foo.bar.com", "baz", new EdmGuidConstant(EdmCoreModel.Instance.GetGuid(false), new Guid("12345678-1234-1234-1234-123456781234"))); model.SetAnnotationValue(container, "http://foo.bar.com", "ham", new EdmBinaryConstant(EdmCoreModel.Instance.GetBinary(false), new byte[] { 13, 14, 10, 13, 11, 14, 14, 15 })); model.SetAnnotationValue(container, "http://foo.bar.com", "spam", new EdmDecimalConstant(EdmCoreModel.Instance.GetDecimal(false), (decimal)3.50)); model.SetAnnotationValue(container, "http://foo.bar.com", "spum", new EdmDateTimeOffsetConstant(EdmCoreModel.Instance.GetDateTimeOffset(false), new DateTimeOffset(2001, 1, 1, 5, 40, 00, new TimeSpan(5, 4, 0)))); model.SetAnnotationValue(container, "http://foo.bar.com", "spork", new EdmDurationConstant(EdmCoreModel.Instance.GetDuration(false), new TimeSpan(5, 4, 3))); model.AddElement(container); IEnumerable<XElement> csdl = new []{XElement.Parse( @"<Schema Namespace=""TestModel"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <EntityContainer Name=""DefaultContainer"" p2:bar=""3.14"" p2:baz=""12345678-1234-1234-1234-123456781234"" p2:foo=""true"" p2:ham=""0D0E0A0D0B0E0E0F"" p2:spam=""3.5"" p2:spork=""PT5H4M3S"" p2:spum=""2001-01-01T05:40:00+05:04"" xmlns:p2=""http://foo.bar.com"" /> </Schema>", LoadOptions.SetLineInfo)}; this.BasicConstructibleModelTestSerializingStockModel(csdl, model); }
public void ToDictionary_ContainsAllStructuralProperties_IfInstanceIsNotNull() { // Arrange EdmModel model = new EdmModel(); EdmEntityType entityType = new EdmEntityType("NS", "Name"); model.AddElement(entityType); model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity))); entityType.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Int32); IEdmTypeReference edmType = new EdmEntityTypeReference(entityType, isNullable: false); SelectExpandWrapper<TestEntity> testWrapper = new SelectExpandWrapper<TestEntity> { Instance = new TestEntity { SampleProperty = 42 }, ModelID = ModelContainer.GetModelID(model) }; // Act var result = testWrapper.ToDictionary(); // Assert Assert.Equal(42, result["SampleProperty"]); }
public CustomersModelWithInheritance() { EdmModel model = new EdmModel(); // Enum type simpleEnum EdmEnumType simpleEnum = new EdmEnumType("NS", "SimpleEnum"); simpleEnum.AddMember(new EdmEnumMember(simpleEnum, "First", new EdmIntegerConstant(0))); simpleEnum.AddMember(new EdmEnumMember(simpleEnum, "Second", new EdmIntegerConstant(1))); simpleEnum.AddMember(new EdmEnumMember(simpleEnum, "Third", new EdmIntegerConstant(2))); model.AddElement(simpleEnum); // complex type address EdmComplexType address = new EdmComplexType("NS", "Address"); address.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String); address.AddStructuralProperty("City", EdmPrimitiveTypeKind.String); address.AddStructuralProperty("State", EdmPrimitiveTypeKind.String); address.AddStructuralProperty("ZipCode", EdmPrimitiveTypeKind.String); address.AddStructuralProperty("Country", EdmPrimitiveTypeKind.String); model.AddElement(address); // open complex type "Account" EdmComplexType account = new EdmComplexType("NS", "Account", null, false, true); account.AddStructuralProperty("Bank", EdmPrimitiveTypeKind.String); account.AddStructuralProperty("CardNum", EdmPrimitiveTypeKind.Int64); model.AddElement(account); EdmComplexType specialAccount = new EdmComplexType("NS", "SpecialAccount", account, false, true); specialAccount.AddStructuralProperty("SpecialCard", EdmPrimitiveTypeKind.String); model.AddElement(specialAccount); // entity type customer EdmEntityType customer = new EdmEntityType("NS", "Customer"); customer.AddKeys(customer.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32)); IEdmProperty customerName = customer.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String); customer.AddStructuralProperty("SimpleEnum", simpleEnum.ToEdmTypeReference(isNullable: false)); customer.AddStructuralProperty("Address", new EdmComplexTypeReference(address, isNullable: true)); customer.AddStructuralProperty("Account", new EdmComplexTypeReference(account, isNullable: true)); IEdmTypeReference primitiveTypeReference = EdmCoreModel.Instance.GetPrimitive( EdmPrimitiveTypeKind.String, isNullable: true); var city = customer.AddStructuralProperty( "City", primitiveTypeReference, defaultValue: null, concurrencyMode: EdmConcurrencyMode.Fixed); model.AddElement(customer); // derived entity type special customer EdmEntityType specialCustomer = new EdmEntityType("NS", "SpecialCustomer", customer); specialCustomer.AddStructuralProperty("SpecialCustomerProperty", EdmPrimitiveTypeKind.Guid); specialCustomer.AddStructuralProperty("SpecialAddress", new EdmComplexTypeReference(address, isNullable: true)); model.AddElement(specialCustomer); // entity type order (open entity type) EdmEntityType order = new EdmEntityType("NS", "Order", null, false, true); // EdmEntityType order = new EdmEntityType("NS", "Order"); order.AddKeys(order.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32)); order.AddStructuralProperty("City", EdmPrimitiveTypeKind.String); order.AddStructuralProperty("Amount", EdmPrimitiveTypeKind.Int32); model.AddElement(order); // derived entity type special order EdmEntityType specialOrder = new EdmEntityType("NS", "SpecialOrder", order, false, true); specialOrder.AddStructuralProperty("SpecialOrderProperty", EdmPrimitiveTypeKind.Guid); model.AddElement(specialOrder); // test entity EdmEntityType testEntity = new EdmEntityType("System.Web.OData.Query.Expressions", "TestEntity"); testEntity.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Binary); model.AddElement(testEntity); // containment // my order EdmEntityType myOrder = new EdmEntityType("NS", "MyOrder"); myOrder.AddKeys(myOrder.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32)); myOrder.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String); model.AddElement(myOrder); // order line EdmEntityType orderLine = new EdmEntityType("NS", "OrderLine"); orderLine.AddKeys(orderLine.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32)); orderLine.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String); model.AddElement(orderLine); EdmNavigationProperty orderLinesNavProp = myOrder.AddUnidirectionalNavigation( new EdmNavigationPropertyInfo { Name = "OrderLines", TargetMultiplicity = EdmMultiplicity.Many, Target = orderLine, ContainsTarget = true, }); EdmNavigationProperty nonContainedOrderLinesNavProp = myOrder.AddUnidirectionalNavigation( new EdmNavigationPropertyInfo { Name = "NonContainedOrderLines", TargetMultiplicity = EdmMultiplicity.Many, Target = orderLine, ContainsTarget = false, }); EdmAction tag = new EdmAction("NS", "tag", returnType: null, isBound: true, entitySetPathExpression: null); tag.AddParameter("entity", new EdmEntityTypeReference(orderLine, false)); model.AddElement(tag); // entity sets EdmEntityContainer container = new EdmEntityContainer("NS", "ModelWithInheritance"); model.AddElement(container); EdmEntitySet customers = container.AddEntitySet("Customers", customer); EdmEntitySet orders = container.AddEntitySet("Orders", order); EdmEntitySet myOrders = container.AddEntitySet("MyOrders", myOrder); // singletons EdmSingleton vipCustomer = container.AddSingleton("VipCustomer", customer); EdmSingleton mary = container.AddSingleton("Mary", customer); EdmSingleton rootOrder = container.AddSingleton("RootOrder", order); // annotations model.SetOptimisticConcurrencyAnnotation(customers, new[] { city }); // containment IEdmContainedEntitySet orderLines = (IEdmContainedEntitySet)myOrders.FindNavigationTarget(orderLinesNavProp); // no-containment IEdmNavigationSource nonContainedOrderLines = myOrders.FindNavigationTarget(nonContainedOrderLinesNavProp); // actions EdmAction upgrade = new EdmAction("NS", "upgrade", returnType: null, isBound: true, entitySetPathExpression: null); upgrade.AddParameter("entity", new EdmEntityTypeReference(customer, false)); model.AddElement(upgrade); EdmAction specialUpgrade = new EdmAction("NS", "specialUpgrade", returnType: null, isBound: true, entitySetPathExpression: null); specialUpgrade.AddParameter("entity", new EdmEntityTypeReference(specialCustomer, false)); model.AddElement(specialUpgrade); // actions bound to collection EdmAction upgradeAll = new EdmAction("NS", "UpgradeAll", returnType: null, isBound: true, entitySetPathExpression: null); upgradeAll.AddParameter("entityset", new EdmCollectionTypeReference(new EdmCollectionType(new EdmEntityTypeReference(customer, false)))); model.AddElement(upgradeAll); EdmAction upgradeSpecialAll = new EdmAction("NS", "UpgradeSpecialAll", returnType: null, isBound: true, entitySetPathExpression: null); upgradeSpecialAll.AddParameter("entityset", new EdmCollectionTypeReference(new EdmCollectionType(new EdmEntityTypeReference(specialCustomer, false)))); model.AddElement(upgradeSpecialAll); // functions IEdmTypeReference returnType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Boolean, isNullable: false); IEdmTypeReference stringType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.String, isNullable: false); IEdmTypeReference intType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: false); EdmFunction IsUpgraded = new EdmFunction( "NS", "IsUpgraded", returnType, isBound: true, entitySetPathExpression: null, isComposable: false); IsUpgraded.AddParameter("entity", new EdmEntityTypeReference(customer, false)); model.AddElement(IsUpgraded); EdmFunction orderByCityAndAmount = new EdmFunction( "NS", "OrderByCityAndAmount", stringType, isBound: true, entitySetPathExpression: null, isComposable: false); orderByCityAndAmount.AddParameter("entity", new EdmEntityTypeReference(customer, false)); orderByCityAndAmount.AddParameter("city", stringType); orderByCityAndAmount.AddParameter("amount", intType); model.AddElement(orderByCityAndAmount); EdmFunction getOrders = new EdmFunction( "NS", "GetOrders", EdmCoreModel.GetCollection(order.ToEdmTypeReference(false)), isBound: true, entitySetPathExpression: null, isComposable: true); getOrders.AddParameter("entity", new EdmEntityTypeReference(customer, false)); getOrders.AddParameter("parameter", intType); model.AddElement(getOrders); EdmFunction IsSpecialUpgraded = new EdmFunction( "NS", "IsSpecialUpgraded", returnType, isBound: true, entitySetPathExpression: null, isComposable: false); IsSpecialUpgraded.AddParameter("entity", new EdmEntityTypeReference(specialCustomer, false)); model.AddElement(IsSpecialUpgraded); EdmFunction getSalary = new EdmFunction( "NS", "GetSalary", stringType, isBound: true, entitySetPathExpression: null, isComposable: false); getSalary.AddParameter("entity", new EdmEntityTypeReference(customer, false)); model.AddElement(getSalary); getSalary = new EdmFunction( "NS", "GetSalary", stringType, isBound: true, entitySetPathExpression: null, isComposable: false); getSalary.AddParameter("entity", new EdmEntityTypeReference(specialCustomer, false)); model.AddElement(getSalary); EdmFunction IsAnyUpgraded = new EdmFunction( "NS", "IsAnyUpgraded", returnType, isBound: true, entitySetPathExpression: null, isComposable: false); EdmCollectionType edmCollectionType = new EdmCollectionType(new EdmEntityTypeReference(customer, false)); IsAnyUpgraded.AddParameter("entityset", new EdmCollectionTypeReference(edmCollectionType)); model.AddElement(IsAnyUpgraded); EdmFunction isCustomerUpgradedWithParam = new EdmFunction( "NS", "IsUpgradedWithParam", returnType, isBound: true, entitySetPathExpression: null, isComposable: false); isCustomerUpgradedWithParam.AddParameter("entity", new EdmEntityTypeReference(customer, false)); isCustomerUpgradedWithParam.AddParameter("city", EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.String, isNullable: false)); model.AddElement(isCustomerUpgradedWithParam); EdmFunction isCustomerLocal = new EdmFunction( "NS", "IsLocal", returnType, isBound: true, entitySetPathExpression: null, isComposable: false); isCustomerLocal.AddParameter("entity", new EdmEntityTypeReference(customer, false)); model.AddElement(isCustomerLocal); EdmFunction entityFunction = new EdmFunction( "NS", "GetCustomer", returnType, isBound: true, entitySetPathExpression: null, isComposable: false); entityFunction.AddParameter("entity", new EdmEntityTypeReference(customer, false)); entityFunction.AddParameter("customer", new EdmEntityTypeReference(customer, false)); model.AddElement(entityFunction); EdmFunction getOrder = new EdmFunction( "NS", "GetOrder", order.ToEdmTypeReference(false), isBound: true, entitySetPathExpression: null, isComposable: true); // Composable getOrder.AddParameter("entity", new EdmEntityTypeReference(customer, false)); getOrder.AddParameter("orderId", intType); model.AddElement(getOrder); // functions bound to collection EdmFunction isAllUpgraded = new EdmFunction("NS", "IsAllUpgraded", returnType, isBound: true, entitySetPathExpression: null, isComposable: false); isAllUpgraded.AddParameter("entityset", new EdmCollectionTypeReference(new EdmCollectionType(new EdmEntityTypeReference(customer, false)))); isAllUpgraded.AddParameter("param", intType); model.AddElement(isAllUpgraded); EdmFunction isSpecialAllUpgraded = new EdmFunction("NS", "IsSpecialAllUpgraded", returnType, isBound: true, entitySetPathExpression: null, isComposable: false); isSpecialAllUpgraded.AddParameter("entityset", new EdmCollectionTypeReference(new EdmCollectionType(new EdmEntityTypeReference(specialCustomer, false)))); isSpecialAllUpgraded.AddParameter("param", intType); model.AddElement(isSpecialAllUpgraded); // navigation properties EdmNavigationProperty ordersNavProp = customer.AddUnidirectionalNavigation( new EdmNavigationPropertyInfo { Name = "Orders", TargetMultiplicity = EdmMultiplicity.Many, Target = order }); mary.AddNavigationTarget(ordersNavProp, orders); vipCustomer.AddNavigationTarget(ordersNavProp, orders); customers.AddNavigationTarget(ordersNavProp, orders); orders.AddNavigationTarget( order.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "Customer", TargetMultiplicity = EdmMultiplicity.ZeroOrOne, Target = customer }), customers); // navigation properties on derived types. EdmNavigationProperty specialOrdersNavProp = specialCustomer.AddUnidirectionalNavigation( new EdmNavigationPropertyInfo { Name = "SpecialOrders", TargetMultiplicity = EdmMultiplicity.Many, Target = order }); vipCustomer.AddNavigationTarget(specialOrdersNavProp, orders); customers.AddNavigationTarget(specialOrdersNavProp, orders); orders.AddNavigationTarget( specialOrder.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "SpecialCustomer", TargetMultiplicity = EdmMultiplicity.ZeroOrOne, Target = customer }), customers); model.SetAnnotationValue<BindableProcedureFinder>(model, new BindableProcedureFinder(model)); // set properties Model = model; Container = container; Customer = customer; Order = order; Address = address; Account = account; SpecialCustomer = specialCustomer; SpecialOrder = specialOrder; Orders = orders; Customers = customers; VipCustomer = vipCustomer; Mary = mary; RootOrder = rootOrder; OrderLine = orderLine; OrderLines = orderLines; NonContainedOrderLines = nonContainedOrderLines; UpgradeCustomer = upgrade; UpgradeSpecialCustomer = specialUpgrade; CustomerName = customerName; IsCustomerUpgraded = isCustomerUpgradedWithParam; IsSpecialCustomerUpgraded = IsSpecialUpgraded; Tag = tag; }
private static IEdmModel GetEnumModel() { EdmModel model = new EdmModel(); EdmEnumType enumType = new EdmEnumType("TestModel", "TestEnum"); enumType.AddMember(new EdmEnumMember(enumType, "FirstValue", new EdmIntegerConstant(0))); enumType.AddMember(new EdmEnumMember(enumType, "FirstValue", new EdmIntegerConstant(1))); model.AddElement(enumType); model.SetAnnotationValue(model.FindDeclaredType("TestModel.TestEnum"), new ClrTypeAnnotation(typeof(TestEnum))); return model; }
public void CsdlWriterShouldFailWithGoodMessageWhenWritingInvalidXml() { var model = new EdmModel(); model.SetEdmVersion(Microsoft.OData.Edm.Library.EdmConstants.EdmVersion4); var fredFlintstone = new EdmComplexType("Flintstones", "Fred"); fredFlintstone.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(false)); model.AddElement(fredFlintstone); // set annotation which will be serialzed (because the value is an IEdmValue) with a name that doesn't match the xml spec for NCName. model.SetAnnotationValue(fredFlintstone, "sap", "-contentversion", new EdmStringConstant(EdmCoreModel.Instance.GetString(false), "hello")); Assert.AreEqual(1, model.DirectValueAnnotations(fredFlintstone).Count(), "Wrong # of Annotations on {0}.", fredFlintstone); var stringWriter = new StringWriter(); var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings() { Indent = true }); try { IEnumerable<EdmError> errors; model.TryWriteCsdl(xmlWriter, out errors); Assert.Fail("Excepted an exception when trying to serialize a direct value annotation which does not match the xml naming spec."); } catch (ArgumentException e) { Assert.AreEqual(e.Message, "Invalid name character in '-contentversion'. The '-' character, hexadecimal value 0x2D, cannot be included in a name.", "Expected exception message did not match."); } xmlWriter.Close(); }
public static EdmModel NestedXElementWithNoValueModel() { EdmModel model = new EdmModel(); EdmComplexType simpleType = new EdmComplexType("DefaultNamespace", "SimpleType"); EdmStructuralProperty simpleTypeId = simpleType.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(true)); model.AddElement(simpleType); XElement annotationElement = new XElement("{http://foo}Annotation", new XElement("{http://foo}Child", new XElement("{http://foo}GrandChild", new XElement("{http://foo}GreatGrandChild", new XElement("{http://foo}GreateGreatGrandChild") ) ) ) ); var annotation = new EdmStringConstant(EdmCoreModel.Instance.GetString(false), annotationElement.ToString()); annotation.SetIsSerializedAsElement(model, true); model.SetAnnotationValue(simpleTypeId, "http://foo", "Annotation", annotation); return model; }
public static EdmModel SetChildAnnotationAsAnnotationModel() { EdmModel model = new EdmModel(); EdmTerm note = new EdmTerm("DefaultNamespace", "Note", EdmPrimitiveTypeKind.Int16); model.AddElement(note); XElement annotationElement = new XElement("{http://foo}Annotation", new XElement("{http://foo1}Child", "1" ) ); var annotation = new EdmStringConstant(EdmCoreModel.Instance.GetString(false), annotationElement.ToString()); annotation.SetIsSerializedAsElement(model, true); model.SetAnnotationValue(note, "http://foo1", "Child", annotation); return model; }
public static EdmModel OutOfLineValueAnnotationWithAnnotationModel() { EdmModel model = new EdmModel(); EdmComplexType simpleType = new EdmComplexType("DefaultNamespace", "SimpleType"); simpleType.AddStructuralProperty("Data", EdmCoreModel.Instance.GetString(true)); model.AddElement(simpleType); EdmTerm note = new EdmTerm("DefaultNamespace", "Note", EdmCoreModel.Instance.GetString(true)); model.AddElement(note); EdmAnnotation valueAnnotation = new EdmAnnotation( simpleType, note, new EdmStringConstant("ComplexTypeNote")); valueAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.OutOfLine); model.AddVocabularyAnnotation(valueAnnotation); XElement annotationElement = new XElement("{http://foo}Annotation", "1"); var annotation = new EdmStringConstant(EdmCoreModel.Instance.GetString(false), annotationElement.ToString()); annotation.SetIsSerializedAsElement(model, true); model.SetAnnotationValue(valueAnnotation, "http://foo", "Annotation", annotation); return model; }
public static EdmModel NavigationPropertyWithAnnotationModel() { EdmModel model = new EdmModel(); EdmEntityType person = new EdmEntityType("DefaultNamespace", "Person"); EdmStructuralProperty personId = person.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false)); person.AddKeys(personId); model.AddElement(person); EdmNavigationProperty friend = person.AddBidirectionalNavigation( new EdmNavigationPropertyInfo() { Name = "Friends", Target = person, TargetMultiplicity = EdmMultiplicity.One }, new EdmNavigationPropertyInfo() { Name = "Self", TargetMultiplicity = EdmMultiplicity.One }); IEdmNavigationProperty self = friend.Partner; XElement annotationElement = new XElement("{http://foo}Annotation", "1"); var annotation = new EdmStringConstant(EdmCoreModel.Instance.GetString(false), annotationElement.ToString()); annotation.SetIsSerializedAsElement(model, true); model.SetAnnotationValue(friend, "http://foo", "Annotation", annotation); return model; }
public static EdmModel ValueTermWithAnnotationModel() { EdmModel model = new EdmModel(); EdmTerm note = new EdmTerm("DefaultNamespace", "Note", EdmCoreModel.Instance.GetString(true)); model.AddElement(note); var annotationElement = new XElement("{http://foo}Annotation", 1); var annotation = new EdmStringConstant(EdmCoreModel.Instance.GetString(false), annotationElement.ToString()); annotation.SetIsSerializedAsElement(model, true); model.SetAnnotationValue(note, "http://foo", "Annotation", annotation); return model; }
public void ValidationShouldFailOnNonXmlValidAnnotation() { var model = new EdmModel(); var fredFlintstone = new EdmComplexType("Flintstones", "Fred"); fredFlintstone.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(false)); model.AddElement(fredFlintstone); // Set annnotation which would be serialized as an attribute to be a value that is invalid per the xml spec. model.SetAnnotationValue(fredFlintstone, "sap", "-content-version", new EdmStringConstant(EdmCoreModel.Instance.GetString(false), "hello")); Assert.AreEqual(1, model.DirectValueAnnotations(fredFlintstone).Count(), "Wrong # of Annotations on {0}.", fredFlintstone); IEnumerable<EdmError> errors; model.Validate(out errors); Assert.AreEqual(1, errors.Count(), "Model expected to be invalid."); EdmError error = errors.Single(); Assert.AreEqual(error.ErrorCode, EdmErrorCode.InvalidName, "Unexpected error code"); Assert.AreEqual(error.ErrorMessage, "The specified name is not allowed: '-content-version'.", "Unexpected error message"); }
public CustomersModelWithInheritance() { EdmModel model = new EdmModel(); // Enum type simpleEnum EdmEnumType simpleEnum = new EdmEnumType("NS", "SimpleEnum"); simpleEnum.AddMember(new EdmEnumMember(simpleEnum, "First", new EdmIntegerConstant(0))); simpleEnum.AddMember(new EdmEnumMember(simpleEnum, "Second", new EdmIntegerConstant(1))); simpleEnum.AddMember(new EdmEnumMember(simpleEnum, "Third", new EdmIntegerConstant(2))); model.AddElement(simpleEnum); // complex type address EdmComplexType address = new EdmComplexType("NS", "Address"); address.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String); address.AddStructuralProperty("City", EdmPrimitiveTypeKind.String); address.AddStructuralProperty("State", EdmPrimitiveTypeKind.String); address.AddStructuralProperty("ZipCode", EdmPrimitiveTypeKind.String); address.AddStructuralProperty("Country", EdmPrimitiveTypeKind.String); model.AddElement(address); // entity type customer EdmEntityType customer = new EdmEntityType("NS", "Customer"); customer.AddKeys(customer.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32)); IEdmProperty customerName = customer.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String); customer.AddStructuralProperty("SimpleEnum", simpleEnum.ToEdmTypeReference(isNullable: false)); customer.AddStructuralProperty("Address", new EdmComplexTypeReference(address, isNullable: true)); IEdmTypeReference primitiveTypeReference = EdmCoreModel.Instance.GetPrimitive( EdmPrimitiveTypeKind.String, isNullable: true); customer.AddStructuralProperty( "City", primitiveTypeReference, defaultValue: null, concurrencyMode: EdmConcurrencyMode.Fixed); model.AddElement(customer); // derived entity type special customer EdmEntityType specialCustomer = new EdmEntityType("NS", "SpecialCustomer", customer); specialCustomer.AddStructuralProperty("SpecialCustomerProperty", EdmPrimitiveTypeKind.Guid); model.AddElement(specialCustomer); // entity type order EdmEntityType order = new EdmEntityType("NS", "Order"); order.AddKeys(order.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32)); order.AddStructuralProperty("Amount", EdmPrimitiveTypeKind.Int32); model.AddElement(order); // derived entity type special order EdmEntityType specialOrder = new EdmEntityType("NS", "SpecialOrder", order); specialOrder.AddStructuralProperty("SpecialOrderProperty", EdmPrimitiveTypeKind.Guid); model.AddElement(specialOrder); // test entity EdmEntityType testEntity = new EdmEntityType("System.Web.OData.Query.Expressions", "TestEntity"); testEntity.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Binary); model.AddElement(testEntity); // entity sets EdmEntityContainer container = new EdmEntityContainer("NS", "ModelWithInheritance"); model.AddElement(container); EdmEntitySet customers = container.AddEntitySet("Customers", customer); EdmEntitySet orders = container.AddEntitySet("Orders", order); // actions EdmAction upgrade = new EdmAction("NS", "upgrade", returnType: null, isBound: true, entitySetPathExpression: null); upgrade.AddParameter("entity", new EdmEntityTypeReference(customer, false)); model.AddElement(upgrade); EdmActionImport upgradeCustomer = container.AddActionImport( "upgrade", upgrade, new EdmEntitySetReferenceExpression(customers)); EdmAction specialUpgrade = new EdmAction("NS", "specialUpgrade", returnType: null, isBound: true, entitySetPathExpression: null); specialUpgrade.AddParameter("entity", new EdmEntityTypeReference(specialCustomer, false)); model.AddElement(specialUpgrade); EdmActionImport upgradeSpecialCustomer = container.AddActionImport( "specialUpgrade", specialUpgrade, new EdmEntitySetReferenceExpression(customers)); // functions IEdmTypeReference returnType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Boolean, isNullable: false); EdmFunction IsUpgraded = new EdmFunction( "NS", "IsUpgraded", returnType, isBound: true, entitySetPathExpression: null, isComposable: false); IsUpgraded.AddParameter("entity", new EdmEntityTypeReference(customer, false)); model.AddElement(IsUpgraded); EdmFunctionImport isCustomerUpgraded = container.AddFunctionImport( "IsUpgraded", IsUpgraded, new EdmEntitySetReferenceExpression(customers)); EdmFunction IsSpecialUpgraded = new EdmFunction( "NS", "IsSpecialUpgraded", returnType, isBound: true, entitySetPathExpression: null, isComposable: false); IsSpecialUpgraded.AddParameter("entity", new EdmEntityTypeReference(specialCustomer, false)); model.AddElement(IsSpecialUpgraded); var isSpecialCustomerUpgraded = container.AddFunctionImport( "IsSpecialUpgraded", IsSpecialUpgraded, new EdmEntitySetReferenceExpression(customers)); EdmFunction IsAnyUpgraded = new EdmFunction( "NS", "IsAnyUpgraded", returnType, isBound: true, entitySetPathExpression: null, isComposable: false); EdmCollectionType edmCollectionType = new EdmCollectionType(new EdmEntityTypeReference(customer, false)); IsAnyUpgraded.AddParameter("entityset", new EdmCollectionTypeReference(edmCollectionType)); model.AddElement(IsAnyUpgraded); container.AddFunctionImport( "IsAnyUpgraded", IsAnyUpgraded, new EdmEntitySetReferenceExpression(customers)); EdmFunction isCustomerUpgradedWithParam = new EdmFunction( "NS", "IsUpgradedWithParam", returnType, isBound: true, entitySetPathExpression: null, isComposable: false); isCustomerUpgradedWithParam.AddParameter("entity", new EdmEntityTypeReference(customer, false)); isCustomerUpgradedWithParam.AddParameter("city", EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.String, isNullable: false)); model.AddElement(isCustomerUpgradedWithParam); container.AddFunctionImport( "IsUpgradedWithParam", isCustomerUpgradedWithParam, new EdmEntitySetReferenceExpression(customers)); EdmFunction isCustomerLocal = new EdmFunction( "NS", "IsLocal", returnType, isBound: true, entitySetPathExpression: null, isComposable: false); isCustomerLocal.AddParameter("entity", new EdmEntityTypeReference(customer, false)); model.AddElement(isCustomerLocal); container.AddFunctionImport( "IsLocal", isCustomerLocal, new EdmEntitySetReferenceExpression(customers)); // navigation properties customers.AddNavigationTarget( customer.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "Orders", TargetMultiplicity = EdmMultiplicity.Many, Target = order }), orders); orders.AddNavigationTarget( order.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "Customer", TargetMultiplicity = EdmMultiplicity.ZeroOrOne, Target = customer }), customers); // navigation properties on derived types. customers.AddNavigationTarget( specialCustomer.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "SpecialOrders", TargetMultiplicity = EdmMultiplicity.Many, Target = order }), orders); orders.AddNavigationTarget( specialOrder.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "SpecialCustomer", TargetMultiplicity = EdmMultiplicity.ZeroOrOne, Target = customer }), customers); model.SetAnnotationValue<BindableProcedureFinder>(model, new BindableProcedureFinder(model)); // set properties Model = model; Container = container; Customer = customer; Order = order; Address = address; SpecialCustomer = specialCustomer; SpecialOrder = specialOrder; Orders = orders; Customers = customers; UpgradeCustomer = upgradeCustomer; UpgradeSpecialCustomer = upgradeSpecialCustomer; CustomerName = customerName; IsCustomerUpgraded = isCustomerUpgraded; IsSpecialCustomerUpgraded = isSpecialCustomerUpgraded; }
public void ValidationShouldFailOnNonXmlValidAnnotationWhichWontBeSerialized() { var model = new EdmModel(); var fredFlintstone = new EdmComplexType("Flintstones", "Fred"); fredFlintstone.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(false)); model.AddElement(fredFlintstone); // Making the value of the annotation not an IEdmValue means that it won't be serialized out as an attribute annotation. // We should still fail on this anyway, since being strict now has fewer consequences than trying to introduce strictness later. model.SetAnnotationValue(fredFlintstone, "sap", "-content-version", 1); Assert.AreEqual(1, model.DirectValueAnnotations(fredFlintstone).Count(), "Wrong # of Annotations on {0}.", fredFlintstone); IEnumerable<EdmError> errors; model.Validate(out errors); Assert.AreEqual(1, errors.Count(), "Model expected to be invalid."); EdmError error = errors.Single(); Assert.AreEqual(error.ErrorCode, EdmErrorCode.InvalidName, "Unexpected error code"); Assert.AreEqual(error.ErrorMessage, "The specified name is not allowed: '-content-version'.", "Unexpected error message"); }
private static IEdmModel GetUnTypedEdmModel() { EdmModel model = new EdmModel(); // Enum type "Color" EdmEnumType colorEnum = new EdmEnumType("NS", "Color"); colorEnum.AddMember(new EdmEnumMember(colorEnum, "Red", new EdmIntegerConstant(0))); colorEnum.AddMember(new EdmEnumMember(colorEnum, "Blue", new EdmIntegerConstant(1))); colorEnum.AddMember(new EdmEnumMember(colorEnum, "Green", new EdmIntegerConstant(2))); model.AddElement(colorEnum); // complex type "Address" EdmComplexType address = new EdmComplexType("NS", "Address"); address.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String); address.AddStructuralProperty("City", EdmPrimitiveTypeKind.String); model.AddElement(address); // derived complex type "SubAddress" EdmComplexType subAddress = new EdmComplexType("NS", "SubAddress", address); subAddress.AddStructuralProperty("Code", EdmPrimitiveTypeKind.Double); model.AddElement(subAddress); // entity type "Customer" EdmEntityType customer = new EdmEntityType("NS", "Customer"); customer.AddKeys(customer.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32)); customer.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String); customer.AddStructuralProperty("Location", new EdmComplexTypeReference(address, isNullable: true)); model.AddElement(customer); // derived entity type special customer EdmEntityType specialCustomer = new EdmEntityType("NS", "SpecialCustomer", customer); specialCustomer.AddStructuralProperty("Title", EdmPrimitiveTypeKind.Guid); model.AddElement(specialCustomer); // entity sets EdmEntityContainer container = new EdmEntityContainer("NS", "Default"); model.AddElement(container); container.AddEntitySet("FCustomers", customer); EdmComplexTypeReference complexType = new EdmComplexTypeReference(address, isNullable: false); EdmCollectionTypeReference complexCollectionType = new EdmCollectionTypeReference(new EdmCollectionType(complexType)); EdmEnumTypeReference enumType = new EdmEnumTypeReference(colorEnum, isNullable: false); EdmCollectionTypeReference enumCollectionType = new EdmCollectionTypeReference(new EdmCollectionType(enumType)); EdmEntityTypeReference entityType = new EdmEntityTypeReference(customer, isNullable: false); EdmCollectionTypeReference entityCollectionType = new EdmCollectionTypeReference(new EdmCollectionType(entityType)); IEdmTypeReference intType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: true); EdmCollectionTypeReference primitiveCollectionType = new EdmCollectionTypeReference(new EdmCollectionType(intType)); // bound functions BoundFunction(model, "IntCollectionFunction", "intValues", primitiveCollectionType, entityType); BoundFunction(model, "ComplexFunction", "address", complexType, entityType); BoundFunction(model, "ComplexCollectionFunction", "addresses", complexCollectionType, entityType); BoundFunction(model, "EnumFunction", "color", enumType, entityType); BoundFunction(model, "EnumCollectionFunction", "colors", enumCollectionType, entityType); BoundFunction(model, "EntityFunction", "customer", entityType, entityType); BoundFunction(model, "CollectionEntityFunction", "customers", entityCollectionType, entityType); // unbound functions UnboundFunction(container, "UnboundIntCollectionFunction", "intValues", primitiveCollectionType); UnboundFunction(container, "UnboundComplexFunction", "address", complexType); UnboundFunction(container, "UnboundComplexCollectionFunction", "addresses", complexCollectionType); UnboundFunction(container, "UnboundEnumFunction", "color", enumType); UnboundFunction(container, "UnboundEnumCollectionFunction", "colors", enumCollectionType); UnboundFunction(container, "UnboundEntityFunction", "customer", entityType); UnboundFunction(container, "UnboundCollectionEntityFunction", "customers", entityCollectionType); model.SetAnnotationValue<BindableProcedureFinder>(model, new BindableProcedureFinder(model)); return model; }
public void ExerciseBadtypes() { IEdmModel model = new EdmModel(); IEdmEntityType entityDef = new EdmEntityType("MyNamespace", "BadEntity"); IEdmEntityTypeReference entityRef = new EdmEntityTypeReference(entityDef, false); IEdmComplexType complexDef = new EdmComplexType("MyNamespace", "BadComplex"); IEdmComplexTypeReference complexRef = new EdmComplexTypeReference(complexDef, false); IEdmCollectionTypeReference badCollectionRef = entityRef.AsCollection(); IEdmEntityTypeReference badEntityRef = complexRef.AsEntity(); IEdmComplexTypeReference badComplexRef = entityRef.AsComplex(); IEdmEntityReferenceTypeReference badEntityRefRef = entityRef.AsEntityReference(); IEdmPrimitiveTypeReference badTemporal = entityRef.AsTemporal(); IEdmPrimitiveTypeReference badDecimal = entityRef.AsDecimal(); IEdmPrimitiveTypeReference badString = entityRef.AsString(); IEdmPrimitiveTypeReference badSpatial = entityRef.AsSpatial(); IEdmPrimitiveTypeReference badBinary = entityRef.AsBinary(); IEdmPrimitiveTypeReference badPrimitive = entityRef.AsPrimitive(); IEdmEnumTypeReference badEnum = entityRef.AsEnum(); Assert.IsTrue(badCollectionRef.IsCollection(), "Bad collection is collection"); Assert.IsTrue(badEntityRef.IsEntity(), "Bad Entity is Entity"); Assert.AreEqual(0, badEntityRef.Key().Count(), "Bad Entity has no key"); Assert.AreEqual("MyNamespace.BadComplex", badEntityRef.FullName(), "Bad named refs keep name"); Assert.AreEqual(EdmSchemaElementKind.TypeDefinition, badEntityRef.EntityDefinition().SchemaElementKind, "Bad named type definitions are still type definitions"); Assert.IsNull(badEntityRef.EntityDefinition().BaseType, "Bad Entity has no base type"); Assert.AreEqual(Enumerable.Empty<IEdmProperty>(), badEntityRef.EntityDefinition().DeclaredProperties, "Bad structured types have no properties"); Assert.IsFalse(badEntityRef.EntityDefinition().IsAbstract, "Bad structured types are not abstract"); Assert.IsFalse(badEntityRef.EntityDefinition().IsOpen, "Bad structured types are not open"); model.SetAnnotationValue(badEntityRef.Definition, "foo", "bar", new EdmStringConstant(new EdmStringTypeReference(EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.String), false), "baz")); Assert.AreEqual(1, model.DirectValueAnnotations(badEntityRef.Definition).Count(), "Bad Entity can hold annotations"); Assert.IsNotNull(model.GetAnnotationValue(badEntityRef.Definition, "foo", "bar"), "Bad Entity can find annotations"); Assert.AreEqual(EdmTermKind.Type, badEntityRef.EntityDefinition().TermKind, "EntityType has correct term kind"); Assert.IsTrue(badComplexRef.IsComplex(), "Bad Complex is Bad"); Assert.IsNull(badComplexRef.ComplexDefinition().FindProperty("PropertyName"), "Bad structured types return null for find property"); Assert.AreEqual(EdmTypeKind.EntityReference, badEntityRefRef.TypeKind(), "Bad Entity Reference is Entity Reference"); Assert.AreEqual(String.Empty, badEntityRefRef.EntityType().Name, "Bad Entity Reference has empty named EntityType"); model.SetAnnotationValue(badEntityRefRef.Definition, "foo", "bar", new EdmStringConstant(new EdmStringTypeReference(EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.String), false), "baz")); Assert.AreEqual(1, model.DirectValueAnnotations(badEntityRefRef.Definition).Count(), "Bad Entity Reference can hold annotations"); Assert.IsNotNull(model.GetAnnotationValue(badEntityRefRef.Definition, "foo", "bar"), "Bad Entity Reference can find annotations"); Assert.AreEqual(EdmPrimitiveTypeKind.None, badPrimitive.PrimitiveKind(), "Bad Primitive has no primitive kind"); Assert.AreEqual(EdmTypeKind.Primitive, badPrimitive.TypeKind(), "Bad Primitive has primitive type kind"); Assert.AreEqual(EdmSchemaElementKind.TypeDefinition, badPrimitive.PrimitiveDefinition().SchemaElementKind, "Bad Primitive is still Type Definition"); Assert.AreEqual("BadEntity", badPrimitive.PrimitiveDefinition().Name, "Bad Primitive retains name"); Assert.AreEqual("MyNamespace", badPrimitive.PrimitiveDefinition().Namespace, "Bad Primitive retains namespace name"); Assert.AreEqual(EdmTypeKind.Enum, badEnum.TypeKind(), "Bad enum has right kind."); Assert.AreEqual(EdmPrimitiveTypeKind.Int32, badEnum.EnumDefinition().UnderlyingType.PrimitiveKind, "Underlying type has correct kind."); Assert.AreEqual(0, badEnum.EnumDefinition().Members.Count(), "bad enum has no members."); Assert.AreEqual(false, badEnum.EnumDefinition().IsFlags, "bad enum not treated as bits."); Assert.AreEqual(EdmSchemaElementKind.TypeDefinition, badEnum.EnumDefinition().SchemaElementKind, "bad enum is type definition."); Assert.AreEqual("BadEntity", badEnum.EnumDefinition().Name, "Bad enum retains name"); Assert.AreEqual("MyNamespace", badEnum.EnumDefinition().Namespace, "Bad enum retains namespace name"); Assert.AreEqual(EdmErrorCode.TypeSemanticsCouldNotConvertTypeReference, badSpatial.Definition.Errors().First().ErrorCode, "Bad spatial has correct error code."); }
public static EdmModel AnnotationWithEntitySetTagInEntityContainerModel() { EdmModel model = new EdmModel(); EdmEntityContainer container = new EdmEntityContainer("Default", "Container"); model.AddElement(container); XElement annotationElement = new XElement("{http://foo}EntitySet", "1" ); var annotation = new EdmStringConstant(EdmCoreModel.Instance.GetString(false), annotationElement.ToString()); annotation.SetIsSerializedAsElement(model, true); model.SetAnnotationValue(container, "http://foo", "EntitySet", annotation); return model; }
public static EdmModel DifferentAnnotationNamespaceModel() { EdmModel model = new EdmModel(); EdmEntityType simpleType = new EdmEntityType("DefaultNamespace", "SimpleType"); EdmStructuralProperty simpleTypeId = simpleType.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false)); simpleType.AddKeys(simpleTypeId); model.AddElement(simpleType); EdmEntityContainer container = new EdmEntityContainer("DefaultNamespace", "Container"); EdmEntitySet simpleSet = container.AddEntitySet("SimpleSet", simpleType); model.AddElement(container); XElement annotationElement = new XElement("{http://foo}Annotation", new XElement("{http://foo1}Child", new XElement("{http://foo2}GrandChild", "1" ) ) ); var annotation = new EdmStringConstant(EdmCoreModel.Instance.GetString(false), annotationElement.ToString()); annotation.SetIsSerializedAsElement(model, true); model.SetAnnotationValue(simpleSet, "http://foo", "Annotation", annotation); return model; }
public void ConstructibleModelODataTestModelAnnotationTestWithAnnotations() { EdmModel model = new EdmModel(); EdmComplexType address = new EdmComplexType("TestModel", "Address"); EdmStructuralProperty addressStreet = address.AddStructuralProperty("Street", EdmCoreModel.Instance.GetString(true)); EdmStructuralProperty addressZip = address.AddStructuralProperty("Zip", EdmCoreModel.Instance.GetInt32(false)); model.SetAnnotationValue(addressZip, "http://docs.oasis-open.org/odata/ns/metadata", "MimeType", new EdmStringConstant(EdmCoreModel.Instance.GetString(false), "text/plain")); model.AddElement(address); EdmEntityType person = new EdmEntityType("TestModel", "PersonType"); EdmStructuralProperty personName = person.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(false)); model.SetAnnotationValue(personName, "http://docs.oasis-open.org/odata/ns/metadata", "MimeType", new EdmStringConstant(EdmCoreModel.Instance.GetString(false), "text/plain")); EdmStructuralProperty personAddress = person.AddStructuralProperty("Address", new EdmComplexTypeReference(address, false)); EdmStructuralProperty personPicture = person.AddStructuralProperty("Picture", EdmCoreModel.Instance.GetStream(false)); EdmStructuralProperty personId = person.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false)); person.AddKeys(personId); model.AddElement(person); EdmEntityContainer container = new EdmEntityContainer("TestModel", "DefaultContainer"); EdmEntitySet personSet = container.AddEntitySet("PersonType", person); EdmEntitySet peopleSet = container.AddEntitySet("People", person); EdmAction serviceOperationAction = new EdmAction("TestModel", "ServiceOperation1", EdmCoreModel.Instance.GetInt32(false)); EdmOperationParameter a = new EdmOperationParameter(serviceOperationAction, "a", EdmCoreModel.Instance.GetInt32(true)); EdmOperationParameter b = new EdmOperationParameter(serviceOperationAction, "b", EdmCoreModel.Instance.GetString(true)); serviceOperationAction.AddParameter(a); serviceOperationAction.AddParameter(b); model.AddElement(serviceOperationAction); EdmOperationImport serviceOperation = new EdmActionImport(container, "ServiceOperation1", serviceOperationAction); model.SetAnnotationValue(serviceOperation, "http://docs.oasis-open.org/odata/ns/metadata", "MimeType", new EdmStringConstant(EdmCoreModel.Instance.GetString(false), "img/jpeg")); container.AddElement(serviceOperation); model.AddElement(container); this.BasicConstructibleModelTestSerializingStockModel(ODataTestModelBuilder.ODataTestModelAnnotationTestWithAnnotations, model); }
public static IEdmModel BuildEfPersonEdmModel() { string Namespace = typeof(EfPerson).Namespace; EdmModel model = new EdmModel(); EdmEntityType person = new EdmEntityType(Namespace, "Person"); person.AddKeys(person.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32, isNullable: false)); person.AddStructuralProperty("Birthday", EdmPrimitiveTypeKind.Date, isNullable: true); model.AddElement(person); EdmEntityContainer container = new EdmEntityContainer(Namespace, "Default"); container.AddEntitySet("EfPeople", person); model.AddElement(container); model.SetAnnotationValue<ClrTypeAnnotation>(person, new ClrTypeAnnotation(typeof(EfPerson))); return model; }
public static EdmModel ComplexNamespaceOverlappingModel() { EdmModel model = new EdmModel(); EdmEntityContainer container = new EdmEntityContainer("Default", "Container"); EdmAction simpleOperationAction = new EdmAction("Default", "SimpleFunction", EdmCoreModel.Instance.GetInt32(false)); model.AddElement(simpleOperationAction); EdmActionImport simpleOperation = new EdmActionImport(container, "SimpleFunction", simpleOperationAction); container.AddElement(simpleOperation); model.AddElement(container); XElement annotationElement = new XElement("{http://foo}Annotation", new XElement("{http://foo}Child", new XElement("{http://foo1}GrandChild", new XElement("{http://foo}GreatGrandChild", "1" ) ) ), new XElement("{http://foo1}Child", new XElement("{http://foo}GrandChild", "1" ) ) ); var annotation = new EdmStringConstant(EdmCoreModel.Instance.GetString(false), annotationElement.ToString()); annotation.SetIsSerializedAsElement(model, true); model.SetAnnotationValue(simpleOperation, "http://foo", "Annotation", annotation); return model; }
public void Property_EntityInstance_EdmObjectHasCollectionProperty() { // Arrange EdmEntityType edmType = new EdmEntityType("NS", "Name"); edmType.AddStructuralProperty( "CollectionProperty", new EdmCollectionTypeReference( new EdmCollectionType(EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: false)))); EdmModel model = new EdmModel(); model.AddElement(edmType); model.SetAnnotationValue<ClrTypeAnnotation>(edmType, new ClrTypeAnnotation(typeof(TestEntity))); Mock<IEdmEntityObject> edmObject = new Mock<IEdmEntityObject>(); object propertyValue = new List<int> { 42 }; edmObject.Setup(e => e.TryGetPropertyValue("CollectionProperty", out propertyValue)).Returns(true); edmObject.Setup(e => e.GetEdmType()).Returns(new EdmEntityTypeReference(edmType, isNullable: false)); EntityInstanceContext entityContext = new EntityInstanceContext { EdmModel = model, EdmObject = edmObject.Object, EntityType = edmType }; // Act object resource = entityContext.EntityInstance; // Assert TestEntity testEntity = Assert.IsType<TestEntity>(resource); Assert.Equal(new[] { 42 }, testEntity.CollectionProperty); }
public void CreateTypeNameExpression_ReturnsConditionalExpression_IfTypeHasDerivedTypes() { // Arrange IEdmEntityType baseType = new EdmEntityType("NS", "BaseType"); IEdmEntityType typeA = new EdmEntityType("NS", "A", baseType); IEdmEntityType typeB = new EdmEntityType("NS", "B", baseType); IEdmEntityType typeAA = new EdmEntityType("NS", "AA", typeA); IEdmEntityType typeAAA = new EdmEntityType("NS", "AAA", typeAA); IEdmEntityType[] types = new[] { baseType, typeA, typeAAA, typeB, typeAA }; EdmModel model = new EdmModel(); foreach (var type in types) { model.AddElement(type); model.SetAnnotationValue(type, new ClrTypeAnnotation(new MockType(type.Name, @namespace: type.Namespace))); } Expression source = Expression.Constant(42); // Act Expression result = SelectExpandBinder.CreateTypeNameExpression(source, baseType, model); // Assert Assert.Equal( result.ToString(), @"IIF((42 Is AAA), ""NS.AAA"", IIF((42 Is AA), ""NS.AA"", IIF((42 Is B), ""NS.B"", IIF((42 Is A), ""NS.A"", ""NS.BaseType""))))"); }