Skip to content

Commit

Permalink
Merge pull request #94 from specter25/jsonparser1
Browse files Browse the repository at this point in the history
[GSoC'21] Pr:6 . JSON saver tests
  • Loading branch information
swinslow committed Aug 6, 2021
2 parents 668152c + 1bc87f7 commit 25bc95b
Show file tree
Hide file tree
Showing 30 changed files with 1,657 additions and 1,071 deletions.
55 changes: 55 additions & 0 deletions examples/10-jsonloader/example_json_loader.go
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

// Example for: *jsonparser2v2*

// This example demonstrates loading an SPDX json from disk into memory,
// and then logging out some attributes to the console .

package main

import (
"fmt"
"os"
"strings"

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

func main() {

// check that we've received the right number of arguments
args := os.Args
if len(args) != 3 {
fmt.Printf("Usage: %v <spdx-file-in> <spdx-file-out>\n", args[0])
fmt.Printf(" Load SPDX 2.2 tag-value file <spdx-file-in>, and\n")
fmt.Printf(" save it out to <spdx-file-out>.\n")
return
}

// open the SPDX file
fileIn := args[1]
r, err := os.Open(fileIn)
if err != nil {
fmt.Printf("Error while opening %v for reading: %v", fileIn, err)
return
}
defer r.Close()

// try to load the SPDX file's contents as a json file, version 2.2
doc, err := jsonloader.Load2_2(r)
if err != nil {
fmt.Printf("Error while parsing %v: %v", args[1], err)
return
}

// if we got here, the file is now loaded into memory.
fmt.Printf("Successfully loaded %s\n", args[1])

fmt.Println(strings.Repeat("=", 80))
fmt.Println("Some Attributes of the Document:")
fmt.Printf("Document Name: %s\n", doc.CreationInfo.DocumentName)
fmt.Printf("DataLicense: %s\n", doc.CreationInfo.DataLicense)
fmt.Printf("Document NameSpace: %s\n", doc.CreationInfo.DocumentNamespace)
fmt.Printf("SPDX Document Version: %s\n", doc.CreationInfo.SPDXVersion)
fmt.Println(strings.Repeat("=", 80))
}
346 changes: 0 additions & 346 deletions examples/8-jsonloader/result.txt

This file was deleted.

File renamed without changes.
417 changes: 0 additions & 417 deletions examples/9-jsonsaver/sample.json

This file was deleted.

@@ -1,18 +1,18 @@
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

// Example for: *tvloader*, *tvsaver*
// Example for: *tvloader*, *jsonsaver*

// This example demonstrates loading an SPDX tag-value file from disk into memory,
// and re-saving it to a different file on disk.
// and re-saving it to a different json file on disk.

package main

import (
"fmt"
"os"

"github.com/spdx/tools-golang/jsonloader"
"github.com/spdx/tools-golang/jsonsaver"
"github.com/spdx/tools-golang/tvloader"
)

func main() {
Expand All @@ -36,7 +36,7 @@ func main() {
defer r.Close()

// try to load the SPDX file's contents as a tag-value file, version 2.2
doc, err := jsonloader.Load2_2(r)
doc, err := tvloader.Load2_2(r)
if err != nil {
fmt.Printf("Error while parsing %v: %v", args[1], err)
return
Expand All @@ -45,7 +45,7 @@ func main() {
// if we got here, the file is now loaded into memory.
fmt.Printf("Successfully loaded %s\n", args[1])

// we can now save it back to disk, using tvsaver.
// we can now save it back to disk, using jsonsaver.

// create a new file for writing
fileOut := args[2]
Expand All @@ -56,7 +56,7 @@ func main() {
}
defer w.Close()

// try to save the document to disk as an SPDX tag-value file, version 2.2
// try to save the document to disk as an SPDX json file, version 2.2
err = jsonsaver.Save2_2(doc, w)
if err != nil {
fmt.Printf("Error while saving %v: %v", fileOut, err)
Expand Down
16 changes: 15 additions & 1 deletion examples/README.md
Expand Up @@ -64,9 +64,23 @@ the same identifier in both documents.
This example demonstrates loading an SPDX rdf file from disk into memory
and then printing the corresponding spdx struct for the document.

## 8-jsonloader
## 8-jsontotv

*jsonloader*, *tvsaver*

This example demonstrates loading an SPDX json from disk into memory
and then re-saving it to a different file on disk in tag-value format.

## 9-tvtojson

*jsonsaver*, *tvloader*

This example demonstrates loading an SPDX tag-value from disk into memory
and then re-saving it to a different file on disk in json format.

## 10-jsonloader

*jsonloader*

This example demonstrates loading an SPDX json from disk into memory
and then logging some of the attributes to the console.
11 changes: 10 additions & 1 deletion jsonloader/jsonloader_test.go
Expand Up @@ -3,6 +3,7 @@
package jsonloader

import (
"bytes"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -45,6 +46,14 @@ func TestLoad2_2(t *testing.T) {
},
wantErr: false,
},
{
name: "fail - invalidjson ",
args: args{
content: bytes.NewReader([]byte(`{"Hello":"HI",}`)),
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -53,7 +62,7 @@ func TestLoad2_2(t *testing.T) {
t.Errorf("Load2_2() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got.CreationInfo, tt.want.CreationInfo) {
if !tt.wantErr && !reflect.DeepEqual(got.CreationInfo, tt.want.CreationInfo) {
t.Errorf("Load2_2() = %v, want %v", got.CreationInfo, tt.want.CreationInfo)
}
})
Expand Down
58 changes: 54 additions & 4 deletions jsonloader/parser2v2/parse_annotations_test.go
Expand Up @@ -31,6 +31,26 @@ func TestJSONSpdxDocument_parseJsonAnnotations2_2(t *testing.T) {
} ]
}
`)
data2 := []byte(`{
"annotations" : [ {
"annotationDate" : "2010-02-10T00:00:00Z",
"annotationType" : "REVIEW",
"annotator" : "Person: Joe Reviewer",
"comment" : "This is just an example. Some of the non-standard licenses look like they are actually BSD 3 clause licenses",
"Hello":"hellp"
}]
}
`)
data3 := []byte(`{
"annotations" : [ {
"annotationDate" : "2010-02-10T00:00:00Z",
"annotationType" : "REVIEW",
"annotator" : "Fasle: Joe Reviewer",
"comment" : "This is just an example. Some of the non-standard licenses look like they are actually BSD 3 clause licenses",
"Hello":"hellp"
}]
}
`)

annotationstest1 := []*spdx.Annotation2_2{
{
Expand Down Expand Up @@ -60,7 +80,12 @@ func TestJSONSpdxDocument_parseJsonAnnotations2_2(t *testing.T) {
}

var specs JSONSpdxDocument
var specs2 JSONSpdxDocument
var specs3 JSONSpdxDocument

json.Unmarshal(data, &specs)
json.Unmarshal(data2, &specs2)
json.Unmarshal(data3, &specs3)

type args struct {
key string
Expand Down Expand Up @@ -88,16 +113,41 @@ func TestJSONSpdxDocument_parseJsonAnnotations2_2(t *testing.T) {
want: annotationstest1,
wantErr: false,
},
{
name: "failure test - invaid creator type",
spec: specs2,
args: args{
key: "annotations",
value: specs2["annotations"],
doc: &spdxDocument2_2{},
SPDXElementID: spdx.DocElementID{DocumentRefID: "", ElementRefID: "DOCUMENT"},
},
want: nil,
wantErr: true,
},
{
name: "failure test - invalid tag",
spec: specs3,
args: args{
key: "annotations",
value: specs3["annotations"],
doc: &spdxDocument2_2{},
SPDXElementID: spdx.DocElementID{DocumentRefID: "", ElementRefID: "DOCUMENT"},
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.spec.parseJsonAnnotations2_2(tt.args.key, tt.args.value, tt.args.doc, tt.args.SPDXElementID); (err != nil) != tt.wantErr {
t.Errorf("JSONSpdxDocument.parseJsonAnnotations2_2() error = %v, wantErr %v", err, tt.wantErr)
}

for i := 0; i < len(tt.want); i++ {
if !reflect.DeepEqual(tt.args.doc.Annotations[i], tt.want[i]) {
t.Errorf("Load2_2() = %v, want %v", tt.args.doc.Annotations[i], tt.want[i])
if !tt.wantErr {
for i := 0; i < len(tt.want); i++ {
if !reflect.DeepEqual(tt.args.doc.Annotations[i], tt.want[i]) {
t.Errorf("Load2_2() = %v, want %v", tt.args.doc.Annotations[i], tt.want[i])
}
}
}

Expand Down
60 changes: 59 additions & 1 deletion jsonloader/parser2v2/parse_creation_info_test.go
Expand Up @@ -153,14 +153,72 @@ func TestJSONSpdxDocument_parseJsonCreationInfo2_2(t *testing.T) {
},
wantErr: false,
},
{
name: "failure : Invalid tag ",
spec: specs,
args: args{
key: "invalid",
value: "This document was created using SPDX 2.0 using licenses from the web site.",
doc: &spdxDocument2_2{},
},
want: &spdx.CreationInfo2_2{ExternalDocumentReferences: map[string]spdx.ExternalDocumentRef2_2{}},
wantErr: true,
},
{
name: "failure : DocRef missing in ExternalRefs",
spec: specs,
args: args{
key: "externalDocumentRefs",
value: []map[string]interface{}{
{
"externalDocumentId": "spdx-tool-1.2",
"spdxDocument": "http://spdx.org/spdxdocs/spdx-tools-v1.2-3F2504E0-4F89-41D3-9A0C-0305E82C3301",
"checksum": map[string]interface{}{
"algorithm": "SHA1",
"checksumValue": "d6a770ba38583ed4bb4525bd96e50461655d2759",
},
},
},
doc: &spdxDocument2_2{},
},
want: nil,
wantErr: true,
},
{
name: "failure : invalid SPDXID",
spec: specs,
args: args{
key: "SPDXID",
value: "DOCUMENT",
doc: &spdxDocument2_2{},
},
want: &spdx.CreationInfo2_2{ExternalDocumentReferences: map[string]spdx.ExternalDocumentRef2_2{}},
wantErr: true,
},
{
name: "failure - invalid creator type",
spec: specs,
args: args{
key: "creationInfo",
value: map[string]interface{}{
"comment": "This package has been shipped in source and binary form.\nThe binaries were created with gcc 4.5.1 and expect to link to\ncompatible system run time libraries.",
"created": "2010-01-29T18:30:22Z",
"creators": []string{"Invalid: LicenseFind-1.0", "Organization: ExampleCodeInspect ()", "Person: Jane Doe ()"},
"licenseListVersion": "3.8",
},
doc: &spdxDocument2_2{},
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.args.doc = &spdxDocument2_2{}
if err := tt.spec.parseJsonCreationInfo2_2(tt.args.key, tt.args.value, tt.args.doc); (err != nil) != tt.wantErr {
t.Errorf("JSONSpdxDocument.parseJsonCreationInfo2_2() error = %v, wantErr %v", err, tt.wantErr)
}
if !reflect.DeepEqual(tt.args.doc.CreationInfo, tt.want) {
if !tt.wantErr && !reflect.DeepEqual(tt.args.doc.CreationInfo, tt.want) {
t.Errorf("Load2_2() = %v, want %v", tt.args.doc.CreationInfo, tt.want)
}

Expand Down
16 changes: 9 additions & 7 deletions jsonloader/parser2v2/parse_files_test.go
Expand Up @@ -27,6 +27,7 @@ func TestJSONSpdxDocument_parseJsonFiles2_2(t *testing.T) {
"fileName" : "./src/org/spdx/parser/DOAPProject.java",
"fileTypes" : [ "SOURCE" ],
"licenseConcluded" : "Apache-2.0",
"attributionTexts":["text1"],
"licenseInfoInFiles" : [ "Apache-2.0" ]
}, {
"SPDXID" : "SPDXRef-CommonsLangSrc",
Expand Down Expand Up @@ -97,13 +98,14 @@ func TestJSONSpdxDocument_parseJsonFiles2_2(t *testing.T) {
Value: "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
},
},
FileCopyrightText: "Copyright 2010, 2011 Source Auditor Inc.",
FileContributor: []string{"Protecode Inc.", "SPDX Technical Team Members", "Open Logic Inc.", "Source Auditor Inc.", "Black Duck Software In.c"},
FileDependencies: []string{"SPDXRef-JenaLib", "SPDXRef-CommonsLangSrc"},
FileName: "./src/org/spdx/parser/DOAPProject.java",
FileType: []string{"SOURCE"},
LicenseConcluded: "Apache-2.0",
LicenseInfoInFile: []string{"Apache-2.0"},
FileCopyrightText: "Copyright 2010, 2011 Source Auditor Inc.",
FileContributor: []string{"Protecode Inc.", "SPDX Technical Team Members", "Open Logic Inc.", "Source Auditor Inc.", "Black Duck Software In.c"},
FileDependencies: []string{"SPDXRef-JenaLib", "SPDXRef-CommonsLangSrc"},
FileName: "./src/org/spdx/parser/DOAPProject.java",
FileType: []string{"SOURCE"},
LicenseConcluded: "Apache-2.0",
FileAttributionTexts: []string{"text1"},
LicenseInfoInFile: []string{"Apache-2.0"},
},
"CommonsLangSrc": {
FileSPDXIdentifier: "CommonsLangSrc",
Expand Down
2 changes: 2 additions & 0 deletions jsonloader/parser2v2/parse_snippets_test.go
Expand Up @@ -21,6 +21,7 @@ func TestJSONSpdxDocument_parseJsonSnippets2_2(t *testing.T) {
"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" : {
Expand Down Expand Up @@ -52,6 +53,7 @@ func TestJSONSpdxDocument_parseJsonSnippets2_2(t *testing.T) {
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",
Expand Down
4 changes: 2 additions & 2 deletions jsonsaver/jsonsaver.go
Expand Up @@ -14,10 +14,10 @@ import (
func Save2_2(doc *spdx.Document2_2, w io.Writer) error {
var b []byte
buf := bytes.NewBuffer(b)
result, err := saver2v2.RenderDocument2_2(doc, buf)
err := saver2v2.RenderDocument2_2(doc, buf)
if err != nil {
return err
}
w.Write(result.Bytes())
w.Write(buf.Bytes())
return nil
}

0 comments on commit 25bc95b

Please sign in to comment.