Debian Codegraph @

rdfwriter/rdfwriter_test.go

raw symbols SCIP
1 package rdfwriter
2
3 import (
4 "bytes"
5 "github.com/spdx/gordf/rdfloader/parser"
6 "github.com/spdx/gordf/uri"
7 "reflect"
8 "testing"
9 )
10
11 // returns a schemaDefinition with spdx and rdf uris.
12 func getSampleSchemaDefinition() map[string]uri.URIRef {
13 schemaDefinition := make(map[string]uri.URIRef)
14
15 // adding rdf uri:
16 uriref, _ := uri.NewURIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
17 schemaDefinition["rdf"] = uriref
18
19 // adding the spdx uri:
20 uriref, _ = uri.NewURIRef("http://spdx.org/rdf/terms#")
21 schemaDefinition["spdx"] = uriref
22
23 return schemaDefinition
24 }
25
26 func TestFilterTriples(t *testing.T) {
27 nodes := getNBlankNodes(10)
28 triples := []*parser.Triple{
29 {nodes[0], nodes[1], nodes[2]},
30 {nodes[3], nodes[1], nodes[4]},
31 {nodes[3], nodes[5], nodes[4]},
32 }
33
34 // TestCase 1: Default Filtering: must return all the triples
35 triplesAfterFiltering := FilterTriples(triples, nil, nil, nil)
36 if len(triples) != len(triplesAfterFiltering) {
37 t.Errorf("default filtering with all filter params nil didn't return all the triples as the output")
38 }
39
40 // TestCase 2: Filtering on subject:
41 triplesAfterFiltering = FilterTriples(triples, &nodes[3].ID, nil, nil)
42 if !reflect.DeepEqual(triplesAfterFiltering, triples[1:]) {
43 t.Errorf("subject filtering faulty")
44 }
45
46 // TestCase 3: Filtering on predicate
47 triplesAfterFiltering = FilterTriples(triples, nil, &nodes[1].ID, nil)
48 if !reflect.DeepEqual(triplesAfterFiltering, triples[:2]) {
49 t.Errorf("predicate filtering faulty")
50 }
51
52 // TestCase 4: Filtering on object
53 triplesAfterFiltering = FilterTriples(triples, nil, nil, &nodes[4].ID)
54 if !reflect.DeepEqual(triplesAfterFiltering, triples[1:]) {
55 t.Errorf("object filtering faulty")
56 }
57
58 // TestCase 4: Filtering on subject and object both
59 triplesAfterFiltering = FilterTriples(triples, &nodes[3].ID, nil, &nodes[4].ID)
60 if !reflect.DeepEqual(triplesAfterFiltering, triples[1:]) {
61 t.Errorf("faulty filtering for 2 attributes at a time")
62 }
63
64 // TestCase 5: Filtering on predicate
65 triplesAfterFiltering = FilterTriples(triples, nil, &nodes[1].ID, nil)
66 if !reflect.DeepEqual(triplesAfterFiltering, triples[:2]) {
67 t.Errorf("predicate filtering faulty")
68 }
69 }
70
71 func TestTriplesToString(t *testing.T) {
72 // init all required variables for the testing.
73 var triples []*parser.Triple
74 schemaDefinition := getSampleSchemaDefinition()
75 tab := " " // 4 spaces as a tab character
76 bnodes := getNBlankNodes(5)
77
78 // TestCase 1: error raised by stringify must be returned by the function too.
79 // stringify will complain that every subject node must be associated with
80 // at least one triple with predicate rdf:type
81 triples = append(triples, &parser.Triple{
82 Subject: bnodes[0],
83 Predicate: bnodes[1],
84 Object: bnodes[2],
85 })
86 _, err := TriplesToString(triples, schemaDefinition, tab)
87 if err == nil {
88 t.Errorf("expected an error stating invalid triples")
89 }
90
91 // TestCase 2: valid input without any triples must return just the root tag with the schemaDefinition.
92 // clearing the schemaDefinition such that the function returns just the root tags.
93 triples = nil // clearing the slice.
94 schemaDefinition = nil
95 output, err := TriplesToString(triples, schemaDefinition, tab)
96 expectedOutput := `<rdf:RDF>
97 </rdf:RDF>`
98 if output != expectedOutput {
99 t.Errorf("expected output: %s, got output: %s", expectedOutput, output)
100 }
101
102 // TestCase 3: simple testcase with more than one triples.
103 schemaDefinition = getSampleSchemaDefinition()
104 spdxRef := schemaDefinition["spdx"]
105 triples = []*parser.Triple{
106 {
107 Subject: bnodes[0],
108 Predicate: &parser.Node{NodeType: parser.IRI, ID: parser.RDFNS + "type"},
109 Object: &parser.Node{NodeType: parser.IRI, ID: spdxRef.String() + "Snippet"},
110 },
111 {
112 Subject: bnodes[0],
113 Predicate: &parser.Node{NodeType: parser.IRI, ID: spdxRef.String() + "randomFragment"},
114 Object: &parser.Node{NodeType: parser.LITERAL, ID: "sample"},
115 },
116 }
117 output, err = TriplesToString(triples, schemaDefinition, tab)
118 if err != nil {
119 t.Errorf("unexpected error: %v", err)
120 }
121 }
122
123 func TestWriteToFile(t *testing.T) {
124 // init all required variables for the testing.
125 var triples []*parser.Triple
126 schemaDefinition := getSampleSchemaDefinition()
127 tab := " " // 4 spaces as a tab character
128 bnodes := getNBlankNodes(10)
129
130 // TestCase 1: checking if the function returns an error when the
131 // TriplesToString function raises an error.
132 // expected error: every subject node must be associated with exactly 1
133 // triple of type rdf:type predicate.
134 triples = append(triples, &parser.Triple{
135 Subject: bnodes[0],
136 Predicate: bnodes[1],
137 Object: bnodes[2],
138 })
139 var b bytes.Buffer
140 err := WriteToFile(&b, triples, schemaDefinition, tab)
141 if err == nil {
142 t.Errorf("expected an error stating invalid triples")
143 }
144
145 // TestCase 2: valid case with no triples and no schema definitions in the root tag.
146 triples = nil
147 schemaDefinition = nil
148 b = bytes.Buffer{}
149 expectedOutput := `<rdf:RDF>
150 </rdf:RDF>`
151 err = WriteToFile(&b, triples, schemaDefinition, tab)
152 if err != nil {
153 t.Errorf("unexpected error: %s", err)
154 }
155 if b.String() != expectedOutput {
156 t.Errorf("wrong output written to the buffer. expected %s, found %s", expectedOutput, b.String())
157 }
158 }
159
160 func Test_stringify(t *testing.T) {
161 bnodes := getNBlankNodes(10)
162 var triples []*parser.Triple
163 nodeToTriples := GetNodeToTriples(triples)
164 schemaDefinition := getSampleSchemaDefinition()
165 invSchemaDefinition := invertSchemaDefinition(schemaDefinition)
166 depth := 0
167 tab := " " // 2 spaces as the tabs
168
169 // TestCase 1: Invalid case with no triples belonging to the passed node.
170 // expected error: every subject node must be associated with exactly 1
171 // triple of type rdf:type predicate
172 _, err := stringify(bnodes[0], nodeToTriples, invSchemaDefinition, depth, tab)
173 if err == nil {
174 t.Errorf("expected error stating nodes must have a triple with predicate of rdf:type")
175 }
176
177 // TestCase 2: invalid base uri in the object must return an error
178 triples = append(triples, &parser.Triple{
179 Subject: bnodes[0],
180 Predicate: &parser.Node{NodeType: parser.IRI, ID: parser.RDFNS + "type"},
181 Object: &parser.Node{NodeType: parser.IRI, ID: "https://inexistent.com/uri#fragment"},
182 })
183 nodeToTriples = GetNodeToTriples(triples)
184 _, err = stringify(bnodes[0], nodeToTriples, invSchemaDefinition, depth, tab)
185 if err == nil {
186 t.Errorf("expeected an error saying uri not defined in the schemaDefinition")
187 }
188
189 // TestCase 3: valid input with only rdf:type triple
190 triples[0].Object.ID = "http://spdx.org/rdf/terms#Snippet"
191 output, _ := stringify(bnodes[0], nodeToTriples, invSchemaDefinition, depth, tab)
192 expectedOutput := `<spdx:Snippet>
193
194 </spdx:Snippet>`
195 if output != expectedOutput {
196 t.Errorf("output is not correct. expected output is %s. found %s", expectedOutput, output)
197 }
198
199 // TestCase 4: invalid input with two triples: rdf:type and second invalid triples
200 triples = append(triples, &parser.Triple{
201 Subject: bnodes[0],
202 Predicate: &parser.Node{NodeType: parser.IRI, ID: ""},
203 Object: &parser.Node{NodeType: parser.LITERAL, ID: "comment"},
204 })
205 nodeToTriples = GetNodeToTriples(triples)
206 _, err = stringify(bnodes[0], nodeToTriples, invSchemaDefinition, depth, tab)
207 if err == nil {
208 t.Errorf("expected an error saying invalid predicate uri")
209 }
210
211 // TestCase 4: input with two triples: rdf:type and rdf:resource
212 triples[1].Predicate.ID = "http://spdx.org/rdf/terms#algorithm"
213 triples[1].Object = &parser.Node{
214 NodeType: parser.RESOURCELITERAL,
215 ID: "http://spdx.org/rdf/terms#checksumAlgorithm_sha256",
216 }
217 nodeToTriples = GetNodeToTriples(triples)
218 output, _ = stringify(bnodes[0], nodeToTriples, invSchemaDefinition, depth, tab)
219 expectedOutput = `<spdx:Snippet>
220 <spdx:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha256"/>
221 </spdx:Snippet>`
222 if output != expectedOutput {
223 t.Errorf("mismatching outputs. Expected:\n%v\n Found: \n%v", expectedOutput, output)
224 }
225
226 // TestCase 5: input with two triples: rdf:type and rdf:comment and second triple of object-type literal
227 triples[1] = &parser.Triple{
228 Subject: bnodes[0],
229 Predicate: &parser.Node{
230 NodeType: parser.IRI,
231 ID: parser.RDFNS + "comment",
232 },
233 Object: &parser.Node{
234 NodeType: parser.LITERAL,
235 ID: "comment",
236 },
237 }
238 nodeToTriples = GetNodeToTriples(triples)
239 _, err = stringify(bnodes[0], nodeToTriples, invSchemaDefinition, depth, tab)
240 if err != nil {
241 t.Errorf("unexpected error: %v", err)
242 }
243
244 // TestCase 6: input with 3 level deep nested triples.
245 spdxRef := schemaDefinition["spdx"]
246 triples = []*parser.Triple{
247 { // N1 rdf:type spdx:externalRef
248 Subject: bnodes[0],
249 Predicate: &parser.Node{
250 NodeType: parser.IRI,
251 ID: parser.RDFNS + "type",
252 },
253 Object: &parser.Node{
254 NodeType: parser.IRI,
255 ID: spdxRef.String() + "externalRef",
256 },
257 },
258 { // N1 spdx:ExternalRef N2
259 Subject: bnodes[0],
260 Predicate: &parser.Node{
261 NodeType: parser.IRI,
262 ID: spdxRef.String() + "ExternalRef",
263 },
264 Object: bnodes[1],
265 },
266 { // N2 rdf:type spdx:referenceType
267 Subject: bnodes[1],
268 Predicate: &parser.Node{
269 NodeType: parser.IRI,
270 ID: parser.RDFNS + "type",
271 },
272 Object: &parser.Node{
273 NodeType: parser.IRI,
274 ID: spdxRef.String() + "referenceType",
275 },
276 },
277 { // N2 spdx:ReferenceType "http://spdx.org/rdf/references/cpe23Type"
278 Subject: bnodes[1],
279 Predicate: &parser.Node{
280 NodeType: parser.IRI,
281 ID: spdxRef.String() + "ReferenceType",
282 },
283 Object: &parser.Node{
284 NodeType: parser.LITERAL,
285 ID: "http://spdx.org/rdf/references/cpe23Type",
286 },
287 },
288 }
289 nodeToTriples = GetNodeToTriples(triples)
290 output, _ = stringify(bnodes[0], nodeToTriples, invSchemaDefinition, depth, tab)
291 expectedOutput = `<spdx:externalRef>
292 <spdx:ExternalRef>
293 <spdx:referenceType>
294 <spdx:ReferenceType>
295 http://spdx.org/rdf/references/cpe23Type
296 </spdx:ReferenceType>
297 </spdx:referenceType>
298 </spdx:ExternalRef>
299 </spdx:externalRef>`
300 if output != expectedOutput {
301 t.Errorf("mismatching outputs. Expected:\n%v\n Found: \n%v", expectedOutput, output)
302 }
303 }
304
305 func Test_getOpeningAndClosingTags(t *testing.T) {
306 nodes := getNBlankNodes(10)
307 tab := ""
308 rdfNSAbbrev := "rdf"
309 invSchemaDefinition := map[string]string{
310 "http://www.w3.org/1999/02/22-rdf-syntax-ns": "rdf",
311 "http://spdx.org/rdf/terms": "spdx",
312 }
313 var triples []*parser.Triple
314
315 // TestCase 1: empty triple list must return an error
316 _, _, err := getOpeningAndClosingTags(triples, rdfNSAbbrev, invSchemaDefinition, tab, nodes[0])
317 if err == nil {
318 t.Errorf("function should've raised an error stating every node must be associated with a triple of predicate rdf:type")
319 }
320
321 // TestCase 2: exactly one triple of predicate rdf:type but the object uri is invalid.
322 // Must raise an error
323 triples = append(triples, &parser.Triple{
324 Subject: nodes[0],
325 Predicate: &parser.Node{NodeType: parser.IRI, ID: parser.RDFNS + "type"},
326 Object: nodes[2],
327 })
328 _, _, err = getOpeningAndClosingTags(triples, rdfNSAbbrev, invSchemaDefinition, tab, nodes[0])
329 if err == nil {
330 t.Errorf("expected an error saying invalid object uri")
331 }
332
333 // TestCase 3: exactly one triple of predicate rdf:type with valid object uri
334 triples[0].Object = &parser.Node{NodeType: parser.IRI, ID: "http://spdx.org/rdf/terms#Snippet"}
335 openingTag, closingTag, err := getOpeningAndClosingTags(triples, rdfNSAbbrev, invSchemaDefinition, tab, nodes[0])
336 expectedOpeningTag, expectedClosingTag := "<spdx:Snippet>", "</spdx:Snippet>"
337 if openingTag != expectedOpeningTag {
338 t.Errorf("wrong opening tag. expected %s, got %s", expectedOpeningTag, openingTag)
339 }
340 if closingTag != expectedClosingTag {
341 t.Errorf("wrong closing tag. expected %s, got %s", expectedClosingTag, closingTag)
342 }
343
344 // TestCase 3: more than one triple of type rdf:type
345 triples = append(triples, triples[0])
346 _, _, err = getOpeningAndClosingTags(triples, rdfNSAbbrev, invSchemaDefinition, tab, nodes[0])
347 if err == nil {
348 t.Error("function should raise an error when there are more than one triples having rdf:type predicate")
349 }
350
351 // resetting triples with only one rdf:type attribute
352 triples = triples[:1]
353
354 // TestCase 4: exactly one nodeID attribute (a valid case)
355 triples = append(triples, &parser.Triple{
356 Subject: nodes[0],
357 Predicate: &parser.Node{NodeType: parser.IRI, ID: parser.RDFNS + "nodeID"},
358 Object: &parser.Node{parser.LITERAL, "Node34"},
359 })
360 openingTag, closingTag, err = getOpeningAndClosingTags(triples, rdfNSAbbrev, invSchemaDefinition, tab, nodes[0])
361 expectedOpeningTag, expectedClosingTag = `<spdx:Snippet rdf:nodeID="Node34">`, "</spdx:Snippet>"
362 if err != nil {
363 t.Errorf("unexpected error: %v", err)
364 }
365 if openingTag != expectedOpeningTag {
366 t.Errorf("wrong opening tag. expected %s, got %s", expectedOpeningTag, openingTag)
367 }
368 if closingTag != expectedClosingTag {
369 t.Errorf("wrong closing tag. expected %s, got %s", expectedClosingTag, closingTag)
370 }
371
372 // TestCase 5: more than one nodeID attribute (an invalid case)
373 triples = append(triples, triples[1])
374 _, _, err = getOpeningAndClosingTags(triples, rdfNSAbbrev, invSchemaDefinition, tab, nodes[0])
375 if err == nil {
376 t.Error("function should raise an error when there are more than one triples having rdf:nodeID predicate")
377 }
378
379 // resetting triples with only no rdf:nodeID and only one rdf:type attribute.
380 triples = triples[:1]
381
382 // TestCase 6: invalid rdf:type uri
383 triples[0].Object = nodes[3]
384 // predicate of triples[0] is rdf:type. It expects the object uri of
385 // type baseName:fragment. but nodes[3] is a blank node with ID "N4"
386 _, _, err = getOpeningAndClosingTags(triples, rdfNSAbbrev, invSchemaDefinition, tab, nodes[0])
387 if err == nil {
388 t.Error("expected an invalid uri error")
389 }
390
391 // TestCase 7: Valid case where we have a rdf:about tag
392 triples = []*parser.Triple{
393 { // rdf:type="http://spdx.org/rdf/terms#Snippet"
394 Subject: nodes[0],
395 Predicate: &parser.Node{NodeType: parser.IRI, ID: parser.RDFNS + "type"},
396 Object: &parser.Node{NodeType: parser.IRI, ID: "http://spdx.org/rdf/terms#Snippet"},
397 },
398 { // rdf:about="http://spdx.org/rdf/terms#Snippet132"
399 Subject: nodes[0],
400 Predicate: &parser.Node{NodeType: parser.IRI, ID: parser.RDFNS + "type"},
401 Object: &parser.Node{NodeType: parser.IRI, ID: "http://spdx.org/rdf/terms#Snippet132"},
402 },
403 }
404 _, _, err = getOpeningAndClosingTags(triples, rdfNSAbbrev, invSchemaDefinition, tab, nodes[0])
405 }
406
407 func Test_getRootTagFromSchemaDefinition(t *testing.T) {
408 schemaDefinition := make(map[string]uri.URIRef)
409 tab := " "
410
411 // TestCase 1: empty schema definition
412 rootTag := getRootTagFromSchemaDefinition(schemaDefinition, tab)
413 expectedOp := "<rdf:RDF>"
414 if rootTag != expectedOp {
415 t.Errorf("incorrect output. expected %s, found %s", expectedOp, rootTag)
416 }
417
418 // TestCase 2: only one url in the schemaDefinition
419 rdfURI, _ := uri.NewURIRef(parser.RDFNS)
420 schemaDefinition["rdf"] = rdfURI
421 rootTag = getRootTagFromSchemaDefinition(schemaDefinition, tab)
422 expectedOp = `<rdf:RDF
423 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">`
424 if rootTag != expectedOp {
425 t.Errorf("incorrect output. expected %s, found %s", expectedOp, rootTag)
426 }
427
428 // Note: not checking for outputs with more than one uri because, the
429 // input to the function is a map object and the function iterates over the keys and
430 // linearly generates the output. Since the keys in a map are unordered,
431 // the output might be different on every run.
432 }