From 054eb919a1867a7b0c171a8c6fcdc3140a7e2903 Mon Sep 17 00:00:00 2001 From: RishabhBhatnagar Date: Thu, 27 Aug 2020 18:51:38 +0530 Subject: Add Tests For Annotation and CreationInfo Signed-off-by: RishabhBhatnagar --- rdfloader/parser2v2/parse_annotation.go | 2 + rdfloader/parser2v2/parse_annotation_test.go | 180 ++++++++++++++++++++++++ rdfloader/parser2v2/parse_creation_info_test.go | 107 ++++++++++++++ 3 files changed, 289 insertions(+) create mode 100644 rdfloader/parser2v2/parse_annotation_test.go create mode 100644 rdfloader/parser2v2/parse_creation_info_test.go (limited to 'rdfloader') 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(` + + 2010-01-29T18:30:22Z + Document level annotation + Company: some company + + + `) + 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(` + + 2010-01-29T18:30:22Z + Document level annotation + Person: Jane Doe + + + `) + 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(` + + 2010-01-29T18:30:22Z + Document level annotation + Person: Jane Doe + + + + `) + 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(` + + 2010-01-29T18:30:22Z + Document level annotation + Person: Jane Doe + + + `) + 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(` + + 2.6 + Person Unknown + Organization: + Tool: spdx2 + 2018-08-24T19:55:34Z + + + `) + 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(` + + 2.6 + Person: fossy (y) + Organization: + Tool: spdx2 + 2018-08-24T19:55:34Z + + + `) + 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(` + + 2.6 + Person: fossy + 2018-08-24T19:55:34Z + comment + + `) + 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 -- cgit v1.2.3