public void ClrTypeMappingPrimitiveTypeOverflowTest() { var edmModel = this.GetParserResult(ClrTypeMappingTestModelBuilder.PrimitiveTypeOverflowTest()); var annotations = edmModel.FindVocabularyAnnotations(edmModel.FindType("NS1.Person")); Func <string, IEdmIntegerConstantExpression> GetIntegerExpression = (termName) => { var valueAnnotation = annotations.Single(n => n.Term.Name == termName) as IEdmValueAnnotation; return((IEdmIntegerConstantExpression)valueAnnotation.Value); }; Func <string, IEdmFloatingConstantExpression> GetFloatExpression = (termName) => { var valueAnnotation = annotations.Single(n => n.Term.Name == termName) as IEdmValueAnnotation; return((IEdmFloatingConstantExpression)valueAnnotation.Value); }; Func <string, IEdmValue> GetEdmValue = (termName) => { var valueAnnotation = annotations.Single(n => n.Term.Name == termName) as IEdmValueAnnotation; var edmToClrEvaluator = new EdmToClrEvaluator(null); return(edmToClrEvaluator.Evaluate(valueAnnotation.Value)); }; var edmToClrConverter = new EdmToClrConverter(); Assert.AreEqual(edmToClrConverter.AsClrValue(GetFloatExpression("SingleValue"), typeof(Single)), float.PositiveInfinity, "It should return Infinit for Single when the value is greater than Single.MaxValue."); Assert.AreEqual(edmToClrConverter.AsClrValue(GetFloatExpression("NegativeSingleValue"), typeof(Single)), float.NegativeInfinity, "It should return Negative Infinit for Single when the value is less than Single.MinValue."); this.VerifyThrowsException(typeof(OverflowException), () => edmToClrConverter.AsClrValue(GetEdmValue("ByteValue"), typeof(byte))); this.VerifyThrowsException(typeof(OverflowException), () => edmToClrConverter.AsClrValue <byte>(GetEdmValue("ByteValue"))); this.VerifyThrowsException(typeof(OverflowException), () => new EdmToClrEvaluator(null).EvaluateToClrValue <byte>(GetIntegerExpression("ByteValue"))); this.VerifyThrowsException(typeof(OverflowException), () => edmToClrConverter.AsClrValue(GetEdmValue("SByteValue"), typeof(sbyte))); this.VerifyThrowsException(typeof(OverflowException), () => edmToClrConverter.AsClrValue <sbyte>(GetEdmValue("SByteValue"))); this.VerifyThrowsException(typeof(OverflowException), () => new EdmToClrEvaluator(null).EvaluateToClrValue <sbyte>(GetIntegerExpression("SByteValue"))); }
public void EvaluateUnboundTerms() { const string applicationCsdl = @"<Schema Namespace=""bar"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <Annotations Target=""bar.Person""> <Annotation Term=""bar.RandomTerm"" Path=""Extra"" /> </Annotations> <EntityType Name=""Person""> <Key> <PropertyRef Name=""Name"" /> </Key> <Property Name=""Name"" Type=""String"" Nullable=""false"" /> <Property Name=""Decoration"" Type=""bar.Decoration""> <Annotation Term=""bar.DecorationTerm"" Qualifier=""Goodness"" String=""Excellent"" /> </Property> </EntityType> <ComplexType Name=""Decoration""> <Property Name=""One"" Type=""Int32"" /> <Property Name=""Two"" Type=""Int32"" /> </ComplexType> </Schema>"; IEdmModel applicationModel = this.Parse(applicationCsdl); IEdmEntityType person = (IEdmEntityType)applicationModel.FindType("bar.Person"); IEdmProperty personDecoration = person.FindProperty("Decoration"); EdmToClrEvaluator evaluator = new EdmToClrEvaluator(null); List <IEdmPropertyValue> decorationPropertyValues = new List <IEdmPropertyValue>(); decorationPropertyValues.Add(new EdmPropertyValue("One", new EdmIntegerConstant(1))); decorationPropertyValues.Add(new EdmPropertyValue("Two", new EdmIntegerConstant(2))); List <IEdmPropertyValue> propertyValues = new List <IEdmPropertyValue>(); propertyValues.Add(new EdmPropertyValue("Name", new EdmStringConstant("Goober"))); propertyValues.Add(new EdmPropertyValue("Decoration", new EdmStructuredValue(null, decorationPropertyValues))); propertyValues.Add(new EdmPropertyValue("Extra", new EdmStringConstant("Extra value!"))); IEdmStructuredValue context = new EdmStructuredValue(new EdmEntityTypeReference(person, false), propertyValues); string random = applicationModel.GetTermValue <string>(context, "bar.RandomTerm", evaluator); Assert.AreEqual("Extra value!", random, "Annotation evaluated value."); IEdmValue randomValue = applicationModel.GetTermValue(context, "bar.RandomTerm", evaluator); Assert.AreEqual("Extra value!", ((IEdmStringValue)randomValue).Value, "Annotation evaluated value."); string goodness = applicationModel.GetTermValue <string>(personDecoration, "bar.DecorationTerm", "Goodness", evaluator); Assert.AreEqual("Excellent", goodness, "Annotation evaluated value."); IEdmValue goodnessValue = applicationModel.GetTermValue(personDecoration, "bar.DecorationTerm", "Goodness", evaluator); Assert.AreEqual("Excellent", ((IEdmStringValue)goodnessValue).Value, "Annotation evaluated value."); IEdmVocabularyAnnotation randomTermAnnotation = applicationModel.FindVocabularyAnnotations <IEdmVocabularyAnnotation>(person, "bar.RandomTerm").Single(); random = evaluator.EvaluateToClrValue <string>(randomTermAnnotation.Value, context); Assert.AreEqual("Extra value!", random, "Annotation evaluated value."); }
public void ClrTypeMappingPrimitiveTypeToObject() { var edmModel = this.GetParserResult(ClrTypeMappingTestModelBuilder.PrimitiveTypeBasicTest()); var annotation = edmModel.FindVocabularyAnnotations(edmModel.FindType("NS1.Person")).Where(n => n.Term == edmModel.FindValueTerm("NS1.Title1")).Single(); var edmValue = new EdmToClrEvaluator(null).Evaluate(((IEdmValueAnnotation)annotation).Value); this.VerifyThrowsException(typeof(InvalidCastException), () => new EdmToClrConverter().AsClrValue <Coordination>(edmValue)); }
public void Term_Constant_OnProperty() { this.SetupModelsAndValues(); const string applicationCsdl = @"<Schema Namespace=""Annotations"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <Annotations Target=""foo.Person/CoolnessIndex""> <Annotation Term=""bar.Int32Value"" Qualifier=""HotIndex"" Int=""-1"" /> <Annotation Term=""bar.StringValue"" String=""Goofy"" /> </Annotations> </Schema>"; IEdmModel applicationModel = this.Parse(applicationCsdl, this.baseModel, this.vocabularyDefinitionModel); EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.operationsLookup); EdmToClrEvaluator clrEvaluator = new EdmToClrEvaluator(this.operationsLookup); IEdmEntityType person = this.baseModel.FindEntityType("foo.Person"); IEdmProperty property = person.FindProperty("CoolnessIndex"); IEdmPropertyValue contextPropertyValue = ((IEdmStructuredValue)this.personValue).FindPropertyValue("CoolnessIndex"); IEdmTerm termInt32Value = this.vocabularyDefinitionModel.FindTerm("bar.Int32Value"); IEdmTerm termStringValue = this.vocabularyDefinitionModel.FindTerm("bar.StringValue"); IEdmVocabularyAnnotation annotation = property.VocabularyAnnotations(applicationModel).SingleOrDefault(a => a.Term == termInt32Value); IEdmExpression expression = annotation.Value; IEdmValue annotationHotIndex = expressionEvaluator.Evaluate(expression); Assert.AreEqual(-1, ((IEdmIntegerValue)annotationHotIndex).Value, "Term annotation value"); annotationHotIndex = applicationModel.GetTermValue(property, termInt32Value, "HotIndex", expressionEvaluator); Assert.AreEqual(-1, ((IEdmIntegerValue)annotationHotIndex).Value, "Term annotation value"); annotationHotIndex = applicationModel.GetTermValue(property, "bar.Int32Value", "HotIndex", expressionEvaluator); Assert.AreEqual(-1, ((IEdmIntegerValue)annotationHotIndex).Value, "Term annotation value"); int hotIndex = applicationModel.GetTermValue <int>(property, termInt32Value, "HotIndex", clrEvaluator); Assert.AreEqual(-1, hotIndex, "Term annotation value"); hotIndex = applicationModel.GetTermValue <int>(property, "bar.Int32Value", "HotIndex", clrEvaluator); Assert.AreEqual(-1, hotIndex, "Term annotation value"); IEdmValue annotationString = applicationModel.GetTermValue(property, termStringValue, expressionEvaluator); Assert.AreEqual("Goofy", ((IEdmStringValue)annotationString).Value, "Term annotation value"); annotationString = applicationModel.GetTermValue(property, "bar.StringValue", expressionEvaluator); Assert.AreEqual("Goofy", ((IEdmStringValue)annotationString).Value, "Term annotation value"); string stringValue = applicationModel.GetTermValue <string>(property, termStringValue, clrEvaluator); Assert.AreEqual("Goofy", stringValue, "Term annotation value"); stringValue = applicationModel.GetTermValue <string>(property, "bar.StringValue", clrEvaluator); Assert.AreEqual("Goofy", stringValue, "Term annotation value"); }
public void ClrTypeMappingValueAnnotationTryPopulateObjectInstance() { this.InitializeOperationDefinitions(); EdmToClrEvaluator ev = new EdmToClrEvaluator(this.operationDefinitions); var edmModel = this.GetParserResult(ClrTypeMappingTestModelBuilder.ValueAnnotationClassTypeBasicTest(), this.operationDeclarationModel); var value = ev.Evaluate(this.GetValueAnnotations(edmModel, edmModel.FindType("NS1.Person"), "TVDisplay").Single().Value); var isObjectPopulated = false; var isObjectInitialized = true; object createdObjectInstance = new Display1() { X = 0, Y = 1 }; TryCreateObjectInstance tryCreateObjectInstance = (IEdmStructuredValue edmValue, Type clrType, EdmToClrConverter converter, out object objectInstance, out bool objectInstanceInitialized) => { objectInstance = createdObjectInstance; objectInstanceInitialized = isObjectPopulated; return(isObjectInitialized); }; ev.EdmToClrConverter = new EdmToClrConverter(tryCreateObjectInstance); this.VerifyThrowsException(typeof(InvalidCastException), () => ev.EdmToClrConverter.AsClrValue(value, typeof(Display2))); isObjectPopulated = false; isObjectInitialized = false; createdObjectInstance = new Display1() { X = 0, Y = 1 }; ev.EdmToClrConverter = new EdmToClrConverter(tryCreateObjectInstance); Assert.IsTrue(CompareObjects((Display2)ev.EdmToClrConverter.AsClrValue(value, typeof(Display2)), new Display2() { X = 10, Y = 20, Origin = new Display1() { X = 10, Y = 20 } }), "The returned object has incorrect values."); isObjectPopulated = true; isObjectInitialized = true; createdObjectInstance = new Display1() { X = 0, Y = 1 }; ev.EdmToClrConverter = new EdmToClrConverter(tryCreateObjectInstance); this.VerifyThrowsException(typeof(InvalidCastException), () => ev.EdmToClrConverter.AsClrValue(value, typeof(Display2))); }
private T ConvertToClrObject <T>(IEdmValueAnnotation valueAnnotation) { var edmClrEvaluator = new EdmToClrEvaluator(this.operationDefinitions); var edmClrConverter = new EdmToClrConverter(); var edmValue = edmClrEvaluator.Evaluate(valueAnnotation.Value); var object1 = edmClrEvaluator.EvaluateToClrValue <T>(valueAnnotation.Value); var object2 = (T)edmClrConverter.AsClrValue(edmValue, typeof(T)); var object3 = edmClrEvaluator.EdmToClrConverter.AsClrValue <T>(edmValue); Assert.IsTrue(CompareObjects(object1, object2), "The results of EvaluateToClrValue and AsClrValue do not match."); Assert.IsTrue(CompareObjects(object2, object3), "The result of the generic version of AsClrValue does not match that of the non-generic version."); return(object1); }
/// <summary> /// Create an <see cref="EdmToClrEvaluator"/> instance used to evaluate an edm value. /// </summary> /// <param name="context">The data service context.</param> /// <returns>The <see cref="EdmToClrEvaluator"/> instance.</returns> private static EdmToClrEvaluator CreateEdmToClrEvaluator(DataServiceContext context) { AnnotationMaterializeHelper helper = new AnnotationMaterializeHelper(context); EdmToClrEvaluator evaluator = new EdmToClrEvaluator( null, null, helper.GetAnnnotationExpressionForType, helper.GetAnnnotationExpressionForProperty, context.Model); evaluator.EdmToClrConverter = new EdmToClrConverter( helper.TryCreateObjectInstance, helper.TryGetClientPropertyInfo, helper.TryGetClrTypeName); return(evaluator); }
public void ClrTypeMappingValueAnnotationEnumTest() { var edmModel = this.GetParserResult(ClrTypeMappingTestModelBuilder.ValueAnnotationEnumTest()); var annotations = edmModel.FindVocabularyAnnotations(edmModel.FindType("NS1.Person")); Func <string, IEdmIntegerConstantExpression> GetIntegerExpression = (termName) => { var valueAnnotation = annotations.Single(n => n.Term.Name == termName) as IEdmValueAnnotation; return((IEdmIntegerConstantExpression)valueAnnotation.Value); }; Func <string, IEdmValue> GetEdmValue = (termName) => { var valueAnnotation = annotations.Single(n => n.Term.Name == termName) as IEdmValueAnnotation; var edmToClrEvaluator = new EdmToClrEvaluator(null); return(edmToClrEvaluator.Evaluate(valueAnnotation.Value)); }; var edmToClrConverter = new EdmToClrConverter(); Assert.AreEqual(edmToClrConverter.AsClrValue(GetIntegerExpression("PersonValueAnnotation3"), typeof(EnumInt)), EnumInt.Member1, "It should return Infinit for Single when the value is greater than Single.MaxValue."); Assert.AreEqual(new EdmToClrEvaluator(null).EvaluateToClrValue <EnumInt>(GetIntegerExpression("PersonValueAnnotation3")), EnumInt.Member1, "It should return Infinit for Single when the value is greater than Single.MaxValue."); Assert.AreEqual(new EdmToClrEvaluator(null).EvaluateToClrValue <EnumInt>(GetIntegerExpression("PersonValueAnnotation4")), (EnumInt)(-2), "It should return Infinit for Single when the value is greater than Single.MaxValue."); #if !SILVERLIGHT this.ValidateClrObjectConverter(this.GetValueAnnotations(edmModel, edmModel.FindType("NS1.Person"), "PersonValueAnnotation1").Single(), new ClassWithEnum() { EnumInt = EnumInt.Member1, EnumByte = (EnumByte)10, EnumULong = EnumULong.Member2 }); #endif this.VerifyThrowsException(typeof(InvalidCastException), () => new EdmToClrEvaluator(null).EvaluateToClrValue <EnumInt>(GetIntegerExpression("PersonValueAnnotation2"))); #if !SILVERLIGHT this.ValidateClrObjectConverter(this.GetValueAnnotations(edmModel, edmModel.FindType("NS1.Person"), "PersonValueAnnotation8").Single(), new ClassWithEnum() { EnumInt = (EnumInt)10, }); #endif }
/// <summary> /// Evaluate IEdmVocabularyAnnotation to an CLR object /// </summary> /// <typeparam name="TResult">The CLR type of the annotation to be returned.</typeparam> /// <param name="context">The data service context.</param> /// <param name="edmValueAnnotation">IEdmVocabularyAnnotation to be evaluated.</param> /// <param name="clientEdmValue">Value to use as context in evaluating the expression.</param> /// <param name="annotationValue">Value of the term evaluated.</param> /// <returns>True if the annotation value can be evaluated, else false.</returns> private static bool TryEvaluateMetadataAnnotation <TResult>(DataServiceContext context, IEdmVocabularyAnnotation edmValueAnnotation, ClientEdmStructuredValue clientEdmValue, out TResult annotationValue) { if (edmValueAnnotation == null) { annotationValue = default(TResult); return(false); } EdmToClrEvaluator evaluator = CreateEdmToClrEvaluator(context); try { annotationValue = evaluator.EvaluateToClrValue <TResult>(edmValueAnnotation.Value, clientEdmValue); } catch (InvalidOperationException) { // When expression contains Path. if the clientEdmValue is null, or the related property is not valid property of the clientEdmValue. // TheEvaluateToClrValue might throw InvalidOperationException; annotationValue = default(TResult); return(false); } return(true); }
public void EvaluateExpressionsOnBaseType() { const string annotatingModelCsdl = @"<Schema Namespace=""foo"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <EntityType Name=""Thing""> <Key> <PropertyRef Name=""ID"" /> </Key> <Property Name=""ID"" Type=""Int32"" /> <Property Name=""Value"" Type=""String"" /> </EntityType> <EntityType Name=""AnotherThing"" BaseType=""Edm.TypeTerm""> <Property Name=""AnotherValue"" Type=""String"" /> </EntityType> <Annotations Target=""foo.Thing""> <Annotation Term=""foo.DistantAge"" Qualifier=""Short"" Int=""27"" /> <Annotation Term=""foo.DistantAge"" Qualifier=""VeryLong"" Int=""127"" /> </Annotations> <EntityType Name=""MiddleThing"" BaseType=""foo.Thing""> <Property Name=""AddSome"" Type=""String"" /> </EntityType> <EntityType Name=""DerivedThing"" BaseType=""foo.MiddleThing""> <Property Name=""AddSomeMore"" Type=""String"" /> </EntityType> </Schema>"; IEnumerable<EdmError> errors; IEdmModel annotatingModel; bool parsed = CsdlReader.TryParse(new XmlReader[] { XmlReader.Create(new StringReader(annotatingModelCsdl)) }, new IEdmModel[] { this.baseModel }, out annotatingModel, out errors); Assert.IsTrue(parsed, "parsed"); Assert.IsTrue(errors.Count() == 0, "No errors"); IEdmEntityType derivedThing = (IEdmEntityType)annotatingModel.FindType("foo.DerivedThing"); EdmPropertyValue id = new EdmPropertyValue("ID", new EdmIntegerConstant(null, 17)); EdmPropertyValue value = new EdmPropertyValue("Value", new EdmStringConstant(null, "Very high value")); EdmPropertyValue addSome = new EdmPropertyValue("AddSome", new EdmStringConstant(null, "More")); EdmPropertyValue addSomeMore = new EdmPropertyValue("AddSomeMore", new EdmStringConstant(null, "Much, much, more")); EdmPropertyValue[] thingProperties = new EdmPropertyValue[] { id, value, addSome, addSomeMore }; IEdmStructuredValue thingValue = new EdmStructuredValue(new EdmEntityTypeReference(derivedThing, false), thingProperties); IEdmEntityType anotherThing = (IEdmEntityType)this.baseModel.FindType("foo.AnotherThing"); IEdmValueTerm distantAge = this.baseModel.FindValueTerm("foo.DistantAge"); EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.builtInFunctions); EdmToClrEvaluator clrEvaluator = new EdmToClrEvaluator(this.builtInFunctions); IEdmValue thingDistantAge = annotatingModel.GetTermValue(thingValue, distantAge, "Short", expressionEvaluator); Assert.AreEqual(27, ((IEdmIntegerValue)thingDistantAge).Value, "Term annotation value"); Assert.AreEqual(127, annotatingModel.GetTermValue<int>(thingValue, distantAge, "VeryLong", clrEvaluator), "Term annotation value too"); }
public void EvaluateExpressionsRoundTripWithQualifiers() { var expectedCsdl = new List<XElement>() { XElement.Parse(@" <Schema Namespace=""foo"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <Annotations Target=""foo.Person""> <Annotation Int=""99"" Qualifier=""3"" Term=""foo.Note"" /> <Annotation Int=""127"" Qualifier=""ReallyOddQualifer1234567890!@#$%^*()_+&"" Term=""foo.Note"" /> <Annotation Int=""127"" Qualifier=""foo+bar"" Term=""foo.Note"" /> </Annotations> <EntityType Name=""Person""> <Key> <PropertyRef Name=""Id"" /> </Key> <Property Name=""Id"" Nullable=""false"" Type=""Edm.Int32"" /> <Property Name=""Name"" Nullable=""true"" Type=""Edm.String"" /> </EntityType> <Term Name=""Note"" Type=""Edm.Int32"" /> </Schema>") }; var csdls = VocabularyTestModelBuilder.AnnotationQualifiersWithNonSimpleValue(); IEdmModel model = this.GetParserResult(csdls); EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.builtInFunctions); EdmToClrEvaluator clrEvaluator = new EdmToClrEvaluator(this.builtInFunctions); var qualifier3 = "3"; var qualifier4 = "ReallyOddQualifer1234567890!@#$%^*()_+&"; var qualifier5 = "foo+bar"; Action<IEdmModel> validateAnnotationQualifier = m => { var note = m.FindValueTerm("foo.Note"); IEdmValue noteValue = m.GetTermValue(this.personValue, note, qualifier3, expressionEvaluator); Assert.AreEqual(99, ((IEdmIntegerValue)noteValue).Value, "Term annotation value"); Assert.AreEqual(127, m.GetTermValue<int>(this.personValue, note, qualifier4, clrEvaluator), "Term annotation value too"); Assert.AreEqual(127, m.GetTermValue<int>(this.personValue, note, qualifier5, clrEvaluator), "Term annotation value too"); noteValue = m.GetTermValue(this.personValue, "foo.Note", qualifier3, expressionEvaluator); Assert.AreEqual(99, ((IEdmIntegerValue)noteValue).Value, "Term annotation value"); Assert.AreEqual(127, m.GetTermValue<int>(this.personValue, "foo.Note", qualifier4, clrEvaluator), "Term annotation value too"); Assert.AreEqual(127, m.GetTermValue<int>(this.personValue, "foo.Note", qualifier5, clrEvaluator), "Term annotation value too"); }; validateAnnotationQualifier(model); IEnumerable<EdmError> errors; var roundTripCsdls = this.GetSerializerResult(model, Microsoft.Test.OData.Utils.Metadata.EdmVersion.V40, out errors).Select(XElement.Parse).ToList(); Assert.AreEqual(0, errors.Count(), "Invalid error count."); new ConstructiveApiCsdlXElementComparer().Compare(expectedCsdl, roundTripCsdls); var roundTripModel = this.GetParserResult(roundTripCsdls); validateAnnotationQualifier(roundTripModel); }
public void EvaluateExpressionsWithQualifiers() { const string annotatingModelCsdl = @"<Schema Namespace=""foo"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <Annotations Target=""foo.Person""> <Annotation Term=""foo.DistantAge"" Qualifier=""Long"" Int=""99"" /> <Annotation Term=""foo.DistantAge"" Qualifier=""VeryLong"" Int=""127"" /> <Annotation Term=""foo.DistantAge"" Qualifier=""Bogus"" Int=""999"" /> </Annotations> </Schema>"; IEnumerable<EdmError> errors; IEdmModel annotatingModel; bool parsed = CsdlReader.TryParse(new XmlReader[] { XmlReader.Create(new StringReader(annotatingModelCsdl)) }, new IEdmModel[] { this.baseModel }, out annotatingModel, out errors); Assert.IsTrue(parsed, "parsed"); Assert.IsTrue(errors.Count() == 0, "No errors"); IEdmValueTerm distantAge = this.baseModel.FindValueTerm("foo.DistantAge"); EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.builtInFunctions); EdmToClrEvaluator clrEvaluator = new EdmToClrEvaluator(this.builtInFunctions); IEdmValue personDistantAge = annotatingModel.GetTermValue(this.personValue, distantAge, "Long", expressionEvaluator); Assert.AreEqual(99, ((IEdmIntegerValue)personDistantAge).Value, "Term annotation value"); Assert.AreEqual(127, annotatingModel.GetTermValue<int>(this.personValue, distantAge, "VeryLong", clrEvaluator), "Term annotation value too"); Assert.AreEqual(999, annotatingModel.GetTermValue<int>(this.personValue, distantAge, "Bogus", clrEvaluator), "Term annotation value too"); personDistantAge = annotatingModel.GetTermValue(this.personValue, "foo.DistantAge", "Long", expressionEvaluator); Assert.AreEqual(99, ((IEdmIntegerValue)personDistantAge).Value, "Term annotation value"); Assert.AreEqual(127, annotatingModel.GetTermValue<int>(this.personValue, "foo.DistantAge", "VeryLong", clrEvaluator), "Term annotation value too"); Assert.AreEqual(999, annotatingModel.GetTermValue<int>(this.personValue, "foo.DistantAge", "Bogus", clrEvaluator), "Term annotation value too"); }
public void EvaluateUnboundPathExpressions() { const string annotatingModelCsdl = @"<Schema Namespace=""foo"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <Annotations Target=""foo.Person""> <Annotation Term=""foo.CoolPersonTerm""> <Record> <PropertyValue Property=""Sobriquet"" Path=""Nickname"" /> <PropertyValue Property=""Assessment"" Path=""Nowhere/CoolnessIndex"" /> <PropertyValue Property=""Street"" Path=""Address/Nowhere"" /> </Record> </Annotation> </Annotations> </Schema>"; IEnumerable<EdmError> errors; IEdmModel annotatingModel; bool parsed = CsdlReader.TryParse(new XmlReader[] { XmlReader.Create(new StringReader(annotatingModelCsdl)) }, new IEdmModel[] { this.baseModel }, out annotatingModel, out errors); Assert.IsTrue(parsed, "parsed"); Assert.IsTrue(errors.Count() == 0, "No errors"); IEdmEntityType person = (IEdmEntityType)this.baseModel.FindType("foo.Person"); IEdmValueTerm coolPersonTerm = this.baseModel.FindValueTerm("foo.CoolPersonTerm"); IEdmEntityType coolPerson = (IEdmEntityType)this.baseModel.FindType("foo.CoolPerson"); IEdmProperty coolSobriquet = coolPerson.FindProperty("Sobriquet"); IEdmProperty coolAssessment = coolPerson.FindProperty("Assessment"); IEdmProperty coolStreet = coolPerson.FindProperty("Street"); EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.builtInFunctions); EdmToClrEvaluator clrEvaluator = new EdmToClrEvaluator(this.builtInFunctions); bool caught = false; try { IEdmValue personCoolSobriquet = annotatingModel.GetPropertyValue(this.personValue, coolPersonTerm, coolSobriquet, expressionEvaluator); } catch (InvalidOperationException) { caught = true; } Assert.IsTrue(caught, "Invalid operation"); caught = false; try { IEdmValue personCoolAssessment = annotatingModel.GetPropertyValue(this.personValue, coolPersonTerm, coolAssessment, expressionEvaluator); } catch (InvalidOperationException) { caught = true; } Assert.IsTrue(caught, "Invalid operation"); caught = false; try { IEdmValue personCoolStreet = annotatingModel.GetPropertyValue(this.personValue, coolPersonTerm, coolStreet, expressionEvaluator); } catch (InvalidOperationException) { caught = true; } Assert.IsTrue(caught, "Invalid operation"); caught = false; try { IEdmValue personCoolSobriquetRedux = annotatingModel.GetPropertyValue(this.personValue, coolPersonTerm, coolSobriquet, expressionEvaluator); } catch (InvalidOperationException) { caught = true; } Assert.IsTrue(caught, "Invalid operation"); }
public void EvaluateDuplicateAndUnboundLabelsGraphConstruction() { const string annotatingModelCsdl = @"<Schema Namespace=""foo"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <Annotations Target=""foo.Person""> <Annotation Term=""foo.Flintstones""> <Collection> <LabeledElement Name=""Fred""> <Record> <PropertyValue Property=""Name"" String=""Fred Flintstone"" /> <PropertyValue Property=""Partner""> <LabeledElementReference Name=""Wilma"" /> </PropertyValue> <PropertyValue Property=""Children""> <Collection> <LabeledElementReference Name=""Pebbles"" /> </Collection> </PropertyValue> </Record> </LabeledElement> <LabeledElement Name=""Wilma""> <Record> <PropertyValue Property=""Name"" String=""Wilma Flintstone"" /> <PropertyValue Property=""Partner""> <LabeledElementReference Name=""Freddy"" /> </PropertyValue> <PropertyValue Property=""Children""> <Collection> <LabeledElementReference Name=""Pebbles"" /> </Collection> </PropertyValue> </Record> </LabeledElement> <LabeledElement Name=""Wilma""> <Record> <PropertyValue Property=""Name"" String=""Wilma Flintstone"" /> <PropertyValue Property=""Partner""> <LabeledElementReference Name=""Frederico"" /> </PropertyValue> <PropertyValue Property=""Children""> <Collection> <LabeledElementReference Name=""Pebbles"" /> </Collection> </PropertyValue> </Record> </LabeledElement> <LabeledElement Name=""Pebbles""> <Record> <PropertyValue Property=""Name"" String=""Pebbles Flintstone"" /> <PropertyValue Property=""Parents""> <Collection> <LabeledElementReference Name=""Fred"" /> <LabeledElementReference Name=""Wilma"" /> <LabeledElementReference Name=""Freddy"" /> </Collection> </PropertyValue> </Record> </LabeledElement> </Collection> </Annotation> </Annotations> </Schema>"; IEnumerable<EdmError> errors; IEdmModel annotatingModel; bool parsed = CsdlReader.TryParse(new XmlReader[] { XmlReader.Create(new StringReader(annotatingModelCsdl)) }, new IEdmModel[] { this.baseModel }, out annotatingModel, out errors); Assert.IsTrue(parsed, "parsed"); Assert.IsTrue(errors.Count() == 0, "No errors"); IEdmEntityType person = (IEdmEntityType)this.baseModel.FindType("foo.Person"); EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.builtInFunctions); EdmToClrEvaluator clrEvaluator = new EdmToClrEvaluator(this.builtInFunctions); // And finally make sure we can handle nulls for bad graph references. clrEvaluator.EdmToClrConverter = new Microsoft.OData.Edm.EdmToClrConversion.EdmToClrConverter( (IEdmStructuredValue edmValue, Type clrType, Microsoft.OData.Edm.EdmToClrConversion.EdmToClrConverter converter, out object objectInstance, out bool objectInstanceInitialized) => { objectInstance = null; objectInstanceInitialized = false; if (clrType == typeof(Person)) { if (edmValue.FindPropertyValue("Parents") != null) { objectInstance = new Child(); } else { objectInstance = new Parentt(); } } else if (clrType == typeof(Parentt)) { objectInstance = new Parentt(); } if (objectInstance is Parentt) { // deal with the renamed field (edm:Partner -> clr:Partnerr). IEdmStructuredValue sv = (IEdmStructuredValue)edmValue; IEdmPropertyValue pv = sv.FindPropertyValue("Partner"); if (pv != null) { // register the object before converting property values in case it's being referenced somewhere down the graph converter.RegisterConvertedObject(edmValue, objectInstance); // call back into converter to get the value of Partner edm property and assign it to Partner clr property. ((Parentt)objectInstance).Partnerr = converter.AsClrValue<Parentt>(pv.Value); } } return objectInstance != null; }); IEdmValueTerm undefined = (IEdmValueTerm)person.VocabularyAnnotations(annotatingModel).ElementAt(0).Term; IEdmCollectionValue flintstones = (IEdmCollectionValue)annotatingModel.GetTermValue(this.personValue, undefined, expressionEvaluator); IEnumerable<Person> clrFlintstones = annotatingModel.GetTermValue<IEnumerable<Person>>(this.personValue, undefined, clrEvaluator); Assert.AreEqual(4, flintstones.Elements.Count(), "Collection value size"); Assert.AreEqual(4, clrFlintstones.Count(), "CLR Collection value size"); IEdmStructuredValue fred = (IEdmStructuredValue)flintstones.Elements.ElementAt(0).Value; Parentt clrFred = (Parentt)clrFlintstones.ElementAt(0); IEdmPropertyValue fredPartner = fred.FindPropertyValue("Partner"); IEdmStructuredValue wilma = (IEdmStructuredValue)flintstones.Elements.ElementAt(1).Value; Parentt clrWilma = (Parentt)clrFlintstones.ElementAt(1); IEdmPropertyValue wilmaPartner = wilma.FindPropertyValue("Partner"); clrFred = (Parentt)clrFlintstones.ElementAt(0); clrWilma = (Parentt)clrFlintstones.ElementAt(1); Assert.IsNull(clrWilma.Partnerr, "Unbound graph reference"); Assert.IsNull(clrFred.Partnerr, "Ambiguous graph reference"); }
public void EvaluateUnboundTerms() { const string applicationCsdl = @"<Schema Namespace=""bar"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <Annotations Target=""bar.Person""> <Annotation Term=""bar.RandomTerm"" Path=""Extra"" /> </Annotations> <EntityType Name=""Person""> <Key> <PropertyRef Name=""Name"" /> </Key> <Property Name=""Name"" Type=""String"" Nullable=""false"" /> <Property Name=""Decoration"" Type=""bar.Decoration""> <Annotation Term=""bar.DecorationTerm"" Qualifier=""Goodness"" String=""Excellent"" /> </Property> </EntityType> <ComplexType Name=""Decoration""> <Property Name=""One"" Type=""Int32"" /> <Property Name=""Two"" Type=""Int32"" /> </ComplexType> </Schema>"; IEdmModel applicationModel = this.Parse(applicationCsdl); IEdmEntityType person = (IEdmEntityType)applicationModel.FindType("bar.Person"); IEdmProperty personDecoration = person.FindProperty("Decoration"); EdmToClrEvaluator evaluator = new EdmToClrEvaluator(null); List<IEdmPropertyValue> decorationPropertyValues = new List<IEdmPropertyValue>(); decorationPropertyValues.Add(new EdmPropertyValue("One", new EdmIntegerConstant(1))); decorationPropertyValues.Add(new EdmPropertyValue("Two", new EdmIntegerConstant(2))); List<IEdmPropertyValue> propertyValues = new List<IEdmPropertyValue>(); propertyValues.Add(new EdmPropertyValue("Name", new EdmStringConstant("Goober"))); propertyValues.Add(new EdmPropertyValue("Decoration", new EdmStructuredValue(null, decorationPropertyValues))); propertyValues.Add(new EdmPropertyValue("Extra", new EdmStringConstant("Extra value!"))); IEdmStructuredValue context = new EdmStructuredValue(new EdmEntityTypeReference(person, false), propertyValues); string random = applicationModel.GetTermValue<string>(context, "bar.RandomTerm", evaluator); Assert.AreEqual("Extra value!", random, "Annotation evaluated value."); IEdmValue randomValue = applicationModel.GetTermValue(context, "bar.RandomTerm", evaluator); Assert.AreEqual("Extra value!", ((IEdmStringValue)randomValue).Value, "Annotation evaluated value."); string goodness = applicationModel.GetTermValue<string>(personDecoration, "bar.DecorationTerm", "Goodness", evaluator); Assert.AreEqual("Excellent", goodness, "Annotation evaluated value."); IEdmValue goodnessValue = applicationModel.GetTermValue(personDecoration, "bar.DecorationTerm", "Goodness", evaluator); Assert.AreEqual("Excellent", ((IEdmStringValue)goodnessValue).Value, "Annotation evaluated value."); IEdmValueAnnotation randomTermAnnotation = applicationModel.FindVocabularyAnnotations<IEdmValueAnnotation>(person, "bar.RandomTerm").Single(); random = evaluator.EvaluateToClrValue<string>(randomTermAnnotation.Value, context); Assert.AreEqual("Extra value!", random, "Annotation evaluated value."); }
public void EvaluateIsTypeExpressions() { const string annotatingModelCsdl = @"<Schema Namespace=""foo"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <Annotations Target=""foo.Person""> <Annotation Term=""foo.Punning0""> <IsType Type=""Int32""> <Path>Living</Path> </IsType> </Annotation> <Annotation Term=""foo.PunningBool1""> <IsType Type=""Byte""> <Int>256</Int> </IsType> </Annotation> <Annotation Term=""foo.PunningBool2""> <IsType Type=""SByte""> <Int>-129</Int> </IsType> </Annotation> <Annotation Term=""foo.PunningBool3""> <IsType Type=""Int16""> <Int>32768</Int> </IsType> </Annotation> <Annotation Term=""foo.PunningBool4""> <IsType Type=""Int32""> <Float>1.1</Float> </IsType> </Annotation> <Annotation Term=""foo.PunningBool5""> <IsType Type=""Collection(Int32)""> <Collection> <Int>1</Int> <String>2</String> <Int>3</Int> <Float>4.1</Float> </Collection> </IsType> </Annotation> <Annotation Term=""foo.PunningBool6""> <IsType Type=""String"" MaxLength=""5""> <String>123456</String> </IsType> </Annotation> <Annotation Term=""foo.PunningBool7""> <IsType Type=""String"" Nullable=""False""> <Null /> </IsType> </Annotation> <Annotation Term=""foo.PunningBool8""> <IsType Type=""foo.Cartoon""> <Record> <PropertyValue Property=""Lead"" String=""Gumby"" /> <PropertyValue Property=""Sidekick"" Int=""144"" /> <PropertyValue Property=""Sponsor""> <Record> <PropertyValue Property=""Name"" String=""ImpendingDoom"" /> </Record> </PropertyValue> </Record> </IsType> </Annotation> <Annotation Term=""foo.PunningBool9""> <IsType Type=""foo.Cartoon""> <Record> <PropertyValue Property=""Lead"" String=""Fred Flintstone"" /> <PropertyValue Property=""Sidekick"" String=""Barney Rubble"" /> <PropertyValue Property=""Sponsor""> <Record> <PropertyValue Property=""NotName"" String=""Slate Gravel"" /> </Record> </PropertyValue> </Record> </IsType> </Annotation> <Annotation Term=""foo.PunningBool11""> <IsType Type=""foo.Pet""> <Record> <PropertyValue Property=""Name""> <Null /> </PropertyValue> <PropertyValue Property=""Age""> <Null /> </PropertyValue> <PropertyValue Property=""Toys""> <Collection> <Null /> </Collection> </PropertyValue> </Record> </IsType> </Annotation> <Annotation Term=""foo.ClearBool0""> <IsType Type=""foo.Address""> <Path>Address</Path> </IsType> </Annotation> <Annotation Term=""foo.ClearBool1""> <IsType Type=""Byte""> <Int>255</Int> </IsType> </Annotation> <Annotation Term=""foo.ClearBool2""> <IsType Type=""SByte""> <Int>-128</Int> </IsType> </Annotation> <Annotation Term=""foo.ClearBool3""> <IsType Type=""Int16""> <Int>32767</Int> </IsType> </Annotation> <Annotation Term=""foo.ClearBool4""> <IsType Type=""Int32""> <Int>32768</Int> </IsType> </Annotation> <Annotation Term=""foo.ClearBool6""> <IsType Type=""String"" MaxLength=""5""> <String>12345</String> </IsType> </Annotation> <Annotation Term=""foo.ClearBool7""> <IsType Type=""String""> <Null /> </IsType> </Annotation> <Annotation Term=""foo.ClearBool8""> <IsType Type=""foo.Cartoon""> <Record> <PropertyValue Property=""Lead"" String=""Rick Dastardly"" /> <PropertyValue Property=""Sidekick"" String=""Muttley"" /> <PropertyValue Property=""ExtraSidekick"" String=""Penelope Pitstop"" /> <PropertyValue Property=""Sponsor""> <Record> <PropertyValue Property=""Name"" String=""OilSlick"" /> </Record> </PropertyValue> </Record> </IsType> </Annotation> <Annotation Term=""foo.ClearBool11""> <IsType Type=""foo.Pet""> <Record> <PropertyValue Property=""Name""> <String>foo</String> </PropertyValue> <PropertyValue Property=""Age""> <Null /> </PropertyValue> <PropertyValue Property=""Toys""> <Collection> <Null /> </Collection> </PropertyValue> </Record> </IsType> </Annotation> </Annotations> </Schema>"; IEdmModel annotatingModel = this.GetParserResult(new String[] { annotatingModelCsdl }, this.baseModel); EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.builtInFunctions); EdmToClrEvaluator clrEvaluator = new EdmToClrEvaluator(this.builtInFunctions); Assert.AreEqual(false, annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.Punning0"), clrEvaluator), "Term annotation value"); Assert.AreEqual(false, annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.PunningBool1"), clrEvaluator), "Term annotation value"); Assert.AreEqual(false, annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.PunningBool2"), clrEvaluator), "Term annotation value"); Assert.AreEqual(false, annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.PunningBool3"), clrEvaluator), "Term annotation value"); Assert.AreEqual(false, annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.PunningBool4"), clrEvaluator), "Term annotation value"); Assert.AreEqual(false, annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.PunningBool5"), clrEvaluator), "Term annotation value"); Assert.AreEqual(false, annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.PunningBool6"), clrEvaluator), "Term annotation value"); Assert.AreEqual(false, annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.PunningBool7"), clrEvaluator), "Term annotation value"); Assert.AreEqual(false, annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.PunningBool8"), clrEvaluator), "Term annotation value"); Assert.AreEqual(false, annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.PunningBool9"), clrEvaluator), "Term annotation value"); Assert.AreEqual(false, annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.PunningBool11"), clrEvaluator), "Term annotation value"); Assert.AreEqual(true, annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.ClearBool0"), clrEvaluator), "Term annotation value"); Assert.AreEqual(true, annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.ClearBool1"), clrEvaluator), "Term annotation value"); Assert.AreEqual(true, annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.ClearBool2"), clrEvaluator), "Term annotation value"); Assert.AreEqual(true, annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.ClearBool3"), clrEvaluator), "Term annotation value"); Assert.AreEqual(true, annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.ClearBool4"), clrEvaluator), "Term annotation value"); Assert.AreEqual(true, annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.ClearBool6"), clrEvaluator), "Term annotation value"); Assert.AreEqual(true, annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.ClearBool7"), clrEvaluator), "Term annotation value"); Assert.AreEqual(true, annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.ClearBool8"), clrEvaluator), "Term annotation value"); Assert.AreEqual(true, annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.ClearBool11"), clrEvaluator), "Term annotation value"); }
public void EvaluateLastChanceFunctionApplicationExpression() { const string annotatingModelCsdl = @"<Schema Namespace=""foo"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <Annotations Target=""foo.Person""> <Annotation Term=""foo.CoolPersonTerm""> <Record> <PropertyValue Property=""Street""> <Apply Function=""Functions.MagicConcat""> <Path>Address/Number</Path> <String>/</String> <Path>Address/Street</Path> <String>/</String> <Path>Address/City</Path> <String>/</String> <Path>Address/State</Path> </Apply> </PropertyValue> </Record> </Annotation> </Annotations> </Schema>"; IEnumerable<EdmError> errors; IEdmModel annotatingModel; bool parsed = CsdlReader.TryParse(new XmlReader[] { XmlReader.Create(new StringReader(annotatingModelCsdl)) }, new IEdmModel[] { this.baseModel, this.builtInFunctionsModel }, out annotatingModel, out errors); Assert.IsTrue(parsed, "parsed"); Assert.IsTrue(errors.Count() == 0, "No errors"); IEdmEntityType person = (IEdmEntityType)this.baseModel.FindType("foo.Person"); IEdmEntityType coolPerson = (IEdmEntityType)this.baseModel.FindType("foo.CoolPerson"); IEdmValueTerm coolPersonTerm = this.baseModel.FindValueTerm("foo.CoolPersonTerm"); IEdmProperty coolStreet = coolPerson.FindProperty("Street"); EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.builtInFunctions, MagicEvaluator); EdmToClrEvaluator clrEvaluator = new EdmToClrEvaluator(this.builtInFunctions, MagicEvaluator); IEdmValue personCoolStreet = annotatingModel.GetPropertyValue(this.personValue, coolPersonTerm, coolStreet, expressionEvaluator); Assert.AreEqual("1/Joey Ramone Place/New York/New York", ((IEdmStringValue)personCoolStreet).Value, "Concat application"); Assert.AreEqual("1/Joey Ramone Place/New York/New York", annotatingModel.GetPropertyValue<string>(this.personValue, coolPersonTerm, coolStreet, clrEvaluator), "Concat application 2"); }
public void ClrTypeMappingValueAnnotationTryCreateObjectInstance() { this.InitializeOperationDefinitions(); EdmToClrEvaluator ev = new EdmToClrEvaluator(this.operationDefinitions); var edmModel = this.GetParserResult(ClrTypeMappingTestModelBuilder.ValueAnnotationClassTypeBasicTest(), this.operationDeclarationModel); var value = ev.Evaluate(this.GetValueAnnotations(edmModel, edmModel.FindType("NS1.Person"), "TVDisplay").Single().Value); var isObjectPopulated = true; var isObjectInitialized = true; object createdObjectInstance = null; TryCreateObjectInstance tryCreateObjectInstance = (IEdmStructuredValue edmValue, Type clrType, EdmToClrConverter converter, out object objectInstance, out bool objectInstanceInitialized) => { objectInstance = createdObjectInstance; objectInstanceInitialized = isObjectPopulated; return(isObjectInitialized); }; ev.EdmToClrConverter = new EdmToClrConverter(tryCreateObjectInstance); Assert.IsNull((Display2)ev.EdmToClrConverter.AsClrValue(value, typeof(Display2)), "The returned object should be null."); isObjectPopulated = false; isObjectInitialized = true; ev.EdmToClrConverter = new EdmToClrConverter(tryCreateObjectInstance); Assert.IsNull((Display2)ev.EdmToClrConverter.AsClrValue(value, typeof(Display2)), "The returned object should be null."); isObjectPopulated = true; isObjectInitialized = false; ev.EdmToClrConverter = new EdmToClrConverter(tryCreateObjectInstance); Assert.IsTrue(CompareObjects((Display2)ev.EdmToClrConverter.AsClrValue(value, typeof(Display2)), new Display2() { X = 10, Y = 20, Origin = new Display1() { X = 10, Y = 20 } }), "The returned object has incorrect values."); isObjectPopulated = false; isObjectInitialized = false; ev.EdmToClrConverter = new EdmToClrConverter(tryCreateObjectInstance); Assert.IsTrue(CompareObjects((Display2)ev.EdmToClrConverter.AsClrValue(value, typeof(Display2)), new Display2() { X = 10, Y = 20, Origin = new Display1() { X = 10, Y = 20 } }), "The returned object has incorrect values."); createdObjectInstance = new Display2() { X = 0, Y = 1, Origin = new Display1 { X = 3, Y = 4 } }; isObjectPopulated = true; isObjectInitialized = true; ev.EdmToClrConverter = new EdmToClrConverter(tryCreateObjectInstance); Assert.IsTrue(CompareObjects((Display2)ev.EdmToClrConverter.AsClrValue(value, typeof(Display2)), createdObjectInstance), "The returned object has incorrect values."); ev.EdmToClrConverter = new EdmToClrConverter((IEdmStructuredValue edmValue, Type clrType, EdmToClrConverter converter, out object objectInstance, out bool objectInstanceInitialized) => { if (clrType == typeof(Display2)) { objectInstance = new Display2() { X = 0, Y = 1, Origin = new Display1 { X = 3, Y = 4 } }; objectInstanceInitialized = false; return(true); } else if (clrType == typeof(Display1)) { objectInstance = new Display1 { X = 3, Y = 4 }; objectInstanceInitialized = false; return(true); } else { objectInstance = null; objectInstanceInitialized = false; return(false); } }); Assert.IsTrue(CompareObjects((Display2)ev.EdmToClrConverter.AsClrValue(value, typeof(Display2)), new Display2() { X = 10, Y = 20, Origin = new Display1() { X = 10, Y = 20 } }), "The returned object has incorrect values."); isObjectPopulated = false; isObjectInitialized = true; createdObjectInstance = new DisplayCoordination(); ev.EdmToClrConverter = new EdmToClrConverter(tryCreateObjectInstance); Coordination actual = (Coordination)ev.EdmToClrConverter.AsClrValue(value, typeof(Coordination)); Coordination expected = new Coordination() { X = 10, Y = 20 }; Assert.AreEqual(expected.X, actual.X, "The returned object has incorrect values. X"); Assert.AreEqual(expected.Y, actual.Y, "The returned object has incorrect values. X"); }
public void EvaluateIfExpressions() { const string annotatingModelCsdl = @"<Schema Namespace=""foo"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <Annotations Target=""foo.Person""> <Annotation Term=""foo.DistantAge""> <If> <Path>Living</Path> <Int>101</Int> <Int>99</Int> </If> </Annotation> <Annotation Term=""foo.NewAge""> <If> <Path>Famous</Path> <Int>101</Int> <Int>99</Int> </If> </Annotation> </Annotations> </Schema>"; IEnumerable<EdmError> errors; IEdmModel annotatingModel; bool parsed = CsdlReader.TryParse(new XmlReader[] { XmlReader.Create(new StringReader(annotatingModelCsdl)) }, new IEdmModel[] { this.baseModel }, out annotatingModel, out errors); Assert.IsTrue(parsed, "parsed"); Assert.IsTrue(errors.Count() == 0, "No errors"); IEdmEntityType person = (IEdmEntityType)this.baseModel.FindType("foo.Person"); IEdmValueTerm distantAge = this.baseModel.FindValueTerm("foo.DistantAge"); IEdmValueTerm newAge = this.baseModel.FindValueTerm("foo.NewAge"); EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.builtInFunctions); EdmToClrEvaluator clrEvaluator = new EdmToClrEvaluator(this.builtInFunctions); IEdmValue personDistantAge = annotatingModel.GetTermValue(this.personValue, distantAge, expressionEvaluator); Assert.AreEqual(99, ((IEdmIntegerValue)personDistantAge).Value, "Term annotation value"); Assert.AreEqual(99, annotatingModel.GetTermValue<int>(this.personValue, distantAge, clrEvaluator), "Term annotation value 2"); IEdmValue personNewAge = annotatingModel.GetTermValue(this.personValue, newAge, expressionEvaluator); Assert.AreEqual(101, ((IEdmIntegerValue)personNewAge).Value, "Term annotation value"); Assert.AreEqual(101, annotatingModel.GetTermValue<int>(this.personValue, newAge, clrEvaluator), "Term annotation value 2"); }
/// <summary> /// Gets the CLR value of a property of a term type that has been applied to the type of a value. /// </summary> /// <typeparam name="T">The CLR type of the value to be returned.</typeparam> /// <param name="model">Model to search for annotations.</param> /// <param name="context">Value to use as context in evaluation.</param> /// <param name="term">Term to search for annotations.</param> /// <param name="property">Property to evaluate.</param> /// <param name="qualifier">Qualifier to apply.</param> /// <param name="evaluator">Evaluator to use to perform expression evaluation.</param> /// <returns>Value of the property evaluated against the supplied value, or default(<typeparamref name="T"/>) if no relevant annotation exists.</returns> public static T GetPropertyValue <T>(this IEdmModel model, IEdmStructuredValue context, IEdmValueTerm term, IEdmProperty property, string qualifier, EdmToClrEvaluator evaluator) { ExceptionUtilities.CheckArgumentNotNull(model, "model"); ExceptionUtilities.CheckArgumentNotNull(context, "context"); ExceptionUtilities.CheckArgumentNotNull(property, "property"); ExceptionUtilities.CheckArgumentNotNull(evaluator, "evaluator"); return(GetPropertyValue <T>(model, context, context.Type.AsEntity().EntityDefinition(), term, property, qualifier, evaluator.EvaluateToClrValue <T>)); }
public void EvaluateInvalidTermIsTypeExpressions() { const string annotatingModelCsdl = @"<Schema Namespace=""foo"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <Annotations Target=""foo.Person""> <Annotation Term=""foo.Clear0""> <IsType Type=""foo.Address""> <Path>Address</Path> </IsType> </Annotation> <Annotation Term=""foo.Clear1""> <IsType Type=""Byte""> <Int>255</Int> </IsType> </Annotation> <Annotation Term=""foo.Clear2""> <IsType Type=""SByte""> <Int>-128</Int> </IsType> </Annotation> <Annotation Term=""foo.Clear3""> <IsType Type=""Int16""> <Int>32767</Int> </IsType> </Annotation> <Annotation Term=""foo.Clear4""> <IsType Type=""Int32""> <Int>32768</Int> </IsType> </Annotation> <Annotation Term=""foo.Clear6""> <IsType Type=""String"" MaxLength=""5""> <String>12345</String> </IsType> </Annotation> <Annotation Term=""foo.Clear7""> <IsType Type=""String""> <Null /> </IsType> </Annotation> <Annotation Term=""foo.Clear8""> <IsType Type=""foo.Cartoon""> <Record> <PropertyValue Property=""Lead"" String=""Rick Dastardly"" /> <PropertyValue Property=""Sidekick"" String=""Muttley"" /> <PropertyValue Property=""ExtraSidekick"" String=""Penelope Pitstop"" /> <PropertyValue Property=""Sponsor""> <Record> <PropertyValue Property=""Name"" String=""OilSlick"" /> </Record> </PropertyValue> </Record> </IsType> </Annotation> <Annotation Term=""foo.Clear11""> <IsType Type=""foo.Pet""> <Record> <PropertyValue Property=""Name""> <String>foo</String> </PropertyValue> <PropertyValue Property=""Age""> <Null /> </PropertyValue> <PropertyValue Property=""Toys""> <Collection> <Null /> </Collection> </PropertyValue> </Record> </IsType> </Annotation> </Annotations> </Schema>"; IEdmModel annotatingModel = this.GetParserResult(new String[] { annotatingModelCsdl }, this.baseModel); EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.builtInFunctions); EdmToClrEvaluator clrEvaluator = new EdmToClrEvaluator(this.builtInFunctions); this.VerifyThrowsException(typeof(InvalidOperationException), () => annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.Punning0"), clrEvaluator)); this.VerifyThrowsException(typeof(InvalidOperationException), () => annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.Clear0"), clrEvaluator)); this.VerifyThrowsException(typeof(InvalidOperationException), () => annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.Clear1"), clrEvaluator)); this.VerifyThrowsException(typeof(InvalidOperationException), () => annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.Clear2"), clrEvaluator)); this.VerifyThrowsException(typeof(InvalidOperationException), () => annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.Clear3"), clrEvaluator)); this.VerifyThrowsException(typeof(InvalidOperationException), () => annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.Clear4"), clrEvaluator)); this.VerifyThrowsException(typeof(InvalidOperationException), () => annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.Clear6"), clrEvaluator)); this.VerifyThrowsException(typeof(InvalidOperationException), () => annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.Clear7"), clrEvaluator)); this.VerifyThrowsException(typeof(InvalidOperationException), () => annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.Clear8"), clrEvaluator)); this.VerifyThrowsException(typeof(InvalidOperationException), () => annotatingModel.GetTermValue<bool>(this.personValue, this.baseModel.FindValueTerm("foo.Clear11"), clrEvaluator)); }
public void EvaluateRecordExpressions() { const string annotatingModelCsdl = @"<Schema Namespace=""foo"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <Annotations Target=""foo.Person""> <Annotation Term=""foo.Undefined1""> <Record> <PropertyValue Property=""X"" Int=""144"" /> <PropertyValue Property=""Y""> <Int>266</Int> </PropertyValue> </Record> </Annotation> <Annotation Term=""foo.Undefined2""> <Record> </Record> </Annotation> </Annotations> </Schema>"; IEnumerable<EdmError> errors; IEdmModel annotatingModel; bool parsed = CsdlReader.TryParse(new XmlReader[] { XmlReader.Create(new StringReader(annotatingModelCsdl)) }, new IEdmModel[] { this.baseModel }, out annotatingModel, out errors); Assert.IsTrue(parsed, "parsed"); Assert.IsTrue(errors.Count() == 0, "No errors"); IEdmEntityType person = (IEdmEntityType)this.baseModel.FindType("foo.Person"); EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.builtInFunctions); EdmToClrEvaluator clrEvaluator = new EdmToClrEvaluator(this.builtInFunctions); IEdmValueTerm undefined = (IEdmValueTerm)person.VocabularyAnnotations(annotatingModel).ElementAt(0).Term; IEdmStructuredValue record = (IEdmStructuredValue)annotatingModel.GetTermValue(this.personValue, undefined, expressionEvaluator); fooUndefined1 fooUndefined1 = annotatingModel.GetTermValue<fooUndefined1>(this.personValue, undefined, clrEvaluator); Assert.AreEqual(2, record.PropertyValues.Count(), "Record value property count"); Assert.AreEqual(144, ((IEdmIntegerValue)record.PropertyValues.ElementAt(0).Value).Value, "Term annotation value"); Assert.AreEqual(266, ((IEdmIntegerValue)record.PropertyValues.ElementAt(1).Value).Value, "Term annotation value"); Assert.AreEqual(266, ((IEdmIntegerValue)record.PropertyValues.ElementAt(1).Value).Value, "Term annotation value"); Assert.AreEqual(144, fooUndefined1.X, "Term annotation value fooUndefined1.X"); Assert.AreEqual(266, fooUndefined1.Y, "Term annotation value fooUndefined1.Y"); undefined = (IEdmValueTerm)person.VocabularyAnnotations(annotatingModel).ElementAt(1).Term; record = (IEdmStructuredValue)annotatingModel.GetTermValue(this.personValue, undefined, expressionEvaluator); fooUndefined2 fooUndefined2 = annotatingModel.GetTermValue<fooUndefined2>(this.personValue, undefined, clrEvaluator); Assert.AreEqual(0, record.PropertyValues.Count(), "Record value property count"); Assert.IsNotNull(fooUndefined2, "fooUndefined2"); }
public void EvaluateCollectionExpressions() { const string annotatingModelCsdl = @"<Schema Namespace=""foo"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <Annotations Target=""foo.Person""> <Annotation Term=""foo.Undefined1""> <Collection> <Int>144</Int> <Int>266</Int> </Collection> </Annotation> <Annotation Term=""foo.Undefined2""> <Collection> <Int>377</Int> </Collection> </Annotation> <Annotation Term=""foo.Undefined3""> <Collection> </Collection> </Annotation> </Annotations> </Schema>"; IEnumerable<EdmError> errors; IEdmModel annotatingModel; bool parsed = CsdlReader.TryParse(new XmlReader[] { XmlReader.Create(new StringReader(annotatingModelCsdl)) }, new IEdmModel[] { this.baseModel }, out annotatingModel, out errors); Assert.IsTrue(parsed, "parsed"); Assert.IsTrue(errors.Count() == 0, "No errors"); IEdmEntityType person = (IEdmEntityType)this.baseModel.FindType("foo.Person"); EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.builtInFunctions); EdmToClrEvaluator clrEvaluator = new EdmToClrEvaluator(this.builtInFunctions); IEdmValueTerm undefined = (IEdmValueTerm)person.VocabularyAnnotations(annotatingModel).ElementAt(0).Term; IEdmCollectionValue collection = (IEdmCollectionValue)annotatingModel.GetTermValue(this.personValue, undefined, expressionEvaluator); IEnumerable<int> clrCollection = annotatingModel.GetTermValue<IEnumerable<int>>(this.personValue, undefined, clrEvaluator); Assert.AreEqual(2, collection.Elements.Count(), "Collection value size"); Assert.AreEqual(144, ((IEdmIntegerValue)collection.Elements.ElementAt(0).Value).Value, "Term annotation value"); Assert.AreEqual(266, ((IEdmIntegerValue)collection.Elements.ElementAt(1).Value).Value, "Term annotation value"); Assert.AreEqual(266, ((IEdmIntegerValue)collection.Elements.ElementAt(1).Value).Value, "Term annotation value"); Assert.AreEqual(144, clrCollection.ElementAt(0), "Term annotation value clrCollection.ElementAt(0)"); Assert.AreEqual(266, clrCollection.ElementAt(1), "Term annotation value clrCollection.ElementAt(1)"); undefined = (IEdmValueTerm)person.VocabularyAnnotations(annotatingModel).ElementAt(1).Term; collection = (IEdmCollectionValue)annotatingModel.GetTermValue(this.personValue, undefined, expressionEvaluator); clrCollection = annotatingModel.GetTermValue<IEnumerable<int>>(this.personValue, undefined, clrEvaluator); Assert.AreEqual(1, collection.Elements.Count(), "Collection value size"); Assert.AreEqual(377, ((IEdmIntegerValue)collection.Elements.ElementAt(0).Value).Value, "Term annotation value"); Assert.AreEqual(377, clrCollection.ElementAt(0), "Term annotation value clrCollection.ElementAt(0)"); undefined = (IEdmValueTerm)person.VocabularyAnnotations(annotatingModel).ElementAt(2).Term; collection = (IEdmCollectionValue)annotatingModel.GetTermValue(this.personValue, undefined, expressionEvaluator); clrCollection = annotatingModel.GetTermValue<IEnumerable<int>>(this.personValue, undefined, clrEvaluator); Assert.AreEqual(0, collection.Elements.Count(), "Collection value size"); Assert.AreEqual(0, clrCollection.Count(), "CLR Collection value size"); }
public void ValueTerm_Constant_OnProperty() { this.SetupModelsAndValues(); const string applicationCsdl = @"<Schema Namespace=""Annotations"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <Annotations Target=""foo.Person/CoolnessIndex""> <Annotation Term=""bar.Int32Value"" Qualifier=""HotIndex"" Int=""-1"" /> <Annotation Term=""bar.StringValue"" String=""Goofy"" /> </Annotations> </Schema>"; IEdmModel applicationModel = this.Parse(applicationCsdl, this.baseModel, this.vocabularyDefinitionModel); EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.operationsLookup); EdmToClrEvaluator clrEvaluator = new EdmToClrEvaluator(this.operationsLookup); IEdmEntityType person = this.baseModel.FindEntityType("foo.Person"); IEdmProperty property = person.FindProperty("CoolnessIndex"); IEdmPropertyValue contextPropertyValue = ((IEdmStructuredValue)this.personValue).FindPropertyValue("CoolnessIndex"); IEdmValueTerm termInt32Value = this.vocabularyDefinitionModel.FindValueTerm("bar.Int32Value"); IEdmValueTerm termStringValue = this.vocabularyDefinitionModel.FindValueTerm("bar.StringValue"); IEdmValueAnnotation annotation = property.VocabularyAnnotations(applicationModel).SingleOrDefault(a => a.Term == termInt32Value) as IEdmValueAnnotation; IEdmExpression expression = annotation.Value; IEdmValue annotationHotIndex = expressionEvaluator.Evaluate(expression); Assert.AreEqual(-1, ((IEdmIntegerValue)annotationHotIndex).Value, "Term annotation value"); annotationHotIndex = applicationModel.GetTermValue(property, termInt32Value, "HotIndex", expressionEvaluator); Assert.AreEqual(-1, ((IEdmIntegerValue)annotationHotIndex).Value, "Term annotation value"); annotationHotIndex = applicationModel.GetTermValue(property, "bar.Int32Value", "HotIndex", expressionEvaluator); Assert.AreEqual(-1, ((IEdmIntegerValue)annotationHotIndex).Value, "Term annotation value"); int hotIndex = applicationModel.GetTermValue<int>(property, termInt32Value, "HotIndex", clrEvaluator); Assert.AreEqual(-1, hotIndex, "Term annotation value"); hotIndex = applicationModel.GetTermValue<int>(property, "bar.Int32Value", "HotIndex", clrEvaluator); Assert.AreEqual(-1, hotIndex, "Term annotation value"); IEdmValue annotationString = applicationModel.GetTermValue(property, termStringValue, expressionEvaluator); Assert.AreEqual("Goofy", ((IEdmStringValue)annotationString).Value, "Term annotation value"); annotationString = applicationModel.GetTermValue(property, "bar.StringValue", expressionEvaluator); Assert.AreEqual("Goofy", ((IEdmStringValue)annotationString).Value, "Term annotation value"); string stringValue = applicationModel.GetTermValue<string>(property, termStringValue, clrEvaluator); Assert.AreEqual("Goofy", stringValue, "Term annotation value"); stringValue = applicationModel.GetTermValue<string>(property, "bar.StringValue", clrEvaluator); Assert.AreEqual("Goofy", stringValue, "Term annotation value"); }