Example #1
0
        public ODataResourceSetSerializerTests()
        {
            _model       = SerializationTestsHelpers.SimpleCustomerOrderModel();
            _customerSet = _model.EntityContainer.FindEntitySet("Customers");
            IEdmComplexType addressType = _model.SchemaElements.OfType <IEdmComplexType>()
                                          .First(c => c.Name == "Address");

            _model.SetAnnotationValue(_customerSet.EntityType(), new ClrTypeAnnotation(typeof(Customer)));
            _model.SetAnnotationValue(addressType, new ClrTypeAnnotation(typeof(Address)));
            _customers = new[] {
                new Customer()
                {
                    FirstName = "Foo",
                    LastName  = "Bar",
                    ID        = 10,
                },
                new Customer()
                {
                    FirstName = "Foo",
                    LastName  = "Bar",
                    ID        = 42,
                }
            };

            _customersType = _model.GetEdmTypeReference(typeof(Customer[])).AsCollection();
            _addressesType = _model.GetEdmTypeReference(typeof(Address[])).AsCollection();
            _writeContext  = new ODataSerializerContext()
            {
                NavigationSource = _customerSet, Model = _model
            };
            _serializerProvider = ODataSerializerProviderFactory.Create();
        }
        public void Ctor_SetsProperty_SerializerProvider()
        {
            ODataSerializerProvider serializerProvider = ODataSerializerProviderFactory.Create();
            var serializer = new Mock <ODataEdmTypeSerializer>(ODataPayloadKind.Unsupported, serializerProvider).Object;

            Assert.Same(serializerProvider, serializer.SerializerProvider);
        }
Example #3
0
 public ODataResourceSetDeserializerTest()
 {
     _model                = GetEdmModel();
     _customerType         = _model.GetEdmTypeReference(typeof(Customer)).AsEntity();
     _customersType        = new EdmCollectionTypeReference(new EdmCollectionType(_customerType));
     _serializerProvider   = ODataSerializerProviderFactory.Create();
     _deserializerProvider = ODataDeserializerProviderFactory.Create();
 }
Example #4
0
        public ODataCollectionSerializerTests()
        {
            _model      = SerializationTestsHelpers.SimpleCustomerOrderModel();
            _edmIntType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: false);

            _serializerProvider = ODataSerializerProviderFactory.Create();
            _collectionType     = new EdmCollectionTypeReference(new EdmCollectionType(_edmIntType));
            _serializer         = new ODataCollectionSerializer(_serializerProvider);
        }
        public ODataDeltaFeedSerializerTests()
        {
            _model       = SerializationTestsHelpers.SimpleCustomerOrderModel();
            _customerSet = _model.EntityContainer.FindEntitySet("Customers");
            _model.SetAnnotationValue(_customerSet.EntityType(), new ClrTypeAnnotation(typeof(Customer)));
            _path      = new ODataPath(new EntitySetSegment(_customerSet));
            _customers = new[] {
                new Customer()
                {
                    FirstName   = "Foo",
                    LastName    = "Bar",
                    ID          = 10,
                    HomeAddress = new Address()
                    {
                        Street  = "Street",
                        ZipCode = null,
                    }
                },
                new Customer()
                {
                    FirstName = "Foo",
                    LastName  = "Bar",
                    ID        = 42,
                }
            };

            _deltaFeedCustomers = new EdmChangedObjectCollection(_customerSet.EntityType());
            EdmDeltaEntityObject newCustomer = new EdmDeltaEntityObject(_customerSet.EntityType());

            newCustomer.TrySetPropertyValue("ID", 10);
            newCustomer.TrySetPropertyValue("FirstName", "Foo");
            EdmDeltaComplexObject newCustomerAddress = new EdmDeltaComplexObject(_model.FindType("Default.Address") as IEdmComplexType);

            newCustomerAddress.TrySetPropertyValue("Street", "Street");
            newCustomerAddress.TrySetPropertyValue("ZipCode", null);
            newCustomer.TrySetPropertyValue("HomeAddress", newCustomerAddress);
            _deltaFeedCustomers.Add(newCustomer);

            _customersType = _model.GetEdmTypeReference(typeof(Customer[])).AsCollection();

            _writeContext = new ODataSerializerContext()
            {
                NavigationSource = _customerSet, Model = _model, Path = _path
            };
            _serializerProvider = ODataSerializerProviderFactory.Create();
        }
        public void CanSerializerSingleton()
        {
            // Arrange
            const string expect = "{" +
                                  "\"@odata.context\":\"http://localhost/odata/$metadata#Boss\"," +
                                  "\"EmployeeId\":987,\"EmployeeName\":\"John Mountain\"}";

            IEdmModel              model       = GetEdmModel();
            IEdmSingleton          singleton   = model.EntityContainer.FindSingleton("Boss");
            var                    request     = GetRequest(model, singleton);
            ODataSerializerContext readContext = new ODataSerializerContext()
            {
#if NETFX // Url is only in AspNet
                Url = new UrlHelper(request),
#endif
                Path             = request.ODataContext().Path,
                Model            = model,
                NavigationSource = singleton
            };

            ODataSerializerProvider serializerProvider = ODataSerializerProviderFactory.Create();
            EmployeeModel           boss = new EmployeeModel {
                EmployeeId = 987, EmployeeName = "John Mountain"
            };
            MemoryStream bufferedStream = new MemoryStream();

            // Act
            ODataResourceSerializer serializer = new ODataResourceSerializer(serializerProvider);

            serializer.WriteObject(boss, typeof(EmployeeModel), GetODataMessageWriter(model, bufferedStream), readContext);

            // Assert
            string result = Encoding.UTF8.GetString(bufferedStream.ToArray());

            Assert.Equal(expect, result);
        }