Ejemplo n.º 1
0
        public void WriteToStreamAsyncReturnsODataRepresentation()
        {
            // Arrange
            ODataConventionModelBuilder modelBuilder = new ODataConventionModelBuilder();

            modelBuilder.EntitySet <WorkItem>("WorkItems");
            IEdmModel model = modelBuilder.GetEdmModel();

            HttpRequestMessage request       = new HttpRequestMessage(HttpMethod.Get, "http://localhost/WorkItems(10)");
            HttpConfiguration  configuration = new HttpConfiguration();
            string             routeName     = "Route";

            configuration.MapODataServiceRoute(routeName, null, model);
            request.SetConfiguration(configuration);
            IEdmEntitySet entitySet = model.EntityContainer.EntitySets().Single();

            request.ODataProperties().Path = new ODataPath(new EntitySetSegment(entitySet),
                                                           new KeySegment(new[] { new KeyValuePair <string, object>("ID", 10) }, entitySet.EntityType(), entitySet));
            request.EnableODataDependencyInjectionSupport(routeName);

            ODataMediaTypeFormatter formatter = CreateFormatterWithJson(model, request, ODataPayloadKind.Resource);

            // Act
            ObjectContent <WorkItem> content = new ObjectContent <WorkItem>(
                (WorkItem)TypeInitializer.GetInstance(SupportedTypes.WorkItem), formatter);

            // Assert
            JsonAssert.Equal(Resources.WorkItemEntry, content.ReadAsStringAsync().Result);
        }
Ejemplo n.º 2
0
        private void IEnumerableOfEntityTypeSerializesAsODataFeed(string expectedContent, bool json)
        {
            ODataMediaTypeFormatter formatter = CreateFormatter();

            IEnumerable <Employee> collectionOfPerson = new Collection <Employee>()
            {
                (Employee)TypeInitializer.GetInstance(SupportedTypes.Employee, 0),
                (Employee)TypeInitializer.GetInstance(SupportedTypes.Employee, 1),
            };

            ObjectContent <IEnumerable <Employee> > content = new ObjectContent <IEnumerable <Employee> >(collectionOfPerson,
                                                                                                          formatter, json ? ODataMediaTypes.ApplicationJsonODataMinimalMetadata :
                                                                                                          ODataMediaTypes.ApplicationAtomXmlTypeFeed);

            string actualContent = content.ReadAsStringAsync().Result;

            if (json)
            {
                JsonAssert.Equal(expectedContent, actualContent);
            }
            else
            {
                RegexReplacement replaceUpdateTime = new RegexReplacement(
                    "<updated>*.*</updated>", "<updated>UpdatedTime</updated>");
                Assert.Xml.Equal(expectedContent, actualContent, replaceUpdateTime);
            }
        }
Ejemplo n.º 3
0
        public void EntityTypeSerializesAsODataEntry()
        {
            ODataMediaTypeFormatter formatter = CreateFormatter();
            Employee employee = (Employee)TypeInitializer.GetInstance(SupportedTypes.Employee);
            ObjectContent <Employee> content = new ObjectContent <Employee>(employee, formatter);

            RegexReplacement replaceUpdateTime = new RegexReplacement("<updated>*.*</updated>", "<updated>UpdatedTime</updated>");

            Assert.Xml.Equal(BaselineResource.TestEntityTypeBasic, content.ReadAsStringAsync().Result, regexReplacements: replaceUpdateTime);
        }
Ejemplo n.º 4
0
        public void EntityTypeSerializesAsODataEntry()
        {
            // Arrange
            ODataMediaTypeFormatter formatter = CreateFormatter();
            Employee employee = (Employee)TypeInitializer.GetInstance(SupportedTypes.Employee);
            ObjectContent <Employee> content = new ObjectContent <Employee>(employee, formatter,
                                                                            ODataMediaTypes.ApplicationJsonODataMinimalMetadata);

            // Act & Assert
            JsonAssert.Equal(Resources.EmployeeEntry, content.ReadAsStringAsync().Result);
        }
Ejemplo n.º 5
0
        public void CollectionOfComplexTypeSerializesAsOData()
        {
            IEnumerable <Person> collectionOfPerson = new Collection <Person>()
            {
                (Person)TypeInitializer.GetInstance(SupportedTypes.Person, 0),
                (Person)TypeInitializer.GetInstance(SupportedTypes.Person, 1),
                (Person)TypeInitializer.GetInstance(SupportedTypes.Person, 2)
            };

            ObjectContent <IEnumerable <Person> > content = new ObjectContent <IEnumerable <Person> >(collectionOfPerson, _formatter);

            Assert.Xml.Equal(BaselineResource.TestCollectionOfPerson, content.ReadAsStringAsync().Result);
        }
Ejemplo n.º 6
0
        public void CollectionOfObjectsSerializesAsOData()
        {
            Collection <object> collectionOfObjects = new Collection <object>();

            collectionOfObjects.Add(1);
            collectionOfObjects.Add("Frank");
            collectionOfObjects.Add(TypeInitializer.GetInstance(SupportedTypes.Person, 2));
            collectionOfObjects.Add(TypeInitializer.GetInstance(SupportedTypes.Employee, 3));

            ObjectContent <Collection <object> > content = new ObjectContent <Collection <object> >(collectionOfObjects, _formatter);

            Assert.Throws <ODataException>(() => content.ReadAsStringAsync().Result);
        }
Ejemplo n.º 7
0
        private void CollectionOfComplexTypeSerializesAsOData(string expectedContent, bool json)
        {
            IEnumerable <Person> collectionOfPerson = new Collection <Person>()
            {
                (Person)TypeInitializer.GetInstance(SupportedTypes.Person, 0),
                (Person)TypeInitializer.GetInstance(SupportedTypes.Person, 1),
                (Person)TypeInitializer.GetInstance(SupportedTypes.Person, 2)
            };

            ObjectContent <IEnumerable <Person> > content = new ObjectContent <IEnumerable <Person> >(collectionOfPerson,
                                                                                                      _formatter, GetMediaType(json));

            AssertEqual(json, expectedContent, content.ReadAsStringAsync().Result);
        }
Ejemplo n.º 8
0
        public void CollectionOfComplexTypeSerializesAsOData()
        {
            // Arrange
            IEnumerable <Person> collectionOfPerson = new Collection <Person>()
            {
                (Person)TypeInitializer.GetInstance(SupportedTypes.Person, 0),
                (Person)TypeInitializer.GetInstance(SupportedTypes.Person, 1),
                (Person)TypeInitializer.GetInstance(SupportedTypes.Person, 2)
            };

            ObjectContent <IEnumerable <Person> > content = new ObjectContent <IEnumerable <Person> >(collectionOfPerson,
                                                                                                      _formatter, ODataMediaTypes.ApplicationJsonODataMinimalMetadata);

            // Act & Assert
            JsonAssert.Equal(Resources.CollectionOfPerson, content.ReadAsStringAsync().Result);
        }
Ejemplo n.º 9
0
        public void IEnumerableOfEntityTypeSerializesAsODataFeed()
        {
            ODataMediaTypeFormatter formatter = CreateFormatter();

            IEnumerable <Employee> collectionOfPerson = new Collection <Employee>()
            {
                (Employee)TypeInitializer.GetInstance(SupportedTypes.Employee, 0),
                (Employee)TypeInitializer.GetInstance(SupportedTypes.Employee, 1),
            };

            ObjectContent <IEnumerable <Employee> > content = new ObjectContent <IEnumerable <Employee> >(collectionOfPerson, formatter);

            RegexReplacement replaceUpdateTime = new RegexReplacement("<updated>*.*</updated>", "<updated>UpdatedTime</updated>");

            Assert.Xml.Equal(BaselineResource.TestFeedOfEmployee, content.ReadAsStringAsync().Result, regexReplacements: replaceUpdateTime);
        }
        private static void WriteToStreamAsyncReturnsODataRepresentation(string expectedContent, bool json)
        {
            ODataConventionModelBuilder modelBuilder = new ODataConventionModelBuilder();

            modelBuilder.EntitySet <WorkItem>("WorkItems");
            IEdmModel model = modelBuilder.GetEdmModel();

            HttpRequestMessage request       = new HttpRequestMessage(HttpMethod.Get, "http://localhost/WorkItems(10)");
            HttpConfiguration  configuration = new HttpConfiguration();
            string             routeName     = "Route";

            configuration.Routes.MapODataRoute(routeName, null, model);
            request.SetConfiguration(configuration);
            request.SetEdmModel(model);
            IEdmEntitySet entitySet = model.EntityContainers().Single().EntitySets().Single();

            request.SetODataPath(new ODataPath(new EntitySetPathSegment(entitySet), new KeyValuePathSegment("10")));
            request.SetODataRouteName(routeName);

            ODataMediaTypeFormatter formatter;

            if (json)
            {
                formatter = CreateFormatterWithJson(model, request, ODataPayloadKind.Entry);
            }
            else
            {
                formatter = CreateFormatter(model, request, ODataPayloadKind.Entry);
            }

            ObjectContent <WorkItem> content = new ObjectContent <WorkItem>(
                (WorkItem)TypeInitializer.GetInstance(SupportedTypes.WorkItem), formatter);

            string actualContent = content.ReadAsStringAsync().Result;

            if (json)
            {
                JsonAssert.Equal(expectedContent, actualContent);
            }
            else
            {
                RegexReplacement replaceUpdateTime = new RegexReplacement(
                    "<updated>*.*</updated>", "<updated>UpdatedTime</updated>");
                Assert.Xml.Equal(expectedContent, actualContent, replaceUpdateTime);
            }
        }
Ejemplo n.º 11
0
        public void IEnumerableOfEntityTypeSerializesAsODataFeed()
        {
            // Arrange
            ODataMediaTypeFormatter formatter = CreateFormatter();

            IEnumerable <Employee> collectionOfPerson = new Collection <Employee>()
            {
                (Employee)TypeInitializer.GetInstance(SupportedTypes.Employee, 0),
                (Employee)TypeInitializer.GetInstance(SupportedTypes.Employee, 1),
            };

            ObjectContent <IEnumerable <Employee> > content = new ObjectContent <IEnumerable <Employee> >(collectionOfPerson,
                                                                                                          formatter, ODataMediaTypes.ApplicationJsonODataMinimalMetadata);

            // Act & Assert
            JsonAssert.Equal(Resources.FeedOfEmployee, content.ReadAsStringAsync().Result);
        }
Ejemplo n.º 12
0
        public async Task EntityTypeSerializesAsODataEntry()
        {
            // Arrange
            const string  routeName = "OData";
            IEdmEntitySet entitySet = _model.EntityContainer.FindEntitySet("employees");
            ODataPath     path      = new ODataPath(new EntitySetSegment(entitySet));

            var      config    = RoutingConfigurationFactory.CreateWithRootContainer(routeName, b => b.AddService(ServiceLifetime.Singleton, s => _model));
            var      request   = RequestFactory.Create(HttpMethod.Get, "http://localhost/property", config, routeName, path);
            var      payload   = new ODataPayloadKind[] { ODataPayloadKind.Resource };
            var      formatter = FormatterTestHelper.GetFormatter(payload, request);
            Employee employee  = (Employee)TypeInitializer.GetInstance(SupportedTypes.Employee);
            var      content   = FormatterTestHelper.GetContent(employee, formatter,
                                                                ODataMediaTypes.ApplicationJsonODataMinimalMetadata);

            // Act & Assert
            JsonAssert.Equal(Resources.EmployeeEntry, await FormatterTestHelper.GetContentResult(content, request));
        }
Ejemplo n.º 13
0
        private void EntityTypeSerializesAsODataEntry(string expectedContent, bool json)
        {
            ODataMediaTypeFormatter formatter = CreateFormatter();
            Employee employee = (Employee)TypeInitializer.GetInstance(SupportedTypes.Employee);
            ObjectContent <Employee> content = new ObjectContent <Employee>(employee, formatter, json ?
                                                                            ODataMediaTypes.ApplicationJsonODataMinimalMetadata : ODataMediaTypes.ApplicationAtomXmlTypeEntry);

            string actualContent = content.ReadAsStringAsync().Result;

            if (json)
            {
                JsonAssert.Equal(expectedContent, actualContent);
            }
            else
            {
                RegexReplacement replaceUpdateTime = new RegexReplacement(
                    "<updated>*.*</updated>", "<updated>UpdatedTime</updated>");
                Assert.Xml.Equal(expectedContent, actualContent, replaceUpdateTime);
            }
        }
Ejemplo n.º 14
0
        public async Task IEnumerableOfEntityTypeSerializesAsODataResourceSet()
        {
            // Arrange
            IEdmEntitySet entitySet = _model.EntityContainer.FindEntitySet("employees");
            ODataPath     path      = new ODataPath(new EntitySetSegment(entitySet));

            var request   = RequestFactory.CreateFromModel(_model, "http://localhost/property", "Route", path);
            var payload   = new ODataPayloadKind[] { ODataPayloadKind.ResourceSet };
            var formatter = FormatterTestHelper.GetFormatter(payload, request);

            IEnumerable <Employee> collectionOfPerson = new Collection <Employee>()
            {
                (Employee)TypeInitializer.GetInstance(SupportedTypes.Employee, 0),
                (Employee)TypeInitializer.GetInstance(SupportedTypes.Employee, 1),
            };

            var content = FormatterTestHelper.GetContent(collectionOfPerson, formatter,
                                                         ODataMediaTypes.ApplicationJsonODataMinimalMetadata);

            // Act & Assert
            JsonAssert.Equal(Resources.FeedOfEmployee, await FormatterTestHelper.GetContentResult(content, request));
        }
        public void WriteToStreamAsyncReturnsODataRepresentation()
        {
            ODataConventionModelBuilder model = new ODataConventionModelBuilder();

            model.EntitySet <WorkItem>("WorkItems");

            HttpConfiguration configuration = new HttpConfiguration();
            var route = configuration.Routes.MapHttpRoute(ODataRouteNames.GetById, "{controller}({id})");

            configuration.Routes.MapHttpRoute(ODataRouteNames.PropertyNavigation, "{controller}({id})/{property}");

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/WorkItems(10)");

            request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;
            request.Properties[HttpPropertyKeys.HttpRouteDataKey]     = new HttpRouteData(route);

            ODataMediaTypeFormatter formatter = new ODataMediaTypeFormatter(model.GetEdmModel())
            {
                Request = request
            };

            ObjectContent <WorkItem> content = new ObjectContent <WorkItem>((WorkItem)TypeInitializer.GetInstance(SupportedTypes.WorkItem), formatter);

            RegexReplacement replaceUpdateTime = new RegexReplacement("<updated>*.*</updated>", "<updated>UpdatedTime</updated>");

            Assert.Xml.Equal(BaselineResource.TestEntityWorkItem, content.ReadAsStringAsync().Result, regexReplacements: replaceUpdateTime);
        }
Ejemplo n.º 16
0
        public void WriteToStreamAsyncReturnsODataRepresentation()
        {
            ODataConventionModelBuilder modelBuilder = new ODataConventionModelBuilder();

            modelBuilder.EntitySet <WorkItem>("WorkItems");
            IEdmModel model = modelBuilder.GetEdmModel();

            HttpConfiguration configuration = new HttpConfiguration();

            configuration.EnableOData(model);

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/WorkItems(10)");

            request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;
            request.Properties[HttpPropertyKeys.HttpRouteDataKey]     = new HttpRouteData(configuration.Routes.First());
            request.Properties["MS_ODataPath"] = new DefaultODataPathHandler(model).Parse("WorkItems(10)");

            ODataMediaTypeFormatter formatter = CreateFormatter(model, request, ODataPayloadKind.Entry);

            ObjectContent <WorkItem> content = new ObjectContent <WorkItem>((WorkItem)TypeInitializer.GetInstance(SupportedTypes.WorkItem), formatter);

            RegexReplacement replaceUpdateTime = new RegexReplacement("<updated>*.*</updated>", "<updated>UpdatedTime</updated>");

            Assert.Xml.Equal(BaselineResource.TestEntityWorkItem, content.ReadAsStringAsync().Result, regexReplacements: replaceUpdateTime);
        }