Example #1
0
        internal protected virtual async Task <IList> ExecuteOeViaHttpClient <T, TResult>(QueryParameters <T, TResult> parameters)
        {
            Uri uri = CreateContainer(0).BaseUri;

            using (var client = new HttpClient())
            {
                client.BaseAddress = new UriBuilder(uri.Scheme, uri.Host, uri.Port).Uri;
                client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", OeRequestHeaders.JsonDefault.ContentType);
                using (HttpResponseMessage httpResponseMessage = await client.GetAsync(uri.LocalPath + "/" + parameters.RequestUri))
                    if (httpResponseMessage.IsSuccessStatusCode)
                    {
                        using (Stream content = await httpResponseMessage.Content.ReadAsStreamAsync())
                        {
                            ODataPath path           = OeParser.ParsePath(EdmModel, client.BaseAddress, new Uri(client.BaseAddress, parameters.RequestUri));
                            var       responseReader = new ResponseReader(EdmModel.GetEdmModel(path));
                            if (typeof(TResult) == typeof(Object))
                            {
                                return(responseReader.Read <T>(content).Cast <Object>().ToList());
                            }
                            else
                            {
                                return(responseReader.Read <TResult>(content).Cast <Object>().ToList());
                            }
                        }
                    }
            }

            return(null);
        }
        public async Task NavigationNextLink()
        {
            String request = "Categories?$expand=Children";

            OeParser parser   = Fixture.CreateParser(request, Fixture.ModelBoundProvider);
            ODataUri odataUri = Fixture.ParseUri(request);

            var response = new MemoryStream();
            await parser.ExecuteQueryAsync(odataUri, OeRequestHeaders.JsonDefault, response, CancellationToken.None).ConfigureAwait(false);

            response.Position = 0;

            var           reader     = new ResponseReader(parser.EdmModel);
            List <Object> categories = reader.Read(response).Cast <Object>().ToList();

            foreach (dynamic category in categories)
            {
                ResponseReader.NavigationInfo navigationInfo = reader.GetNavigationInfo(category.Children);
                String actual   = Uri.UnescapeDataString(navigationInfo.NextPageLink.OriginalString);
                String expected = $"http://dummy/Categories?$filter=ParentId eq {category.Id}&$select=DateTime,Id,Name,ParentId";

                ODataUri actualOdataUri = Fixture.ParseUri(actual);
                var      selectedItems  = actualOdataUri.SelectAndExpand.SelectedItems.Cast <PathSelectItem>().OrderBy(i => ((PropertySegment)i.SelectedPath.FirstSegment).Property.Name);
                actualOdataUri.SelectAndExpand = new SelectExpandClause(selectedItems, actualOdataUri.SelectAndExpand.AllSelected);
                actual = Uri.UnescapeDataString(actualOdataUri.BuildUri(ODataUrlKeyDelimiter.Parentheses).OriginalString);

                Assert.Equal(expected, actual);
            }
        }
Example #3
0
        public async Task NextPageLink()
        {
            String request = "OrderItems?$orderby=Id&$count=true";

            ODataUri odataUri = Fixture.ParseUri(request);
            var parser = new OeParser(odataUri.ServiceRoot, Fixture.OeDataAdapter, Fixture.EdmModel);
            var uri = new Uri(odataUri.ServiceRoot, request);
            OeRequestHeaders requestHeaders = OeRequestHeaders.JsonDefault.SetMaxPageSize(2);

            long count = -1;
            var fromOe = new List<Object>();
            do
            {
                var response = new MemoryStream();
                await parser.ExecuteGetAsync(uri, requestHeaders, response, CancellationToken.None).ConfigureAwait(false);
                var reader = new ResponseReader(Fixture.EdmModel, Fixture.OeDataAdapter);
                response.Position = 0;

                List<Object> result = reader.Read(response).Cast<Object>().ToList();
                Assert.InRange(result.Count, 0, requestHeaders.MaxPageSize);
                fromOe.AddRange(result);

                if (count < 0)
                    count = reader.ResourceSet.Count.GetValueOrDefault();
                uri = reader.ResourceSet.NextPageLink;
            }
            while (uri != null);
            Assert.Equal(count, fromOe.Count);

            IList fromDb;
            using (var context = Fixture.CreateContext())
                fromDb = context.OrderItems.OrderBy(i => i.Id).ToList();

            TestHelper.Compare(fromDb, fromOe, null);
        }
Example #4
0
        internal async Task CountExpandNested()
        {
            String request = "Orders?$expand=Items($count=true)&orderby=Id";

            ODataUri odataUri = Fixture.ParseUri(request);
            var      parser   = new OeParser(odataUri.ServiceRoot, Fixture.OeDataAdapter, Fixture.EdmModel);
            var      uri      = new Uri(odataUri.ServiceRoot, request);

            var response = new MemoryStream();
            await parser.ExecuteQueryAsync(odataUri, OeRequestHeaders.JsonDefault, response, CancellationToken.None).ConfigureAwait(false);

            response.Position = 0;

            var actualCounts = new List <long>();
            var reader       = new ResponseReader(Fixture.EdmModel, Fixture.OeDataAdapter.EntitySetMetaAdapters);

            foreach (dynamic order in reader.Read(response))
            {
                var navigationProperty = (IEnumerable)order.Items;
                actualCounts.Add(reader.GetResourceSet(navigationProperty).Count.Value);
            }

            List <long> expectedCounts;

            using (var dbContext = Fixture.CreateContext())
                expectedCounts = dbContext.Orders.OrderBy(o => o.Id).Select(i => (long)i.Items.Count()).ToList();

            Assert.Equal(expectedCounts, actualCounts);
        }
        private static async Task <T[]> Execute <T>(String request, Object requestData, Func <OrderContext, IEnumerable <T> > fromDbFunc)
        {
            var fixture = new DbFixtureInitDb();

            fixture.Initalize();

            var parser         = new OeParser(new Uri("http://dummy/"), fixture.OeDataAdapter, fixture.EdmModel);
            var responseStream = new MemoryStream();

            var requestUri = new Uri(@"http://dummy/" + request);

            if (requestData == null)
            {
                await parser.ExecuteGetAsync(requestUri, OeRequestHeaders.JsonDefault, responseStream, CancellationToken.None);
            }
            else
            {
                String data          = JsonConvert.SerializeObject(requestData);
                var    requestStream = new MemoryStream(Encoding.UTF8.GetBytes(data));
                await parser.ExecutePostAsync(requestUri, OeRequestHeaders.JsonDefault, requestStream, responseStream, CancellationToken.None);
            }

            var reader = new ResponseReader(fixture.EdmModel, fixture.DbDataAdapter.EntitySetMetaAdapters);

            responseStream.Position = 0;
            T[] fromOe;
            if (typeof(T) == typeof(int))
            {
                String count = new StreamReader(responseStream).ReadToEnd();
                fromOe = count == "" ? null : new T[] { (T)(Object)int.Parse(count) };
            }
            else
            {
                fromOe = reader.Read <T>(responseStream).ToArray();
            }

            if (fromDbFunc == null)
            {
                return(fromOe);
            }

            T[] fromDb;
            using (var orderContext = (OrderContext)fixture.DbDataAdapter.CreateDataContext())
                fromDb = fromDbFunc(orderContext).ToArray();

            var settings = new JsonSerializerSettings()
            {
                DateFormatString      = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffff",
                DateTimeZoneHandling  = DateTimeZoneHandling.Utc,
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                NullValueHandling     = NullValueHandling.Ignore
            };
            String jsonOe = JsonConvert.SerializeObject(fromOe, settings);
            String jsonDb = JsonConvert.SerializeObject(fromDb, settings);

            Console.WriteLine(requestUri);
            Assert.Equal(jsonDb, jsonOe);

            return(fromOe);
        }
Example #6
0
        public async Task NavigationNextPageLink()
        {
            String request = "Orders?$expand=Items($filter=Count gt 0 or Count eq null;$orderby=Id;$count=true)&$orderby=Id&$count=true";

            ODataUri odataUri = Fixture.ParseUri(request);
            var parser = new OeParser(odataUri.ServiceRoot, Fixture.OeDataAdapter, Fixture.EdmModel);
            var requestUri = new Uri(odataUri.ServiceRoot, request);
            Uri uri = requestUri;
            OeRequestHeaders requestHeaders = OeRequestHeaders.JsonDefault.SetMaxPageSize(1).SetNavigationNextLink(true);

            long count = -1;
            var fromOe = new List<Object>();
            do
            {
                var response = new MemoryStream();
                await parser.ExecuteGetAsync(uri, requestHeaders, response, CancellationToken.None).ConfigureAwait(false);
                response.Position = 0;

                var reader = new ResponseReader(Fixture.EdmModel, Fixture.OeDataAdapter);
                List<Object> result = reader.Read(response).Cast<Object>().ToList();
                Assert.InRange(result.Count, 0, requestHeaders.MaxPageSize);
                fromOe.AddRange(result);

                var navigationPropertyParser = new OeParser(odataUri.ServiceRoot, Fixture.OeDataAdapter, Fixture.EdmModel);
                foreach (dynamic order in result)
                {
                    var navigationProperty = (IEnumerable)order.Items;
                    ODataResourceSetBase resourceSet = reader.GetResourceSet(navigationProperty);

                    var navigationPropertyResponse = new MemoryStream();
                    await navigationPropertyParser.ExecuteGetAsync(resourceSet.NextPageLink, OeRequestHeaders.JsonDefault, navigationPropertyResponse, CancellationToken.None).ConfigureAwait(false);
                    navigationPropertyResponse.Position = 0;

                    var navigationPropertyReader = new ResponseReader(Fixture.EdmModel, Fixture.OeDataAdapter);
                    foreach (dynamic orderItem in navigationPropertyReader.Read(navigationPropertyResponse))
                        order.Items.Add(orderItem);

                    Assert.Equal(navigationPropertyReader.ResourceSet.Count, order.Items.Count);
                }

                if (count < 0)
                    count = reader.ResourceSet.Count.GetValueOrDefault();

                uri = reader.ResourceSet.NextPageLink;
            }
            while (uri != null);

            Assert.Equal(count, fromOe.Count);

            var exprectedResponse = new MemoryStream();
            var expectedParser = new OeParser(odataUri.ServiceRoot, Fixture.OeDataAdapter, Fixture.EdmModel);
            await expectedParser.ExecuteGetAsync(requestUri, OeRequestHeaders.JsonDefault, exprectedResponse, CancellationToken.None).ConfigureAwait(false);
            exprectedResponse.Position = 0;

            var exprectedReader = new ResponseReader(Fixture.EdmModel, Fixture.OeDataAdapter);
            List<Object> expectedResult = exprectedReader.Read(exprectedResponse).Cast<Object>().ToList();

            TestHelper.Compare(expectedResult, fromOe, null);
        }
        private static async Task <Object[]> Execute <T>(String request, Object requestData, Func <OrderContext, IEnumerable <T> > fromDbFunc)
        {
            var fixture = new RDBNull_DbFixtureInitDb();
            await fixture.Initalize().ConfigureAwait(false);

            var responseStream = new MemoryStream();

            OeParser parser     = fixture.CreateParser(request, null);
            ODataUri odataUri   = fixture.ParseUri(request);
            var      requestUri = new Uri(parser.BaseUri, request);

            if (requestData == null)
            {
                await parser.ExecuteOperationAsync(odataUri, OeRequestHeaders.JsonDefault, null, responseStream, CancellationToken.None).ConfigureAwait(false);
            }
            else
            {
                String data          = fixture.SerializeRequestData(requestData);
                var    requestStream = new MemoryStream(Encoding.UTF8.GetBytes(data));
                await parser.ExecuteOperationAsync(odataUri, OeRequestHeaders.JsonDefault, requestStream, responseStream, CancellationToken.None).ConfigureAwait(false);
            }

            ODataPath path   = OeParser.ParsePath(fixture.DbEdmModel, parser.BaseUri, requestUri);
            var       reader = new ResponseReader(fixture.DbEdmModel.GetEdmModel(path), fixture.ServiceProvider);

            responseStream.Position = 0;
            Object[] fromOe;
            if (typeof(T) == typeof(int))
            {
                String count = new StreamReader(responseStream).ReadToEnd();
                fromOe = count == "" ? null : new Object[] { int.Parse(count) };
            }
            else if (typeof(T) == typeof(String))
            {
                String json    = new StreamReader(responseStream).ReadToEnd();
                var    jobject = (Newtonsoft.Json.Linq.JObject)JsonConvert.DeserializeObject(json);
                var    jarray  = (Newtonsoft.Json.Linq.JArray)jobject["value"];
                fromOe = jarray.Select(j => (String)j).ToArray();
            }
            else
            {
                fromOe = reader.Read(responseStream).Cast <Object>().ToArray();
            }

            if (fromDbFunc == null)
            {
                return(fromOe);
            }

            T[] fromDb;
            using (OrderContext orderContext = fixture.CreateContext())
                fromDb = fromDbFunc(orderContext).ToArray();

            TestHelper.Compare(fromDb, fromOe, null);
            return(fromOe);
        }
        public async Task FillNextLinkProperties(OeParser parser, Object entity, CancellationToken token)
        {
            using (var response = new MemoryStream())
                foreach (KeyValuePair <PropertyInfo, ODataResourceSetBase> propertyResourceSet in GetResourceSets(entity))
                {
                    response.SetLength(0);
                    await parser.ExecuteGetAsync(propertyResourceSet.Value.NextPageLink, OeRequestHeaders.JsonDefault, response, token).ConfigureAwait(false);

                    response.Position = 0;

                    var navigationPropertyReader = new ResponseReader(parser.Model, parser.DataAdapter.EntitySetMetaAdapters);
                    AddItems(entity, propertyResourceSet.Key, navigationPropertyReader.Read(response));
                }
        }
Example #9
0
        public async Task CountQueryParameterFilter()
        {
            String request = "OrderItems?$filter=OrderId eq 1&$count=true&$top=1";

            ODataUri odataUri = Fixture.ParseUri(request);
            var parser = new OeParser(odataUri.ServiceRoot, Fixture.OeDataAdapter, Fixture.EdmModel);

            var response = new MemoryStream();
            await parser.ExecuteQueryAsync(odataUri, OeRequestHeaders.JsonDefault, response, CancellationToken.None).ConfigureAwait(false);
            response.Position = 0;

            var reader = new ResponseReader(Fixture.EdmModel, Fixture.OeDataAdapter);
            reader.Read(response).Cast<Object>().Single();

            int? expectedCount;
            using (var dbContext = Fixture.CreateContext())
                expectedCount = dbContext.OrderItems.Count(i => i.OrderId == 1);

            Assert.Equal(expectedCount, reader.ResourceSet.Count);
        }
Example #10
0
        public async Task NextPageLink()
        {
            String request = "OrderItems?$orderby=Id&$count=true";

            const int pageSize = 2;
            var       pageNextLinkModelBoundBuilder = new PageNextLinkModelBoundBuilder(Fixture.OeEdmModel, Fixture.IsSqlite);

            Query.OeModelBoundProvider modelBoundProvider = pageNextLinkModelBoundBuilder.BuildProvider(pageSize, false);
            OeParser parser = Fixture.CreateParser(request, modelBoundProvider);
            var      uri    = new Uri(parser.BaseUri, request);

            long count  = -1;
            var  fromOe = new List <Object>();

            do
            {
                var response = new MemoryStream();
                await parser.ExecuteGetAsync(uri, OeRequestHeaders.JsonDefault, response, CancellationToken.None).ConfigureAwait(false);

                var reader = new ResponseReader(parser.EdmModel);
                response.Position = 0;

                List <Object> result = reader.Read(response).Cast <Object>().ToList();
                Assert.InRange(result.Count, 0, pageSize);
                fromOe.AddRange(result);

                if (count < 0)
                {
                    count = reader.ResourceSet.Count.GetValueOrDefault();
                }
                uri = reader.ResourceSet.NextPageLink;
            }while (uri != null);
            Assert.Equal(count, fromOe.Count);

            IList fromDb;

            using (var context = Fixture.CreateContext())
                fromDb = context.OrderItems.OrderBy(i => i.Id).ToList();

            TestHelper.Compare(fromDb, fromOe, null);
        }
Example #11
0
        public async Task NavigationNextPageLink()
        {
            String request = "Orders?$expand=Items($filter=Count gt 0 or Count eq null;$orderby=Id;$count=true)&$orderby=Id&$count=true";

            ODataUri  odataUri   = Fixture.ParseUri(request);
            IEdmModel edmModel   = Fixture.OeEdmModel.GetEdmModel(odataUri.Path);
            var       requestUri = new Uri(odataUri.ServiceRoot, request);

            var expectedResponse = new MemoryStream();
            var expectedParser   = new OeParser(odataUri.ServiceRoot, edmModel);
            await expectedParser.ExecuteGetAsync(requestUri, OeRequestHeaders.JsonDefault, expectedResponse, CancellationToken.None).ConfigureAwait(false);

            expectedResponse.Position = 0;

            var           exprectedReader = new ResponseReader(edmModel);
            List <Object> expectedResult  = exprectedReader.Read(expectedResponse).Cast <Object>().ToList();

            List <Object> fromOe = await OrdersCountItemsCount(Fixture, request, i => i.Count == null || i.Count > 0, 1, true);

            TestHelper.Compare(expectedResult, fromOe, null);
        }
Example #12
0
        public async Task NavigationNextLink()
        {
            String request = "Categories?$expand=Children";

            OeParser parser   = Fixture.CreateParser(request, Fixture.ModelBoundProvider);
            ODataUri odataUri = Fixture.ParseUri(request);

            var response = new MemoryStream();
            await parser.ExecuteQueryAsync(odataUri, OeRequestHeaders.JsonDefault, response, CancellationToken.None).ConfigureAwait(false);

            response.Position = 0;

            var           reader     = new ResponseReader(parser.EdmModel);
            List <Object> categories = reader.Read(response).Cast <Object>().ToList();

            foreach (dynamic category in categories)
            {
                ResponseReader.NavigationInfo navigationInfo = reader.GetNavigationInfo(category.Children);
                String actual   = Uri.UnescapeDataString(navigationInfo.NextPageLink.OriginalString);
                String expected = $"http://dummy/Categories?$filter=ParentId eq {category.Id}";
                Assert.Equal(expected, actual);
            }
        }
Example #13
0
        public async Task WithItems()
        {
            String request = $"Orders/WithItems(itemIds=[1,2,3])";

            IList <Model.OrderItem> fromOe = null;
            Uri uri = DbFixtureInitDb.ContainerFactory().BaseUri;

            using (var client = new HttpClient())
            {
                client.BaseAddress = new UriBuilder(uri.Scheme, uri.Host, uri.Port).Uri;
                client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", OeRequestHeaders.JsonDefault.ContentType);
                using (HttpResponseMessage httpResponseMessage = await client.GetAsync(uri.LocalPath + "/" + request))
                    if (httpResponseMessage.IsSuccessStatusCode)
                    {
                        using (Stream content = await httpResponseMessage.Content.ReadAsStreamAsync())
                        {
                            var responseReader = new ResponseReader(Fixture.EdmModel);
                            fromOe = responseReader.Read <Model.OrderItem>(content).ToList();
                        }
                    }
            }

            Assert.Equal(new[] { 1, 2, 3 }, fromOe == null ? Enumerable.Empty <int>() : fromOe.Select(i => i.Id).OrderBy(id => id));
        }
Example #14
0
        public async Task FillNextLinkProperties(OeParser parser, CancellationToken token)
        {
            using (var response = new MemoryStream())
                foreach (KeyValuePair <Object, Dictionary <PropertyInfo, NavigationInfo> > navigationPropertyEntity in NavigationInfoEntities)
                {
                    foreach (KeyValuePair <PropertyInfo, NavigationInfo> propertyResourceSet in navigationPropertyEntity.Value)
                    {
                        Uri requestUri = propertyResourceSet.Value.NextPageLink;
                        while (requestUri != null)
                        {
                            response.SetLength(0);
                            await parser.ExecuteGetAsync(requestUri, OeRequestHeaders.JsonDefault, response, token).ConfigureAwait(false);

                            response.Position = 0;

                            var navigationPropertyReader = new ResponseReader(EdmModel, _serviceProvider);
                            AddItems(navigationPropertyEntity.Key, propertyResourceSet.Key, navigationPropertyReader.Read(response));
                            await navigationPropertyReader.FillNextLinkProperties(parser, token).ConfigureAwait(false);

                            requestUri = navigationPropertyReader.ResourceSet.NextPageLink;
                        }
                    }
                }
        }
Example #15
0
        private static async Task <Object[]> Execute <T>(String request, Object requestData, Func <OrderContext, IEnumerable <T> > fromDbFunc)
        {
            var fixture = new RDBNull_DbFixtureInitDb();
            await fixture.Initalize();

            var parser         = new OeParser(new Uri("http://dummy/"), fixture.OeEdmModel);
            var responseStream = new MemoryStream();

            var requestUri = new Uri(@"http://dummy/" + request);

            if (requestData == null)
            {
                await parser.ExecuteGetAsync(requestUri, OeRequestHeaders.JsonDefault, responseStream, CancellationToken.None);
            }
            else
            {
                String data          = JsonConvert.SerializeObject(requestData);
                var    requestStream = new MemoryStream(Encoding.UTF8.GetBytes(data));
                await parser.ExecutePostAsync(requestUri, OeRequestHeaders.JsonDefault, requestStream, responseStream, CancellationToken.None);
            }

            ODataPath path   = OeParser.ParsePath(fixture.OeEdmModel, new Uri("http://dummy/"), requestUri);
            var       reader = new ResponseReader(fixture.OeEdmModel.GetEdmModel(path));

            responseStream.Position = 0;
            Object[] fromOe;
            if (typeof(T) == typeof(int))
            {
                String count = new StreamReader(responseStream).ReadToEnd();
                fromOe = count == "" ? null : new Object[] { int.Parse(count) };
            }
            else if (typeof(T) == typeof(String))
            {
                String json    = new StreamReader(responseStream).ReadToEnd();
                var    jobject = (Newtonsoft.Json.Linq.JObject)JsonConvert.DeserializeObject(json);
                var    jarray  = (Newtonsoft.Json.Linq.JArray)jobject["value"];
                fromOe = jarray.Select(j => (String)j).ToArray();
            }
            else
            {
                fromOe = reader.Read(responseStream).Cast <Object>().ToArray();
            }

            if (fromDbFunc == null)
            {
                return(fromOe);
            }

            T[] fromDb;
            using (OrderContext orderContext = fixture.CreateContext())
                fromDb = fromDbFunc(orderContext).ToArray();

            var settings = new JsonSerializerSettings()
            {
                DateFormatString      = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffff",
                DateTimeZoneHandling  = DateTimeZoneHandling.Utc,
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                NullValueHandling     = NullValueHandling.Ignore
            };
            String jsonOe = JsonConvert.SerializeObject(fromOe, settings);
            String jsonDb = JsonConvert.SerializeObject(fromDb, settings);

            Console.WriteLine(requestUri);
            Assert.Equal(jsonDb, jsonOe);

            return(fromOe);
        }
Example #16
0
        public async Task <IList> ExecuteOe <TResult>(String requestUri, bool navigationNextLink, int pageSize)
        {
            OeModelBoundProvider modelBoundProvider = ModelBoundProvider;

            if (modelBoundProvider == null)
            {
                var modelBoundProviderBuilder = new PageNextLinkModelBoundBuilder(OeEdmModel, IsSqlite);
                modelBoundProvider = modelBoundProviderBuilder.BuildProvider(pageSize, navigationNextLink);
            }

            OeParser         parser         = CreateParser(requestUri, modelBoundProvider);
            var              uri            = new Uri(parser.BaseUri, requestUri);
            OeRequestHeaders requestHeaders = OeRequestHeaders.JsonDefault.SetMaxPageSize(pageSize);

            long     count = -1;
            ODataUri odataUri;
            var      fromOe = new List <Object>();

            do
            {
                odataUri = OeParser.ParseUri(parser.EdmModel, parser.BaseUri, uri);
                var response = new MemoryStream();
                await parser.ExecuteQueryAsync(odataUri, requestHeaders, response, CancellationToken.None).ConfigureAwait(false);

                response.Position = 0;

                List <Object>  result;
                ResponseReader responseReader;
                if (typeof(TResult).IsPrimitive)
                {
                    TypeConverter converter = TypeDescriptor.GetConverter(typeof(TResult));
                    return(new Object[] { converter.ConvertFromString(new StreamReader(response).ReadToEnd()) });
                }
                else if (typeof(TResult) == typeof(Object) && (requestUri.Contains("$apply=") || requestUri.Contains("$compute=")))
                {
                    responseReader = new OpenTypeResponseReader(TestHelper.GetEdmModel(DbEdmModel, odataUri.Path));
                    result         = responseReader.Read(response).Cast <Object>().ToList();
                }
                else
                {
                    responseReader = new ResponseReader(TestHelper.GetEdmModel(DbEdmModel, odataUri.Path));
                    result         = responseReader.Read(response).Cast <Object>().ToList();
                }

                if (pageSize > 0)
                {
                    Xunit.Assert.InRange(result.Count, 0, requestHeaders.MaxPageSize);
                }
                fromOe.AddRange(result);

                await responseReader.FillNextLinkProperties(parser, CancellationToken.None).ConfigureAwait(false);

                if (count < 0)
                {
                    count = responseReader.ResourceSet.Count.GetValueOrDefault();
                }

                uri = responseReader.ResourceSet.NextPageLink;
            }while (uri != null);

            if (odataUri.QueryCount != null)
            {
                Xunit.Assert.Equal(count, fromOe.Count);
            }

            return(fromOe);
        }
Example #17
0
        public async Task <IList> ExecuteOe <TResult>(String requestUri, bool navigationNextLink, int pageSize)
        {
            ODataUri         odataUri       = ParseUri(requestUri);
            var              parser         = new OeParser(odataUri.ServiceRoot, OeDataAdapter, EdmModel);
            var              uri            = new Uri(odataUri.ServiceRoot, requestUri);
            OeRequestHeaders requestHeaders = OeRequestHeaders.JsonDefault.SetMaxPageSize(pageSize).SetNavigationNextLink(navigationNextLink);

            long count  = -1;
            var  fromOe = new List <Object>();

            do
            {
                var response = new MemoryStream();
                await parser.ExecuteGetAsync(uri, requestHeaders, response, CancellationToken.None).ConfigureAwait(false);

                response.Position = 0;

                List <Object>  result;
                ResponseReader responseReader;
                if (typeof(TResult).IsPrimitive)
                {
                    TypeConverter converter = TypeDescriptor.GetConverter(typeof(TResult));
                    return(new Object[] { converter.ConvertFromString(new StreamReader(response).ReadToEnd()) });
                }
                else if (typeof(TResult) == typeof(Object))
                {
                    responseReader = new OpenTypeResponseReader(EdmModel, DbDataAdapter);
                    result         = responseReader.Read(response).Cast <Object>().ToList();
                }
                else
                {
                    responseReader = new ResponseReader(EdmModel, DbDataAdapter);
                    result         = responseReader.Read <TResult>(response).Cast <Object>().ToList();
                }

                if (pageSize > 0)
                {
                    Xunit.Assert.InRange(result.Count, 0, requestHeaders.MaxPageSize);
                }
                fromOe.AddRange(result);

                foreach (Object entity in fromOe)
                {
                    await responseReader.FillNextLinkProperties(odataUri.ServiceRoot, entity, CancellationToken.None).ConfigureAwait(false);
                }

                if (count < 0)
                {
                    count = responseReader.ResourceSet.Count.GetValueOrDefault();
                }

                uri = responseReader.ResourceSet.NextPageLink;
            }while (uri != null);

            if (odataUri.QueryCount != null)
            {
                Xunit.Assert.Equal(count, fromOe.Count);
            }

            return(fromOe);
        }
Example #18
0
        internal static async Task <List <Object> > OrdersCountItemsCount(DbFixture fixture, String request, Func <Model.OrderItem, bool> orderItemPredicate,
                                                                          int maxPageSize, bool navigationNextLink)
        {
            Query.OeModelBoundProvider modelBoundProvider = fixture.ModelBoundProvider;
            if (modelBoundProvider == null)
            {
                var modelBoundProviderBuilder = new PageNextLinkModelBoundBuilder(fixture.OeEdmModel, fixture.IsSqlite);
                modelBoundProvider = modelBoundProviderBuilder.BuildProvider(maxPageSize, navigationNextLink);
            }

            OeParser parser = fixture.CreateParser(request, modelBoundProvider);
            Uri      uri    = new Uri(parser.BaseUri, request);

            int  expectedCount;
            long count  = -1;
            var  fromOe = new List <Object>();

            do
            {
                var response = new MemoryStream();
                await parser.ExecuteGetAsync(uri, OeRequestHeaders.JsonDefault.SetMaxPageSize(maxPageSize), response, CancellationToken.None).ConfigureAwait(false);

                response.Position = 0;

                var           reader = new ResponseReader(parser.EdmModel);
                List <Object> result = reader.Read(response).Cast <Object>().ToList();
                fromOe.AddRange(result);

                if (maxPageSize > 0)
                {
                    Assert.InRange(result.Count, 0, maxPageSize);
                }

                var navigationPropertyParser = new OeParser(parser.BaseUri, parser.EdmModel, fixture.ModelBoundProvider);
                foreach (dynamic order in result)
                {
                    using (var dbContext = fixture.CreateContext())
                    {
                        String orderName = order.Name;
                        expectedCount = dbContext.OrderItems.Where(i => i.Order.Name == orderName).ToList().Where(orderItemPredicate).Count();
                    }

                    var navigationProperty             = (IEnumerable)order.Items;
                    ResponseReader.NavigationInfo info = reader.GetNavigationInfo(navigationProperty);

                    if (!navigationNextLink && !uri.OriginalString.Contains("$skiptoken="))
                    {
                        Assert.Equal(expectedCount, info.Count);
                    }

                    var navigationPropertyResponse = new MemoryStream();
                    await navigationPropertyParser.ExecuteGetAsync(info.NextPageLink, OeRequestHeaders.JsonDefault, navigationPropertyResponse, CancellationToken.None).ConfigureAwait(false);

                    navigationPropertyResponse.Position = 0;

                    var navigationPropertyReader = new ResponseReader(parser.EdmModel);
                    foreach (dynamic orderItem in navigationPropertyReader.Read(navigationPropertyResponse))
                    {
                        order.Items.Add(orderItem);
                    }

                    if (navigationNextLink)
                    {
                        Assert.Equal(expectedCount, navigationPropertyReader.ResourceSet.Count);
                    }
                }

                if (count < 0)
                {
                    count = reader.ResourceSet.Count.GetValueOrDefault();
                }

                uri = reader.ResourceSet.NextPageLink;
            }while (uri != null);

            using (var dbContext = fixture.CreateContext())
                expectedCount = dbContext.Orders.Count();
            Assert.Equal(expectedCount, count);

            return(fromOe);
        }