aboutsummaryrefslogtreecommitdiff
path: root/rdfloader
diff options
context:
space:
mode:
authorRishabhBhatnagar <bhatnagarrishabh4@gmail.com>2020-08-27 14:46:09 +0530
committerRishabhBhatnagar <bhatnagarrishabh4@gmail.com>2020-08-27 14:46:09 +0530
commit6a86ef8c699cb448effc306f5212f4f6ca14b854 (patch)
tree86adbe5a161ae1b8f9e625cd51e339b74dd22d6c /rdfloader
parentf652816522876a038fed37d869039ee38311f253 (diff)
downloadspdx-tools-6a86ef8c699cb448effc306f5212f4f6ca14b854.tar.gz
Add Tests And Bug Fixes For Relationship
Signed-off-by: RishabhBhatnagar <bhatnagarrishabh4@gmail.com>
Diffstat (limited to 'rdfloader')
-rw-r--r--rdfloader/parser2v2/parse_relationship.go6
-rw-r--r--rdfloader/parser2v2/parse_relationship_test.go302
2 files changed, 305 insertions, 3 deletions
diff --git a/rdfloader/parser2v2/parse_relationship.go b/rdfloader/parser2v2/parse_relationship.go
index b4c4704..91f44f8 100644
--- a/rdfloader/parser2v2/parse_relationship.go
+++ b/rdfloader/parser2v2/parse_relationship.go
@@ -47,7 +47,7 @@ func (parser *rdfParser2_2) parseRelationship(triple *gordfParser.Triple) (err e
if len(typeTriples) != 1 {
return fmt.Errorf("expected %s to have exactly one rdf:type triple. found %d triples", subTriple.Object, len(typeTriples))
}
- err = parser.parseRelatedElementFromTriple(reln, typeTriples[0])
+ err = parser.parseRelatedElementFromTriple(&reln, typeTriples[0])
if err != nil {
return err
}
@@ -65,7 +65,7 @@ func (parser *rdfParser2_2) parseRelationship(triple *gordfParser.Triple) (err e
return nil
}
-func (parser *rdfParser2_2) parseRelatedElementFromTriple(reln spdx.Relationship2_2, triple *gordfParser.Triple) error {
+func (parser *rdfParser2_2) parseRelatedElementFromTriple(reln *spdx.Relationship2_2, triple *gordfParser.Triple) error {
// iterate over relatedElement Type and check which SpdxElement it is.
var err error
switch triple.Object.ID {
@@ -105,7 +105,6 @@ func (parser *rdfParser2_2) parseRelatedElementFromTriple(reln spdx.Relationship
return nil
}
-
// references like RefA and RefB of any relationship
func getReferenceFromURI(uri string) (spdx.DocElementID, error) {
fragment := getLastPartOfURI(uri)
@@ -119,6 +118,7 @@ func getReferenceFromURI(uri string) (spdx.DocElementID, error) {
return ExtractDocElementID(fragment)
}
+// note: relationshipType is case sensitive.
func getRelationshipTypeFromURI(relnTypeURI string) (string, error) {
relnTypeURI = strings.TrimSpace(relnTypeURI)
lastPart := getLastPartOfURI(relnTypeURI)
diff --git a/rdfloader/parser2v2/parse_relationship_test.go b/rdfloader/parser2v2/parse_relationship_test.go
new file mode 100644
index 0000000..0aa8315
--- /dev/null
+++ b/rdfloader/parser2v2/parse_relationship_test.go
@@ -0,0 +1,302 @@
+package parser2v2
+
+import (
+ "github.com/RishabhBhatnagar/gordf/rdfwriter"
+ "github.com/spdx/tools-golang/spdx"
+ "reflect"
+ "testing"
+)
+
+func Test_getReferenceFromURI(t *testing.T) {
+ // TestCase 1: noassertion uri
+ ref, err := getReferenceFromURI(SPDX_NOASSERTION_CAPS)
+ if err != nil {
+ t.Errorf("unexpected error: %v", err)
+ }
+ if ref.DocumentRefID != "" {
+ t.Errorf("reference's documentRefID should've been empty, found %s", ref.DocumentRefID)
+ }
+ if ref.ElementRefID != "NOASSERTION" {
+ t.Errorf("mismatching elementRefID. Found %s, expected %s", ref.ElementRefID, "NOASSERTION")
+ }
+
+ // TestCase 2: NONE uri
+ ref, err = getReferenceFromURI(SPDX_NONE_CAPS)
+ if err != nil {
+ t.Errorf("unexpected error: %v", err)
+ }
+ if ref.DocumentRefID != "" {
+ t.Errorf("reference's documentRefID should've been empty, found %s", ref.DocumentRefID)
+ }
+ if ref.ElementRefID != "NONE" {
+ t.Errorf("mismatching elementRefID. Found %s, expected %s", ref.ElementRefID, "NONE")
+ }
+
+ // TestCase 3: Valid URI
+ ref, err = getReferenceFromURI(NS_SPDX + "SPDXRef-item1")
+ if err != nil {
+ t.Errorf("unexpected error: %v", err)
+ }
+ if ref.DocumentRefID != "" {
+ t.Errorf("reference's documentRefID should've been empty, found %s", ref.DocumentRefID)
+ }
+ if ref.ElementRefID != "item1" {
+ t.Errorf("mismatching elementRefID. Found %s, expected %s", ref.ElementRefID, "item1")
+ }
+
+ // TestCase 3: Invalid URI
+ _, err = getReferenceFromURI(NS_SPDX + "item1")
+ if err == nil {
+ t.Errorf("should've raised an error for invalid input")
+ }
+}
+
+func Test_getRelationshipTypeFromURI(t *testing.T) {
+ // TestCase 1: valid relationshipType
+ relnType := "expandedFromArchive"
+ op, err := getRelationshipTypeFromURI(NS_SPDX + "relationshipType_" + relnType)
+ if err != nil {
+ t.Errorf("error getting relationship type from a valid input")
+ }
+ if op != relnType {
+ t.Errorf("expected %s, found %s", relnType, op)
+ }
+
+ // TestCase2: invalid relationshipType
+ relnType = "invalidRelationship"
+ _, err = getRelationshipTypeFromURI(NS_SPDX + "relationshipType_" + relnType)
+ if err == nil {
+ t.Errorf("should've raised an error for an invalid input(%s)", relnType)
+ }
+}
+
+func Test_rdfParser2_2_parseRelatedElementFromTriple(t *testing.T) {
+ // TestCase 1: Package as a related element
+ parser, _ := parserFromBodyContent(`
+ <spdx:Relationship>
+ <spdx:relatedSpdxElement>
+ <spdx:Package rdf:about="http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301#SPDXRef-Saxon"/>
+ </spdx:relatedSpdxElement>
+ </spdx:Relationship>
+ `)
+ reln := &spdx.Relationship2_2{}
+ triple := rdfwriter.FilterTriples(parser.gordfParserObj.Triples, nil, &RDF_TYPE, &SPDX_PACKAGE)[0]
+ err := parser.parseRelatedElementFromTriple(reln, triple)
+ if err != nil {
+ t.Errorf("error parsing a valid example")
+ }
+ expectedRefA := spdx.DocElementID{
+ DocumentRefID: "",
+ ElementRefID: "",
+ }
+ if !reflect.DeepEqual(expectedRefA, reln.RefA) {
+ t.Errorf("expected %+v, found %+v", expectedRefA, reln.RefA)
+ }
+ expectedRefB := spdx.DocElementID{
+ DocumentRefID: "",
+ ElementRefID: "Saxon",
+ }
+ if !reflect.DeepEqual(expectedRefB, reln.RefB) {
+ t.Errorf("expected %+v, found %+v", expectedRefB, reln.RefB)
+ }
+
+ // TestCase 2: File as a related element
+ parser, _ = parserFromBodyContent(`
+ <spdx:Relationship>
+ <spdx:relatedSpdxElement>
+ <spdx:File rdf:about="http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301#SPDXRef-Saxon"/>
+ </spdx:relatedSpdxElement>
+ </spdx:Relationship>
+ `)
+ reln = &spdx.Relationship2_2{}
+ triple = rdfwriter.FilterTriples(parser.gordfParserObj.Triples, nil, &RDF_TYPE, &SPDX_FILE)[0]
+ err = parser.parseRelatedElementFromTriple(reln, triple)
+ if err != nil {
+ t.Errorf("error parsing a valid example")
+ }
+ expectedRefA = spdx.DocElementID{
+ DocumentRefID: "",
+ ElementRefID: "",
+ }
+ if !reflect.DeepEqual(expectedRefA, reln.RefA) {
+ t.Errorf("expected %+v, found %+v", expectedRefA, reln.RefA)
+ }
+ expectedRefB = spdx.DocElementID{
+ DocumentRefID: "",
+ ElementRefID: "Saxon",
+ }
+ if !reflect.DeepEqual(expectedRefB, reln.RefB) {
+ t.Errorf("expected %+v, found %+v", expectedRefB, reln.RefB)
+ }
+
+ parser, _ = parserFromBodyContent(`
+ <spdx:Relationship>
+ <spdx:relatedSpdxElement>
+ <spdx:SpdxElement rdf:about="http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301#SPDXRef-File"/>
+ </spdx:relatedSpdxElement>
+ </spdx:Relationship>
+ `)
+ reln = &spdx.Relationship2_2{}
+ triple = rdfwriter.FilterTriples(parser.gordfParserObj.Triples, nil, &RDF_TYPE, &SPDX_SPDX_ELEMENT)[0]
+ err = parser.parseRelatedElementFromTriple(reln, triple)
+ if err != nil {
+ t.Errorf("error parsing a valid example")
+ }
+ expectedRefA = spdx.DocElementID{
+ DocumentRefID: "",
+ ElementRefID: "",
+ }
+ if !reflect.DeepEqual(expectedRefA, reln.RefA) {
+ t.Errorf("expected %+v, found %+v", expectedRefA, reln.RefA)
+ }
+ expectedRefB = spdx.DocElementID{
+ DocumentRefID: "",
+ ElementRefID: "File",
+ }
+ if !reflect.DeepEqual(expectedRefB, reln.RefB) {
+ t.Errorf("expected %+v, found %+v", expectedRefB, reln.RefB)
+ }
+}
+
+func Test_rdfParser2_2_parseRelationship(t *testing.T) {
+ // TestCase 1: invalid RefA
+ parser, _ := parserFromBodyContent(`
+ <spdx:File>
+ <spdx:relationship>
+ <spdx:Relationship>
+ <spdx:relatedSpdxElement>
+ <spdx:Package rdf:about="http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301#SPDXRef-Saxon"/>
+ </spdx:relatedSpdxElement>
+ </spdx:Relationship>
+ </spdx:relationship>
+ </spdx:File>
+ `)
+ triple := rdfwriter.FilterTriples(parser.gordfParserObj.Triples, nil, &SPDX_RELATIONSHIP, nil)[0]
+ err := parser.parseRelationship(triple)
+ if err == nil {
+ t.Errorf("should've raised an error due to invalid RefA")
+ }
+
+ // TestCase 3: invalid RefB
+ parser, _ = parserFromBodyContent(`
+ <spdx:File rdf:about="http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301#SPDXRef-File">
+ <spdx:relationship>
+ <spdx:Relationship>
+ <spdx:relatedSpdxElement>
+ <spdx:Package rdf:about="http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301#Saxon"/>
+ </spdx:relatedSpdxElement>
+ </spdx:Relationship>
+ </spdx:relationship>
+ </spdx:File>
+ `)
+ triple = rdfwriter.FilterTriples(parser.gordfParserObj.Triples, nil, &SPDX_RELATIONSHIP, nil)[0]
+ err = parser.parseRelationship(triple)
+ if err == nil {
+ t.Errorf("should've raised an error due to invalid RefB")
+ }
+
+ // TestCase 3: more than one typeTriple for relatedElement
+ parser, _ = parserFromBodyContent(`
+ <spdx:File rdf:about="http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301#SPDXRef-File">
+ <spdx:relationship>
+ <spdx:Relationship>
+ <spdx:relatedSpdxElement>
+ <spdx:Package rdf:about="http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301#SPDXRef-Saxon"/>
+ </spdx:relatedSpdxElement>
+ <spdx:relatedSpdxElement>
+ <spdx:File/>
+ </spdx:relatedSpdxElement>
+ </spdx:Relationship>
+ </spdx:relationship>
+ </spdx:File>
+ `)
+ triple = rdfwriter.FilterTriples(parser.gordfParserObj.Triples, nil, &SPDX_RELATIONSHIP, nil)[0]
+ err = parser.parseRelationship(triple)
+ if err == nil {
+ t.Errorf("should've raised an error due to more than one type triples")
+ }
+
+ // TestCase 4: undefined relatedSpdxElement
+ parser, _ = parserFromBodyContent(`
+ <spdx:File rdf:about="http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301#SPDXRef-File">
+ <spdx:relationship>
+ <spdx:Relationship>
+ <spdx:relatedSpdxElement>
+ <spdx:Unknown rdf:about="http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301#SPDXRef-Saxon"/>
+ </spdx:relatedSpdxElement>
+ </spdx:Relationship>
+ </spdx:relationship>
+ </spdx:File>
+ `)
+ triple = rdfwriter.FilterTriples(parser.gordfParserObj.Triples, nil, &SPDX_RELATIONSHIP, nil)[0]
+ err = parser.parseRelationship(triple)
+ if err == nil {
+ t.Errorf("should've raised an error due to unknown relatedElement")
+ }
+
+ // TestCase 5: unknown predicate inside a relationship
+ parser, _ = parserFromBodyContent(`
+ <spdx:File rdf:about="http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301#SPDXRef-File">
+ <spdx:relationship>
+ <spdx:Relationship>
+ <spdx:relatedSpdxElement>
+ <spdx:Unknown rdf:about="http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301#SPDXRef-Saxon"/>
+ </spdx:relatedSpdxElement>
+ <spdx:unknownPredicate/>
+ </spdx:Relationship>
+ </spdx:relationship>
+ </spdx:File>
+ `)
+ triple = rdfwriter.FilterTriples(parser.gordfParserObj.Triples, nil, &SPDX_RELATIONSHIP, nil)[0]
+ err = parser.parseRelationship(triple)
+ if err == nil {
+ t.Errorf("should've raised an error due to unknown predicate in a relationship")
+ }
+
+ // TestCase 6: completely valid example:
+ parser, _ = parserFromBodyContent(`
+ <spdx:File rdf:about="http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301#SPDXRef-File">
+ <spdx:relationship>
+ <spdx:Relationship>
+ <spdx:relationshipType rdf:resource="http://spdx.org/rdf/terms#relationshipType_describes"/>
+ <spdx:relatedSpdxElement>
+ <spdx:Package rdf:about="http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301#SPDXRef-Saxon"/>
+ </spdx:relatedSpdxElement>
+ <rdfs:comment>comment</rdfs:comment>
+ </spdx:Relationship>
+ </spdx:relationship>
+ </spdx:File>
+ `)
+ triple = rdfwriter.FilterTriples(parser.gordfParserObj.Triples, nil, &SPDX_RELATIONSHIP, nil)[0]
+ err = parser.parseRelationship(triple)
+ if err != nil {
+ t.Errorf("unexpected error parsing a valid relationship: %v", err)
+ }
+ // validating parsed attributes
+ if len(parser.doc.Relationships) != 1 {
+ t.Errorf("after parsing a valid relationship, doc should've had 1 relationship, found %d", len(parser.doc.Relationships))
+ }
+ reln := parser.doc.Relationships[0]
+ expectedRelnType := "describes"
+ if reln.Relationship != expectedRelnType {
+ t.Errorf("expected %s, found %s", expectedRelnType, reln.Relationship)
+ }
+ expectedRefA := spdx.DocElementID{
+ DocumentRefID: "",
+ ElementRefID: "File",
+ }
+ if !reflect.DeepEqual(expectedRefA, reln.RefA) {
+ t.Errorf("expected %+v, found %+v", expectedRefA, reln.RefA)
+ }
+ expectedRefB := spdx.DocElementID{
+ DocumentRefID: "",
+ ElementRefID: "Saxon",
+ }
+ if !reflect.DeepEqual(expectedRefB, reln.RefB) {
+ t.Errorf("expected %+v, found %+v", expectedRefB, reln.RefB)
+ }
+ expectedComment := "comment"
+ if reln.RelationshipComment != expectedComment {
+ t.Errorf("expected %v, found %v", expectedComment, reln.RelationshipComment)
+ }
+}