xref: /aosp_15_r20/external/spdx-tools/tvsaver/saver2v2/save_annotation_test.go (revision ba677afa8f67bb56cbc794f4d0e378e0da058e16)
1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2
3package saver2v2
4
5import (
6	"bytes"
7	"testing"
8
9	"github.com/spdx/tools-golang/spdx/common"
10	"github.com/spdx/tools-golang/spdx/v2_2"
11)
12
13// ===== Annotation section Saver tests =====
14func TestSaver2_2AnnotationSavesTextForPerson(t *testing.T) {
15	ann := &v2_2.Annotation{
16		Annotator:                common.Annotator{AnnotatorType: "Person", Annotator: "John Doe"},
17		AnnotationDate:           "2018-10-10T17:52:00Z",
18		AnnotationType:           "REVIEW",
19		AnnotationSPDXIdentifier: common.MakeDocElementID("", "DOCUMENT"),
20		AnnotationComment:        "This is an annotation about the SPDX document",
21	}
22
23	// what we want to get, as a buffer of bytes
24	// no trailing blank newline
25	want := bytes.NewBufferString(`Annotator: Person: John Doe
26AnnotationDate: 2018-10-10T17:52:00Z
27AnnotationType: REVIEW
28SPDXREF: SPDXRef-DOCUMENT
29AnnotationComment: This is an annotation about the SPDX document
30`)
31
32	// render as buffer of bytes
33	var got bytes.Buffer
34	err := renderAnnotation2_2(ann, &got)
35	if err != nil {
36		t.Errorf("Expected nil error, got %v", err)
37	}
38
39	// check that they match
40	c := bytes.Compare(want.Bytes(), got.Bytes())
41	if c != 0 {
42		t.Errorf("Expected %v, got %v", want.String(), got.String())
43	}
44}
45
46func TestSaver2_2AnnotationSavesTextForOrganization(t *testing.T) {
47	ann := &v2_2.Annotation{
48		Annotator:                common.Annotator{AnnotatorType: "Organization", Annotator: "John Doe, Inc."},
49		AnnotationDate:           "2018-10-10T17:52:00Z",
50		AnnotationType:           "REVIEW",
51		AnnotationSPDXIdentifier: common.MakeDocElementID("", "DOCUMENT"),
52		AnnotationComment:        "This is an annotation about the SPDX document",
53	}
54
55	// what we want to get, as a buffer of bytes
56	// no trailing blank newline
57	want := bytes.NewBufferString(`Annotator: Organization: John Doe, Inc.
58AnnotationDate: 2018-10-10T17:52:00Z
59AnnotationType: REVIEW
60SPDXREF: SPDXRef-DOCUMENT
61AnnotationComment: This is an annotation about the SPDX document
62`)
63
64	// render as buffer of bytes
65	var got bytes.Buffer
66	err := renderAnnotation2_2(ann, &got)
67	if err != nil {
68		t.Errorf("Expected nil error, got %v", err)
69	}
70
71	// check that they match
72	c := bytes.Compare(want.Bytes(), got.Bytes())
73	if c != 0 {
74		t.Errorf("Expected %v, got %v", want.String(), got.String())
75	}
76}
77
78func TestSaver2_2AnnotationSavesTextForTool(t *testing.T) {
79	ann := &v2_2.Annotation{
80		Annotator:                common.Annotator{AnnotatorType: "Tool", Annotator: "magictool-1.1"},
81		AnnotationDate:           "2018-10-10T17:52:00Z",
82		AnnotationType:           "REVIEW",
83		AnnotationSPDXIdentifier: common.MakeDocElementID("", "DOCUMENT"),
84		AnnotationComment:        "This is an annotation about the SPDX document",
85	}
86
87	// what we want to get, as a buffer of bytes
88	// no trailing blank newline
89	want := bytes.NewBufferString(`Annotator: Tool: magictool-1.1
90AnnotationDate: 2018-10-10T17:52:00Z
91AnnotationType: REVIEW
92SPDXREF: SPDXRef-DOCUMENT
93AnnotationComment: This is an annotation about the SPDX document
94`)
95
96	// render as buffer of bytes
97	var got bytes.Buffer
98	err := renderAnnotation2_2(ann, &got)
99	if err != nil {
100		t.Errorf("Expected nil error, got %v", err)
101	}
102
103	// check that they match
104	c := bytes.Compare(want.Bytes(), got.Bytes())
105	if c != 0 {
106		t.Errorf("Expected %v, got %v", want.String(), got.String())
107	}
108}
109
110// note that the annotation has no optional or multiple fields
111