private void ParsingStoreHandlerNQuadsImplicitActual()
        {
            this.EnsureTestData("test.nq");

            TripleStore store = new TripleStore();

            NQuadsParser parser = new NQuadsParser();

            parser.Load(store, "test.nq");

            Assert.IsTrue(store.HasGraph(new Uri("http://www.dotnetrdf.org/configuration#")), "Configuration Vocab Graph should have been parsed from Dataset");
            Graph configOrig = new Graph();

            configOrig.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");
            IGraph config = store[new Uri("http://www.dotnetrdf.org/configuration#")];

            Assert.AreEqual(configOrig, config, "Configuration Vocab Graphs should have been equal");

            Assert.IsTrue(store.HasGraph(new Uri("http://www.dotnetrdf.org/leviathan#")), "Leviathan Function Library Graph should have been parsed from Dataset");
            Graph lvnOrig = new Graph();

            lvnOrig.LoadFromEmbeddedResource("VDS.RDF.Query.Expressions.LeviathanFunctionLibrary.ttl");
            IGraph lvn = store[new Uri("http://www.dotnetrdf.org/leviathan#")];

            Assert.AreEqual(lvnOrig, lvn, "Leviathan Function Library Graphs should have been equal");
        }
        public void JsonLdParserTests(string inputPath, string contextPath, string expectedOutputPath, string baseIri,
                                      string processorMode, string expandContextPath, bool compactArrays)
        {
            var processorOptions = MakeProcessorOptions(inputPath, baseIri, processorMode, expandContextPath,
                                                        compactArrays);
            var contextJson    = contextPath == null ? null : File.ReadAllText(contextPath);
            var contextElement = contextJson == null ? null : JToken.Parse(contextJson);
            var nqParser       = new NQuadsParser(NQuadsSyntax.Rdf11);
            var expectedStore  = new TripleStore();

            nqParser.Load(expectedStore, expectedOutputPath);
            FixStringLiterals(expectedStore);
            var jsonldParser = new JsonLdParser(processorOptions);
            var actualStore  = new TripleStore();

            jsonldParser.Load(actualStore, inputPath);
            Assert.True(expectedStore.Graphs.Count.Equals(actualStore.Graphs.Count),
                        $"Test failed for input {Path.GetFileName(inputPath)}.\r\nActual graph count {actualStore.Graphs.Count} does not match expected graph count {expectedStore.Graphs.Count}.");
            foreach (var expectGraph in expectedStore.Graphs)
            {
                Assert.True(actualStore.HasGraph(expectGraph.BaseUri),
                            $"Test failed for input {Path.GetFileName(inputPath)}.\r\nCould not find expected graph {expectGraph.BaseUri}");
                var actualGraph  = actualStore.Graphs[expectGraph.BaseUri];
                var bNodeMapping = new Dictionary <INode, INode>();
                var graphsEqual  = actualGraph.Equals(expectGraph, out bNodeMapping);
                if (!graphsEqual)
                {
                    var    ser           = new NQuadsWriter();
                    string expectedLines = MakeNQuadsList(expectedStore);
                    string actualLines   = MakeNQuadsList(actualStore);
                    Assert.True(graphsEqual,
                                $"Test failed for input {Path.GetFileName(inputPath)}.\r\nGraph {expectGraph.BaseUri} differs in actual output from expected output.\r\nExpected:\r\n{expectedLines}\r\nActual:\r\n{actualLines}");
                }
            }
        }
#pragma warning disable xUnit1026 // Theory methods should use all of their parameters
        public void JsonLdWriterTests(string inputPath, string contextPath, string expectedOutputPath, bool useNativeTypes, bool useRdfType)
#pragma warning restore xUnit1026 // Theory methods should use all of their parameters
        {
            var nqParser = new NQuadsParser(NQuadsSyntax.Rdf11);
            var input    = new TripleStore();

            nqParser.Load(input, inputPath);
            FixStringLiterals(input);
            var expectedOutputJson = File.ReadAllText(expectedOutputPath);
            var expectedOutput     = JToken.Parse(expectedOutputJson);
            var jsonLdWriter       =
                new JsonLdWriter(new JsonLdWriterOptions {
                UseNativeTypes = useNativeTypes, UseRdfType = useRdfType
            });
            var actualOutput = jsonLdWriter.SerializeStore(input);

            try
            {
                Assert.True(DeepEquals(expectedOutput, actualOutput, true, true),
                            $"Test failed for input {Path.GetFileName(inputPath)}\nExpected:\n{expectedOutput}\nActual:\n{actualOutput}");
            }
            catch (DeepEqualityFailure ex)
            {
                Assert.True(false,
                            $"Test failed for input {Path.GetFileName(inputPath)}\nExpected:\n{expectedOutput}\nActual:\n{actualOutput}\nMatch Failured: {ex}");
            }
        }
Esempio n. 4
0
        public void ParsingStoreHandlerNQuadsExplicit()
        {
            this.EnsureTestData("test.nq");

            TripleStore store = new TripleStore();

            NQuadsParser parser = new NQuadsParser();

            parser.Load(new StoreHandler(store), "test.nq");

            Assert.True(store.HasGraph(new Uri("http://graphs/1")), "Configuration Vocab Graph should have been parsed from Dataset");
            Graph configOrig = new Graph();

            configOrig.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");
            IGraph config = store[new Uri("http://graphs/1")];

            Assert.Equal(configOrig, config);

            Assert.True(store.HasGraph(new Uri("http://graphs/2")), "Leviathan Function Library Graph should have been parsed from Dataset");
            Graph lvnOrig = new Graph();

            lvnOrig.LoadFromEmbeddedResource("VDS.RDF.Query.Expressions.LeviathanFunctionLibrary.ttl");
            IGraph lvn = store[new Uri("http://graphs/2")];

            Assert.Equal(lvnOrig, lvn);
        }
Esempio n. 5
0
        private IGraph CreateGraph(string s)
        {
            var nqr = new NQuadsParser();
            var ts  = new TripleStore();

            nqr.Load(ts, new StringReader(s));
            return(ts.Graphs.FirstOrDefault());
        }
        protected void ParsingStoreHandlerBlankNodesNQuadsActual()
        {
            EnsureTestData("test-bnodes.nq");

            NQuadsParser parser = new NQuadsParser();
            TripleStore  store  = new TripleStore();

            parser.Load(store, "test-bnodes.nq");

            EnsureTestResults(store);
        }
        private void TestWriteToStoreDatasetsHandler(IStorageProvider manager)
        {
            NodeFactory factory = new NodeFactory();
            INode       a       = factory.CreateUriNode(new Uri("http://example.org/a"));
            INode       b       = factory.CreateUriNode(new Uri("http://example.org/b"));
            INode       c       = factory.CreateUriNode(new Uri("http://example.org/c"));
            INode       d       = factory.CreateUriNode(new Uri("http://example.org/d"));

            Uri graphB = new Uri("http://example.org/graphs/b");
            Uri graphD = new Uri("http://example.org/graphs/d");

            //Try to ensure that the target Graphs do not exist
            if (manager.DeleteSupported)
            {
                manager.DeleteGraph(TestGraphUri);
                manager.DeleteGraph(graphB);
                manager.DeleteGraph(graphD);
            }
            else
            {
                Graph g = new Graph();
                g.BaseUri = TestGraphUri;
                manager.SaveGraph(g);
                g.BaseUri = graphB;
                manager.SaveGraph(g);
                g.BaseUri = graphD;
                manager.SaveGraph(g);
            }

            //Do the parsing and thus the loading
            WriteToStoreHandler handler = new WriteToStoreHandler(manager, TestGraphUri);
            NQuadsParser        parser  = new NQuadsParser();

            parser.Load(handler, new StreamReader("writetostore.nq"));

            //Load the expected Graphs
            Graph def = new Graph();

            manager.LoadGraph(def, TestGraphUri);
            Graph gB = new Graph();

            manager.LoadGraph(gB, graphB);
            Graph gD = new Graph();

            manager.LoadGraph(gD, graphD);

            Assert.AreEqual(2, def.Triples.Count, "Should be two triples in the default Graph");
            Assert.IsTrue(def.ContainsTriple(new Triple(a, a, a)), "Default Graph should have the a triple");
            Assert.AreEqual(1, gB.Triples.Count, "Should be one triple in the b Graph");
            Assert.IsTrue(gB.ContainsTriple(new Triple(b, b, b)), "b Graph should have the b triple");
            Assert.IsTrue(def.ContainsTriple(new Triple(c, c, c)), "Default Graph should have the c triple");
            Assert.AreEqual(1, gD.Triples.Count, "Should be one triple in the d Graph");
            Assert.IsTrue(gD.ContainsTriple(new Triple(d, d, d)), "d Graph should have the d triple");
        }
        private void ParsingStoreHandlerNQuadsCountingActual()
        {
            this.EnsureTestData("test.nq");

            NQuadsParser      parser  = new NQuadsParser();
            StoreCountHandler counter = new StoreCountHandler();

            parser.Load(counter, "test.nq");

            Graph configOrig = new Graph();

            configOrig.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");
            Graph lvnOrig = new Graph();

            lvnOrig.LoadFromEmbeddedResource("VDS.RDF.Query.Expressions.LeviathanFunctionLibrary.ttl");

            Assert.AreEqual(2, counter.GraphCount, "Expected 2 Graphs to be counted");
            Assert.AreEqual(configOrig.Triples.Count + lvnOrig.Triples.Count, counter.TripleCount, "Expected Triple Count to be sum of Triple Counts in two input Graphs");
        }
Esempio n. 9
0
        public void ParsingStoreHandlerNQuadsCounting()
        {
            this.EnsureTestData("test.nq");

            NQuadsParser      parser  = new NQuadsParser();
            StoreCountHandler counter = new StoreCountHandler();

            parser.Load(counter, "test.nq");

            Graph configOrig = new Graph();

            configOrig.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");
            Graph lvnOrig = new Graph();

            lvnOrig.LoadFromEmbeddedResource("VDS.RDF.Query.Expressions.LeviathanFunctionLibrary.ttl");

            Assert.Equal(2, counter.GraphCount);
            Assert.Equal(configOrig.Triples.Count + lvnOrig.Triples.Count, counter.TripleCount);
        }
        public virtual void JsonLdParserTests(string testId, JsonLdTestType testType, string inputPath, string contextPath,
                                              string expectedOutputPath, JsonLdErrorCode expectedErrorCode, string baseIri,
                                              string processorMode, string expandContextPath, bool compactArrays, string rdfDirection)
        {
            var processorOptions = MakeProcessorOptions(inputPath, baseIri, processorMode, expandContextPath,
                                                        compactArrays, rdfDirection);
            var jsonldParser = new JsonLdParser(processorOptions);
            var actualStore  = new TripleStore();

            switch (testType)
            {
            case JsonLdTestType.PositiveEvaluationTest:
                var nqParser      = new NQuadsParser(NQuadsSyntax.Rdf11);
                var expectedStore = new TripleStore();
                nqParser.Load(expectedStore, expectedOutputPath);
                FixStringLiterals(expectedStore);
                jsonldParser.Load(actualStore, inputPath);
                Assert.True(expectedStore.Graphs.Count.Equals(actualStore.Graphs.Count) ||
                            (expectedStore.Graphs.Count == 0 && actualStore.Graphs.Count == 1 &&
                             actualStore.Graphs[null].IsEmpty),
                            $"Test failed for input {Path.GetFileName(inputPath)}.\r\nActual graph count {actualStore.Graphs.Count} does not match expected graph count {expectedStore.Graphs.Count}.");
                AssertStoresEqual(expectedStore, actualStore, Path.GetFileName(inputPath));
                break;

            case JsonLdTestType.NegativeEvaluationTest:
                var exception =
                    Assert.Throws <JsonLdProcessorException>(() => jsonldParser.Load(actualStore, inputPath));
                Assert.Equal(expectedErrorCode, exception.ErrorCode);
                break;

            case JsonLdTestType.PositiveSyntaxTest:
                // Positive syntax test should load input file without raising any exceptions
                jsonldParser.Load(actualStore, inputPath);
                break;

            default:
                Assert.True(false, $"Test type {testType} is not currently supported for the JSON-LD Parser tests");
                break;
            }
        }
Esempio n. 11
0
        public static JToken FromRdf(IEnumerable <string> statements)
        {
            var stringBuilder = new StringBuilder();

            foreach (var item in statements)
            {
                stringBuilder.AppendLine(item);
            }
            var reader = new StringReader(stringBuilder.ToString());

            var store  = new TripleStore();
            var parser = new NQuadsParser(NQuadsSyntax.Rdf11);

            parser.Load(store, reader);

            var ldWriter     = new JsonLdWriter();
            var stringWriter = new System.IO.StringWriter();

            ldWriter.Save(store, stringWriter);

            return(JToken.Parse(stringWriter.ToString()));
        }
#pragma warning disable xUnit1026 // Theory methods should use all of their parameters
        public virtual void JsonLdWriterTests(string testId, JsonLdTestType testType, string inputPath, string contextPath,
                                              string expectedOutputPath, JsonLdErrorCode expectErrorCode, bool useNativeTypes, bool useRdfType, bool ordered, string rdfDirection)
#pragma warning restore xUnit1026 // Theory methods should use all of their parameters
        {
            var nqParser = new NQuadsParser(NQuadsSyntax.Rdf11);
            var input    = new TripleStore();

            nqParser.Load(input, inputPath);
            FixStringLiterals(input);
            var writerOptions = new JsonLdWriterOptions
            {
                UseNativeTypes = useNativeTypes, UseRdfType = useRdfType, Ordered = ordered
            };

            if (rdfDirection != null)
            {
                switch (rdfDirection)
                {
                case "i18n-datatype":
                    writerOptions.RdfDirection = JsonLdRdfDirectionMode.I18NDatatype;
                    break;

                case "compound-literal":
                    writerOptions.RdfDirection = JsonLdRdfDirectionMode.CompoundLiteral;
                    break;

                default:
                    throw new Exception($"Test {testId} specifies an unrecognized value for the rdfDirection option: {rdfDirection}.");
                }
            }
            var jsonLdWriter = new JsonLdWriter(writerOptions);

            switch (testType)
            {
            case JsonLdTestType.PositiveEvaluationTest:
            {
                var actualOutput       = jsonLdWriter.SerializeStore(input);
                var expectedOutputJson = File.ReadAllText(expectedOutputPath);
                var expectedOutput     = JToken.Parse(expectedOutputJson);

                try
                {
                    Assert.True(DeepEquals(expectedOutput, actualOutput, true, true),
                                $"Test failed for input {Path.GetFileName(inputPath)}\nExpected:\n{expectedOutput}\nActual:\n{actualOutput}");
                }
                catch (DeepEqualityFailure ex)
                {
                    Assert.True(false,
                                $"Test failed for input {Path.GetFileName(inputPath)}\nExpected:\n{expectedOutput}\nActual:\n{actualOutput}\nMatch Failured: {ex}");
                }

                break;
            }

            case JsonLdTestType.NegativeEvaluationTest:
            {
                var exception = Assert.Throws <JsonLdProcessorException>(() => jsonLdWriter.SerializeStore(input));
                Assert.Equal(expectErrorCode, exception.ErrorCode);
                break;
            }

            case JsonLdTestType.PositiveSyntaxTest:
                var _ = jsonLdWriter.SerializeStore(input);
                break;

            case JsonLdTestType.NegativeSyntaxTest:
                Assert.ThrowsAny <JsonLdProcessorException>(() => jsonLdWriter.SerializeStore(input));
                break;
            }
        }