1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 2 3package parser2v3 4 5import ( 6 "testing" 7 8 "github.com/spdx/tools-golang/spdx/v2_3" 9) 10 11func Test_setAnnotatorFromString(t *testing.T) { 12 // TestCase 1: Empty String must raise an error 13 ann := &v2_3.Annotation{} 14 input := "" 15 err := setAnnotatorFromString(input, ann) 16 if err == nil { 17 t.Error("should've raised an error for an empty string") 18 } 19 20 // TestCase 2: Invalid annotator type 21 ann = &v2_3.Annotation{} 22 input = "Company: some_company" 23 err = setAnnotatorFromString(input, ann) 24 if err == nil { 25 t.Errorf("should've raised an error for an unknown annotator type") 26 } 27 28 // TestCase 3: Valid annotator 29 ann = &v2_3.Annotation{} 30 input = "Person: Rishabh" 31 err = setAnnotatorFromString(input, ann) 32 if err != nil { 33 t.Errorf("unexpected error for a valid annotator") 34 } 35 if ann.Annotator.AnnotatorType != "Person" { 36 t.Errorf("wrnog annotator type: expected: %s, found: %s", "Person", ann.Annotator) 37 } 38 if ann.Annotator.Annotator != "Rishabh" { 39 t.Errorf("wrong annotator: expected: %s, found: %s", "Rishabh", ann.Annotator) 40 } 41} 42 43func Test_setAnnotationType(t *testing.T) { 44 ann := &v2_3.Annotation{} 45 // TestCase 1: invalid input (empty annotationType) 46 err := setAnnotationType("", ann) 47 if err == nil { 48 t.Errorf("expected an error for empty input") 49 } 50 51 // TestCase 2: invalid input (unknown annotation type) 52 err = setAnnotationType(NS_SPDX+"annotationType_unknown", ann) 53 if err == nil { 54 t.Errorf("expected an error for invalid annotationType") 55 } 56 57 // TestCase 3: valid input (annotationType_other) 58 err = setAnnotationType(SPDX_ANNOTATION_TYPE_OTHER, ann) 59 if err != nil { 60 t.Errorf("unexpected error: %v", err) 61 } 62 if ann.AnnotationType != "OTHER" { 63 t.Errorf("expected: OTHER, found: %s", ann.AnnotationType) 64 } 65 66 // TestCase 4: valid input (annotationType_review) 67 err = setAnnotationType(SPDX_ANNOTATION_TYPE_REVIEW, ann) 68 if err != nil { 69 t.Errorf("unexpected error: %v", err) 70 } 71 if ann.AnnotationType != "REVIEW" { 72 t.Errorf("expected: REVIEW, found: %s", ann.AnnotationType) 73 } 74} 75 76func Test_setAnnotationToParser(t *testing.T) { 77 // TestCase 1: doc is nil (must raise an error) 78 parser, _ := parserFromBodyContent(``) 79 parser.doc = nil 80 err := setAnnotationToParser(parser, &v2_3.Annotation{}) 81 if err == nil { 82 t.Errorf("empty doc should've raised an error") 83 } 84 85 // TestCase 2: empty annotations should create a new annotations 86 // list and append the input to it. 87 parser, _ = parserFromBodyContent(``) 88 parser.doc.Annotations = nil 89 err = setAnnotationToParser(parser, &v2_3.Annotation{}) 90 if err != nil { 91 t.Errorf("unexpected error: %v", err) 92 } 93 if len(parser.doc.Annotations) != 1 { 94 t.Errorf("expected doc to have 1 annotation, found %d", len(parser.doc.Annotations)) 95 } 96} 97 98func Test_rdfParser2_3_parseAnnotationFromNode(t *testing.T) { 99 // TestCase 1: invalid annotator must raise an error 100 parser, _ := parserFromBodyContent(` 101 <spdx:Annotation> 102 <spdx:annotationDate>2010-01-29T18:30:22Z</spdx:annotationDate> 103 <rdfs:comment>Document level annotation</rdfs:comment> 104 <spdx:annotator>Company: some company</spdx:annotator> 105 <spdx:annotationType rdf:resource="http://spdx.org/rdf/terms#annotationType_other"/> 106 </spdx:Annotation> 107 `) 108 node := parser.gordfParserObj.Triples[0].Subject 109 err := parser.parseAnnotationFromNode(node) 110 if err == nil { 111 t.Errorf("wrong annotator type should've raised an error") 112 } 113 114 // TestCase 2: wrong annotation type should raise an error 115 parser, _ = parserFromBodyContent(` 116 <spdx:Annotation> 117 <spdx:annotationDate>2010-01-29T18:30:22Z</spdx:annotationDate> 118 <rdfs:comment>Document level annotation</rdfs:comment> 119 <spdx:annotator>Person: Jane Doe</spdx:annotator> 120 <spdx:annotationType rdf:resource="http://spdx.org/rdf/terms#annotationType_unknown"/> 121 </spdx:Annotation> 122 `) 123 node = parser.gordfParserObj.Triples[0].Subject 124 err = parser.parseAnnotationFromNode(node) 125 if err == nil { 126 t.Errorf("wrong annotation type should've raised an error") 127 } 128 129 // TestCase 3: unknown predicate should also raise an error 130 parser, _ = parserFromBodyContent(` 131 <spdx:Annotation> 132 <spdx:annotationDate>2010-01-29T18:30:22Z</spdx:annotationDate> 133 <rdfs:comment>Document level annotation</rdfs:comment> 134 <spdx:annotator>Person: Jane Doe</spdx:annotator> 135 <spdx:annotationType rdf:resource="http://spdx.org/rdf/terms#annotationType_unknown"/> 136 <spdx:unknownPredicate /> 137 </spdx:Annotation> 138 `) 139 node = parser.gordfParserObj.Triples[0].Subject 140 err = parser.parseAnnotationFromNode(node) 141 if err == nil { 142 t.Errorf("unknown predicate must raise an error") 143 } 144 145 // TestCase 4: completely valid annotation 146 parser, _ = parserFromBodyContent(` 147 <spdx:Annotation> 148 <spdx:annotationDate>2010-01-29T18:30:22Z</spdx:annotationDate> 149 <rdfs:comment>Document level annotation</rdfs:comment> 150 <spdx:annotator>Person: Jane Doe</spdx:annotator> 151 <spdx:annotationType rdf:resource="http://spdx.org/rdf/terms#annotationType_other"/> 152 </spdx:Annotation> 153 `) 154 node = parser.gordfParserObj.Triples[0].Subject 155 err = parser.parseAnnotationFromNode(node) 156 if err != nil { 157 t.Errorf("error parsing valid a annotation") 158 } 159 if n := len(parser.doc.Annotations); n != 1 { 160 t.Errorf("document should've had only one annotation, found %d", n) 161 } 162 ann := parser.doc.Annotations[0] 163 // validating all the attributes of the annotations 164 expectedComment := "Document level annotation" 165 if ann.AnnotationComment != expectedComment { 166 t.Errorf(`expected: "%s", found "%s"`, expectedComment, ann.AnnotationComment) 167 } 168 expectedDate := "2010-01-29T18:30:22Z" 169 if expectedDate != ann.AnnotationDate { 170 t.Errorf(`expected: "%s", found "%s"`, expectedDate, ann.AnnotationDate) 171 } 172 expectedAnnotator := "Jane Doe" 173 if expectedAnnotator != ann.Annotator.Annotator { 174 t.Errorf(`expected: "%s", found "%s"`, expectedAnnotator, ann.Annotator) 175 } 176 if ann.Annotator.AnnotatorType != "Person" { 177 t.Errorf(`expected: "%s", found "%s"`, "Person", ann.Annotator.AnnotatorType) 178 } 179 expectedAnnotationType := "OTHER" 180 if expectedAnnotationType != ann.AnnotationType { 181 t.Errorf(`expected: "%s", found "%s"`, expectedAnnotationType, ann.AnnotationType) 182 } 183} 184