1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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)
}
}
|