| 1 |
package rdfwriter |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/spdx/gordf/rdfloader/parser" |
| 5 |
"github.com/spdx/gordf/uri" |
| 6 |
"reflect" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
func max(n1, n2 int) int { |
| 11 |
if n1 > n2 { |
| 12 |
return n1 |
| 13 |
} |
| 14 |
return n2 |
| 15 |
} |
| 16 |
|
| 17 |
func getInvSchema() map[string]string { |
| 18 |
return map[string]string{ |
| 19 |
"http://spdx.org/rdf/terms": "spdx", |
| 20 |
"http://www.w3.org/1999/02/22-rdf-syntax-ns": "rdf", |
| 21 |
"http://www.w3.org/2000/01/rdf-schema": "rdfs", |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
func getNBlankNodes(n int) (blankNodes []*parser.Node) { |
| 28 |
blankNodes = make([]*parser.Node, max(0, n)) |
| 29 |
|
| 30 |
blankNodeGetter := parser.BlankNodeGetter{} |
| 31 |
for i := 0; i < n; i++ { |
| 32 |
newBlankNode := blankNodeGetter.Get() |
| 33 |
blankNodes[i] = &newBlankNode |
| 34 |
} |
| 35 |
return |
| 36 |
} |
| 37 |
|
| 38 |
func Test_getAdjacencyList(t *testing.T) { |
| 39 |
|
| 40 |
|
| 41 |
adjList := GetAdjacencyList([]*parser.Triple{}) |
| 42 |
recoveryDS := GetNodeToTriples([]*parser.Triple{}) |
| 43 |
if len(adjList) > 0 || len(recoveryDS) > 0 { |
| 44 |
t.Errorf("empty input is having non-empty output. Adjacency List: %v, recoveryDS: %v", adjList, recoveryDS) |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
blankNodes := getNBlankNodes(5) |
| 61 |
triples := []*parser.Triple{ |
| 62 |
{ |
| 63 |
Subject: blankNodes[0], |
| 64 |
Predicate: blankNodes[1], |
| 65 |
Object: blankNodes[2], |
| 66 |
}, { |
| 67 |
Subject: blankNodes[0], |
| 68 |
Predicate: blankNodes[3], |
| 69 |
Object: blankNodes[4], |
| 70 |
}, |
| 71 |
} |
| 72 |
adjList = GetAdjacencyList(triples) |
| 73 |
|
| 74 |
if len(adjList) != 3 { |
| 75 |
t.Errorf("adjacency list for the given graph should've only one key. Found %v keys", len(adjList)) |
| 76 |
} |
| 77 |
if nChildren := len(adjList[blankNodes[0]]); nChildren != 2 { |
| 78 |
t.Errorf("Node 0 should've exactly 2 children. Found %v children", nChildren) |
| 79 |
} |
| 80 |
|
| 81 |
for i := 1; i < len(blankNodes); i++ { |
| 82 |
if nChildren := len(adjList[blankNodes[i]]); nChildren > 0 { |
| 83 |
t.Errorf("N%v should have no neighbors. Found %v neighbors", i+1, nChildren) |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
func TestTopologicalSortTriples(t *testing.T) { |
| •89 |
t.Skip("Debian patched to skip flaky test...") |
| 90 |
nodes := getNBlankNodes(5) |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
triples := []*parser.Triple{ |
| 97 |
{nodes[0], nodes[1], nodes[2]}, |
| 98 |
} |
| 99 |
|
| 100 |
sortedTriples, err := TopologicalSortTriples(triples) |
| 101 |
if err != nil { |
| 102 |
t.Errorf("unexpected parsing a single triple list. Error: %v", err) |
| 103 |
} |
| 104 |
if len(sortedTriples) != len(triples) { |
| 105 |
t.Errorf("sorted triples doesn't have a proper dimension. Expected %v triples, found %v triples", len(triples), len(sortedTriples)) |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
nodes = getNBlankNodes(5) |
| 122 |
triples = []*parser.Triple{ |
| 123 |
{Subject: nodes[0], Predicate: nodes[1], Object: nodes[3]}, |
| 124 |
{Subject: nodes[0], Predicate: nodes[2], Object: nodes[3]}, |
| 125 |
{Subject: nodes[3], Predicate: nodes[4], Object: nodes[0]}, |
| 126 |
} |
| 127 |
sortedTriples, err = TopologicalSortTriples(triples) |
| 128 |
|
| 129 |
|
| 130 |
expectedTriples := []*parser.Triple{ |
| 131 |
{Subject: nodes[3], Predicate: nodes[4], Object: nodes[0]}, |
| 132 |
{Subject: nodes[0], Predicate: nodes[1], Object: nodes[3]}, |
| 133 |
{Subject: nodes[0], Predicate: nodes[2], Object: nodes[3]}, |
| 134 |
} |
| 135 |
anotherConfig := []*parser.Triple{ |
| 136 |
{Subject: nodes[0], Predicate: nodes[1], Object: nodes[3]}, |
| 137 |
{Subject: nodes[0], Predicate: nodes[2], Object: nodes[3]}, |
| 138 |
{Subject: nodes[3], Predicate: nodes[4], Object: nodes[0]}, |
| 139 |
} |
| 140 |
if !reflect.DeepEqual(sortedTriples, expectedTriples) && !reflect.DeepEqual(sortedTriples, anotherConfig) { |
| 141 |
t.Errorf("sorted triples are not in correct order") |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
func Test_topologicalSort(t *testing.T) { |
| 146 |
nodes := getNBlankNodes(5) |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
adjList := map[*parser.Node][]*parser.Node{ |
| 154 |
nodes[0]: {nodes[2]}, |
| 155 |
} |
| 156 |
|
| 157 |
_, err := topologicalSort(adjList) |
| 158 |
if err == nil { |
| 159 |
t.Error("expected an error reporting \"extra nodes found\"") |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
adjList = map[*parser.Node][]*parser.Node{ |
| 164 |
nodes[0]: {nodes[2]}, |
| 165 |
nodes[2]: {}, |
| 166 |
} |
| 167 |
sortedNodes, err := topologicalSort(adjList) |
| 168 |
if err != nil { |
| 169 |
t.Errorf("unexpected error: %v", err) |
| 170 |
} |
| 171 |
expectedOutput := []*parser.Node{nodes[2], nodes[0]} |
| 172 |
if !reflect.DeepEqual(sortedNodes, expectedOutput) { |
| 173 |
t.Errorf("nodes are not sorted correctly. \nExpected: %v, \nFound: %v", sortedNodes, expectedOutput) |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
func Test_topologicalSortHelper(t *testing.T) { |
| 178 |
|
| 179 |
var lastIndex int |
| 180 |
var adjList map[*parser.Node][]*parser.Node |
| 181 |
var visited map[*parser.Node]bool |
| 182 |
var resultList []*parser.Node |
| 183 |
|
| 184 |
reinitializeSatellites := func(nNodes int) { |
| 185 |
|
| 186 |
lastIndex = 0 |
| 187 |
visited = make(map[*parser.Node]bool, nNodes) |
| 188 |
resultList = make([]*parser.Node, nNodes) |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
numberNodes := 8 |
| 211 |
nodes := getNBlankNodes(numberNodes) |
| 212 |
triples := []*parser.Triple{ |
| 213 |
{Subject: nodes[0], Predicate: nodes[1], Object: nodes[2]}, |
| 214 |
{Subject: nodes[2], Predicate: nodes[6], Object: nodes[7]}, |
| 215 |
{Subject: nodes[0], Predicate: nodes[3], Object: nodes[4]}, |
| 216 |
{Subject: nodes[3], Predicate: nodes[5], Object: nodes[7]}, |
| 217 |
} |
| 218 |
adjList = GetAdjacencyList(triples) |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
reinitializeSatellites(numberNodes) |
| 223 |
inexistentNode := parser.Node{NodeType: parser.BLANK, ID: "sample node"} |
| 224 |
err := topologicalSortHelper(&inexistentNode, &lastIndex, adjList, &visited, &resultList) |
| 225 |
if err == nil { |
| 226 |
t.Errorf("inexistent node should've raised an error") |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
reinitializeSatellites(numberNodes) |
| 231 |
err = topologicalSortHelper(nodes[7], &lastIndex, adjList, &visited, &resultList) |
| 232 |
if err != nil { |
| 233 |
t.Errorf("unexpected error: %v", err) |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
if lastIndex != 1 { |
| 238 |
t.Errorf("Expected exactly 1 node in the result list. Found %v nodes", lastIndex) |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
reinitializeSatellites(numberNodes) |
| 244 |
_ = topologicalSortHelper(nodes[2], &lastIndex, adjList, &visited, &resultList) |
| 245 |
if lastIndex != 2 { |
| 246 |
t.Errorf("Expected exactly 1 node in the result list. Found %v nodes", lastIndex) |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
if resultList[0] != nodes[7] || resultList[1] != nodes[2] { |
| 251 |
t.Error("resultList is not set properly") |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
reinitializeSatellites(numberNodes) |
| 256 |
_ = topologicalSortHelper(nodes[0], &lastIndex, adjList, &visited, &resultList) |
| 257 |
if lastIndex != 4 { |
| 258 |
t.Errorf("after parsing all nodes, resultList must've 4 nodes. Found %v nodes", lastIndex) |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
if resultList[0] != nodes[7] || resultList[lastIndex-1] != nodes[0] { |
| 263 |
t.Error("order of resultList if not correct") |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
func TestDisjointSet(t *testing.T) { |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
nodes := getNBlankNodes(8) |
| 279 |
triples := []*parser.Triple{ |
| 280 |
{Subject: nodes[0], Predicate: nodes[5], Object: nodes[1]}, |
| 281 |
{Subject: nodes[1], Predicate: nodes[6], Object: nodes[2]}, |
| 282 |
{Subject: nodes[3], Predicate: nodes[7], Object: nodes[4]}, |
| 283 |
} |
| 284 |
parent := DisjointSet(triples) |
| 285 |
nSets := 0 |
| 286 |
for subject := range parent { |
| 287 |
if parent[subject] == nil { |
| 288 |
nSets++ |
| 289 |
} |
| 290 |
} |
| 291 |
if nSets != 2 { |
| 292 |
t.Errorf("Expected Graph to have exactly %d disjoint sets. Found %d sets", 2, nSets) |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
nodes = getNBlankNodes(12) |
| 304 |
triples = []*parser.Triple{ |
| 305 |
{Subject: nodes[0], Predicate: nodes[8], Object: nodes[1]}, |
| 306 |
{Subject: nodes[2], Predicate: nodes[9], Object: nodes[3]}, |
| 307 |
{Subject: nodes[4], Predicate: nodes[10], Object: nodes[5]}, |
| 308 |
{Subject: nodes[6], Predicate: nodes[11], Object: nodes[7]}, |
| 309 |
} |
| 310 |
parent = DisjointSet(triples) |
| 311 |
nSets = 0 |
| 312 |
for node := range parent { |
| 313 |
if parent[node] == nil { |
| 314 |
nSets++ |
| 315 |
} |
| 316 |
} |
| 317 |
if nSets != len(triples) { |
| 318 |
t.Errorf("Mismatch in the number of sets. expected %v disjoint sets, found %v sets", len(triples), nSets) |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
func Test_any(t *testing.T) { |
| 323 |
|
| 324 |
|
| 325 |
listStrings := []string{"c", "b", "a"} |
| 326 |
|
| 327 |
|
| 328 |
targetString := "a" |
| 329 |
if !any(targetString, listStrings) { |
| 330 |
|
| 331 |
t.Errorf("couldn't find %v in %v. even though it exits in the list", targetString, listStrings) |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
targetString = "z" |
| 336 |
if any(targetString, listStrings) { |
| 337 |
|
| 338 |
t.Errorf("found %v in the list: %v. even though it doesn't exist in the list", targetString, listStrings) |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
func Test_invertSchemaDefinition(t *testing.T) { |
| 343 |
|
| 344 |
spdxString := "http://spdx.org/rdf/terms" |
| 345 |
spdxURI, _ := uri.NewURIRef(parser.RDFNS) |
| 346 |
schemaDefinition := map[string]uri.URIRef{ |
| 347 |
"spdx": spdxURI, |
| 348 |
} |
| 349 |
|
| 350 |
inv := invertSchemaDefinition(schemaDefinition) |
| 351 |
expected := map[string]string{spdxString: "spdx"} |
| 352 |
if reflect.DeepEqual(inv, expected) { |
| 353 |
t.Errorf("expected %v, found %v", expected, inv) |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
func Test_getRDFNSAbbreviation(t *testing.T) { |
| 358 |
invSchemaDefinition := make(map[string]string) |
| 359 |
|
| 360 |
|
| 361 |
abbrev := getRDFNSAbbreviation(invSchemaDefinition) |
| 362 |
if abbrev != "rdf" { |
| 363 |
t.Errorf("by default, the rdf namespace should be abbreviated by rdf and not %s", abbrev) |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
expectedAbbrev := "rdfNS" |
| 368 |
invSchemaDefinition[parser.RDFNS] = expectedAbbrev |
| 369 |
abbrev = getRDFNSAbbreviation(invSchemaDefinition) |
| 370 |
if abbrev != expectedAbbrev { |
| 371 |
t.Errorf("expected %s abbreviation, found %v", expectedAbbrev, abbrev) |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
func Test_getRestTriples(t *testing.T) { |
| 376 |
nodes := getNBlankNodes(7) |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
triples := []*parser.Triple{ |
| 381 |
{ |
| 382 |
Subject: nodes[0], |
| 383 |
Predicate: &parser.Node{NodeType: parser.IRI, ID: parser.RDFNS + "type"}, |
| 384 |
Object: nodes[1]}, |
| 385 |
{ |
| 386 |
Subject: nodes[2], |
| 387 |
Predicate: &parser.Node{NodeType: parser.IRI, ID: parser.RDFNS + "nodeID"}, |
| 388 |
Object: nodes[3], |
| 389 |
}, |
| 390 |
} |
| 391 |
restTriples := getRestTriples(triples) |
| 392 |
if n := len(restTriples); n != 0 { |
| 393 |
t.Errorf("expected empty output. got %d nodes", n) |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
triples = append(triples, &parser.Triple{ |
| 399 |
Subject: nodes[4], |
| 400 |
Predicate: nodes[5], |
| 401 |
Object: nodes[6], |
| 402 |
}) |
| 403 |
restTriples = getRestTriples(triples) |
| 404 |
if n := len(restTriples); n != 1 { |
| 405 |
t.Errorf("expected only one extra triple. found %d triples", n) |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
func Test_shortenURI(t *testing.T) { |
| 410 |
invSchema := getInvSchema() |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
uriref := "http://spdx.org/rdf/terms#" |
| 415 |
_, err := shortenURI(uriref, invSchema) |
| 416 |
if err == nil { |
| 417 |
t.Errorf("didn't raise any error for url with no fragment") |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
uriref = "https://www.googlge.com/terms#website" |
| 423 |
_, err = shortenURI(uriref, invSchema) |
| 424 |
if err == nil { |
| 425 |
t.Errorf("expected an error stating baseURI doesn't exist in the given schema") |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
uriref = "http://spdx.org/rdf/terms#Snippet" |
| 430 |
expectedOP := "spdx:Snippet" |
| 431 |
shortURI, err := shortenURI(uriref, invSchema) |
| 432 |
if err != nil { |
| 433 |
t.Errorf("unexpected error converting a valid URI") |
| 434 |
} |
| 435 |
if shortURI != expectedOP { |
| 436 |
t.Errorf("expected output: %v, found: %v", expectedOP, shortURI) |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
func Test_getRootNodes(t *testing.T) { |
| 441 |
nodes := getNBlankNodes(10) |
| 442 |
|
| 443 |
|
| 444 |
triples := []*parser.Triple{ |
| 445 |
{Subject: nodes[0], Predicate: nodes[1], Object: nodes[0]}, |
| 446 |
} |
| 447 |
roots := GetRootNodes(triples) |
| 448 |
if len(roots) != 0 { |
| 449 |
t.Errorf("expected no roots as output. found %v", roots) |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
triples = []*parser.Triple{ |
| 466 |
{Subject: nodes[0], Predicate: nodes[1], Object: nodes[2]}, |
| 467 |
{Subject: nodes[3], Predicate: nodes[4], Object: nodes[5]}, |
| 468 |
{Subject: nodes[6], Predicate: nodes[7], Object: nodes[8]}, |
| 469 |
} |
| 470 |
roots = GetRootNodes(triples) |
| 471 |
if len(roots) != len(triples) { |
| 472 |
t.Errorf("expected %v root nodes, found %v nodes", len(roots), len(triples)) |
| 473 |
} |
| 474 |
} |