public void ReadingTypeDefinitionPayloadWithEdmTypeAnnotationJsonLight() { EdmModel model = new EdmModel(); EdmEntityType entityType = new EdmEntityType("NS", "Person"); entityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32); EdmTypeDefinition weightType = new EdmTypeDefinition("NS", "Weight", EdmPrimitiveTypeKind.Double); EdmTypeDefinitionReference weightTypeRef = new EdmTypeDefinitionReference(weightType, false); entityType.AddStructuralProperty("Weight", weightTypeRef); EdmTypeDefinition heightType = new EdmTypeDefinition("NS", "Height", EdmPrimitiveTypeKind.Double); EdmComplexType complexType = new EdmComplexType("NS", "OpenAddress"); EdmComplexTypeReference complexTypeRef = new EdmComplexTypeReference(complexType, true); EdmTypeDefinition addressType = new EdmTypeDefinition("NS", "Address", EdmPrimitiveTypeKind.String); EdmTypeDefinitionReference addressTypeRef = new EdmTypeDefinitionReference(addressType, false); complexType.AddStructuralProperty("CountryRegion", addressTypeRef); entityType.AddStructuralProperty("Address", complexTypeRef); model.AddElement(weightType); model.AddElement(heightType); model.AddElement(addressType); model.AddElement(complexType); model.AddElement(entityType); EdmEntityContainer container = new EdmEntityContainer("EntityNs", "MyContainer"); EdmEntitySet entitySet = container.AddEntitySet("People", entityType); model.AddElement(container); const string payload = "{" + "\"@odata.context\":\"http://www.example.com/$metadata#EntityNs.MyContainer.People/$entity\"," + "\"@odata.id\":\"http://mytest\"," + "\"Id\":0," + "\"[email protected]\":\"#Edm.Double\"," + "\"Weight\":60.5," + "\"Address\":{\"[email protected]\":\"#Edm.String\",\"CountryRegion\":\"China\"}" + "}"; ODataEntry entry = null; this.ReadEntryPayload(model, payload, entitySet, entityType, reader => { entry = entry ?? reader.Item as ODataEntry; }); Assert.IsNotNull(entry, "entry shouldn't be null"); IList <ODataProperty> propertyList = entry.Properties.ToList(); propertyList[1].Name.Should().Be("Weight"); propertyList[1].Value.Should().Be(60.5); var address = propertyList[2].Value as ODataComplexValue; address.Properties.FirstOrDefault(s => string.Equals(s.Name, "CountryRegion", StringComparison.OrdinalIgnoreCase)).Value.Should().Be("China"); }
public void TestEdmTypeDefinitionConstructorWithPrimitiveType() { var intAlias = new EdmTypeDefinition("MyNS", "TestInt", EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Int32)); Assert.Equal("MyNS", intAlias.Namespace); Assert.Equal("TestInt", intAlias.Name); Assert.Equal(EdmTypeKind.TypeDefinition, intAlias.TypeKind); Assert.Equal(EdmSchemaElementKind.TypeDefinition, intAlias.SchemaElementKind); Assert.Equal(EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Int32), intAlias.UnderlyingType); var stringAlias = new EdmTypeDefinition("MyNamespace", "TestString", EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.String)); Assert.Equal("MyNamespace", stringAlias.Namespace); Assert.Equal("TestString", stringAlias.Name); Assert.Equal(EdmTypeKind.TypeDefinition, stringAlias.TypeKind); Assert.Equal(EdmSchemaElementKind.TypeDefinition, stringAlias.SchemaElementKind); Assert.Same(EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.String), stringAlias.UnderlyingType); var decimalAlias = new EdmTypeDefinition("TestNS", "TestDecimal", EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Decimal)); Assert.Equal("TestNS", decimalAlias.Namespace); Assert.Equal("TestDecimal", decimalAlias.Name); Assert.Equal(EdmTypeKind.TypeDefinition, decimalAlias.TypeKind); Assert.Equal(EdmSchemaElementKind.TypeDefinition, decimalAlias.SchemaElementKind); Assert.Same(EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Decimal), decimalAlias.UnderlyingType); var booleanAlias = new EdmTypeDefinition("TestNamespace", "TestBoolean", EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Boolean)); Assert.Equal("TestNamespace", booleanAlias.Namespace); Assert.Equal("TestBoolean", booleanAlias.Name); Assert.Equal(EdmTypeKind.TypeDefinition, booleanAlias.TypeKind); Assert.Equal(EdmSchemaElementKind.TypeDefinition, booleanAlias.SchemaElementKind); Assert.Same(EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Boolean), booleanAlias.UnderlyingType); }
public void TypeDefinitionUnitTest() { EdmTypeDefinition typeDef = new EdmTypeDefinition("NS", "Length", EdmPrimitiveTypeKind.Int32); EdmTypeDefinitionReference typeRef = new EdmTypeDefinitionReference(typeDef, true); typeRef.TypeDefinition().Should().Be(typeDef); }
public void TestEdmTypeDefinitionConstructorWithPrimitiveType() { var intAlias = new EdmTypeDefinition("MyNS", "TestInt", EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Int32)); intAlias.Namespace.Should().Be("MyNS"); intAlias.Name.Should().Be("TestInt"); intAlias.TypeKind.Should().Be(EdmTypeKind.TypeDefinition); intAlias.SchemaElementKind.Should().Be(EdmSchemaElementKind.TypeDefinition); intAlias.UnderlyingType.Should().Be(EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Int32)); var stringAlias = new EdmTypeDefinition("MyNamespace", "TestString", EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.String)); stringAlias.Namespace.Should().Be("MyNamespace"); stringAlias.Name.Should().Be("TestString"); stringAlias.TypeKind.Should().Be(EdmTypeKind.TypeDefinition); stringAlias.SchemaElementKind.Should().Be(EdmSchemaElementKind.TypeDefinition); stringAlias.UnderlyingType.Should().Be(EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.String)); var decimalAlias = new EdmTypeDefinition("TestNS", "TestDecimal", EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Decimal)); decimalAlias.Namespace.Should().Be("TestNS"); decimalAlias.Name.Should().Be("TestDecimal"); decimalAlias.TypeKind.Should().Be(EdmTypeKind.TypeDefinition); decimalAlias.SchemaElementKind.Should().Be(EdmSchemaElementKind.TypeDefinition); decimalAlias.UnderlyingType.Should().Be(EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Decimal)); var booleanAlias = new EdmTypeDefinition("TestNamespace", "TestBoolean", EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Boolean)); booleanAlias.Namespace.Should().Be("TestNamespace"); booleanAlias.Name.Should().Be("TestBoolean"); booleanAlias.TypeKind.Should().Be(EdmTypeKind.TypeDefinition); booleanAlias.SchemaElementKind.Should().Be(EdmSchemaElementKind.TypeDefinition); booleanAlias.UnderlyingType.Should().Be(EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Boolean)); }
public void ReadingTypeDefinitionPayloadWithIncompatibleEdmTypeShouldFail() { EdmModel model = new EdmModel(); EdmEntityType entityType = new EdmEntityType("NS", "Person"); entityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32); EdmTypeDefinition weightType = new EdmTypeDefinition("NS", "Weight", EdmPrimitiveTypeKind.Double); entityType.AddStructuralProperty("Height", EdmPrimitiveTypeKind.Int32); model.AddElement(weightType); model.AddElement(entityType); EdmEntityContainer container = new EdmEntityContainer("EntityNs", "MyContainer"); EdmEntitySet entitySet = container.AddEntitySet("People", entityType); model.AddElement(container); const string payload = "{" + "\"@odata.context\":\"http://www.example.com/$metadata#EntityNs.MyContainer.People/$entity\"," + "\"@odata.id\":\"http://mytest\"," + "\"Id\":0," + "\"[email protected]\":\"#NS.Weight\"," + "\"Height\":180" + "}"; Action read = () => this.ReadEntryPayload(model, payload, entitySet, entityType, reader => { }); read.ShouldThrow <ODataException>().WithMessage(Strings.ValidationUtils_IncompatibleType("NS.Weight", "Edm.Int32")); }
public void TestEdmTypeDefinitionConstructorWithPrimitiveTypeKind() { var intAlias = new EdmTypeDefinition("MyNS", "TestInt", EdmPrimitiveTypeKind.Int32); Assert.Equal("MyNS", intAlias.Namespace); Assert.Equal("TestInt", intAlias.Name); Assert.Equal(EdmTypeKind.TypeDefinition, intAlias.TypeKind); Assert.Equal(EdmSchemaElementKind.TypeDefinition, intAlias.SchemaElementKind); Assert.Equal(EdmPrimitiveTypeKind.Int32, intAlias.UnderlyingType.PrimitiveKind); var stringAlias = new EdmTypeDefinition("MyNamespace", "TestString", EdmPrimitiveTypeKind.String); Assert.Equal("MyNamespace", stringAlias.Namespace); Assert.Equal("TestString", stringAlias.Name); Assert.Equal(EdmTypeKind.TypeDefinition, stringAlias.TypeKind); Assert.Equal(EdmSchemaElementKind.TypeDefinition, stringAlias.SchemaElementKind); Assert.Equal(EdmPrimitiveTypeKind.String, stringAlias.UnderlyingType.PrimitiveKind); var decimalAlias = new EdmTypeDefinition("TestNS", "TestDecimal", EdmPrimitiveTypeKind.Decimal); Assert.Equal("TestNS", decimalAlias.Namespace); Assert.Equal("TestDecimal", decimalAlias.Name); Assert.Equal(EdmTypeKind.TypeDefinition, decimalAlias.TypeKind); Assert.Equal(EdmSchemaElementKind.TypeDefinition, decimalAlias.SchemaElementKind); Assert.Equal(EdmPrimitiveTypeKind.Decimal, decimalAlias.UnderlyingType.PrimitiveKind); var booleanAlias = new EdmTypeDefinition("TestNamespace", "TestBoolean", EdmPrimitiveTypeKind.Boolean); Assert.Equal("TestNamespace", booleanAlias.Namespace); Assert.Equal("TestBoolean", booleanAlias.Name); Assert.Equal(EdmTypeKind.TypeDefinition, booleanAlias.TypeKind); Assert.Equal(EdmSchemaElementKind.TypeDefinition, booleanAlias.SchemaElementKind); Assert.Equal(EdmPrimitiveTypeKind.Boolean, booleanAlias.UnderlyingType.PrimitiveKind); }
public void TestModelWithTypeDefinition() { var model = new EdmModel(); var addressType = new EdmTypeDefinition("MyNS", "Address", EdmPrimitiveTypeKind.String); model.AddElement(addressType); var weightType = new EdmTypeDefinition("MyNS", "Weight", EdmPrimitiveTypeKind.Decimal); model.AddElement(weightType); var personType = new EdmEntityType("MyNS", "Person"); var addressTypeReference = new EdmTypeDefinitionReference(addressType, false); personType.AddStructuralProperty("Address", addressTypeReference); addressTypeReference.Definition.Should().Be(addressType); addressTypeReference.IsNullable.Should().BeFalse(); var weightTypeReference = new EdmTypeDefinitionReference(weightType, true); personType.AddStructuralProperty("Weight", weightTypeReference); weightTypeReference.Definition.Should().Be(weightType); weightTypeReference.IsNullable.Should().BeTrue(); var personId = personType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32); personType.AddKeys(personId); }
public void Convert_CheckTypeDefinitionPrimitives(object odataValue, EdmPrimitiveTypeKind edmTypeKind, Type clrType, object expectedResult) { var edmTypeDefinition = new EdmTypeDefinition(clrType.Namespace, clrType.Name, edmTypeKind); var edmTypeDefReference = new EdmTypeDefinitionReference(edmTypeDefinition, false); var result = ODataModelBinderConverter.Convert(odataValue, edmTypeDefReference, clrType, "IsActive", null, null); Assert.NotNull(result); Assert.IsType(clrType, result); Assert.Equal(expectedResult, result); }
public void GetClrTypeNameShouldReturnUnderlyingTypeForTypeDefinition(EdmPrimitiveTypeKind primitiveTypeKind, bool isReferenceType) { var underlyingType = new EdmPrimitiveType(primitiveTypeKind); var typeDefinition = new EdmTypeDefinition("namespace", "Field", underlyingType); var typeDefinitionReference = new EdmTypeDefinitionReference(typeDefinition, false); var nullableTypeDefinitionReference = new EdmTypeDefinitionReference(typeDefinition, true); var underlyingTypeName = ODataT4CodeGenerator.Utils.GetClrTypeName(underlyingType, template); var nullableName = isReferenceType ? underlyingTypeName : $"global::System.Nullable<{underlyingTypeName}>"; ODataT4CodeGenerator.Utils.GetClrTypeName(typeDefinitionReference, false, template, context).Should().Be(underlyingTypeName); ODataT4CodeGenerator.Utils.GetClrTypeName(nullableTypeDefinitionReference, false, template, context).Should().Be(nullableName); }
public void BuildEdmExpression_Works_ForTypeDefinitionValue() { // Arrange EdmTypeDefinition definition = new EdmTypeDefinition("NS", "MyTypeDefinition", EdmPrimitiveTypeKind.Int32); // Act IEdmExpression exp = IEdmTermExtensions.BuildEdmExpression(definition, "42"); // Assert Assert.NotNull(exp); EdmIntegerConstant constant = Assert.IsType <EdmIntegerConstant>(exp); Assert.Equal(42, constant.Value); }
public void UInt16RoundtripJsonLightTest() { var uint16 = new EdmTypeDefinition("NS", "UInt16", EdmPrimitiveTypeKind.Double); var uint16Ref = new EdmTypeDefinitionReference(uint16, true); this.model.AddElement(uint16); this.model.SetPrimitiveValueConverter(uint16Ref, UInt16ValueConverter.Instance); var values = new[] { (UInt16)123, UInt16.MinValue, UInt16.MaxValue }; this.VerifyUIntValuesRoundtripWithTypeInformation(values, "NS.UInt16"); }
public void UInt64RoundtripJsonLightTest() { var uint64 = new EdmTypeDefinition("NS", "UInt64", EdmPrimitiveTypeKind.String); var uint64Ref = new EdmTypeDefinitionReference(uint64, true); this.model.AddElement(uint64); this.model.SetPrimitiveValueConverter(uint64Ref, UInt64ValueConverter.Instance); var values = new[] { (UInt64)456, UInt64.MinValue, UInt64.MaxValue }; this.VerifyUIntValuesRoundtripWithTypeInformation(values, "NS.UInt64"); }
public void WriteTypeDefinitionPayloadWithIncompatibleTypeShouldFail() { EdmModel model = new EdmModel(); EdmEntityType entityType = new EdmEntityType("NS", "Person"); entityType.AddKeys(entityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32)); EdmTypeDefinition weightType = new EdmTypeDefinition("NS", "Weight", EdmPrimitiveTypeKind.Double); EdmTypeDefinitionReference weightTypeRef = new EdmTypeDefinitionReference(weightType, false); entityType.AddStructuralProperty("Weight", weightTypeRef); model.AddElement(weightType); model.AddElement(entityType); EdmEntityContainer container = new EdmEntityContainer("EntityNs", "MyContainer"); EdmEntitySet entitySet = container.AddEntitySet("People", entityType); model.AddElement(container); ODataResource entry = new ODataResource() { TypeName = "NS.Person", Properties = new[] { new ODataProperty { Name = "Id", Value = 1 }, new ODataProperty { Name = "Weight", Value = "abc" }, } }; Action write = () => this.WriterEntry(model, entry, entitySet, entityType); write.Throws <ODataException>(Strings.ValidationUtils_IncompatiblePrimitiveItemType("Edm.String", "True", "NS.Weight", "False")); }
public async Task ReadTypeDefinitionParameterAsync() { var moneyTypeDefinition = new EdmTypeDefinition("NS", "Money", EdmPrimitiveTypeKind.Decimal); this.model.AddElement(moneyTypeDefinition); var setPriceAction = new EdmAction("NS", "SetPrice", null); setPriceAction.AddParameter("price", new EdmTypeDefinitionReference(moneyTypeDefinition, false)); this.model.AddElement(setPriceAction); var payload = "{\"price\":17.30}"; await SetupJsonLightParameterDeserializerAndRunTestAsync( payload, async (jsonLightParameterDeserializer) => { var parameterRead = await jsonLightParameterDeserializer.ReadNextParameterAsync(this.propertyAndAnnotationCollector); Assert.True(parameterRead); }, setPriceAction); }
public void CanWriteEdmTypeDefinitionWithEdmPrimitiveTypeButValidationFailed() { string expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?>" + "<edmx:Edmx Version=\"4.0\" xmlns:edmx=\"http://docs.oasis-open.org/odata/ns/edmx\">" + "<edmx:DataServices>" + "<Schema Namespace=\"NS\" xmlns=\"http://docs.oasis-open.org/odata/ns/edm\">" + "<TypeDefinition Name=\"MyType\" UnderlyingType=\"Edm.PrimitiveType\" />" + "</Schema>" + "</edmx:DataServices>" + "</edmx:Edmx>"; EdmModel model = new EdmModel(); EdmTypeDefinition definition = new EdmTypeDefinition("NS", "MyType", EdmPrimitiveTypeKind.PrimitiveType); model.AddElement(definition); IEnumerable <EdmError> errors; Assert.False(model.Validate(out errors)); Assert.Equal(1, errors.Count()); string csdlStr = GetCsdl(model, CsdlTarget.OData); Assert.Equal(expected, csdlStr); }
public void UnsignedIntAndTypeDefinitionRoundtripJsonLightIntegrationTest() { var model = new EdmModel(); var uint16 = new EdmTypeDefinition("MyNS", "UInt16", EdmPrimitiveTypeKind.Double); var uint16Ref = new EdmTypeDefinitionReference(uint16, false); model.AddElement(uint16); model.SetPrimitiveValueConverter(uint16Ref, UInt16ValueConverter.Instance); var uint64 = new EdmTypeDefinition("MyNS", "UInt64", EdmPrimitiveTypeKind.String); var uint64Ref = new EdmTypeDefinitionReference(uint64, false); model.AddElement(uint64); model.SetPrimitiveValueConverter(uint64Ref, UInt64ValueConverter.Instance); var guidType = new EdmTypeDefinition("MyNS", "Guid", EdmPrimitiveTypeKind.Int64); var guidRef = new EdmTypeDefinitionReference(guidType, true); model.AddElement(guidType); var personType = new EdmEntityType("MyNS", "Person"); personType.AddKeys(personType.AddStructuralProperty("ID", uint64Ref)); personType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String); personType.AddStructuralProperty("FavoriteNumber", uint16Ref); personType.AddStructuralProperty("Age", model.GetUInt32("MyNS", true)); personType.AddStructuralProperty("Guid", guidRef); personType.AddStructuralProperty("Weight", EdmPrimitiveTypeKind.Double); personType.AddStructuralProperty("Money", EdmPrimitiveTypeKind.Decimal); model.AddElement(personType); var container = new EdmEntityContainer("MyNS", "Container"); var peopleSet = container.AddEntitySet("People", personType); model.AddElement(container); var stream = new MemoryStream(); IODataResponseMessage message = new InMemoryMessage { Stream = stream }; message.StatusCode = 200; var writerSettings = new ODataMessageWriterSettings(); writerSettings.SetServiceDocumentUri(new Uri("http://host/service")); var messageWriter = new ODataMessageWriter(message, writerSettings, model); var entryWriter = messageWriter.CreateODataResourceWriter(peopleSet); var entry = new ODataResource { TypeName = "MyNS.Person", Properties = new[] { new ODataProperty { Name = "ID", Value = UInt64.MaxValue }, new ODataProperty { Name = "Name", Value = "Foo" }, new ODataProperty { Name = "FavoriteNumber", Value = (UInt16)250 }, new ODataProperty { Name = "Age", Value = (UInt32)123 }, new ODataProperty { Name = "Guid", Value = Int64.MinValue }, new ODataProperty { Name = "Weight", Value = 123.45 }, new ODataProperty { Name = "Money", Value = Decimal.MaxValue } } }; entryWriter.WriteStart(entry); entryWriter.WriteEnd(); entryWriter.Flush(); stream.Position = 0; StreamReader reader = new StreamReader(stream); string payload = reader.ReadToEnd(); Assert.Equal("{\"@odata.context\":\"http://host/service/$metadata#People/$entity\",\"ID\":\"18446744073709551615\",\"Name\":\"Foo\",\"FavoriteNumber\":250.0,\"Age\":123,\"Guid\":-9223372036854775808,\"Weight\":123.45,\"Money\":79228162514264337593543950335}", payload); #if NETCOREAPP1_1 stream = new MemoryStream(Encoding.GetEncoding(0).GetBytes(payload)); #else stream = new MemoryStream(Encoding.Default.GetBytes(payload)); #endif message = new InMemoryMessage { Stream = stream }; message.StatusCode = 200; var readerSettings = new ODataMessageReaderSettings(); var messageReader = new ODataMessageReader(message, readerSettings, model); var entryReader = messageReader.CreateODataResourceReader(peopleSet, personType); Assert.True(entryReader.Read()); var entryReaded = entryReader.Item as ODataResource; var propertiesReaded = entryReaded.Properties.ToList(); var propertiesGiven = entry.Properties.ToList(); Assert.Equal(propertiesReaded.Count, propertiesGiven.Count); for (int i = 0; i < propertiesReaded.Count; ++i) { Assert.Equal(propertiesReaded[i].Name, propertiesGiven[i].Name); Assert.Equal(propertiesReaded[i].Value.GetType(), propertiesGiven[i].Value.GetType()); Assert.Equal(propertiesReaded[i].Value, propertiesGiven[i].Value); } }
public static IEdmModel CreateModel(string ns) { EdmModel model = new EdmModel(); var defaultContainer = new EdmEntityContainer(ns, "DefaultContainer"); model.AddElement(defaultContainer); var nameType = new EdmTypeDefinition(ns, "Name", EdmPrimitiveTypeKind.String); model.AddElement(nameType); var addressType = new EdmComplexType(ns, "Address"); addressType.AddProperty(new EdmStructuralProperty(addressType, "Road", new EdmTypeDefinitionReference(nameType, false))); addressType.AddProperty(new EdmStructuralProperty(addressType, "City", EdmCoreModel.Instance.GetString(false))); model.AddElement(addressType); var personType = new EdmEntityType(ns, "Person"); var personIdProperty = new EdmStructuralProperty(personType, "PersonId", EdmCoreModel.Instance.GetInt32(false)); personType.AddProperty(personIdProperty); personType.AddKeys(new IEdmStructuralProperty[] { personIdProperty }); personType.AddProperty(new EdmStructuralProperty(personType, "FirstName", new EdmTypeDefinitionReference(nameType, false))); personType.AddProperty(new EdmStructuralProperty(personType, "LastName", new EdmTypeDefinitionReference(nameType, false))); personType.AddProperty(new EdmStructuralProperty(personType, "Address", new EdmComplexTypeReference(addressType, true))); personType.AddProperty(new EdmStructuralProperty(personType, "Descriptions", new EdmCollectionTypeReference(new EdmCollectionType(new EdmTypeDefinitionReference(nameType, false))))); model.AddElement(personType); var peopleSet = new EdmEntitySet(defaultContainer, "People", personType); defaultContainer.AddElement(peopleSet); var numberComboType = new EdmComplexType(ns, "NumberCombo"); numberComboType.AddProperty(new EdmStructuralProperty(numberComboType, "Small", model.GetUInt16(ns, false))); numberComboType.AddProperty(new EdmStructuralProperty(numberComboType, "Middle", model.GetUInt32(ns, false))); numberComboType.AddProperty(new EdmStructuralProperty(numberComboType, "Large", model.GetUInt64(ns, false))); model.AddElement(numberComboType); var productType = new EdmEntityType(ns, "Product"); var productIdProperty = new EdmStructuralProperty(productType, "ProductId", model.GetUInt16(ns, false)); productType.AddProperty(productIdProperty); productType.AddKeys(new IEdmStructuralProperty[] { productIdProperty }); productType.AddProperty(new EdmStructuralProperty(productType, "Quantity", model.GetUInt32(ns, false))); productType.AddProperty(new EdmStructuralProperty(productType, "NullableUInt32", model.GetUInt32(ns, true))); productType.AddProperty(new EdmStructuralProperty(productType, "LifeTimeInSeconds", model.GetUInt64(ns, false))); productType.AddProperty(new EdmStructuralProperty(productType, "TheCombo", new EdmComplexTypeReference(numberComboType, true))); productType.AddProperty(new EdmStructuralProperty(productType, "LargeNumbers", new EdmCollectionTypeReference(new EdmCollectionType(model.GetUInt64(ns, false))))); model.AddElement(productType); var productsSet = new EdmEntitySet(defaultContainer, "Products", productType); defaultContainer.AddElement(productsSet); //Bound Function: bound to entity, return defined type var getFullNameFunction = new EdmFunction(ns, "GetFullName", new EdmTypeDefinitionReference(nameType, false), true, null, false); getFullNameFunction.AddParameter("person", new EdmEntityTypeReference(personType, false)); getFullNameFunction.AddParameter("nickname", new EdmTypeDefinitionReference(nameType, false)); model.AddElement(getFullNameFunction); //Bound Action: bound to entity, return UInt64 var extendLifeTimeAction = new EdmAction(ns, "ExtendLifeTime", model.GetUInt64(ns, false), true, null); extendLifeTimeAction.AddParameter("product", new EdmEntityTypeReference(productType, false)); extendLifeTimeAction.AddParameter("seconds", model.GetUInt32(ns, false)); model.AddElement(extendLifeTimeAction); //UnBound Action: ResetDataSource var resetDataSourceAction = new EdmAction(ns, "ResetDataSource", null, false, null); model.AddElement(resetDataSourceAction); defaultContainer.AddActionImport(resetDataSourceAction); IEnumerable <EdmError> errors = null; model.Validate(out errors); return(model); }
public void ReadingTypeDefinitionPayloadWithEdmTypeAnnotationJsonLight() { EdmModel model = new EdmModel(); EdmEntityType entityType = new EdmEntityType("NS", "Person"); entityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32); EdmTypeDefinition weightType = new EdmTypeDefinition("NS", "Weight", EdmPrimitiveTypeKind.Double); EdmTypeDefinitionReference weightTypeRef = new EdmTypeDefinitionReference(weightType, false); entityType.AddStructuralProperty("Weight", weightTypeRef); EdmTypeDefinition heightType = new EdmTypeDefinition("NS", "Height", EdmPrimitiveTypeKind.Double); EdmComplexType complexType = new EdmComplexType("NS", "OpenAddress"); EdmComplexTypeReference complexTypeRef = new EdmComplexTypeReference(complexType, true); EdmTypeDefinition addressType = new EdmTypeDefinition("NS", "Address", EdmPrimitiveTypeKind.String); EdmTypeDefinitionReference addressTypeRef = new EdmTypeDefinitionReference(addressType, false); complexType.AddStructuralProperty("CountryRegion", addressTypeRef); entityType.AddStructuralProperty("Address", complexTypeRef); model.AddElement(weightType); model.AddElement(heightType); model.AddElement(addressType); model.AddElement(complexType); model.AddElement(entityType); EdmEntityContainer container = new EdmEntityContainer("EntityNs", "MyContainer"); EdmEntitySet entitySet = container.AddEntitySet("People", entityType); model.AddElement(container); const string payload = "{" + "\"@odata.context\":\"http://www.example.com/$metadata#EntityNs.MyContainer.People/$entity\"," + "\"@odata.id\":\"http://mytest\"," + "\"Id\":0," + "\"[email protected]\":\"#Edm.Double\"," + "\"Weight\":60.5," + "\"Address\":{\"[email protected]\":\"#Edm.String\",\"CountryRegion\":\"China\"}" + "}"; List <ODataResource> entries = new List <ODataResource>(); ODataNestedResourceInfo navigationLink = null; this.ReadEntryPayload(model, payload, entitySet, entityType, reader => { switch (reader.State) { case ODataReaderState.ResourceStart: entries.Add(reader.Item as ODataResource); break; case ODataReaderState.NestedResourceInfoStart: navigationLink = (ODataNestedResourceInfo)reader.Item; break; default: break; } }); Assert.Equal(2, entries.Count); IList <ODataProperty> propertyList = entries[0].Properties.ToList(); propertyList[1].Name.Should().Be("Weight"); propertyList[1].Value.Should().Be(60.5); navigationLink.Name.Should().Be("Address"); var address = entries[1]; address.Properties.FirstOrDefault(s => string.Equals(s.Name, "CountryRegion", StringComparison.OrdinalIgnoreCase)).Value.Should().Be("China"); }
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); edmModel.AddElement(edmEntityType); // Initialize open ComplexType: OpenAddress. this.openAddressType = new EdmComplexType("TestNamespace", "OpenAddress", baseType: null, isAbstract: false, isOpen: true); this.openAddressType.AddStructuralProperty("CountryRegion", EdmPrimitiveTypeKind.String); 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.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" }; }
public void WriteTypeDefinitionPayloadShouldWork() { EdmModel model = new EdmModel(); EdmEntityType entityType = new EdmEntityType("NS", "Person"); entityType.AddKeys(entityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32)); EdmTypeDefinition weightType = new EdmTypeDefinition("NS", "Weight", EdmPrimitiveTypeKind.Double); EdmTypeDefinitionReference weightTypeRef = new EdmTypeDefinitionReference(weightType, false); entityType.AddStructuralProperty("Weight", weightTypeRef); EdmComplexType complexType = new EdmComplexType("NS", "Address"); EdmComplexTypeReference complexTypeRef = new EdmComplexTypeReference(complexType, true); EdmTypeDefinition addressType = new EdmTypeDefinition("NS", "Address", EdmPrimitiveTypeKind.String); EdmTypeDefinitionReference addressTypeRef = new EdmTypeDefinitionReference(addressType, false); complexType.AddStructuralProperty("CountryRegion", addressTypeRef); entityType.AddStructuralProperty("Address", complexTypeRef); model.AddElement(weightType); model.AddElement(addressType); model.AddElement(complexType); model.AddElement(entityType); EdmEntityContainer container = new EdmEntityContainer("EntityNs", "MyContainer"); EdmEntitySet entitySet = container.AddEntitySet("People", entityType); model.AddElement(container); ODataEntry entry = new ODataEntry() { TypeName = "NS.Person", Properties = new[] { new ODataProperty { Name = "Id", Value = 1 }, new ODataProperty { Name = "Weight", Value = 60.5 }, new ODataProperty { Name = "Address", Value = new ODataComplexValue { Properties = new[] { new ODataProperty { Name = "CountryRegion", Value = "China" } } } } } }; string outputPayload = this.WriterEntry(model, entry, entitySet, entityType); const string expectedMinimalPayload = "{" + "\"@odata.context\":\"http://www.example.com/$metadata#People/$entity\"," + "\"Id\":1," + "\"Weight\":60.5," + "\"Address\":{\"CountryRegion\":\"China\"}" + "}"; outputPayload.Should().Be(expectedMinimalPayload); outputPayload = this.WriterEntry(model, entry, entitySet, entityType, true); const string expectedFullPayload = "{" + "\"@odata.context\":\"http://www.example.com/$metadata#People/$entity\"," + "\"@odata.type\":\"#NS.Person\"," + "\"@odata.id\":\"People(1)\"," + "\"@odata.editLink\":\"People(1)\"," + "\"Id\":1," + "\"Weight\":60.5," + "\"Address\":{\"CountryRegion\":\"China\"}" + "}"; outputPayload.Should().Be(expectedFullPayload); }
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" }; }
public void WriteTypeDefinitionAsKeyPayloadShouldWork() { EdmModel model = new EdmModel(); EdmTypeDefinition uint32Type = new EdmTypeDefinition("NS", "UInt32", EdmPrimitiveTypeKind.String); EdmTypeDefinitionReference uint32Reference = new EdmTypeDefinitionReference(uint32Type, false); model.AddElement(uint32Type); EdmEntityType entityType = new EdmEntityType("NS", "Person"); entityType.AddKeys(entityType.AddStructuralProperty("Id1", uint32Reference), entityType.AddStructuralProperty("Id2", EdmPrimitiveTypeKind.Int32)); model.SetPrimitiveValueConverter(uint32Reference, new MyUInt32Converter()); EdmTypeDefinition weightType = new EdmTypeDefinition("NS", "Weight", EdmPrimitiveTypeKind.Double); EdmTypeDefinitionReference weightTypeRef = new EdmTypeDefinitionReference(weightType, false); entityType.AddStructuralProperty("Weight", weightTypeRef); EdmComplexType complexType = new EdmComplexType("NS", "Address"); EdmComplexTypeReference complexTypeRef = new EdmComplexTypeReference(complexType, true); EdmTypeDefinition addressType = new EdmTypeDefinition("NS", "Address", EdmPrimitiveTypeKind.String); EdmTypeDefinitionReference addressTypeRef = new EdmTypeDefinitionReference(addressType, false); complexType.AddStructuralProperty("CountryRegion", addressTypeRef); entityType.AddStructuralProperty("Address", complexTypeRef); model.AddElement(weightType); model.AddElement(addressType); model.AddElement(complexType); model.AddElement(entityType); EdmEntityContainer container = new EdmEntityContainer("EntityNs", "MyContainer"); EdmEntitySet entitySet = container.AddEntitySet("People", entityType); model.AddElement(container); ODataResource entry = new ODataResource() { TypeName = "NS.Person", Properties = new[] { new ODataProperty { Name = "Id1", Value = (UInt32)1 }, new ODataProperty { Name = "Id2", Value = 2 }, new ODataProperty { Name = "Weight", Value = 60.5 }, } }; ODataNestedResourceInfo address = new ODataNestedResourceInfo() { Name = "Address", IsCollection = false }; ODataResource addressResource = new ODataResource { Properties = new[] { new ODataProperty { Name = "CountryRegion", Value = "China" } } }; string outputPayload = this.WriterEntry(model, entry, entitySet, entityType, false, (writer) => { writer.WriteStart(entry); writer.WriteStart(address); writer.WriteStart(addressResource); writer.WriteEnd(); writer.WriteEnd(); writer.WriteEnd(); }); const string expectedMinimalPayload = "{" + "\"@odata.context\":\"http://www.example.com/$metadata#People/$entity\"," + "\"Id1\":\"1\"," + "\"Id2\":2," + "\"Weight\":60.5," + "\"Address\":{\"CountryRegion\":\"China\"}" + "}"; Assert.Equal(expectedMinimalPayload, outputPayload); outputPayload = this.WriterEntry(model, entry, entitySet, entityType, true, (writer) => { writer.WriteStart(entry); writer.WriteStart(address); writer.WriteStart(addressResource); writer.WriteEnd(); writer.WriteEnd(); writer.WriteEnd(); }); const string expectedFullPayload = "{" + "\"@odata.context\":\"http://www.example.com/$metadata#People/$entity\"," + "\"@odata.type\":\"#NS.Person\"," + "\"@odata.id\":\"People(Id1='1',Id2=2)\"," + "\"@odata.editLink\":\"People(Id1='1',Id2=2)\"," + "\"Id1\":\"1\"," + "\"Id2\":2," + "\"Weight\":60.5," + "\"Address\":{\"CountryRegion\":\"China\"}" + "}"; Assert.Equal(expectedFullPayload, outputPayload); }