示例#1
0
        public void SingleModel_Annotation_OnEntityType()
        {
            const string applicationCsdl =
                @"<Schema Namespace=""bar"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <Term Name=""MoreTransformedPersonTerm"" Type=""bar.MoreTransformedPerson"" />
    <Annotations Target=""bar.Person"">
        <Annotation Term=""bar.MoreTransformedPersonTerm"">
            <Record>
                <PropertyValue Property=""Description"" String=""I know it!"" />
            </Record>
        </Annotation>
    </Annotations>
    <EntityType Name=""MoreTransformedPerson"" BaseType=""bar.TransformedPerson"">
        <Property Name=""Description"" Type=""String"" />
    </EntityType>
    <EntityType Name=""TransformedPerson"">
        <Property Name=""Age"" Type=""Int32"" Nullable=""false"" />
    </EntityType>
    <EntityType Name=""Person"">
        <Key>
            <PropertyRef Name=""Name"" />
        </Key>
        <Property Name=""Name"" Type=""String"" Nullable=""false"" />
    </EntityType>
</Schema>";
            IEdmModel applicationModel = this.Parse(applicationCsdl);
            EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(null);

            var       term              = applicationModel.FindTerm("bar.MoreTransformedPersonTerm");
            var       property          = applicationModel.FindEntityType("bar.MoreTransformedPerson").FindProperty("Description");
            var       fakeContextValue  = new EdmStructuredValue(new EdmEntityTypeReference(applicationModel.FindEntityType("bar.Person"), true), Enumerable.Empty <IEdmPropertyValue>());
            IEdmValue valueOfAnnotation = applicationModel.GetPropertyValue(fakeContextValue, term, property, expressionEvaluator);

            Assert.AreEqual("I know it!", ((IEdmStringValue)valueOfAnnotation).Value, "Annotation evaluated value.");
        }
示例#2
0
        static ODataVocabularyReader()
        {
            // Create a new evaluator to evaluate the delayedValue expressions of annotations from the edm model
            _edmEvaluator = new EdmExpressionEvaluator(new Dictionary <IEdmOperation, Func <IEdmValue[], IEdmValue> >());
            _registeredVocabularyTypes = new Dictionary <string, Dictionary <string, IEdmSchemaElement> >();
            IEdmModel _capabilitiesModel;

            // TODO: As above, Extend / modify this to more clearly support custom annotation registration.
            // Tracked by https://github.com/Microsoft/vipr/issues/59
            IEnumerable <EdmError> errors;

            if (!CsdlReader.TryParse(new[] { XmlReader.Create(new StringReader(Vipr.Reader.OData.v4.Properties.Resources.CapabilitiesVocabularies)) }, out _capabilitiesModel, out errors))
            {
                throw new InvalidOperationException("Could not load capabilities vocabulary from resources");
            }

            // Store each type in the dictionary for easier access
            foreach (var element in _capabilitiesModel.SchemaElements)
            {
                if (!_registeredVocabularyTypes.ContainsKey(element.Namespace))
                {
                    _registeredVocabularyTypes.Add(element.Namespace, new Dictionary <string, IEdmSchemaElement>());
                }

                _registeredVocabularyTypes[element.Namespace].Add(element.Name, element);
            }
        }
示例#3
0
        public void Term_Constant_OnBaseEntityTypesWithMultipleVocabularyAnnotation()
        {
            this.SetupModelsAndValues();

            const string applicationCsdl =
                @"<Schema Namespace=""Annotations.Application"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <Annotations Target=""foo.Person"">
        <Annotation Term=""bar.Int32Value"" Qualifier=""q1.q1"" Int=""100"" />
        <Annotation Term=""bar.Int32Value"" Qualifier=""q2.q2"" Int=""200"" />
    </Annotations>
</Schema>";
            IEdmModel applicationModel = this.Parse(applicationCsdl, this.baseModel, this.vocabularyDefinitionModel);
            EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.operationsLookup);

            IEdmTerm termInt32Value = this.vocabularyDefinitionModel.FindTerm("bar.Int32Value");

            IEdmValue annotationOnPerson = applicationModel.GetTermValue(this.personValue, termInt32Value, "q1.q1", expressionEvaluator);

            Assert.AreEqual(100, ((IEdmIntegerValue)annotationOnPerson).Value, "Term annotation value is wrong.");

            annotationOnPerson = applicationModel.GetTermValue(this.personValue, termInt32Value, "q2.q2", expressionEvaluator);
            Assert.AreEqual(200, ((IEdmIntegerValue)annotationOnPerson).Value, "Term annotation value is wrong.");

            IEdmValue annotationInherited = applicationModel.GetTermValue(this.professionalValue, termInt32Value, "q1.q1", expressionEvaluator);

            Assert.AreEqual(100, ((IEdmIntegerValue)annotationInherited).Value, "Term annotation value is wrong.");

            annotationInherited = applicationModel.GetTermValue(this.professionalValue, termInt32Value, "q2.q2", expressionEvaluator);
            Assert.AreEqual(200, ((IEdmIntegerValue)annotationInherited).Value, "Term annotation value is wrong.");
        }
示例#4
0
        public void Term_Constant_BinaryWithOddDigits()
        {
            this.SetupModelsAndValues();

            const string applicationCsdl =
                @"<Schema Namespace=""Annotations"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <Annotations Target=""foo.Person"">
        <Annotation Term=""bar.BinaryValue"" Binary=""123"" />
    </Annotations>
</Schema>";
            IEdmModel applicationModel = this.Parse(applicationCsdl, this.baseModel, this.vocabularyDefinitionModel);
            EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.operationsLookup);

            IEdmTerm  term;
            IEdmValue annotationValue;

            term            = this.vocabularyDefinitionModel.FindTerm("bar.BinaryValue");
            annotationValue = applicationModel.GetTermValue(this.personValue, term, expressionEvaluator);
            byte[] v = ((IEdmBinaryConstantExpression)annotationValue).Value;
            Assert.AreEqual(0, v.Length, "Binary value length");

            Assert.IsTrue(((IEdmBinaryConstantExpression)annotationValue).IsBad(), "annotation has errors");

            var expectedErrors = new EdmLibTestErrors()
            {
                { null, null, EdmErrorCode.InvalidBinary },
            };

            this.VerifySemanticValidation(applicationModel, EdmVersion.V40, expectedErrors);
        }
示例#5
0
        public void Term_Constant_OnDerivedEntityType()
        {
            this.SetupModelsAndValues();

            const string applicationCsdl =
                @"<Schema Namespace=""Annotations"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <Annotations Target=""foo.Person"">
        <Annotation Term=""bar.Int32Value"" Qualifier=""onPerson"" Int=""100"" />
    </Annotations>
    <Annotations Target=""foo.Professional"">
        <Annotation Term=""bar.Int32Value"" Qualifier=""onProfessional"" Int=""999"" />
    </Annotations>
</Schema>";
            IEdmModel applicationModel = this.Parse(applicationCsdl, this.baseModel, this.vocabularyDefinitionModel);
            EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.operationsLookup);

            IEdmTerm termInt32Value = this.vocabularyDefinitionModel.FindTerm("bar.Int32Value");

            IEdmValue annotationOnPerson = applicationModel.GetTermValue(this.personValue, termInt32Value, expressionEvaluator);

            Assert.AreEqual(100, ((IEdmIntegerValue)annotationOnPerson).Value, "Term annotation value");

            IEdmValue annotationOnProfessional = applicationModel.GetTermValue(this.professionalValue, termInt32Value, "onProfessional", expressionEvaluator);

            Assert.AreEqual(999, ((IEdmIntegerValue)annotationOnProfessional).Value, "Term annotation value");
        }
示例#6
0
        public void Term_Path_OnProperty()
        {
            this.SetupModelsAndValues();

            const string applicationCsdl =
                @"<Schema Namespace=""Annotations"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <Annotations Target=""foo.Person/ContactInfo"">
        <Annotation Term=""bar.Int16Value"" Qualifier=""WorkPhoneLocal"" Path=""WorkPhone/Local"" />
    </Annotations>
</Schema>";
            IEdmModel applicationModel = this.Parse(applicationCsdl, this.baseModel, this.vocabularyDefinitionModel);
            EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.operationsLookup);

            IEdmEntityType    person               = this.baseModel.FindEntityType("foo.Person");
            IEdmProperty      property             = person.FindProperty("ContactInfo");
            IEdmPropertyValue contextPropertyValue = ((IEdmStructuredValue)this.personValue).FindPropertyValue("ContactInfo");

            IEdmTerm termInt16Value = this.vocabularyDefinitionModel.FindTerm("bar.Int16Value");
            // ?? Assumes Entity always??
            // IEdmValue annotationHotIndex = applicationModel.GetTermValue(contextPropertyValue.Value, termInt16Value, evaluator);
            IEdmVocabularyAnnotation annotation = property.VocabularyAnnotations(applicationModel).SingleOrDefault(a => a.Term == termInt16Value);
            IEdmExpression           expression = annotation.Value;
            IEdmValue annotationWorkphoneLocal  = expressionEvaluator.Evaluate(expression, (IEdmStructuredValue)contextPropertyValue.Value);

            Assert.AreEqual(9991234, ((IEdmIntegerValue)annotationWorkphoneLocal).Value, "Term annotation value");
        }
示例#7
0
        public void Term_Path_OnEntityType()
        {
            this.SetupModelsAndValues();

            const string applicationCsdl =
                @"<Schema Namespace=""Annotations"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <Annotations Target=""foo.Person"">
        <Annotation Term=""bar.Int32Value"" Qualifier=""CoolnessIndex"" Path=""CoolnessIndex"" />
        <Annotation Term=""bar.Int64Value"" Qualifier=""WorkPhoneLocal"" Path=""ContactInfo/WorkPhone/Local"" />
        <Annotation Term=""bar.StringValue"" Qualifier=""ZipMain"" Path=""Address/Zip/Main"" />
    </Annotations>
</Schema>";
            IEdmModel applicationModel = this.Parse(applicationCsdl, this.baseModel, this.vocabularyDefinitionModel);
            EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.operationsLookup);

            IEdmTerm  termInt32Value          = this.vocabularyDefinitionModel.FindTerm("bar.Int32Value");
            IEdmValue annotationCoolnessIndex = applicationModel.GetTermValue(this.personValue, termInt32Value, expressionEvaluator);

            Assert.AreEqual(Int32.MaxValue, ((IEdmIntegerValue)annotationCoolnessIndex).Value, "Term annotation value");

            IEdmTerm  termStringValue   = this.vocabularyDefinitionModel.FindTerm("bar.StringValue");
            IEdmValue annotationZipMain = applicationModel.GetTermValue(this.personValue, termStringValue, expressionEvaluator);

            Assert.AreEqual("98052", ((IEdmStringValue)annotationZipMain).Value, "Term annotation value");

            IEdmTerm  termInt64Value           = this.vocabularyDefinitionModel.FindTerm("bar.Int64Value");
            IEdmValue annotationWorkPhoneLocal = applicationModel.GetTermValue(this.personValue, termInt64Value, expressionEvaluator);

            Assert.AreEqual(9991234, ((IEdmIntegerValue)annotationWorkPhoneLocal).Value, "Term annotation value");
        }
示例#8
0
        public void Term_FunctionApplication_OnNavigationProperty()
        {
            this.SetupModelsAndValues();

            const string applicationCsdl =
                @"<Schema Namespace=""Annotations"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <Annotations Target=""foo.Person/Address"">
        <Annotation Term=""bar.StringValue"">
            <Apply Function=""Functions.StringConcat"">
                <Path>Zip/Main</Path>
                <Apply Function=""Functions.StringConcat"">
                    <String>-</String>
                    <Path>Zip/Extension</Path>
                </Apply>
            </Apply>
        </Annotation>
    </Annotations>
</Schema>";
            IEdmModel applicationModel = this.Parse(applicationCsdl, this.baseModel, this.vocabularyDefinitionModel, this.operationsModel);
            EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.operationsLookup);

            IEdmEntityType    person               = this.baseModel.FindEntityType("foo.Person");
            IEdmProperty      property             = person.FindProperty("Address");
            IEdmPropertyValue contextPropertyValue = ((IEdmStructuredValue)this.personValue).FindPropertyValue("Address");

            IEdmTerm termStringValue = this.vocabularyDefinitionModel.FindTerm("bar.StringValue");
            IEdmVocabularyAnnotation annotationString = property.VocabularyAnnotations(applicationModel).SingleOrDefault(a => a.Term == termStringValue);
            IEdmValue annotationStringValue           = expressionEvaluator.Evaluate(annotationString.Value, (IEdmStructuredValue)contextPropertyValue.Value);

            Assert.AreEqual("98052-0000", ((IEdmStringValue)annotationStringValue).Value, "Term annotation value");
        }
示例#9
0
        public void Term_TypeTerm_If_OnEntityContainer()
        {
            this.SetupModelsAndValues();

            const string applicationCsdl =
                @"<Schema Namespace=""Annotations"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <Annotations Target=""foo.fooContainer"">
        <Annotation Term=""bar.Int32Value"">
            <If>
                <Apply Function=""Functions.True"" />
                <Int>999</Int>
                <Int>30</Int>
            </If>                
        </Annotation>
    </Annotations>
</Schema>";
            IEdmModel applicationModel = this.Parse(applicationCsdl, this.baseModel, this.vocabularyDefinitionModel, this.operationsModel);
            EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.operationsLookup);

            IEdmEntityContainer container = this.baseModel.FindEntityContainer("fooContainer");

            IEdmTerm termInt32Value = this.vocabularyDefinitionModel.FindTerm("bar.Int32Value");
            IEdmVocabularyAnnotation valueAnnotation = applicationModel.FindVocabularyAnnotations <IEdmVocabularyAnnotation>(container, termInt32Value).SingleOrDefault();

            // ?? why do I need a context here?
            IEdmValue valueOfValueAnnotation = expressionEvaluator.Evaluate(valueAnnotation.Value, this.personValue);

            Assert.AreEqual(999, ((IEdmIntegerValue)valueOfValueAnnotation).Value, "Annotation evaluated value.");
        }
示例#10
0
        public void Term_EntityValue_OnEntityType()
        {
            this.SetupModelsAndValues();

            const string applicationCsdl =
                @"<Schema Namespace=""Annotations"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <Annotations Target=""foo.Person"">
        <Annotation Term=""bar.EntityValue"">
            <Record>
                <PropertyValue Property=""Description"" String=""the summary."" />
                <PropertyValue Property=""Age"" Int=""15"" />
            </Record>
        </Annotation>
    </Annotations>
</Schema>";
            IEdmModel applicationModel = this.Parse(applicationCsdl, this.baseModel, this.vocabularyDefinitionModel, this.operationsModel);
            EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.operationsLookup);

            IEdmTerm  termEntityValue       = this.vocabularyDefinitionModel.FindTerm("bar.EntityValue");
            IEdmValue annotationEntityValue = applicationModel.GetTermValue(this.personValue, termEntityValue, expressionEvaluator);

            var structuredValue = (IEdmStructuredValue)annotationEntityValue;

            Assert.IsNotNull(structuredValue, "value should be StructuredValue.");
            Assert.AreEqual(2, structuredValue.PropertyValues.Count(), "StructuredValue property count.");
            Assert.AreEqual("Description", structuredValue.PropertyValues.ElementAt(1).Name, "StructuredValue property Name.");
            Assert.AreEqual("the summary.", ((IEdmStringValue)structuredValue.PropertyValues.ElementAt(1).Value).Value, "StructuredValue property Value.");

            var entityReference = structuredValue.Type.AsEntity();

            Assert.IsNotNull(entityReference, "StructuredValue.Type should be entity");
            Assert.AreSame(this.vocabularyDefinitionModel.FindType("bar.MoreTransformedPerson"), entityReference.EntityDefinition(), "StructuredValue.Type EntityDefinition");
        }
示例#11
0
        public void Term_FunctionApplication_WithOverloads_OnEntitySet()
        {
            this.SetupModelsAndValues();

            const string applicationCsdl =
                @"<Schema Namespace=""Annotations"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <Annotations Target=""foo.fooContainer/PersonSet"">
        <Annotation Term=""bar.StringValue"">
            <If>
                <Apply Function=""Functions.True"" />
                <Apply Function=""Functions.StringConcat"">
                    <String>1</String>
                    <String>2</String>
                    <String>3</String>
                </Apply>
                <String>foo</String>
            </If>                
        </Annotation>
    </Annotations>
</Schema>";
            IEdmModel applicationModel = this.Parse(applicationCsdl, this.baseModel, this.vocabularyDefinitionModel, this.operationsModel);
            EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.operationsLookup);

            IEdmEntitySet personSet = this.baseModel.FindEntityContainer("fooContainer").FindEntitySet("PersonSet");

            IEdmTerm term = this.vocabularyDefinitionModel.FindTerm("bar.StringValue");
            IEdmVocabularyAnnotation valueAnnotation = applicationModel.FindVocabularyAnnotations <IEdmVocabularyAnnotation>(personSet, term).SingleOrDefault();

            IEdmValue valueOfAnnotation = expressionEvaluator.Evaluate(valueAnnotation.Value, this.personValue);

            Assert.AreEqual("123", ((IEdmStringValue)valueOfAnnotation).Value, "Annotation evaluated value.");
        }
示例#12
0
        private void VerifyExpressionEdmValueKind(IEdmExpression expression, EdmValueKind expressionKind)
        {
            Dictionary <IEdmOperation, Func <IEdmValue[], IEdmValue> > builtInFunctions = new Dictionary <IEdmOperation, Func <IEdmValue[], IEdmValue> >();
            var evaluator = new EdmExpressionEvaluator(builtInFunctions);

            var expressionValue = evaluator.Evaluate(expression) as IEdmValue;

            Assert.IsNotNull(expressionValue, "Invalid expression value.");
            Assert.AreEqual(expressionKind, expressionValue.ValueKind, "Invalid expression value kind.");
        }
示例#13
0
        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");
        }
示例#14
0
        //[TestMethod, Variation(Id = 202, SkipReason = @"[EdmLib] Evaluator extension method assumes value is always EntityValue  -- postponed")]
        public void GetTermValueWithNonEntityTest()
        {
            const string annotatingModelCsdl =
                @"<Schema Namespace=""foo"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <Term Name=""DistantAge"" Type=""Int32"" />
</Schema>";
            var aValue = new EdmStructuredValue(null, new IEdmPropertyValue[]
            {
                new EdmPropertyValue("P1", new EdmStringConstant("aStringValueOne")),
                new EdmPropertyValue("P2", new EdmStringConstant("aStringValueTwo")),
            });
            var edmModel  = this.GetParserResult(new XElement[] { XElement.Parse(annotatingModelCsdl) });
            var evaluator = new EdmExpressionEvaluator(null);

            edmModel.GetTermValue(aValue, edmModel.FindTerm("foo.DistantAge"), evaluator);
        }
示例#15
0
        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 ValueTerm_EntityValue_OnEntityType()
        {
            this.SetupModelsAndValues();

            const string applicationCsdl =
@"<Schema Namespace=""Annotations"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <Annotations Target=""foo.Person"">
        <Annotation Term=""bar.EntityValue"">
            <Record>
                <PropertyValue Property=""Description"" String=""the summary."" />
                <PropertyValue Property=""Age"" Int=""15"" />
            </Record>
        </Annotation>
    </Annotations>
</Schema>";
            IEdmModel applicationModel = this.Parse(applicationCsdl, this.baseModel, this.vocabularyDefinitionModel, this.operationsModel);
            EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.operationsLookup);

            IEdmValueTerm termEntityValue = this.vocabularyDefinitionModel.FindValueTerm("bar.EntityValue");
            IEdmValue annotationEntityValue = applicationModel.GetTermValue(this.personValue, termEntityValue, expressionEvaluator);

            var structuredValue = (IEdmStructuredValue)annotationEntityValue;
            Assert.IsNotNull(structuredValue, "value should be StructuredValue.");
            Assert.AreEqual(2, structuredValue.PropertyValues.Count(), "StructuredValue property count.");
            Assert.AreEqual("Description", structuredValue.PropertyValues.ElementAt(1).Name, "StructuredValue property Name.");
            Assert.AreEqual("the summary.", ((IEdmStringValue)structuredValue.PropertyValues.ElementAt(1).Value).Value, "StructuredValue property Value.");

            var entityReference = structuredValue.Type.AsEntity();
            Assert.IsNotNull(entityReference, "StructuredValue.Type should be entity");
            Assert.AreSame(this.vocabularyDefinitionModel.FindType("bar.MoreTransformedPerson"), entityReference.EntityDefinition(), "StructuredValue.Type EntityDefinition");
        }
示例#17
0
 public void EvaluateEnumExpressionOfMultiValues()
 {
     var color = new EdmEnumType("Ns", "Color", true);
     var blue = color.AddMember("Blue", new EdmIntegerConstant(1));
     var white = color.AddMember("White", new EdmIntegerConstant(2));
     var enumExpression = new EdmEnumMemberExpression(blue, white);
     EdmExpressionEvaluator evaluator = new EdmExpressionEvaluator(null);
     var value = evaluator.Evaluate(enumExpression) as IEdmEnumValue;
     value.Type.Definition.Should().Be(color);
     (value.Value as EdmIntegerConstant).Value.Should().Be(3);
 }
示例#18
0
        /// <summary>
        /// Gets the <see cref="IEdmValue"/> of a property of a term type that has been applied to the type of a value.
        /// </summary>
        /// <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="expressionEvaluator">Evaluator to use to perform expression evaluation.</param>
        /// <returns>Value of the property evaluated against the supplied value, or null if no relevant annotation exists.</returns>
        public static IEdmValue GetPropertyValue(this IEdmModel model, IEdmStructuredValue context, IEdmValueTerm term, IEdmProperty property, string qualifier, EdmExpressionEvaluator expressionEvaluator)
        {
            ExceptionUtilities.CheckArgumentNotNull(model, "model");
            ExceptionUtilities.CheckArgumentNotNull(context, "context");
            ExceptionUtilities.CheckArgumentNotNull(property, "property");
            ExceptionUtilities.CheckArgumentNotNull(expressionEvaluator, "expressionEvaluator");

            return(GetPropertyValue <IEdmValue>(model, context, context.Type.AsEntity().EntityDefinition(), term, property, qualifier, expressionEvaluator.Evaluate));
        }
        //[TestMethod, Variation(Id = 202, SkipReason = @"[EdmLib] Evaluator extension method assumes value is always EntityValue  -- postponed")]
        public void GetTermValueWithNonEntityTest()
        {
            const string annotatingModelCsdl =
@"<Schema Namespace=""foo"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <Term Name=""DistantAge"" Type=""Int32"" />
</Schema>";
            var aValue = new EdmStructuredValue(null, new IEdmPropertyValue[]
            {
                new EdmPropertyValue("P1", new EdmStringConstant("aStringValueOne")),
                new EdmPropertyValue("P2", new EdmStringConstant("aStringValueTwo")),
            });
            var edmModel = this.GetParserResult(new XElement[] { XElement.Parse(annotatingModelCsdl) });
            var evaluator = new EdmExpressionEvaluator(null);
            edmModel.GetTermValue(aValue, edmModel.FindValueTerm("foo.DistantAge"), evaluator);
        }
示例#20
0
        public void EvaluateGraphConstructionWithoutCollection()
        {
            const string annotatingModelCsdl =
@"<Schema Namespace=""foo"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <Annotations Target=""foo.Person"">
        <Annotation Term=""foo.FredFlintstone"">
            <LabeledElement Name=""Fred"">
                <Record>
                    <PropertyValue Property=""Name"" String=""Fred Flintstone"" />
                    <PropertyValue Property=""Partner"">
                        <LabeledElement Name=""Wilma"">
                            <Record>
                                <PropertyValue Property=""Name"" String=""Wilma Flintstone"" />
                                <PropertyValue Property=""Partner"">
                                    <LabeledElementReference Name=""Fred"" />
                                </PropertyValue>
                                <PropertyValue Property=""Children"">
                                    <Collection>
                                        <LabeledElementReference Name=""Pebbles"" />
                                    </Collection>
                                </PropertyValue>
                                </Record>
                        </LabeledElement>
                    </PropertyValue>
                    <PropertyValue Property=""Children"">
                        <Collection>
                            <LabeledElement Name=""Pebbles"">
                                <Record>
                                    <PropertyValue Property=""Name"" String=""Pebbles Flintstone"" />
                                    <PropertyValue Property=""Parents"">
                                        <Collection>
                                            <LabeledElementReference Name=""Fred"" />
                                            <LabeledElementReference Name=""Wilma"" />
                                        </Collection>
                                    </PropertyValue>
                                </Record>
                            </LabeledElement>
                        </Collection>
                    </PropertyValue>
                </Record>
            </LabeledElement>
        </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);
            
            IEdmValueTerm undefined = (IEdmValueTerm)person.VocabularyAnnotations(annotatingModel).ElementAt(0).Term;
            IEdmStructuredValue fred = (IEdmStructuredValue)annotatingModel.GetTermValue(this.personValue, undefined, expressionEvaluator);

            IEdmStructuredValue wilma = (IEdmStructuredValue)fred.FindPropertyValue("Partner").Value;
            IEdmPropertyValue wilmaPartner = wilma.FindPropertyValue("Partner");

            Assert.IsNotNull(wilmaPartner, "Partner property");
            Assert.AreSame(fred, wilmaPartner.Value, "Graph reference identity");
            
            IEdmValue fredChild = ((IEdmCollectionValue)(fred.FindPropertyValue("Children").Value)).Elements.First().Value;
            IEdmValue wilmaChild = ((IEdmCollectionValue)(wilma.FindPropertyValue("Children").Value)).Elements.First().Value;
           
            Assert.AreSame(fredChild, wilmaChild, "Graph reference identity");

            IEdmCollectionValue pebblesParents = (IEdmCollectionValue)((IEdmStructuredValue)fredChild).FindPropertyValue("Parents").Value;
            Assert.AreSame(fred, pebblesParents.Elements.ElementAt(0).Value, "Graph reference identity");
            Assert.AreSame(wilma, pebblesParents.Elements.ElementAt(1).Value, "Graph reference identity");
        }
示例#21
0
        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");
        }
示例#22
0
        public void EvaluateExpressionsWithoutContext()
        {
            EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.builtInFunctions);

            IEdmExpression integerExpression = new EdmIntegerConstant(44);
            IEdmExpression stringExpression = new EdmStringConstant("Yow!");

            IEdmValue integerValue = expressionEvaluator.Evaluate(integerExpression, null);
            IEdmValue stringValue = expressionEvaluator.Evaluate(stringExpression);

            Assert.AreEqual(44, ((IEdmIntegerValue)integerValue).Value, "Integer constant value");
            Assert.AreEqual("Yow!", ((IEdmStringValue)stringValue).Value, "String constant value");
        }
示例#23
0
        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");
        }
示例#24
0
        //[TestMethod, Variation(Id = 102, SkipReason=@"[EdmLib] EdmExpressionEvaluator throws NullReferenceException when EdmStructureValue has null properties. -- postponed")]
        public void EvaluateExpressionsNullPropertiesTest()
        {
            var aValue = new EdmStructuredValue(null, null);
            EdmExpressionEvaluator evaluator = new EdmExpressionEvaluator(null);
            var path = new EdmPathExpression();
            this.VerifyThrowsException(typeof(ArgumentNullException), () => evaluator.Evaluate(path, aValue));

            path = new EdmPathExpression("path");
            this.VerifyThrowsException(typeof(ArgumentNullException), () => evaluator.Evaluate(path, aValue)); // System.NullReferenceException
        }
示例#25
0
        public void EvaluateExpressionsDuplicatePropertiesTest()
        {
            var aValue = new EdmStructuredValue(null, new IEdmPropertyValue[]
            {
                new EdmPropertyValue("P1", new EdmStringConstant("aStringValueOne")),
                new EdmPropertyValue("P1", new EdmStringConstant("aStringValueTwo")),
            });
            var evaluator = new EdmExpressionEvaluator(null);
            var pathP1 = new EdmPathExpression("P1");
            Assert.AreEqual("aStringValueOne", ((IEdmStringConstantExpression)evaluator.Evaluate(pathP1, aValue)).Value, "The evaluated value is not correct.");

            aValue = new EdmStructuredValue(null, new IEdmPropertyValue[]
            {
                new EdmPropertyValue("", new EdmStringConstant("aStringValueOne")),
                new EdmPropertyValue("", new EdmStringConstant("aStringValueTwo")),
            });
            var pathEmpty = new EdmPathExpression("");
            Assert.AreEqual("aStringValueOne", ((IEdmStringConstantExpression)evaluator.Evaluate(pathEmpty, aValue)).Value, "The evaluated value is not correct.");
        }
示例#26
0
 public void EvaluateEnumReferenceExpression()
 {
     var color = new EdmEnumType("Ns", "Color", true);
     var blue = color.AddMember("Blue", new EdmIntegerConstant(1));
     color.AddMember("White", new EdmIntegerConstant(2));
     var enumReferenceExpression = new EdmEnumMemberReferenceExpression(blue);
     EdmExpressionEvaluator evaluator = new EdmExpressionEvaluator(null);
     var value = evaluator.Evaluate(enumReferenceExpression) as IEdmEnumValue;
     value.Type.Definition.Should().Be(color);
     value.Value.As<EdmIntegerConstant>().Value.Should().Be(1);
 }
示例#27
0
 public void EvaluateEnumExpressionOfMultiValuesWithStringUnderlyingTypeShouldThrow()
 {
     var color = new EdmEnumType("Ns", "Color", EdmPrimitiveTypeKind.String, true);
     var blue = color.AddMember("Blue", new EdmStringConstant("1"));
     var white = color.AddMember("White", new EdmStringConstant("2"));
     var enumExpression = new EdmEnumMemberExpression(blue, white);
     EdmExpressionEvaluator evaluator = new EdmExpressionEvaluator(null);
     Action action = () => evaluator.Evaluate(enumExpression);
     action.ShouldThrow<InvalidOperationException>().WithMessage("Type Ns.Color cannot be assigned with multi-values.");
 }
示例#28
0
 public void EvaluateEnumExpressionOfMultiValuesWithoutFlagsShouldThrow()
 {
     var color = new EdmEnumType("Ns", "Color");
     var blue = color.AddMember("Blue", new EdmIntegerConstant(1));
     var white = color.AddMember("White", new EdmIntegerConstant(2));
     var enumExpression = new EdmEnumMemberExpression(blue, white);
     EdmExpressionEvaluator evaluator = new EdmExpressionEvaluator(null);
     Action action = () => evaluator.Evaluate(enumExpression);
     action.ShouldThrow<InvalidOperationException>().WithMessage("Type Ns.Color cannot be assigned with multi-values.");
 }
示例#29
0
        public void EvaluateCastExpressions()
        {
            const string annotatingModelCsdl =
@"<Schema Namespace=""foo"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <Annotations Target=""foo.Person"">
        <Annotation Term=""foo.Punning0"">
            <Cast Type=""Int32"">
                <Path>Living</Path>
            </Cast>
        </Annotation>
        <Annotation Term=""foo.Punning1"">
            <Cast Type=""Byte"">
                <Int>256</Int>
            </Cast>
        </Annotation>
        <Annotation Term=""foo.Punning2"">
            <Cast Type=""SByte"">
                <Int>-129</Int>
            </Cast>
        </Annotation>
        <Annotation Term=""foo.Punning3"">
            <Cast Type=""Int16"">
                <Int>32768</Int>
            </Cast>
        </Annotation>
        <Annotation Term=""foo.Punning4"">
            <Cast Type=""Int32"">
                <Float>1.1</Float>
            </Cast>
        </Annotation>
        <Annotation Term=""foo.Punning5"">
            <Cast Type=""Collection(Int32)"">
                <Collection>
                    <Int>1</Int>
                    <String>2</String>
                    <Int>3</Int>
                    <Float>4.1</Float>
                </Collection>
            </Cast>
        </Annotation>
        <Annotation Term=""foo.Punning6"">
            <Cast Type=""String"" MaxLength=""5"">
                <String>123456</String>
            </Cast>
        </Annotation>
        <Annotation Term=""foo.Punning7"">
            <Cast Type=""String"" Nullable=""False"">
                <Null />
            </Cast>
        </Annotation>
        <Annotation Term=""foo.Punning8"">
            <Cast 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>
            </Cast>
        </Annotation>
        <Annotation Term=""foo.Punning9"">
            <Cast 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>
            </Cast>
        </Annotation>
        <Annotation Term=""foo.Punning10"">
            <Cast Type=""foo.RealSponsor"">
                <Cast Type=""foo.Sponsor"">
                    <Record Type=""foo.FakeSponsor"">
                        <PropertyValue Property=""Name"" String=""Nomad"" />
                        <PropertyValue Property=""Secret"" String=""Ariel"" />
                    </Record>
                </Cast>
            </Cast>
        </Annotation>
        <Annotation Term=""foo.Punning11"">
            <Cast Type=""foo.Pet"">
                <Record>
                    <PropertyValue Property=""Name"">
                        <Null />
                    </PropertyValue>
                    <PropertyValue Property=""Age"">
                        <Null />
                    </PropertyValue>
                    <PropertyValue Property=""Toys"">
                        <Collection>
                            <Null />
                        </Collection>
                    </PropertyValue>
                </Record>
            </Cast>
        </Annotation>
        <Annotation Term=""foo.Clear0"">
            <Cast Type=""foo.Address"">
                <Path>Address</Path>
            </Cast>
        </Annotation>
        <Annotation Term=""foo.Clear1"">
            <Cast Type=""Byte"">
                <Int>255</Int>
            </Cast>
        </Annotation>
        <Annotation Term=""foo.Clear2"">
            <Cast Type=""SByte"">
                <Int>-128</Int>
            </Cast>
        </Annotation>
        <Annotation Term=""foo.Clear3"">
            <Cast Type=""Int16"">
                <Int>32767</Int>
            </Cast>
        </Annotation>
        <Annotation Term=""foo.Clear4"">
            <Cast Type=""Int32"">
                <Int>32768</Int>
            </Cast>
        </Annotation>
        <Annotation Term=""foo.Clear6"">
            <Cast Type=""String"" MaxLength=""5"">
                <String>12345</String>
            </Cast>
        </Annotation>
        <Annotation Term=""foo.Clear7"">
            <Cast Type=""String"">
                <Null />
            </Cast>
        </Annotation>
        <Annotation Term=""foo.Clear8"">
            <Cast 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>
            </Cast>
        </Annotation>
        <Annotation Term=""foo.Clear10"">
            <Cast Type=""foo.RealSponsor"">
                <Cast Type=""foo.Sponsor"">
                    <Record Type=""foo.RealSponsor"">
                        <PropertyValue Property=""Name"" String=""Nomad"" />
                        <PropertyValue Property=""Secret"" String=""Ariel"" />
                    </Record>
                </Cast>
            </Cast>
        </Annotation>
        <Annotation Term=""foo.Clear11"">
            <Cast Type=""foo.Pet"">
                <Record>
                    <PropertyValue Property=""Name"">
                        <String>foo</String>
                    </PropertyValue>
                    <PropertyValue Property=""Age"">
                        <Null />
                    </PropertyValue>
                    <PropertyValue Property=""Toys"">
                        <Collection>
                            <Null />
                        </Collection>
                    </PropertyValue>
                </Record>
            </Cast>
        </Annotation>
    </Annotations>
</Schema>";

            IEdmModel annotatingModel = this.GetParserResult(new String[] { annotatingModelCsdl }, this.baseModel);
            IEdmEntityType person = (IEdmEntityType)this.baseModel.FindType("foo.Person");
            EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.builtInFunctions);

            this.VerifyThrowsException(typeof(InvalidOperationException), () => annotatingModel.GetTermValue(this.personValue, this.baseModel.FindValueTerm("foo.Punning0"), expressionEvaluator));
            this.VerifyThrowsException(typeof(InvalidOperationException), () => annotatingModel.GetTermValue(this.personValue, this.baseModel.FindValueTerm("foo.Punning1"), expressionEvaluator));
            this.VerifyThrowsException(typeof(InvalidOperationException), () => annotatingModel.GetTermValue(this.personValue, this.baseModel.FindValueTerm("foo.Punning2"), expressionEvaluator));
            this.VerifyThrowsException(typeof(InvalidOperationException), () => annotatingModel.GetTermValue(this.personValue, this.baseModel.FindValueTerm("foo.Punning3"), expressionEvaluator));
            this.VerifyThrowsException(typeof(InvalidOperationException), () => annotatingModel.GetTermValue(this.personValue, this.baseModel.FindValueTerm("foo.Punning4"), expressionEvaluator));
            
            IEdmValue personPunning = annotatingModel.GetTermValue(this.personValue, this.baseModel.FindValueTerm("foo.Punning5"), expressionEvaluator);
            Assert.AreEqual(EdmValueKind.Collection, personPunning.ValueKind, "Cast succeeds");
            bool odd = true;
            int count = 0;
            foreach (IEdmDelayedValue element in ((IEdmCollectionValue)personPunning).Elements)
            {
                count++;
                IEdmValue elementValue;

                if (odd)
                {
                    elementValue = element.Value;
                    Assert.AreEqual(EdmValueKind.Integer, elementValue.ValueKind, "Cast succeeds");
                    Assert.AreEqual(count, ((IEdmIntegerValue)elementValue).Value, "Cast succeeds");
                }
                else
                {
                    this.VerifyThrowsException(typeof(InvalidOperationException), () => elementValue = element.Value);
                }

                odd = !odd;
            }

            this.VerifyThrowsException(typeof(InvalidOperationException), () => annotatingModel.GetTermValue(this.personValue, this.baseModel.FindValueTerm("foo.Punning6"), expressionEvaluator));
            this.VerifyThrowsException(typeof(InvalidOperationException), () => annotatingModel.GetTermValue(this.personValue, this.baseModel.FindValueTerm("foo.Punning7"), expressionEvaluator));
            this.VerifyThrowsException(typeof(InvalidOperationException), () => annotatingModel.GetTermValue(this.personValue, this.baseModel.FindValueTerm("foo.Punning8"), expressionEvaluator));
            this.VerifyThrowsException(typeof(InvalidOperationException), () => annotatingModel.GetTermValue(this.personValue, this.baseModel.FindValueTerm("foo.Punning9"), expressionEvaluator));
            this.VerifyThrowsException(typeof(InvalidOperationException), () => annotatingModel.GetTermValue(this.personValue, this.baseModel.FindValueTerm("foo.Punning10"), expressionEvaluator));
            this.VerifyThrowsException(typeof(InvalidOperationException), () => annotatingModel.GetTermValue(this.personValue, this.baseModel.FindValueTerm("foo.Punning11"), expressionEvaluator));

            IEdmValue personClear = annotatingModel.GetTermValue(this.personValue, this.baseModel.FindValueTerm("foo.Clear0"), expressionEvaluator);
            Assert.AreEqual("Joey Ramone Place", ((IEdmStringValue)((IEdmStructuredValue)personClear).FindPropertyValue("Street").Value).Value, "Cast succeeds");

            personClear = annotatingModel.GetTermValue(this.personValue, this.baseModel.FindValueTerm("foo.Clear1"), expressionEvaluator);
            Assert.AreEqual(255, ((IEdmIntegerValue)personClear).Value, "Cast succeeds");

            personClear = annotatingModel.GetTermValue(this.personValue, this.baseModel.FindValueTerm("foo.Clear2"), expressionEvaluator);
            Assert.AreEqual(-128, ((IEdmIntegerValue)personClear).Value, "Cast succeeds");

            personClear = annotatingModel.GetTermValue(this.personValue, this.baseModel.FindValueTerm("foo.Clear3"), expressionEvaluator);
            Assert.AreEqual(32767, ((IEdmIntegerValue)personClear).Value, "Cast succeeds");

            personClear = annotatingModel.GetTermValue(this.personValue, this.baseModel.FindValueTerm("foo.Clear4"), expressionEvaluator);
            Assert.AreEqual(32768, ((IEdmIntegerValue)personClear).Value, "Cast succeeds");

            personClear = annotatingModel.GetTermValue(this.personValue, this.baseModel.FindValueTerm("foo.Clear6"), expressionEvaluator);
            Assert.AreEqual("12345", ((IEdmStringValue)personClear).Value, "Cast succeeds");

            personClear = annotatingModel.GetTermValue(this.personValue, this.baseModel.FindValueTerm("foo.Clear7"), expressionEvaluator);
            Assert.AreEqual(EdmValueKind.Null, personClear.ValueKind, "Cast succeeds");

            personClear = annotatingModel.GetTermValue(this.personValue, this.baseModel.FindValueTerm("foo.Clear8"), expressionEvaluator);
            Assert.AreEqual(EdmValueKind.Structured, personClear.ValueKind, "Cast succeeds");
            Assert.AreEqual("Rick Dastardly", ((IEdmStringValue)((IEdmStructuredValue)personClear).FindPropertyValue("Lead").Value).Value, "Property value");
            Assert.AreEqual("Muttley", ((IEdmStringValue)((IEdmStructuredValue)personClear).FindPropertyValue("Sidekick").Value).Value, "Property value");

            personClear = annotatingModel.GetTermValue(this.personValue, this.baseModel.FindValueTerm("foo.Clear10"), expressionEvaluator);
            Assert.AreEqual(EdmValueKind.Structured, personClear.ValueKind, "Cast succeeds");
            Assert.AreEqual("Nomad", ((IEdmStringValue)((IEdmStructuredValue)personClear).FindPropertyValue("Name").Value).Value, "Property value");
            Assert.AreEqual("Ariel", ((IEdmStringValue)((IEdmStructuredValue)personClear).FindPropertyValue("Secret").Value).Value, "Property value");

            personClear = annotatingModel.GetTermValue(this.personValue, this.baseModel.FindValueTerm("foo.Clear11"), expressionEvaluator);
            Assert.AreEqual(EdmValueKind.Structured, personClear.ValueKind, "Cast succeeds");
            Assert.AreEqual("foo", ((IEdmStringValue)((IEdmStructuredValue)personClear).FindPropertyValue("Name").Value).Value, "Property value");
            Assert.AreEqual(EdmValueKind.Null, ((IEdmStructuredValue)personClear).FindPropertyValue("Age").Value.ValueKind, "Invalid Value Kind");
            Assert.AreEqual(EdmValueKind.Null, (((IEdmCollectionValue)((IEdmStructuredValue)personClear).FindPropertyValue("Toys").Value).Elements.First().Value).ValueKind, "Invalid Value Kind");
        }
        public void ValueTerm_OperationApplication_WithOverloads_OnEntitySet()
        {
            this.SetupModelsAndValues();

            const string applicationCsdl =
@"<Schema Namespace=""Annotations"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <Annotations Target=""foo.fooContainer/PersonSet"">
        <Annotation Term=""bar.StringValue"">
            <If>
                <Apply Function=""Functions.True"" />
                <Apply Function=""Functions.StringConcat"">
                    <String>1</String>
                    <String>2</String>
                    <String>3</String>
                </Apply>
                <String>foo</String>
            </If>                
        </Annotation>
    </Annotations>
</Schema>";
            IEdmModel applicationModel = this.Parse(applicationCsdl, this.baseModel, this.vocabularyDefinitionModel, this.operationsModel);
            EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.operationsLookup);

            IEdmEntitySet personSet = this.baseModel.FindEntityContainer("fooContainer").FindEntitySet("PersonSet");

            IEdmValueTerm term = this.vocabularyDefinitionModel.FindValueTerm("bar.StringValue");
            IEdmValueAnnotation valueAnnotation = applicationModel.FindVocabularyAnnotations<IEdmValueAnnotation>(personSet, term).SingleOrDefault();

            IEdmValue valueOfAnnotation = expressionEvaluator.Evaluate(valueAnnotation.Value, this.personValue);
            Assert.AreEqual("123", ((IEdmStringValue)valueOfAnnotation).Value, "Annotation evaluated value.");
        }
示例#31
0
        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");
        }
示例#32
0
        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!@#$%^*()_+&amp;"" 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);
        }
示例#33
0
        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");
        }
示例#34
0
        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");
        }
示例#35
0
        public void Term_Constant_AllTypes_OnEntityType()
        {
            this.SetupModelsAndValues();

            // Not handled:
            //      <Annotation Term=""bar.GeographyValue"" Geography=""xxx"" />
            //      <Annotation Term=""bar.GeometryValue"" Geometry=""xxx"" />
            const string applicationCsdl =
                @"<Schema Namespace=""Annotations"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <Annotations Target=""foo.Person"">
        <Annotation Term=""bar.BinaryValue"" Binary=""1234"" />        
        <Annotation Term=""bar.BooleanValue"" Bool=""true"" />
        <Annotation Term=""bar.ByteValue"" Int=""255"" />
        <Annotation Term=""bar.DateValue"" Date=""2014-08-08"" />
        <Annotation Term=""bar.DateTimeOffsetValue"" DateTimeOffset=""2001-10-26T19:32:52+00:00"" />
        <Annotation Term=""bar.DecimalValue"" Decimal=""12.345"" />
        <Annotation Term=""bar.DoubleValue"" Float=""3.1416"" />
        <Annotation Term=""bar.GuidValue"" Guid=""4ae71c81-c21a-40a2-8d53-f1a29ed4a2f2"" />
        <Annotation Term=""bar.Int16Value"" Int=""0"" />
        <Annotation Term=""bar.Int32Value"" Int=""100"" />
        <Annotation Term=""bar.Int64Value"" Int=""9999"" />
        <Annotation Term=""bar.DurationValue"" Duration=""PT1M59S"" />
        <Annotation Term=""bar.SByteValue"" Int=""-128"" />
        <Annotation Term=""bar.SingleValue"" Float=""3.1416E10"" />
        <Annotation Term=""bar.StringValue"" String=""I am a string."" />
        <Annotation Term=""bar.TimeOfDayValue"" TimeOfDay=""1:30:59.123"" />
    </Annotations>
</Schema>";
            IEdmModel applicationModel = this.Parse(applicationCsdl, this.baseModel, this.vocabularyDefinitionModel);
            EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.operationsLookup);

            IEdmTerm  term;
            IEdmValue annotationValue;

            term            = this.vocabularyDefinitionModel.FindTerm("bar.BinaryValue");
            annotationValue = applicationModel.GetTermValue(this.personValue, term, expressionEvaluator);
            byte[] v = ((IEdmBinaryValue)annotationValue).Value;
            Assert.AreEqual(2, v.Length, "Binary value length");
            Assert.AreEqual(0x12, v[0], "Binary value");
            Assert.AreEqual(0x34, v[1], "Binary value");

            term            = this.vocabularyDefinitionModel.FindTerm("bar.BooleanValue");
            annotationValue = applicationModel.GetTermValue(this.personValue, term, expressionEvaluator);
            Assert.AreEqual(true, ((IEdmBooleanValue)annotationValue).Value, "Term annotation value");

            term            = this.vocabularyDefinitionModel.FindTerm("bar.ByteValue");
            annotationValue = applicationModel.GetTermValue(this.personValue, term, expressionEvaluator);
            Assert.AreEqual(255, ((IEdmIntegerValue)annotationValue).Value, "Term annotation value");

            term            = this.vocabularyDefinitionModel.FindTerm("bar.DateValue");
            annotationValue = applicationModel.GetTermValue(this.personValue, term, expressionEvaluator);
            Assert.AreEqual(new Date(2014, 8, 8), ((IEdmDateValue)annotationValue).Value, "Term annotation value");

            term            = this.vocabularyDefinitionModel.FindTerm("bar.DateTimeOffsetValue");
            annotationValue = applicationModel.GetTermValue(this.personValue, term, expressionEvaluator);
            Assert.AreEqual(DateTimeOffset.Parse("2001-10-26T19:32:52+00:00"), ((IEdmDateTimeOffsetValue)annotationValue).Value, "Term annotation value");

            term            = this.vocabularyDefinitionModel.FindTerm("bar.DecimalValue");
            annotationValue = applicationModel.GetTermValue(this.personValue, term, expressionEvaluator);
            Assert.AreEqual(12.345M, ((IEdmDecimalValue)annotationValue).Value, "Term annotation value");

            term            = this.vocabularyDefinitionModel.FindTerm("bar.DoubleValue");
            annotationValue = applicationModel.GetTermValue(this.personValue, term, expressionEvaluator);
            Assert.AreEqual(3.1416, ((IEdmFloatingValue)annotationValue).Value, "Term annotation value");

            term            = this.vocabularyDefinitionModel.FindTerm("bar.GuidValue");
            annotationValue = applicationModel.GetTermValue(this.personValue, term, expressionEvaluator);
            Assert.AreEqual(Guid.Parse("4ae71c81-c21a-40a2-8d53-f1a29ed4a2f2"), ((IEdmGuidValue)annotationValue).Value, "Term annotation value");

            term            = this.vocabularyDefinitionModel.FindTerm("bar.Int16Value");
            annotationValue = applicationModel.GetTermValue(this.personValue, term, expressionEvaluator);
            Assert.AreEqual(0, ((IEdmIntegerValue)annotationValue).Value, "Term annotation value");

            term            = this.vocabularyDefinitionModel.FindTerm("bar.Int32Value");
            annotationValue = applicationModel.GetTermValue(this.personValue, term, expressionEvaluator);
            Assert.AreEqual(100, ((IEdmIntegerValue)annotationValue).Value, "Term annotation value");

            term            = this.vocabularyDefinitionModel.FindTerm("bar.Int64Value");
            annotationValue = applicationModel.GetTermValue(this.personValue, term, expressionEvaluator);
            Assert.AreEqual(9999, ((IEdmIntegerValue)annotationValue).Value, "Term annotation value");

            term            = this.vocabularyDefinitionModel.FindTerm("bar.DurationValue");
            annotationValue = applicationModel.GetTermValue(this.personValue, term, expressionEvaluator);
            Assert.AreEqual(new TimeSpan(0, 1, 59), ((IEdmDurationValue)annotationValue).Value, "Term annotation value");

            term            = this.vocabularyDefinitionModel.FindTerm("bar.SByteValue");
            annotationValue = applicationModel.GetTermValue(this.personValue, term, expressionEvaluator);
            Assert.AreEqual(-128, ((IEdmIntegerValue)annotationValue).Value, "Term annotation value");

            term            = this.vocabularyDefinitionModel.FindTerm("bar.SingleValue");
            annotationValue = applicationModel.GetTermValue(this.personValue, term, expressionEvaluator);
            Assert.AreEqual(3.1416E10, ((IEdmFloatingValue)annotationValue).Value, "Term annotation value");

            term            = this.vocabularyDefinitionModel.FindTerm("bar.StringValue");
            annotationValue = applicationModel.GetTermValue(this.personValue, term, expressionEvaluator);
            Assert.AreEqual("I am a string.", ((IEdmStringValue)annotationValue).Value, "Term annotation value");

            term            = this.vocabularyDefinitionModel.FindTerm("bar.TimeOfDayValue");
            annotationValue = applicationModel.GetTermValue(this.personValue, term, expressionEvaluator);
            Assert.AreEqual(new TimeOfDay(1, 30, 59, 123), ((IEdmTimeOfDayValue)annotationValue).Value, "Term annotation value");
        }
示例#36
0
        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");
        }
示例#37
0
        private void VerifyExpressionEdmValueKind(IEdmExpression expression, EdmValueKind expressionKind)
        {
            Dictionary<IEdmOperation, Func<IEdmValue[], IEdmValue>> builtInFunctions = new Dictionary<IEdmOperation, Func<IEdmValue[], IEdmValue>>();
            var evaluator = new EdmExpressionEvaluator(builtInFunctions);

            var expressionValue = evaluator.Evaluate(expression) as IEdmValue;
            Assert.IsNotNull(expressionValue, "Invalid expression value.");
            Assert.AreEqual(expressionKind, expressionValue.ValueKind, "Invalid expression value kind.");
        }
示例#38
0
        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");
        }
        public void SingleModel_Annotation_OnEntityType()
        {
            const string applicationCsdl =
@"<Schema Namespace=""bar"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <Term Name=""MoreTransformedPersonTerm"" Type=""bar.MoreTransformedPerson"" />
    <Annotations Target=""bar.Person"">
        <Annotation Term=""bar.MoreTransformedPersonTerm"">
            <Record>
                <PropertyValue Property=""Description"" String=""I know it!"" />
            </Record>
        </Annotation>
    </Annotations>
    <EntityType Name=""MoreTransformedPerson"" BaseType=""bar.TransformedPerson"">
        <Property Name=""Description"" Type=""String"" />
    </EntityType>
    <EntityType Name=""TransformedPerson"">
        <Property Name=""Age"" Type=""Int32"" Nullable=""false"" />
    </EntityType>
    <EntityType Name=""Person"">
        <Key>
            <PropertyRef Name=""Name"" />
        </Key>
        <Property Name=""Name"" Type=""String"" Nullable=""false"" />
    </EntityType>
</Schema>";
            IEdmModel applicationModel = this.Parse(applicationCsdl);
            EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(null);

            var term = applicationModel.FindValueTerm("bar.MoreTransformedPersonTerm");
            var property = applicationModel.FindEntityType("bar.MoreTransformedPerson").FindProperty("Description");
            var fakeContextValue = new EdmStructuredValue(new EdmEntityTypeReference(applicationModel.FindEntityType("bar.Person"), true), Enumerable.Empty<IEdmPropertyValue>());
            IEdmValue valueOfAnnotation = applicationModel.GetPropertyValue(fakeContextValue, term, property, expressionEvaluator);

            Assert.AreEqual("I know it!", ((IEdmStringValue)valueOfAnnotation).Value, "Annotation evaluated value.");
        }
示例#40
0
        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 ValueTerm_OperationApplication_OnNavigationProperty()
        {
            this.SetupModelsAndValues();

            const string applicationCsdl =
@"<Schema Namespace=""Annotations"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <Annotations Target=""foo.Person/Address"">
        <Annotation Term=""bar.StringValue"">
            <Apply Function=""Functions.StringConcat"">
                <Path>Zip/Main</Path>
                <Apply Function=""Functions.StringConcat"">
                    <String>-</String>
                    <Path>Zip/Extension</Path>
                </Apply>
            </Apply>
        </Annotation>
    </Annotations>
</Schema>";
            IEdmModel applicationModel = this.Parse(applicationCsdl, this.baseModel, this.vocabularyDefinitionModel, this.operationsModel);
            EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.operationsLookup);

            IEdmEntityType person = this.baseModel.FindEntityType("foo.Person");
            IEdmProperty property = person.FindProperty("Address");
            IEdmPropertyValue contextPropertyValue = ((IEdmStructuredValue)this.personValue).FindPropertyValue("Address");

            IEdmValueTerm termStringValue = this.vocabularyDefinitionModel.FindValueTerm("bar.StringValue");
            IEdmValueAnnotation annotationString = property.VocabularyAnnotations(applicationModel).SingleOrDefault(a => a.Term == termStringValue) as IEdmValueAnnotation;
            IEdmValue annotationStringValue = expressionEvaluator.Evaluate(annotationString.Value, (IEdmStructuredValue)contextPropertyValue.Value);
            Assert.AreEqual("98052-0000", ((IEdmStringValue)annotationStringValue).Value, "Term annotation value");
        }
示例#42
0
        public void EvaluateExpressionsInvalidCasesTest()
        {
            Action<IEnumerable<IEdmPropertyValue>, IEnumerable<IEdmPropertyValue>> compareProperties = (expected, actual) =>
                {
                    Assert.IsTrue(expected == null && actual == null || expected.Count() == actual.Count() && !expected.Except(actual).Any(), "Actual property values are different from the expected ones.");
                };
            var pathInvalid = new EdmPathExpression("DoesNotExist.><#!");
            var pathNoSegment = new EdmPathExpression();
            var pathValid = new EdmPathExpression("P1");
            var pathValidInvalid = new EdmPathExpression("P1", "DoesNotExist.><#!");
            EdmExpressionEvaluator evaluator = new EdmExpressionEvaluator(null);

            IEdmStructuredValue aValue = new EdmStructuredValue(null, new IEdmPropertyValue[]
            {
                new EdmPropertyValue("P1", new EdmStringConstant("aStringValue")),
                new EdmPropertyValue("P3", new EdmIntegerConstant(110))
            });
            compareProperties(aValue.PropertyValues, ((IEdmStructuredValue)evaluator.Evaluate(pathNoSegment, aValue)).PropertyValues);
            this.VerifyThrowsException(typeof(InvalidOperationException), () => evaluator.Evaluate(pathNoSegment));
            this.VerifyThrowsException(typeof(InvalidOperationException), () => evaluator.Evaluate(pathNoSegment, null));
            this.VerifyThrowsException(typeof(ArgumentNullException), () => evaluator.Evaluate(pathNoSegment, aValue, null));
            this.VerifyThrowsException(typeof(InvalidOperationException), () => evaluator.Evaluate(pathInvalid, aValue));
            this.VerifyThrowsException(typeof(InvalidOperationException), () => evaluator.Evaluate(pathValid, aValue, EdmCoreModel.Instance.GetInt32(true)));
            this.VerifyThrowsException(typeof(InvalidOperationException), () => evaluator.Evaluate(pathValidInvalid, aValue));

            aValue = new EdmStructuredValue(null, new IEdmPropertyValue[] { });
            compareProperties(aValue.PropertyValues, ((IEdmStructuredValue)evaluator.Evaluate(pathNoSegment, aValue)).PropertyValues);
            this.VerifyThrowsException(typeof(InvalidOperationException), () => evaluator.Evaluate(pathInvalid, aValue));


            aValue = new MutableStructuredValue();
            compareProperties(aValue.PropertyValues, ((IEdmStructuredValue)evaluator.Evaluate(pathNoSegment, aValue)).PropertyValues);

            aValue = new EdmStructuredValue(null, new IEdmPropertyValue[]
            {
                new EdmPropertyValue("", new EdmStringConstant("aStringValue")),
                new EdmPropertyValue("", new EdmStringConstant("aStringValue")),
            });
            compareProperties(aValue.PropertyValues, ((IEdmStructuredValue)evaluator.Evaluate(pathNoSegment, aValue)).PropertyValues);
            this.VerifyThrowsException(typeof(InvalidOperationException), () => evaluator.Evaluate(pathInvalid, aValue));

            aValue = new EdmStructuredValue(null, new IEdmPropertyValue[]
            {
                new EdmPropertyValue("P1", new EdmStringConstant("aStringValueOne")),
                new EdmPropertyValue("P1", new EdmStringConstant("aStringValueTwo")),
            });
            compareProperties(aValue.PropertyValues, ((IEdmStructuredValue)evaluator.Evaluate(pathNoSegment, aValue)).PropertyValues);
            this.VerifyThrowsException(typeof(InvalidOperationException), () => evaluator.Evaluate(pathInvalid, aValue));

            aValue = new EdmStructuredValue(null, new IEdmPropertyValue[]
            {
                new MutablePropertyValue() { Value = new EdmStringConstant("aStringValueOne") },
                new EdmPropertyValue("P1", new EdmStringConstant("aStringValueTwo")),
            });
            this.VerifyThrowsException(typeof(ArgumentNullException), () => new EdmPathExpression((string)null));
            compareProperties(aValue.PropertyValues, ((IEdmStructuredValue)evaluator.Evaluate(pathNoSegment, aValue)).PropertyValues);
            Assert.AreEqual("aStringValueTwo", ((IEdmStringConstantExpression)evaluator.Evaluate(pathValid, aValue)).Value, "The evaluated value is not correct.");
        }
示例#43
0
        public void EvaluatePathOverValueWithManyProperties()
        {
            List<IEdmPropertyValue> contextValues = new List<IEdmPropertyValue>();
            for (int i = 0; i < 10000; i++)
            {
                contextValues.Add(new EdmPropertyValue("P" + i.ToString(), new EdmIntegerConstant(i)));
            }

            EdmStructuredValue context = new EdmStructuredValue(null, contextValues);

            EdmExpressionEvaluator evaluator = new EdmExpressionEvaluator(this.builtInFunctions);

            EdmPathExpression path = new EdmPathExpression("P0");
            IEdmValue first = evaluator.Evaluate(path, context);
            Assert.AreEqual(0, ((IEdmIntegerValue)first).Value, "Property value");

            path = new EdmPathExpression("P555");
            IEdmValue middle = evaluator.Evaluate(path, context);
            Assert.AreEqual(555, ((IEdmIntegerValue)middle).Value, "Property value");

            path = new EdmPathExpression("P9999");
            IEdmValue last = evaluator.Evaluate(path, context);
            Assert.AreEqual(9999, ((IEdmIntegerValue)last).Value, "Property value");
        }
        public void ValueTerm_TypeTerm_If_OnEntityContainer()
        {
            this.SetupModelsAndValues();

            const string applicationCsdl =
@"<Schema Namespace=""Annotations"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <Annotations Target=""foo.fooContainer"">
        <Annotation Term=""bar.Int32Value"">
            <If>
                <Apply Function=""Functions.True"" />
                <Int>999</Int>
                <Int>30</Int>
            </If>                
        </Annotation>
    </Annotations>
</Schema>";
            IEdmModel applicationModel = this.Parse(applicationCsdl, this.baseModel, this.vocabularyDefinitionModel, this.operationsModel);
            EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.operationsLookup);

            IEdmEntityContainer container = this.baseModel.FindEntityContainer("fooContainer");

            IEdmValueTerm termInt32Value = this.vocabularyDefinitionModel.FindValueTerm("bar.Int32Value");
            IEdmValueAnnotation valueAnnotation = applicationModel.FindVocabularyAnnotations<IEdmValueAnnotation>(container, termInt32Value).SingleOrDefault();

            // ?? why do I need a context here?
            IEdmValue valueOfValueAnnotation = expressionEvaluator.Evaluate(valueAnnotation.Value, this.personValue);
            Assert.AreEqual(999, ((IEdmIntegerValue)valueOfValueAnnotation).Value, "Value annotation evaluated value.");
        }
示例#45
0
        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 ValueTerm_Path_OnProperty()
        {
            this.SetupModelsAndValues();

            const string applicationCsdl =
@"<Schema Namespace=""Annotations"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
    <Annotations Target=""foo.Person/ContactInfo"">
        <Annotation Term=""bar.Int16Value"" Qualifier=""WorkPhoneLocal"" Path=""WorkPhone/Local"" />
    </Annotations>
</Schema>";
            IEdmModel applicationModel = this.Parse(applicationCsdl, this.baseModel, this.vocabularyDefinitionModel);
            EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.operationsLookup);

            IEdmEntityType person = this.baseModel.FindEntityType("foo.Person");
            IEdmProperty property = person.FindProperty("ContactInfo");
            IEdmPropertyValue contextPropertyValue = ((IEdmStructuredValue)this.personValue).FindPropertyValue("ContactInfo");

            IEdmValueTerm termInt16Value = this.vocabularyDefinitionModel.FindValueTerm("bar.Int16Value");
            // ?? Assumes Entity always??
            // IEdmValue annotationHotIndex = applicationModel.GetTermValue(contextPropertyValue.Value, termInt16Value, evaluator);
            IEdmValueAnnotation annotation = property.VocabularyAnnotations(applicationModel).SingleOrDefault(a => a.Term == termInt16Value) as IEdmValueAnnotation;
            IEdmExpression expression = annotation.Value;
            IEdmValue annotationWorkphoneLocal = expressionEvaluator.Evaluate(expression, (IEdmStructuredValue)contextPropertyValue.Value);
            Assert.AreEqual(9991234, ((IEdmIntegerValue)annotationWorkphoneLocal).Value, "Term annotation value");
        }