aboutsummaryrefslogtreecommitdiff
path: root/jsonloader/parser2v2/parse_snippets_test.go
blob: b25bee55d80fc0c978023d7d3635389a6d6f4634 (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
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package parser2v2

import (
	"encoding/json"
	"log"
	"reflect"
	"testing"

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

func TestJSONSpdxDocument_parseJsonSnippets2_2(t *testing.T) {

	data := []byte(`{
		"snippets" : [ {
			"SPDXID" : "SPDXRef-Snippet",
			"comment" : "This snippet was identified as significant and highlighted in this Apache-2.0 file, when a commercial scanner identified it as being derived from file foo.c in package xyz which is licensed under GPL-2.0.",
			"copyrightText" : "Copyright 2008-2010 John Smith",
			"licenseComments" : "The concluded license was taken from package xyz, from which the snippet was copied into the current file. The concluded license information was found in the COPYING.txt file in package xyz.",
			"licenseConcluded" : "GPL-2.0-only",
			"licenseInfoInSnippets" : [ "GPL-2.0-only" ],
			"attributionTexts":["text1"],
			"name" : "from linux kernel",
			"ranges" : [ {
			  "endPointer" : {
				"lineNumber" : 23,
				"reference" : "SPDXRef-DoapSource"
			  },
			  "startPointer" : {
				"lineNumber" : 5,
				"reference" : "SPDXRef-DoapSource"
			  }
			}, {
			  "endPointer" : {
				"offset" : 420,
				"reference" : "SPDXRef-DoapSource"
			  },
			  "startPointer" : {
				"offset" : 310,
				"reference" : "SPDXRef-DoapSource"
			  }
			} ],
			"snippetFromFile" : "SPDXRef-DoapSource"
		  } ]
		}
  	`)

	filetest1 := spdx.File2_2{
		FileSPDXIdentifier: "DoapSource",
		FileChecksums:      map[spdx.ChecksumAlgorithm]spdx.Checksum{},
		Snippets: map[spdx.ElementID]*spdx.Snippet2_2{
			"Snippet": {
				SnippetSPDXIdentifier:         "Snippet",
				SnippetAttributionTexts:       []string{"text1"},
				SnippetFromFileSPDXIdentifier: spdx.DocElementID{ElementRefID: "DoapSource"},
				SnippetComment:                "This snippet was identified as significant and highlighted in this Apache-2.0 file, when a commercial scanner identified it as being derived from file foo.c in package xyz which is licensed under GPL-2.0.",
				SnippetCopyrightText:          "Copyright 2008-2010 John Smith",
				SnippetLicenseComments:        "The concluded license was taken from package xyz, from which the snippet was copied into the current file. The concluded license information was found in the COPYING.txt file in package xyz.",
				SnippetLicenseConcluded:       "GPL-2.0-only",
				LicenseInfoInSnippet:          []string{"GPL-2.0-only"},
				SnippetName:                   "from linux kernel",
				SnippetByteRangeStart:         310,
				SnippetByteRangeEnd:           420,
				SnippetLineRangeStart:         5,
				SnippetLineRangeEnd:           23,
			},
		},
	}

	var specs JSONSpdxDocument
	err := json.Unmarshal(data, &specs)

	if err != nil {
		log.Fatal(err)
	}
	type args struct {
		key   string
		value interface{}
		doc   *spdxDocument2_2
	}
	tests := []struct {
		name    string
		spec    JSONSpdxDocument
		args    args
		want    *spdx.File2_2
		wantErr bool
	}{
		// TODO: Add test cases.
		{
			name: "successTest",
			spec: specs,
			args: args{
				key:   "snippets",
				value: specs["snippets"],
				doc: &spdxDocument2_2{UnpackagedFiles: map[spdx.ElementID]*spdx.File2_2{
					"DoapSource": {
						FileSPDXIdentifier: "DoapSource",
						FileChecksums:      map[spdx.ChecksumAlgorithm]spdx.Checksum{},
						Snippets:           map[spdx.ElementID]*spdx.Snippet2_2{},
					},
				}},
			},
			want:    &filetest1,
			wantErr: false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if err := tt.spec.parseJsonSnippets2_2(tt.args.key, tt.args.value, tt.args.doc); (err != nil) != tt.wantErr {
				t.Errorf("JSONSpdxDocument.parseJsonSnippets2_2() error = %v, wantErr %v", err, tt.wantErr)
			}

			for k, v := range tt.want.Snippets {
				if !reflect.DeepEqual(tt.args.doc.UnpackagedFiles["DoapSource"].Snippets[k], v) {
					t.Errorf("Load2_2() = %v, want %v", tt.args.doc.UnpackagedFiles["DoapSource"].Snippets[k], v)
				}
			}

		})
	}
}