aboutsummaryrefslogtreecommitdiff
path: root/rdfloader/parser2v3/parse_creation_info_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'rdfloader/parser2v3/parse_creation_info_test.go')
-rw-r--r--rdfloader/parser2v3/parse_creation_info_test.go107
1 files changed, 107 insertions, 0 deletions
diff --git a/rdfloader/parser2v3/parse_creation_info_test.go b/rdfloader/parser2v3/parse_creation_info_test.go
new file mode 100644
index 0000000..354ac73
--- /dev/null
+++ b/rdfloader/parser2v3/parse_creation_info_test.go
@@ -0,0 +1,107 @@
+// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
+
+package parser2v3
+
+import (
+ "testing"
+
+ "github.com/spdx/tools-golang/spdx/v2_3"
+)
+
+func Test_setCreator(t *testing.T) {
+ // TestCase 1: invalid creator (empty)
+ input := ""
+ err := setCreator(input, &v2_3.CreationInfo{})
+ 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, &v2_3.CreationInfo{})
+ if err == nil {
+ t.Errorf("shoud've raised an error due to unknown entity type")
+ }
+
+ // TestCase 3: valid input
+ input = "Person: Jane Doe"
+ ci := &v2_3.CreationInfo{}
+ err = setCreator(input, ci)
+ if err != nil {
+ t.Errorf("error parsing a valid input: %v", err)
+ }
+ if len(ci.Creators) != 1 {
+ t.Errorf("creationInfo should've had 1 creatorPersons, found %d", len(ci.Creators))
+ }
+ expectedPerson := "Jane Doe"
+ if ci.Creators[0].Creator != expectedPerson {
+ t.Errorf("expected %s, found %s", expectedPerson, ci.Creators[0])
+ }
+}
+
+func Test_rdfParser2_3_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:created>2018-08-24T19:55:34Z</spdx:created>
+ </spdx:CreationInfo>
+ `)
+ ciNode := parser.gordfParserObj.Triples[0].Subject
+ err := parser.parseCreationInfoFromNode(&v2_3.CreationInfo{}, 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(&v2_3.CreationInfo{}, 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 := &v2_3.CreationInfo{}
+ 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.Creators)
+ if n != 1 {
+ t.Errorf("expected 1 creatorPersons, found %d", n)
+ }
+ if ci.Creators[0].Creator != "fossy" {
+ t.Errorf("expected %s, found %s", "fossy", ci.Creators[0].Creator)
+ }
+ 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)
+ }
+}