aboutsummaryrefslogtreecommitdiff
path: root/builder/builder2v2/build_creation_info_test.go
blob: 188bd74c2d3f2377ee7fee435582048bdda341e3 (plain)
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
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package builder2v2

import (
	"fmt"
	"testing"

	"github.com/spdx/tools-golang/spdx"
)

// ===== CreationInfo section builder tests =====
func TestBuilder2_2CanBuildCreationInfoSection(t *testing.T) {

	namespacePrefix := "https://github.com/swinslow/spdx-docs/spdx-go/testdata-whatever-"
	creatorType := "Organization"
	creator := "Jane Doe LLC"
	testValues := make(map[string]string)
	testValues["Created"] = "2018-10-20T16:48:00Z"
	packageName := "project1"
	verificationCode := "TESTCODE"

	ci, err := BuildCreationInfoSection2_2(packageName, verificationCode, namespacePrefix, creatorType, creator, testValues)
	if err != nil {
		t.Fatalf("expected nil error, got %v", err)
	}

	if ci == nil {
		t.Fatalf("expected non-nil CreationInfo, got nil")
	}
	if ci.SPDXVersion != "SPDX-2.2" {
		t.Errorf("expected %s, got %s", "SPDX-2.2", ci.SPDXVersion)
	}
	if ci.DataLicense != "CC0-1.0" {
		t.Errorf("expected %s, got %s", "CC0-1.0", ci.DataLicense)
	}
	if ci.SPDXIdentifier != spdx.ElementID("DOCUMENT") {
		t.Errorf("expected %s, got %v", "DOCUMENT", ci.SPDXIdentifier)
	}
	if ci.DocumentName != "project1" {
		t.Errorf("expected %s, got %s", "project1", ci.DocumentName)
	}
	wantNamespace := fmt.Sprintf("https://github.com/swinslow/spdx-docs/spdx-go/testdata-whatever-project1-%s", verificationCode)
	if ci.DocumentNamespace != wantNamespace {
		t.Errorf("expected %s, got %s", wantNamespace, ci.DocumentNamespace)
	}
	if len(ci.CreatorPersons) != 0 {
		t.Fatalf("expected %d, got %d", 0, len(ci.CreatorPersons))
	}
	if len(ci.CreatorOrganizations) != 1 {
		t.Fatalf("expected %d, got %d", 1, len(ci.CreatorOrganizations))
	}
	if ci.CreatorOrganizations[0] != "Jane Doe LLC" {
		t.Errorf("expected %s, got %s", "Jane Doe LLC", ci.CreatorOrganizations[0])
	}
	if len(ci.CreatorTools) != 1 {
		t.Fatalf("expected %d, got %d", 1, len(ci.CreatorTools))
	}
	if ci.CreatorTools[0] != "github.com/spdx/tools-golang/builder" {
		t.Errorf("expected %s, got %s", "github.com/spdx/tools-golang/builder", ci.CreatorTools[0])
	}
	if ci.Created != "2018-10-20T16:48:00Z" {
		t.Errorf("expected %s, got %s", "2018-10-20T16:48:00Z", ci.Created)
	}
}

func TestBuilder2_2CanBuildCreationInfoSectionWithCreatorPerson(t *testing.T) {
	namespacePrefix := "https://github.com/swinslow/spdx-docs/spdx-go/testdata-whatever-"
	creatorType := "Person"
	creator := "John Doe"
	testValues := make(map[string]string)
	testValues["Created"] = "2018-10-20T16:48:00Z"
	packageName := "project1"
	verificationCode := "TESTCODE"

	ci, err := BuildCreationInfoSection2_2(packageName, verificationCode, namespacePrefix, creatorType, creator, testValues)
	if err != nil {
		t.Fatalf("expected nil error, got %v", err)
	}

	if ci == nil {
		t.Fatalf("expected non-nil CreationInfo, got nil")
	}
	if len(ci.CreatorPersons) != 1 {
		t.Fatalf("expected %d, got %d", 1, len(ci.CreatorPersons))
	}
	if ci.CreatorPersons[0] != "John Doe" {
		t.Errorf("expected %s, got %s", "John Doe", ci.CreatorPersons[0])
	}
	if len(ci.CreatorOrganizations) != 0 {
		t.Fatalf("expected %d, got %d", 0, len(ci.CreatorOrganizations))
	}
	if len(ci.CreatorTools) != 1 {
		t.Fatalf("expected %d, got %d", 1, len(ci.CreatorTools))
	}
	if ci.CreatorTools[0] != "github.com/spdx/tools-golang/builder" {
		t.Errorf("expected %s, got %s", "github.com/spdx/tools-golang/builder", ci.CreatorTools[0])
	}
}

func TestBuilder2_2CanBuildCreationInfoSectionWithCreatorTool(t *testing.T) {
	namespacePrefix := "https://github.com/swinslow/spdx-docs/spdx-go/testdata-whatever-"
	creatorType := "Tool"
	creator := "some-other-tool-2.1"
	testValues := make(map[string]string)
	testValues["Created"] = "2018-10-20T16:48:00Z"
	packageName := "project1"
	verificationCode := "TESTCODE"

	ci, err := BuildCreationInfoSection2_2(packageName, verificationCode, namespacePrefix, creatorType, creator, testValues)
	if err != nil {
		t.Fatalf("expected nil error, got %v", err)
	}

	if ci == nil {
		t.Fatalf("expected non-nil CreationInfo, got nil")
	}
	if len(ci.CreatorPersons) != 0 {
		t.Fatalf("expected %d, got %d", 0, len(ci.CreatorPersons))
	}
	if len(ci.CreatorOrganizations) != 0 {
		t.Fatalf("expected %d, got %d", 0, len(ci.CreatorOrganizations))
	}
	if len(ci.CreatorTools) != 2 {
		t.Fatalf("expected %d, got %d", 2, len(ci.CreatorTools))
	}
	if ci.CreatorTools[0] != "github.com/spdx/tools-golang/builder" {
		t.Errorf("expected %s, got %s", "github.com/spdx/tools-golang/builder", ci.CreatorTools[0])
	}
	if ci.CreatorTools[1] != "some-other-tool-2.1" {
		t.Errorf("expected %s, got %s", "some-other-tool-2.1", ci.CreatorTools[1])
	}
}

func TestBuilder2_2CanBuildCreationInfoSectionWithInvalidPerson(t *testing.T) {
	namespacePrefix := "https://github.com/swinslow/spdx-docs/spdx-go/testdata-whatever-"
	creatorType := "Whatever"
	creator := "John Doe"
	testValues := make(map[string]string)
	testValues["Created"] = "2018-10-20T16:48:00Z"
	packageName := "project1"
	verificationCode := "TESTCODE"

	ci, err := BuildCreationInfoSection2_2(packageName, verificationCode, namespacePrefix, creatorType, creator, testValues)
	if err != nil {
		t.Fatalf("expected nil error, got %v", err)
	}

	if ci == nil {
		t.Fatalf("expected non-nil CreationInfo, got nil")
	}
	if len(ci.CreatorPersons) != 1 {
		t.Fatalf("expected %d, got %d", 1, len(ci.CreatorPersons))
	}
	if ci.CreatorPersons[0] != "John Doe" {
		t.Errorf("expected %s, got %s", "John Doe", ci.CreatorPersons[0])
	}
	if len(ci.CreatorOrganizations) != 0 {
		t.Fatalf("expected %d, got %d", 0, len(ci.CreatorOrganizations))
	}
	if len(ci.CreatorTools) != 1 {
		t.Fatalf("expected %d, got %d", 1, len(ci.CreatorTools))
	}
	if ci.CreatorTools[0] != "github.com/spdx/tools-golang/builder" {
		t.Errorf("expected %s, got %s", "github.com/spdx/tools-golang/builder", ci.CreatorTools[0])
	}
}