aboutsummaryrefslogtreecommitdiff
path: root/rdfloader
diff options
context:
space:
mode:
authorRishabhBhatnagar <bhatnagarrishabh4@gmail.com>2020-08-27 18:51:38 +0530
committerRishabhBhatnagar <bhatnagarrishabh4@gmail.com>2020-08-27 18:51:38 +0530
commit054eb919a1867a7b0c171a8c6fcdc3140a7e2903 (patch)
tree01182207e755bd195efa9ffed2ab4b19e0aaf159 /rdfloader
parent6a86ef8c699cb448effc306f5212f4f6ca14b854 (diff)
downloadspdx-tools-054eb919a1867a7b0c171a8c6fcdc3140a7e2903.tar.gz
Add Tests For Annotation and CreationInfo
Signed-off-by: RishabhBhatnagar <bhatnagarrishabh4@gmail.com>
Diffstat (limited to 'rdfloader')
-rw-r--r--rdfloader/parser2v2/parse_annotation.go2
-rw-r--r--rdfloader/parser2v2/parse_annotation_test.go180
-rw-r--r--rdfloader/parser2v2/parse_creation_info_test.go107
3 files changed, 289 insertions, 0 deletions
diff --git a/rdfloader/parser2v2/parse_annotation.go b/rdfloader/parser2v2/parse_annotation.go
index 5d9cdbc..6a47d84 100644
--- a/rdfloader/parser2v2/parse_annotation.go
+++ b/rdfloader/parser2v2/parse_annotation.go
@@ -51,6 +51,7 @@ func setAnnotationToParser(parser *rdfParser2_2, annotation *spdx.Annotation2_2)
return nil
}
+// annotator is of type [Person|Organization|Tool]:String
func setAnnotatorFromString(annotatorString string, ann *spdx.Annotation2_2) error {
subkey, subvalue, err := ExtractSubs(annotatorString, ":")
if err != nil {
@@ -64,6 +65,7 @@ func setAnnotatorFromString(annotatorString string, ann *spdx.Annotation2_2) err
return fmt.Errorf("unrecognized Annotator type %v while parsing annotation", subkey)
}
+// it can be NS_SPDX+annotationType_[review|other]
func setAnnotationType(annType string, ann *spdx.Annotation2_2) error {
switch annType {
case SPDX_ANNOTATION_TYPE_OTHER:
diff --git a/rdfloader/parser2v2/parse_annotation_test.go b/rdfloader/parser2v2/parse_annotation_test.go
new file mode 100644
index 0000000..8c6fdcf
--- /dev/null
+++ b/rdfloader/parser2v2/parse_annotation_test.go
@@ -0,0 +1,180 @@
+package parser2v2
+
+import (
+ "github.com/spdx/tools-golang/spdx"
+ "testing"
+)
+
+func Test_setAnnotatorFromString(t *testing.T) {
+ // TestCase 1: Empty String must raise an error
+ ann := &spdx.Annotation2_2{}
+ input := ""
+ err := setAnnotatorFromString(input, ann)
+ if err == nil {
+ t.Errorf("should've raised an error for an empty string")
+ }
+
+ // TestCase 2: Invalid annotator type
+ ann = &spdx.Annotation2_2{}
+ input = "Company: some_company"
+ err = setAnnotatorFromString(input, ann)
+ if err == nil {
+ t.Errorf("should've raised an error for an unknown annotator type")
+ }
+
+ // TestCase 3: Valid annotator
+ ann = &spdx.Annotation2_2{}
+ input = "Person: Rishabh"
+ err = setAnnotatorFromString(input, ann)
+ if err != nil {
+ t.Errorf("unexpected error for a valid annotator")
+ }
+ if ann.AnnotatorType != "Person" {
+ t.Errorf("wrnog annotator type: expected: %s, found: %s", "Person", ann.Annotator)
+ }
+ if ann.Annotator != "Rishabh" {
+ t.Errorf("wrong annotator: expected: %s, found: %s", "Rishabh", ann.Annotator)
+ }
+}
+
+func Test_setAnnotationType(t *testing.T) {
+ ann := &spdx.Annotation2_2{}
+ // TestCase 1: invalid input (empty annotationType)
+ err := setAnnotationType("", ann)
+ if err == nil {
+ t.Errorf("expected an error for empty input")
+ }
+
+ // TestCase 2: invalid input (unknown annotation type)
+ err = setAnnotationType(NS_SPDX+"annotationType_unknown", ann)
+ if err == nil {
+ t.Errorf("expected an error for invalid annotationType")
+ }
+
+ // TestCase 3: valid input (annotationType_other)
+ err = setAnnotationType(SPDX_ANNOTATION_TYPE_OTHER, ann)
+ if err != nil {
+ t.Errorf("unexpected error: %v", err)
+ }
+ if ann.AnnotationType != "OTHER" {
+ t.Errorf("expected: OTHER, found: %s", ann.AnnotationType)
+ }
+
+ // TestCase 4: valid input (annotationType_review)
+ err = setAnnotationType(SPDX_ANNOTATION_TYPE_REVIEW, ann)
+ if err != nil {
+ t.Errorf("unexpected error: %v", err)
+ }
+ if ann.AnnotationType != "REVIEW" {
+ t.Errorf("expected: REVIEW, found: %s", ann.AnnotationType)
+ }
+}
+
+func Test_setAnnotationToParser(t *testing.T) {
+ // TestCase 1: doc is nil (must raise an error)
+ parser, _ := parserFromBodyContent(``)
+ parser.doc = nil
+ err := setAnnotationToParser(parser, &spdx.Annotation2_2{})
+ if err == nil {
+ t.Errorf("empty doc should've raised an error")
+ }
+
+ // TestCase 2: empty annotations should create a new annotations
+ // list and append the input to it.
+ parser, _ = parserFromBodyContent(``)
+ parser.doc.Annotations = nil
+ err = setAnnotationToParser(parser, &spdx.Annotation2_2{})
+ if err != nil {
+ t.Errorf("unexpected error: %v", err)
+ }
+ if len(parser.doc.Annotations) != 1 {
+ t.Errorf("expected doc to have 1 annotation, found %d", len(parser.doc.Annotations))
+ }
+}
+
+func Test_rdfParser2_2_parseAnnotationFromNode(t *testing.T) {
+ // TestCase 1: invalid annotator must raise an error
+ parser, _ := parserFromBodyContent(`
+ <spdx:Annotation>
+ <spdx:annotationDate>2010-01-29T18:30:22Z</spdx:annotationDate>
+ <rdfs:comment>Document level annotation</rdfs:comment>
+ <spdx:annotator>Company: some company</spdx:annotator>
+ <spdx:annotationType rdf:resource="http://spdx.org/rdf/terms#annotationType_other"/>
+ </spdx:Annotation>
+ `)
+ node := parser.gordfParserObj.Triples[0].Subject
+ err := parser.parseAnnotationFromNode(node)
+ if err == nil {
+ t.Errorf("wrong annotator type should've raised an error")
+ }
+
+ // TestCase 2: wrong annotation type should raise an error
+ parser, _ = parserFromBodyContent(`
+ <spdx:Annotation>
+ <spdx:annotationDate>2010-01-29T18:30:22Z</spdx:annotationDate>
+ <rdfs:comment>Document level annotation</rdfs:comment>
+ <spdx:annotator>Person: Jane Doe</spdx:annotator>
+ <spdx:annotationType rdf:resource="http://spdx.org/rdf/terms#annotationType_unknown"/>
+ </spdx:Annotation>
+ `)
+ node = parser.gordfParserObj.Triples[0].Subject
+ err = parser.parseAnnotationFromNode(node)
+ if err == nil {
+ t.Errorf("wrong annotation type should've raised an error")
+ }
+
+ // TestCase 3: unknown predicate should also raise an error
+ parser, _ = parserFromBodyContent(`
+ <spdx:Annotation>
+ <spdx:annotationDate>2010-01-29T18:30:22Z</spdx:annotationDate>
+ <rdfs:comment>Document level annotation</rdfs:comment>
+ <spdx:annotator>Person: Jane Doe</spdx:annotator>
+ <spdx:annotationType rdf:resource="http://spdx.org/rdf/terms#annotationType_unknown"/>
+ <spdx:unknownPredicate />
+ </spdx:Annotation>
+ `)
+ node = parser.gordfParserObj.Triples[0].Subject
+ err = parser.parseAnnotationFromNode(node)
+ if err == nil {
+ t.Errorf("unknown predicate must raise an error")
+ }
+
+ // TestCase 4: completely valid annotation
+ parser, _ = parserFromBodyContent(`
+ <spdx:Annotation>
+ <spdx:annotationDate>2010-01-29T18:30:22Z</spdx:annotationDate>
+ <rdfs:comment>Document level annotation</rdfs:comment>
+ <spdx:annotator>Person: Jane Doe</spdx:annotator>
+ <spdx:annotationType rdf:resource="http://spdx.org/rdf/terms#annotationType_other"/>
+ </spdx:Annotation>
+ `)
+ node = parser.gordfParserObj.Triples[0].Subject
+ err = parser.parseAnnotationFromNode(node)
+ if err != nil {
+ t.Errorf("error parsing valid a annotation")
+ }
+ if n := len(parser.doc.Annotations); n != 1 {
+ t.Errorf("document should've had only one annotation, found %d", n)
+ }
+ ann := parser.doc.Annotations[0]
+ // validating all the attributes of the annotations
+ expectedComment := "Document level annotation"
+ if ann.AnnotationComment != expectedComment {
+ t.Errorf(`expected: "%s", found "%s"`, expectedComment, ann.AnnotationComment)
+ }
+ expectedDate := "2010-01-29T18:30:22Z"
+ if expectedDate != ann.AnnotationDate {
+ t.Errorf(`expected: "%s", found "%s"`, expectedDate, ann.AnnotationDate)
+ }
+ expectedAnnotator := "Jane Doe"
+ if expectedAnnotator != ann.Annotator {
+ t.Errorf(`expected: "%s", found "%s"`, expectedAnnotator, ann.Annotator)
+ }
+ if ann.AnnotatorType != "Person" {
+ t.Errorf(`expected: "%s", found "%s"`, "Person", ann.AnnotatorType)
+ }
+ expectedAnnotationType := "OTHER"
+ if expectedAnnotationType != ann.AnnotationType {
+ t.Errorf(`expected: "%s", found "%s"`, expectedAnnotationType, ann.AnnotationType)
+ }
+}
diff --git a/rdfloader/parser2v2/parse_creation_info_test.go b/rdfloader/parser2v2/parse_creation_info_test.go
new file mode 100644
index 0000000..fbe28b5
--- /dev/null
+++ b/rdfloader/parser2v2/parse_creation_info_test.go
@@ -0,0 +1,107 @@
+package parser2v2
+
+import (
+ "github.com/spdx/tools-golang/spdx"
+ "testing"
+)
+
+func Test_setCreator(t *testing.T) {
+ // TestCase 1: invalid creator (empty)
+ input := ""
+ err := setCreator(input, &spdx.CreationInfo2_2{})
+ if err == nil {
+ t.Errorf("shoud've raised an error due to invalid input")
+ }
+
+ // TestCase 2: invalid entity type
+ input = "Company: some company"
+ err = setCreator(input, &spdx.CreationInfo2_2{})
+ if err == nil {
+ t.Errorf("shoud've raised an error due to unknown entity type")
+ }
+
+ // TestCase 3: valid input
+ input = "Person: Jane Doe"
+ ci := &spdx.CreationInfo2_2{}
+ err = setCreator(input, ci)
+ if err != nil {
+ t.Errorf("error parsing a valid input: %v", err)
+ }
+ if len(ci.CreatorPersons) != 1 {
+ t.Errorf("creationInfo should've had 1 creatorPersons, found %d", len(ci.CreatorPersons))
+ }
+ expectedPerson := "Jane Doe"
+ if ci.CreatorPersons[0] != expectedPerson {
+ t.Errorf("expected %s, found %s", expectedPerson, ci.CreatorPersons[0])
+ }
+}
+
+func Test_rdfParser2_2_parseCreationInfoFromNode(t *testing.T) {
+ // TestCase 1: invalid creator must raise an error
+ parser, _ := parserFromBodyContent(`
+ <spdx:CreationInfo>
+ <spdx:licenseListVersion>2.6</spdx:licenseListVersion>
+ <spdx:creator>Person Unknown</spdx:creator>
+ <spdx:creator>Organization: </spdx:creator>
+ <spdx:creator>Tool: spdx2</spdx:creator>
+ <spdx:created>2018-08-24T19:55:34Z</spdx:created>
+ <spdx:unknownPredicate />
+ </spdx:CreationInfo>
+ `)
+ ciNode := parser.gordfParserObj.Triples[0].Subject
+ err := parser.parseCreationInfoFromNode(&spdx.CreationInfo2_2{}, ciNode)
+ if err == nil {
+ t.Errorf("invalid creator must raise an error")
+ }
+
+ // TestCase 2: unknown predicate must also raise an error
+ parser, _ = parserFromBodyContent(`
+ <spdx:CreationInfo>
+ <spdx:licenseListVersion>2.6</spdx:licenseListVersion>
+ <spdx:creator>Person: fossy (y)</spdx:creator>
+ <spdx:creator>Organization: </spdx:creator>
+ <spdx:creator>Tool: spdx2</spdx:creator>
+ <spdx:created>2018-08-24T19:55:34Z</spdx:created>
+ <spdx:unknownPredicate />
+ </spdx:CreationInfo>
+ `)
+ ciNode = parser.gordfParserObj.Triples[0].Subject
+ err = parser.parseCreationInfoFromNode(&spdx.CreationInfo2_2{}, ciNode)
+ if err == nil {
+ t.Errorf("unknown predicate must raise an error")
+ }
+
+ // TestCase 2: unknown predicate must also raise an error
+ parser, _ = parserFromBodyContent(`
+ <spdx:CreationInfo>
+ <spdx:licenseListVersion>2.6</spdx:licenseListVersion>
+ <spdx:creator>Person: fossy</spdx:creator>
+ <spdx:created>2018-08-24T19:55:34Z</spdx:created>
+ <rdfs:comment>comment</rdfs:comment>
+ </spdx:CreationInfo>
+ `)
+ ciNode = parser.gordfParserObj.Triples[0].Subject
+ ci := &spdx.CreationInfo2_2{}
+ err = parser.parseCreationInfoFromNode(ci, ciNode)
+ if err != nil {
+ t.Errorf("unexpected error: %v", err)
+ }
+ if ci.LicenseListVersion != "2.6" {
+ t.Errorf(`expected %s, found %s`, "2.6", ci.LicenseListVersion)
+ }
+ n := len(ci.CreatorPersons)
+ if n != 1 {
+ t.Errorf("expected 1 creatorPersons, found %d", n)
+ }
+ if ci.CreatorPersons[0] != "fossy" {
+ t.Errorf("expected %s, found %s", "fossy", ci.CreatorPersons[0])
+ }
+ expectedCreated := "2018-08-24T19:55:34Z"
+ if ci.Created != expectedCreated {
+ t.Errorf("expected %s, found %s", expectedCreated, ci.Created)
+ }
+ expectedComment := "comment"
+ if ci.CreatorComment != expectedComment {
+ t.Errorf("expected %s, found %s", expectedComment, ci.CreatorComment)
+ }
+} \ No newline at end of file