aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorspecter25 <ujjwalcoding012@gmail.com>2021-06-12 18:28:56 +0530
committerspecter25 <ujjwalcoding012@gmail.com>2021-06-12 18:28:56 +0530
commit7831df4930566d5adc846ca5021676166f6b3d6c (patch)
tree95b0bb9d88431adbc2ff109cdab806ee1f3f65bd
parent783b242ebba84bfc42bcd926dd17ef16992f3413 (diff)
downloadspdx-tools-7831df4930566d5adc846ca5021676166f6b3d6c.tar.gz
Parse annotations from json to spdx and write tests
Signed-off-by: specter25 <ujjwalcoding012@gmail.com>
-rw-r--r--jsonloader2v2/jsonfiles/test.json72
-rw-r--r--jsonloader2v2/jsonloader.go10
-rw-r--r--jsonloader2v2/jsonloader_test.go153
-rw-r--r--jsonloader2v2/parse_annotations.go46
-rw-r--r--jsonloader2v2/parse_annotations_test.go99
5 files changed, 375 insertions, 5 deletions
diff --git a/jsonloader2v2/jsonfiles/test.json b/jsonloader2v2/jsonfiles/test.json
new file mode 100644
index 0000000..5e344ed
--- /dev/null
+++ b/jsonloader2v2/jsonfiles/test.json
@@ -0,0 +1,72 @@
+{
+ "SPDXID" : "SPDXRef-DOCUMENT",
+ "spdxVersion" : "SPDX-2.2",
+ "creationInfo" : {
+ "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" : [ "Tool: LicenseFind-1.0", "Organization: ExampleCodeInspect ()", "Person: Jane Doe ()" ],
+ "licenseListVersion" : "3.8"
+ },
+ "name" : "SPDX-Tools-v2.0",
+ "dataLicense" : "CC0-1.0",
+ "comment" : "This document was created using SPDX 2.0 using licenses from the web site.",
+ "externalDocumentRefs" : [ {
+ "externalDocumentId" : "DocumentRef-spdx-tool-1.2",
+ "checksum" : {
+ "algorithm" : "SHA1",
+ "checksumValue" : "d6a770ba38583ed4bb4525bd96e50461655d2759"
+ },
+ "spdxDocument" : "http://spdx.org/spdxdocs/spdx-tools-v1.2-3F2504E0-4F89-41D3-9A0C-0305E82C3301"
+ } ],
+ "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"
+ }, {
+ "annotationDate" : "2011-03-13T00:00:00Z",
+ "annotationType" : "REVIEW",
+ "annotator" : "Person: Suzanne Reviewer",
+ "comment" : "Another example reviewer."
+ }, {
+ "annotationDate" : "2010-01-29T18:30:22Z",
+ "annotationType" : "OTHER",
+ "annotator" : "Person: Jane Doe ()",
+ "comment" : "Document level annotation"
+ } ],
+ "documentNamespace" : "http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301",
+ "relationships" : [ {
+ "spdxElementId" : "SPDXRef-DOCUMENT",
+ "relatedSpdxElement" : "DocumentRef-spdx-tool-1.2:SPDXRef-ToolsElement",
+ "relationshipType" : "COPY_OF"
+ }, {
+ "spdxElementId" : "SPDXRef-DOCUMENT",
+ "relatedSpdxElement" : "SPDXRef-Package",
+ "relationshipType" : "CONTAINS"
+ }, {
+ "spdxElementId" : "SPDXRef-DOCUMENT",
+ "relatedSpdxElement" : "SPDXRef-File",
+ "relationshipType" : "DESCRIBES"
+ }, {
+ "spdxElementId" : "SPDXRef-DOCUMENT",
+ "relatedSpdxElement" : "SPDXRef-Package",
+ "relationshipType" : "DESCRIBES"
+ }, {
+ "spdxElementId" : "SPDXRef-Package",
+ "relatedSpdxElement" : "SPDXRef-Saxon",
+ "relationshipType" : "DYNAMIC_LINK"
+ }, {
+ "spdxElementId" : "SPDXRef-Package",
+ "relatedSpdxElement" : "SPDXRef-JenaLib",
+ "relationshipType" : "CONTAINS"
+ }, {
+ "spdxElementId" : "SPDXRef-JenaLib",
+ "relatedSpdxElement" : "SPDXRef-Package",
+ "relationshipType" : "CONTAINS"
+ }, {
+ "spdxElementId" : "SPDXRef-File",
+ "relatedSpdxElement" : "SPDXRef-fromDoap-0",
+ "relationshipType" : "GENERATED_FROM"
+ } ]
+ }
+ \ No newline at end of file
diff --git a/jsonloader2v2/jsonloader.go b/jsonloader2v2/jsonloader.go
index a775eab..b655475 100644
--- a/jsonloader2v2/jsonloader.go
+++ b/jsonloader2v2/jsonloader.go
@@ -48,11 +48,11 @@ func (spec JSONSpdxDocument) newDocument(doc *spdxDocument2_2) error {
if err != nil {
return err
}
- // case "annotations":
- // err := spec.parseJsonAnnotations2_2(key, val, doc)
- // if err != nil {
- // return err
- // }
+ case "annotations":
+ err := spec.parseJsonAnnotations2_2(key, val, doc)
+ if err != nil {
+ return err
+ }
case "relationships":
err := spec.parseJsonRelationships2_2(key, val, doc)
if err != nil {
diff --git a/jsonloader2v2/jsonloader_test.go b/jsonloader2v2/jsonloader_test.go
new file mode 100644
index 0000000..5ed2cef
--- /dev/null
+++ b/jsonloader2v2/jsonloader_test.go
@@ -0,0 +1,153 @@
+// Package jsonloader is used to load and parse SPDX JSON documents
+// into tools-golang data structures.
+// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
+
+package jsonloader2v2
+
+import (
+ "io/ioutil"
+ "log"
+ "reflect"
+ "testing"
+
+ "github.com/spdx/tools-golang/spdx"
+)
+
+//TODO: json validity check
+//TODO: passsing an unrecornized key
+
+func TestLoad2_2(t *testing.T) {
+
+ jsonData, err := ioutil.ReadFile("jsonfiles/test.json") // b has type []byte
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ type args struct {
+ content []byte
+ }
+ tests := []struct {
+ name string
+ args args
+ want *spdxDocument2_2
+ wantErr bool
+ }{
+ // TODO: Add test cases.
+ {
+ name: "True test",
+ args: args{content: jsonData},
+ want: &spdxDocument2_2{
+ CreationInfo: &spdx.CreationInfo2_2{
+ DataLicense: "CC0-1.0",
+ SPDXVersion: "SPDX-2.2",
+ SPDXIdentifier: "DOCUMENT",
+ DocumentComment: "This document was created using SPDX 2.0 using licenses from the web site.",
+ LicenseListVersion: "3.8",
+ Created: "2010-01-29T18:30:22Z",
+ CreatorPersons: []string{"Jane Doe"},
+ CreatorOrganizations: []string{"ExampleCodeInspect"},
+ CreatorTools: []string{"LicenseFind-1.0"},
+ DocumentName: "SPDX-Tools-v2.0",
+ DocumentNamespace: "http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301",
+ CreatorComment: "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.",
+ ExternalDocumentReferences: map[string]spdx.ExternalDocumentRef2_2{
+ "spdx-tool-1.2": {
+ DocumentRefID: "spdx-tool-1.2",
+ URI: "http://spdx.org/spdxdocs/spdx-tools-v1.2-3F2504E0-4F89-41D3-9A0C-0305E82C3301",
+ Alg: "SHA1",
+ Checksum: "d6a770ba38583ed4bb4525bd96e50461655d2759",
+ },
+ },
+ },
+ Annotations: []*spdx.Annotation2_2{
+ {
+ AnnotationDate: "2010-02-10T00:00:00Z",
+ AnnotationType: "REVIEW",
+ AnnotatorType: "Person",
+ Annotator: "Joe Reviewer",
+ AnnotationComment: "This is just an example. Some of the non-standard licenses look like they are actually BSD 3 clause licenses",
+ },
+ {
+ AnnotationDate: "2011-03-13T00:00:00Z",
+ AnnotationType: "REVIEW",
+ AnnotatorType: "Person",
+ Annotator: "Suzanne Reviewer",
+ AnnotationComment: "Another example reviewer.",
+ },
+ {
+ AnnotationDate: "2010-01-29T18:30:22Z",
+ AnnotationType: "OTHER",
+ AnnotatorType: "Person",
+ Annotator: "Jane Doe ()",
+ AnnotationComment: "Document level annotation",
+ },
+ },
+ Relationships: []*spdx.Relationship2_2{
+ {
+ RefA: spdx.DocElementID{DocumentRefID: "", ElementRefID: "DOCUMENT"},
+ RefB: spdx.DocElementID{DocumentRefID: "spdx-tool-1.2", ElementRefID: "ToolsElement"},
+ Relationship: "COPY_OF",
+ },
+ {
+ RefA: spdx.DocElementID{DocumentRefID: "", ElementRefID: "DOCUMENT"},
+ RefB: spdx.DocElementID{DocumentRefID: "", ElementRefID: "Package"},
+ Relationship: "CONTAINS",
+ },
+ {
+ RefA: spdx.DocElementID{DocumentRefID: "", ElementRefID: "DOCUMENT"},
+ RefB: spdx.DocElementID{DocumentRefID: "", ElementRefID: "File"},
+ Relationship: "DESCRIBES",
+ },
+ {
+ RefA: spdx.DocElementID{DocumentRefID: "", ElementRefID: "DOCUMENT"},
+ RefB: spdx.DocElementID{DocumentRefID: "", ElementRefID: "Package"},
+ Relationship: "DESCRIBES",
+ },
+ {
+ RefA: spdx.DocElementID{DocumentRefID: "", ElementRefID: "Package"},
+ RefB: spdx.DocElementID{DocumentRefID: "", ElementRefID: "Saxon"},
+ Relationship: "DYNAMIC_LINK",
+ },
+ {
+ RefA: spdx.DocElementID{DocumentRefID: "", ElementRefID: "Package"},
+ RefB: spdx.DocElementID{DocumentRefID: "", ElementRefID: "JenaLib"},
+ Relationship: "CONTAINS",
+ },
+ {
+ RefA: spdx.DocElementID{DocumentRefID: "", ElementRefID: "JenaLib"},
+ RefB: spdx.DocElementID{DocumentRefID: "", ElementRefID: "Package"},
+ Relationship: "CONTAINS",
+ },
+ {
+ RefA: spdx.DocElementID{DocumentRefID: "", ElementRefID: "File"},
+ RefB: spdx.DocElementID{DocumentRefID: "", ElementRefID: "fromDoap-0"},
+ Relationship: "GENERATED_FROM",
+ },
+ },
+ },
+ wantErr: false,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got, err := Load2_2(tt.args.content)
+ if (err != nil) != tt.wantErr {
+ t.Errorf("Load2_2() error = %v, wantErr %v", err, tt.wantErr)
+ return
+ }
+ if !reflect.DeepEqual(got.CreationInfo, tt.want.CreationInfo) {
+ t.Errorf("Load2_2() = %v, want %v", got.CreationInfo, tt.want.CreationInfo)
+ }
+ for i := 0; i < len(got.Annotations); i++ {
+ if !reflect.DeepEqual(got.Annotations[i], tt.want.Annotations[i]) {
+ t.Errorf("Load2_2() = %v, want %v", got.Annotations[i], tt.want.Annotations[i])
+ }
+ }
+ for i := 0; i < len(got.Relationships); i++ {
+ if !reflect.DeepEqual(got.Relationships[i], tt.want.Relationships[i]) {
+ t.Errorf("Load2_2() = %v, want %v", got.Relationships[i], tt.want.Relationships[i])
+ }
+ }
+ })
+ }
+}
diff --git a/jsonloader2v2/parse_annotations.go b/jsonloader2v2/parse_annotations.go
new file mode 100644
index 0000000..9126370
--- /dev/null
+++ b/jsonloader2v2/parse_annotations.go
@@ -0,0 +1,46 @@
+package jsonloader2v2
+
+import (
+ "fmt"
+ "reflect"
+
+ "github.com/spdx/tools-golang/spdx"
+)
+
+func (spec JSONSpdxDocument) parseJsonAnnotations2_2(key string, value interface{}, doc *spdxDocument2_2) error {
+ //FIXME: SPDXID property not defined in spec but it is needed
+ if reflect.TypeOf(value).Kind() == reflect.Slice {
+ annotations := reflect.ValueOf(value)
+ for i := 0; i < annotations.Len(); i++ {
+ annotation := annotations.Index(i).Interface().(map[string]interface{})
+ ann := spdx.Annotation2_2{}
+ // Remove loop all properties are mandatory in annotations
+ for k, v := range annotation {
+ switch k {
+ case "annotationDate":
+ ann.AnnotationDate = v.(string)
+ case "annotationType":
+ ann.AnnotationType = v.(string)
+ case "comment":
+ ann.AnnotationComment = v.(string)
+ case "annotator":
+ subkey, subvalue, err := extractSubs(v.(string))
+ if err != nil {
+ return err
+ }
+ if subkey != "Person" && subkey != "Organization" && subkey != "Tool" {
+ return fmt.Errorf("unrecognized Annotator type %v", subkey)
+ }
+ ann.AnnotatorType = subkey
+ ann.Annotator = subvalue
+
+ default:
+ return fmt.Errorf("received unknown tag %v in Annotation section", k)
+ }
+ }
+ doc.Annotations = append(doc.Annotations, &ann)
+ }
+
+ }
+ return nil
+}
diff --git a/jsonloader2v2/parse_annotations_test.go b/jsonloader2v2/parse_annotations_test.go
new file mode 100644
index 0000000..6e0690b
--- /dev/null
+++ b/jsonloader2v2/parse_annotations_test.go
@@ -0,0 +1,99 @@
+package jsonloader2v2
+
+import (
+ "encoding/json"
+ "reflect"
+ "testing"
+
+ "github.com/spdx/tools-golang/spdx"
+)
+
+func TestJSONSpdxDocument_parseJsonAnnotations2_2(t *testing.T) {
+
+ data := []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"
+ }, {
+ "annotationDate" : "2011-03-13T00:00:00Z",
+ "annotationType" : "REVIEW",
+ "annotator" : "Person: Suzanne Reviewer",
+ "comment" : "Another example reviewer."
+ }, {
+ "annotationDate" : "2010-01-29T18:30:22Z",
+ "annotationType" : "OTHER",
+ "annotator" : "Person: Jane Doe ()",
+ "comment" : "Document level annotation"
+ } ]
+ }
+ `)
+
+ annotationstest1 := []*spdx.Annotation2_2{
+ {
+ AnnotationDate: "2010-02-10T00:00:00Z",
+ AnnotationType: "REVIEW",
+ AnnotatorType: "Person",
+ Annotator: "Joe Reviewer",
+ AnnotationComment: "This is just an example. Some of the non-standard licenses look like they are actually BSD 3 clause licenses",
+ },
+ {
+ AnnotationDate: "2011-03-13T00:00:00Z",
+ AnnotationType: "REVIEW",
+ AnnotatorType: "Person",
+ Annotator: "Suzanne Reviewer",
+ AnnotationComment: "Another example reviewer.",
+ },
+ {
+ AnnotationDate: "2010-01-29T18:30:22Z",
+ AnnotationType: "OTHER",
+ AnnotatorType: "Person",
+ Annotator: "Jane Doe ()",
+ AnnotationComment: "Document level annotation",
+ },
+ }
+
+ var specs JSONSpdxDocument
+ json.Unmarshal(data, &specs)
+
+ type args struct {
+ key string
+ value interface{}
+ doc *spdxDocument2_2
+ }
+ tests := []struct {
+ name string
+ spec JSONSpdxDocument
+ args args
+ want []*spdx.Annotation2_2
+ wantErr bool
+ }{
+ // TODO: Add test cases.
+ {
+ name: "successTest",
+ spec: specs,
+ args: args{
+ key: "annotations",
+ value: specs["annotations"],
+ doc: &spdxDocument2_2{},
+ },
+ want: annotationstest1,
+ wantErr: false,
+ },
+ }
+ 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); (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])
+ }
+ }
+
+ })
+ }
+}