Пример #1
0
        public async Task DataClient_Ensure_BlankNode()
        {
            int         count   = 0;
            TestHandler handler = new TestHandler((request) =>
            {
                count++;
                var response     = new HttpResponseMessage(HttpStatusCode.OK);
                response.Content = new TestContent(TestJson.BlankNode);
                return(response);
            });

            CacheHttpClient httpClient = new CacheHttpClient(handler);

            using (var client = new DataClient(httpClient, new MemoryFileCache()))
            {
                // start with a bare file
                JObject bare = await client.GetFile(new Uri("http://test/mySearch"));

                // this property is inline
                var json = await client.Ensure(new Uri("http://test/doc#a"), new Uri[] { new Uri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type") });

                Assert.Equal(1, count);
                Assert.Equal("Child", json["@type"]);
            }
        }
Пример #2
0
        public async Task DataClient_Basic_UseCache()
        {
            int         count   = 0;
            TestHandler handler = new TestHandler((request) =>
            {
                count++;
                var response     = new HttpResponseMessage(HttpStatusCode.OK);
                response.Content = new TestContent(TestJson.BasicGraph);

                return(response);
            });

            CacheHttpClient httpClient = new CacheHttpClient(handler);

            using (var client = new DataClient(httpClient, new MemoryFileCache()))
            {
                var json = await client.GetFile(new Uri("http://test/doc"));

                json = await client.GetFile(new Uri("http://test/doc"));

                json = await client.GetFile(new Uri("http://test/doc"));

                Assert.Equal(1, count);
            }
        }
Пример #3
0
        public async Task DataClient_Ensure_EmptyCache()
        {
            int         count   = 0;
            TestHandler handler = new TestHandler((request) =>
            {
                count++;

                var response = new HttpResponseMessage(HttpStatusCode.OK);

                if (request.RequestUri.AbsoluteUri == "http://test/docBare")
                {
                    response.Content = new TestContent(TestJson.BasicGraphBare);
                }

                if (request.RequestUri.AbsoluteUri == "http://test/doc")
                {
                    response.Content = new TestContent(TestJson.BasicGraph);
                }

                return(response);
            });

            CacheHttpClient httpClient = new CacheHttpClient(handler);

            using (var client = new DataClient(httpClient, new MemoryFileCache()))
            {
                // start with a child property from nothing
                var json = await client.Ensure(new Uri("http://test/doc#c"), new Uri[] { new Uri("http://schema.org/test#name") });

                Assert.Equal(1, count);
                Assert.Equal("grandChildC", json["name"].ToString());
            }
        }
Пример #4
0
        public async Task DataClient_Basic_EntityCache()
        {
            int         count   = 0;
            TestHandler handler = new TestHandler((request) =>
            {
                count++;
                var response     = new HttpResponseMessage(HttpStatusCode.OK);
                response.Content = new TestContent(TestJson.BasicGraph);

                return(response);
            });

            CacheHttpClient httpClient = new CacheHttpClient(handler);

            using (var client = new DataClient(httpClient, new MemoryFileCache()))
            {
                // verify no exceptions are thrown here
                var json = await client.GetFile(new Uri("http://test/doc"), TimeSpan.MinValue, true);

                json = await client.GetFile(new Uri("http://test/doc"), TimeSpan.MinValue, true);

                json = await client.GetFile(new Uri("http://test/doc"), TimeSpan.MinValue, true);

                Assert.Equal(3, count);
            }
        }
Пример #5
0
        public async Task DataClient_Ensure_JToken_Corrupt()
        {
            int         count   = 0;
            TestHandler handler = new TestHandler((request) =>
            {
                count++;

                var response = new HttpResponseMessage(HttpStatusCode.OK);

                if (request.RequestUri.AbsoluteUri == "http://test/docBare")
                {
                    response.Content = new TestContent(TestJson.BasicGraphBare);
                }

                if (request.RequestUri.AbsoluteUri == "http://test/doc")
                {
                    response.Content = new TestContent(TestJson.BasicGraph);
                }

                return(response);
            });

            CacheHttpClient httpClient = new CacheHttpClient(handler);

            using (var client = new DataClient(httpClient, new MemoryFileCache()))
            {
                // start with a bare file
                JObject bare = await client.GetFile(new Uri("http://test/docBare"));

                JObject obj = new JObject();
                obj.Add("blah", "blah");

                // this property is inline
                var json = await client.Ensure(obj, new Uri[] { new Uri("http://schema.org/test#name") });

                Assert.Equal(1, count);
                Assert.True(Object.ReferenceEquals(obj, json));
            }
        }
Пример #6
0
        public async Task DataClient_GetEntity_404()
        {
            int         count   = 0;
            TestHandler handler = new TestHandler((request) =>
            {
                count++;
                var response     = new HttpResponseMessage(HttpStatusCode.NotFound);
                response.Content = new TestContent(TestJson.BasicGraph);

                return(response);
            });

            CacheHttpClient httpClient = new CacheHttpClient(handler);

            using (var client = new DataClient(httpClient, new MemoryFileCache()))
            {
                var json = await client.GetEntity(new Uri("http://test/doc"));

                Assert.Equal(5, count);
                Assert.Equal("404", json["HttpStatusCode"].ToString());
            }
        }
Пример #7
0
        public async Task DataClient_Ensure_NonRDF()
        {
            int         count   = 0;
            TestHandler handler = new TestHandler((request) =>
            {
                count++;
                var response     = new HttpResponseMessage(HttpStatusCode.OK);
                response.Content = new TestContent(TestJson.NonRDF);
                return(response);
            });

            CacheHttpClient httpClient = new CacheHttpClient(handler);

            using (var client = new DataClient(httpClient, new MemoryFileCache()))
            {
                // this property is inline
                var json = await client.Ensure(new Uri("http://test/doc#a"), new Uri[] { new Uri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type") });

                Assert.Equal(1, count);
                Assert.Null(json);
            }
        }
Пример #8
0
        public async Task DataClient_Ensure_Inline()
        {
            int         count   = 0;
            TestHandler handler = new TestHandler((request) =>
            {
                count++;

                var response = new HttpResponseMessage(HttpStatusCode.OK);

                if (request.RequestUri.AbsoluteUri == "http://test/docBare")
                {
                    response.Content = new TestContent(TestJson.BasicGraphBare);
                }

                if (request.RequestUri.AbsoluteUri == "http://test/doc")
                {
                    response.Content = new TestContent(TestJson.BasicGraph);
                }

                return(response);
            });

            CacheHttpClient httpClient = new CacheHttpClient(handler);

            using (var client = new DataClient(httpClient, new MemoryFileCache()))
            {
                // start with a bare file
                await client.GetFile(new Uri("http://test/docBare"));

                // this property is inline
                var json = await client.Ensure(new Uri("http://test/doc#a"), new Uri[] { new Uri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type") });

                Assert.Equal(1, count);
                Assert.Equal("Child", json["@type"].ToString());
            }
        }
Пример #9
0
        public async Task DataClient_Ensure_NonExist()
        {
            int         count   = 0;
            TestHandler handler = new TestHandler((request) =>
            {
                count++;

                var response = new HttpResponseMessage(HttpStatusCode.OK);

                if (request.RequestUri.AbsoluteUri == "http://test/docBare")
                {
                    response.Content = new TestContent(TestJson.BasicGraphBare);
                }

                if (request.RequestUri.AbsoluteUri == "http://test/doc")
                {
                    response.Content = new TestContent(TestJson.BasicGraph);
                }

                return(response);
            });

            CacheHttpClient httpClient = new CacheHttpClient(handler);

            using (var client = new DataClient(httpClient, new MemoryFileCache()))
            {
                // start with a bare file
                await client.GetFile(new Uri("http://test/docBare"));

                // this property is inline
                var json = await client.Ensure(new Uri("http://test/doc#zzzzzz"), new Uri[] { new Uri("http://nevergoingtoexist/blah") });

                Assert.Equal(2, count);
                Assert.Null(json);
            }
        }
Пример #10
0
        public async Task DataClient_Ensure_JToken_MissingInline()
        {
            int         count   = 0;
            TestHandler handler = new TestHandler((request) =>
            {
                count++;

                var response = new HttpResponseMessage(HttpStatusCode.OK);

                if (request.RequestUri.AbsoluteUri == "http://test/docBare")
                {
                    response.Content = new TestContent(TestJson.BasicGraphBare);
                }

                if (request.RequestUri.AbsoluteUri == "http://test/doc")
                {
                    response.Content = new TestContent(TestJson.BasicGraph);
                }

                return(response);
            });

            CacheHttpClient httpClient = new CacheHttpClient(handler);

            using (var client = new DataClient(httpClient, new MemoryFileCache()))
            {
                // start with a bare file
                JObject bare = await client.GetFile(new Uri("http://test/docBare"));

                // this property is inline
                var json = await client.Ensure(bare["test:items"].First, new Uri[] { new Uri("http://schema.org/test#name") });

                Assert.Equal(2, count);
                Assert.Equal("childA", json["test:name"]);
            }
        }