public void ApplyODataDerivedComplexValueForBaseComplexTypeProperty()
        {
            TestMaterializerContext context = new TestMaterializerContext();
            ComplexTypeWithChildComplexType complexInstance = new ComplexTypeWithChildComplexType();
            complexInstance.InnerComplexProperty = new DerivedComplexType {DerivedProp = 1};

            //In a true client, a TypeResolver will be used to resolve derived property type.
            context.ResolveTypeForMaterializationOverrideFunc = (Type type, string name) =>
            {
                if (name == "DerivedComplexType")
                {
                    var edmType = context.Model.GetOrCreateEdmType(typeof(DerivedComplexType));
                    return new ClientTypeAnnotation(edmType, typeof(DerivedComplexType), "DerivedComplexType", context.Model);
                }
                else
                {
                    var edmType = context.Model.GetOrCreateEdmType(typeof(ComplexTypeWithChildComplexType));
                    return new ClientTypeAnnotation(edmType, typeof(ComplexTypeWithChildComplexType), "ComplexTypeWithChildComplexType", context.Model);
                }
            };

            IEdmType t = context.Model.GetOrCreateEdmType(typeof(DerivedComplexType));
            ODataProperty property = new ODataProperty() { Name = "InnerComplexProperty", Value = new ODataComplexValue { TypeName = "DerivedComplexType", Properties = new ODataProperty[] { new ODataProperty { Name = "DerivedProp", Value = 1 } } } };

            Action test = () => this.CreatePrimitiveValueMaterializationPolicy(context).ApplyDataValue(context.ResolveTypeForMaterialization(typeof(ComplexTypeWithChildComplexType), null), property, complexInstance);
            test.ShouldNotThrow();
        }
        public void ShortIntegrationTestToValidateEntryShouldBeRead()
        {
            var odataEntry = new ODataEntry() { Id = new Uri("http://services.odata.org/OData/OData.svc/Customers(0)") };
            odataEntry.Properties = new ODataProperty[] { new ODataProperty() { Name = "ID", Value = 0 }, new ODataProperty() { Name = "Description", Value = "Simple Stuff" } };

            var clientEdmModel = new ClientEdmModel(ODataProtocolVersion.V4);
            var context = new DataServiceContext();
            MaterializerEntry.CreateEntry(odataEntry, ODataFormat.Atom, true, clientEdmModel);
            var materializerContext = new TestMaterializerContext() {Model = clientEdmModel, Context = context};
            var adapter = new EntityTrackingAdapter(new TestEntityTracker(), MergeOption.OverwriteChanges, clientEdmModel, context);
            QueryComponents components = new QueryComponents(new Uri("http://foo.com/Service"), new Version(4, 0), typeof(Customer), null, new Dictionary<Expression, Expression>());

            var entriesMaterializer = new ODataEntriesEntityMaterializer(new ODataEntry[] { odataEntry }, materializerContext, adapter, components, typeof(Customer), null, ODataFormat.Atom);
            
            var customersRead = new List<Customer>();

            // This line will call ODataEntityMaterializer.ReadImplementation() which will reconstruct the entity, and will get non-public setter called.
            while (entriesMaterializer.Read())
            {
                customersRead.Add(entriesMaterializer.CurrentValue as Customer);
            }

            customersRead.Should().HaveCount(1);
            customersRead[0].ID.Should().Be(0);
            customersRead[0].Description.Should().Be("Simple Stuff");
        }
        public void ApplyNonExistantPropertyWithIgnoreMissingPropertiesShouldNotError()
        {
            TestMaterializerContext context = new TestMaterializerContext() {IgnoreMissingProperties = true};
            CollectionValueMaterializationPolicyTests.Point point = new CollectionValueMaterializationPolicyTests.Point();
            ODataProperty property = new ODataProperty() {Name = "Z", Value = 10};

            this.CreatePrimitiveValueMaterializationPolicy(context).ApplyDataValue(context.ResolveTypeForMaterialization(typeof(CollectionValueMaterializationPolicyTests.Point), null), property, point);
        }
        public void ApplyODataComplexValueForCollectionPropertyShouldError()
        {
            TestMaterializerContext context = new TestMaterializerContext();
            ComplexTypeWithPrimitiveCollection complexInstance = new ComplexTypeWithPrimitiveCollection();
            ODataProperty property = new ODataProperty() { Name = "Strings", Value = new ODataComplexValue() };

            Action test = () => this.CreatePrimitiveValueMaterializationPolicy(context).ApplyDataValue(context.ResolveTypeForMaterialization(typeof(ComplexTypeWithPrimitiveCollection), null), property, complexInstance);
            test.ShouldThrow<InvalidOperationException>().WithMessage(DSClient.Strings.AtomMaterializer_InvalidCollectionItem(property.Name));
        }
        internal ComplexValueMaterializationPolicy CreatePrimitiveValueMaterializationPolicy(TestMaterializerContext context)
        {
            var lazyPrimitivePropertyConverter = new DSClient.SimpleLazy<PrimitivePropertyConverter>(() => new PrimitivePropertyConverter(ODataFormat.Json));
            var primitiveValueMaterializerPolicy = new PrimitiveValueMaterializationPolicy(context, lazyPrimitivePropertyConverter);
            var complexPolicy = new ComplexValueMaterializationPolicy(context, lazyPrimitivePropertyConverter);
            var collectionPolicy = new CollectionValueMaterializationPolicy(context, primitiveValueMaterializerPolicy);
            var intanceAnnotationPolicy = new InstanceAnnotationMaterializationPolicy(context);

            collectionPolicy.ComplexValueMaterializationPolicy = complexPolicy;
            complexPolicy.CollectionValueMaterializationPolicy = collectionPolicy;
            complexPolicy.InstanceAnnotationMaterializationPolicy = intanceAnnotationPolicy;

            return complexPolicy;
        }
 internal ComplexValueMaterializationPolicy CreatePrimitiveValueMaterializationPolicy()
 {
     TestMaterializerContext context = new TestMaterializerContext() { Context = new DSClient.DataServiceContext() };
     return CreatePrimitiveValueMaterializationPolicy(context);
 }
        public void NullValueShouldBeAppliedToSubComplexValueProperty()
        {
            TestMaterializerContext context = new TestMaterializerContext();
            ComplexTypeWithChildComplexType complexInstance = new ComplexTypeWithChildComplexType();
            complexInstance.InnerComplexProperty = new ChildComplexType();
            ODataProperty property = new ODataProperty() { Name = "InnerComplexProperty", Value = null };

            this.CreatePrimitiveValueMaterializationPolicy(context).ApplyDataValue(context.ResolveTypeForMaterialization(typeof(ComplexTypeWithChildComplexType), null), property, complexInstance);
            Assert.IsNull(complexInstance.InnerComplexProperty);
        }
        public void ValueShouldBeAppliedRegardlessIfPropertyStartsNullOrNot()
        {
            foreach (var startingPropertyState in new ChildComplexType[] {null, new ChildComplexType()})
            {
                TestMaterializerContext context = new TestMaterializerContext();
                ComplexTypeWithChildComplexType complexInstance = new ComplexTypeWithChildComplexType();
                complexInstance.InnerComplexProperty = startingPropertyState;
                ODataProperty property = new ODataProperty() {Name = "InnerComplexProperty", Value = new ODataComplexValue() {Properties = new ODataProperty[] {new ODataProperty() {Name = "Prop", Value = 1}}}};

                this.CreatePrimitiveValueMaterializationPolicy(context).ApplyDataValue(context.ResolveTypeForMaterialization(typeof(ComplexTypeWithChildComplexType), null), property, complexInstance);
                complexInstance.InnerComplexProperty.Prop.Should().Be(1);
            }
        }
        public void ApplyODataCollectionValueToNullCollectionProperty()
        {
            TestMaterializerContext context = new TestMaterializerContext();
            ComplexTypeWithPrimitiveCollection complexInstance = new ComplexTypeWithPrimitiveCollection();
            complexInstance.Strings = null;
            ODataProperty property = new ODataProperty() { Name = "Strings", Value = new ODataCollectionValue() { Items = new string[] { "foo" }, TypeName = typeof(ComplexTypeWithPrimitiveCollection).FullName } };

            this.CreatePrimitiveValueMaterializationPolicy(context).ApplyDataValue(context.ResolveTypeForMaterialization(typeof(ComplexTypeWithPrimitiveCollection), null), property, complexInstance);
            complexInstance.Strings.Should().HaveCount(1);
            complexInstance.Strings[0].Should().Be("foo");
        }
        public void ApplyODataCollectionValueToNonNullExistingCollectionProperty()
        {
            TestMaterializerContext context = new TestMaterializerContext();
            ComplexTypeWithPrimitiveCollection complexInstance = new ComplexTypeWithPrimitiveCollection();
            complexInstance.Strings.Add("ShouldBeCleared");
            ODataProperty property = new ODataProperty() { Name = "Strings", Value = new ODataCollectionValue() };

            this.CreatePrimitiveValueMaterializationPolicy(context).ApplyDataValue(context.ResolveTypeForMaterialization(typeof(ComplexTypeWithPrimitiveCollection), null), property, complexInstance);
            complexInstance.Strings.Should().HaveCount(0);
        }