Debian Codegraph @

rdfloader/parser/parser_test.go

raw symbols SCIP
1 package parser
2
3 import (
4 "bufio"
5 "bytes"
6 xmlreader "github.com/spdx/gordf/rdfloader/xmlreader"
7 "io"
8 "testing"
9 )
10
11 func xmlreaderFromString(fileContent string) xmlreader.XMLReader {
12 return xmlreader.XMLReaderFromFileObject(bufio.NewReader(io.Reader(bytes.NewReader([]byte(fileContent)))))
13 }
14
15 func TestTriple_Hash(t *testing.T) {
16 testTriple := Triple{
17 Subject: &Node{BLANK, ""},
18 Predicate: &Node{BLANK, ""},
19 Object: &Node{BLANK, ""},
20 }
21
22 expectedHash := "{(BNODE, ); (BNODE, ); (BNODE, )}"
23 if expectedHash != testTriple.Hash() {
24 t.Errorf("expected %v, found %v", expectedHash, testTriple.Hash())
25 }
26 }
27
28 func TestNew(t *testing.T) {
29 // testing if the initialized parameters are okay.
30 newParser := New()
31
32 // Triples should be initially empty
33 if len(newParser.Triples) > 0 {
34 t.Errorf("Initialized parser shouldn't have any triple")
35 }
36 }
37
38 func TestParser_Parse(t *testing.T) {
39
40 // TestCase 1: only root tag in the document.
41 func() {
42 emptyValidRDF := `
43 <rdf:RDF
44 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
45 xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
46 </rdf:RDF>`
47 rdfParser := New()
48 xmlReader := xmlreaderFromString(emptyValidRDF)
49 rootBlock, err := xmlReader.Read()
50 if err != nil {
51 return
52 }
53 err = rdfParser.Parse(rootBlock)
54 // there shouldn't be any error parsing the content
55 if err != nil {
56 t.Errorf("unexpected error parsing the document: %v", err)
57 }
58 // there shouldn't be any triple in the parsed document.
59 if len(rdfParser.Triples) != 0 {
60 t.Errorf("empty document must have no triples. Found %v", rdfParser.Triples)
61 }
62 }()
63
64 // TestCase 2:
65 // empty rdf with prolog
66 func() {
67 emptyRDFWithProlog := `<? xml version="1.0" ?>
68 <rdf:RDF
69 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
70 xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
71 </rdf:RDF>`
72 xmlReader := xmlreaderFromString(emptyRDFWithProlog)
73 rootBlock, err := xmlReader.Read()
74 if err != nil {
75 return
76 }
77 rdfParser := New()
78 err = rdfParser.Parse(rootBlock)
79 // there shouldn't be any error parsing the content
80 if err != nil {
81 t.Errorf("unexpected error parsing the document: %v", err)
82 }
83 // there shouldn't be any triple in the parsed document and
84 // the prolog is not counted in the triples
85 if len(rdfParser.Triples) != 0 {
86 t.Errorf("empty document must have no triples. Found %v", rdfParser.Triples)
87 }
88 }()
89
90 // TestCase 3:
91 // Invalid RDF with stray characters before closing tag.
92 func() {
93 invalidRDF := "......<rdf:RDF>"
94 xmlReader := xmlreaderFromString(invalidRDF)
95 rootBlock, err := xmlReader.Read()
96 if err != nil {
97 return
98 }
99 rdfParser := New()
100 err = rdfParser.Parse(rootBlock)
101 if err == nil {
102 t.Errorf("should've raised an error stating stray characters found")
103 }
104 }()
105
106 // TestCase 4:
107 // Valid RDF with single valid triple.
108 func() {
109 twoTripleRDF := `
110 <rdf:RDF
111 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
112 xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
113 xmlns:example="https://www.sample.com/example">
114 <rdf:Description>
115 <example:Tag> Name </example:Tag>
116 </rdf:Description>
117 </rdf:RDF>`
118 xmlReader := xmlreaderFromString(twoTripleRDF)
119 rootBlock, err := xmlReader.Read()
120 if err != nil {
121 return
122 }
123 rdfParser := New()
124 err = rdfParser.Parse(rootBlock)
125 if err != nil {
126 t.Errorf("error parsing a valid rdf file. Error: %v", err)
127 }
128 if len(rdfParser.Triples) != 2 {
129 t.Errorf("expected rdfParser to have exactly two triples. %v triples found", len(rdfParser.Triples))
130 }
131 }()
132
133 // TestCase 5: extra tag in the rdf.
134 func() {
135 extraTagRDF := `
136 <rdf:RDF
137 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
138 xmlns:example="https://www.sample.com/example">
139 <rdf:Description>
140 <example:Tag> Name </example:Tag>
141 </rdf:Description>
142 <example:extraTag>
143 </rdf:RDF>`
144 xmlReader := xmlreaderFromString(extraTagRDF)
145 rootBlock, err := xmlReader.Read()
146 if err != nil {
147 return
148 }
149 rdfParser := New()
150 err = rdfParser.Parse(rootBlock)
151 if err == nil {
152 t.Errorf("expected an EOF error")
153 }
154 }()
155
156 // TestCase 6: mismatch tag
157 func() {
158 invalidRDF := `
159 <rdf:RDF
160 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
161 </rdf:rdf>`
162 xmlReader := xmlreaderFromString(invalidRDF)
163 rootBlock, err := xmlReader.Read()
164 if err != nil {
165 return
166 }
167 rdfParser := New()
168 err = rdfParser.Parse(rootBlock)
169 if err == nil {
170 t.Errorf("expected an error stating opening and closing tags are not same")
171 }
172 }()
173 }
174
175 func Test_parseHeaderBlock(t *testing.T) {
176 // parseHeaderBlock returns all the schema definitions in the input rootBlock.
177
178 // TestCase 1: empty root block without any children or attributes.
179 // should return a map with only rdf namespace declaration
180 rootBlock := xmlreader.Block{
181 OpeningTag: xmlreader.Tag{
182 SchemaName: "",
183 Name: "",
184 Attrs: nil,
185 },
186 Value: "",
187 Children: nil,
188 }
189 schemaDefinition, err := parseHeaderBlock(rootBlock)
190 if err != nil {
191 t.Errorf("unexpected error : %v", err)
192 return
193 }
194 if n := len(schemaDefinition); n != 1 {
195 t.Errorf("expected schemaDefinition to have exactly one default namespace declaration of rdf. found %d declarations", n)
196 }
197 // check if the "rdf" uri is correct
198 ref := schemaDefinition["rdf"]
199 if ref.String() != RDFNS {
200 t.Errorf("default schema uri for rdf should be %s, found %s", RDFNS, ref.String())
201 }
202
203 // TestCase 2: root block with invalid schema name attribute and no children
204 rootBlock = xmlreader.Block{
205 OpeningTag: xmlreader.Tag{
206 SchemaName: "",
207 Name: "",
208 Attrs: []xmlreader.Attribute{
209 {
210 Name: "rdf",
211 SchemaName: "xmlns",
212 Value: "invalid uri",
213 },
214 },
215 },
216 Value: "",
217 Children: nil,
218 }
219 _, err = parseHeaderBlock(rootBlock)
220 if err == nil {
221 t.Error("should've raised an error saying invalid uri received")
222 }
223
224 // TestCase 3: redefined "rdf" attribute must not be over-written by the default namespace.
225 newRDFNS := "https://www.sample.com/rdf#"
226 rootBlock = xmlreader.Block{
227 OpeningTag: xmlreader.Tag{
228 SchemaName: "",
229 Name: "",
230 Attrs: []xmlreader.Attribute{
231 {
232 Name: "rdf",
233 SchemaName: "xmlns",
234 Value: newRDFNS,
235 },
236 },
237 },
238 Value: "",
239 Children: nil,
240 }
241 schemaDefinition, err = parseHeaderBlock(rootBlock)
242 uriRef := schemaDefinition["rdf"]
243 if uriRef.String() != newRDFNS {
244 t.Errorf("expected uri: %s, found %s", newRDFNS, uriRef.String())
245 }
246
247 // TestCase 3: Valid Case: exactly one namespace declaration which is not rdf.
248 doapNS := `http://usefulinc.com/ns/doap#`
249 rootBlock = xmlreader.Block{
250 OpeningTag: xmlreader.Tag{
251 SchemaName: "",
252 Name: "",
253 Attrs: []xmlreader.Attribute{
254 {
255 Name: "doap",
256 SchemaName: "xmlns",
257 Value: doapNS,
258 },
259 },
260 },
261 Value: "",
262 Children: nil,
263 }
264 schemaDefinition, err = parseHeaderBlock(rootBlock)
265 if len(schemaDefinition) != 2 {
266 t.Errorf("after parsing rootblock with one namespace " +
267 "declaration(not xmlns:rdf), the schema-definition " +
268 "should've two elements")
269 }
270 rdfURI := schemaDefinition["rdf"]
271 doapURI := schemaDefinition["doap"]
272 if rdfURI.String() != RDFNS || doapURI.String() != doapNS {
273 t.Errorf("invalid schema definition: %v", schemaDefinition)
274 }
275 }