github.com/google/osv-scalibr@v0.4.1/extractor/filesystem/language/java/purl/purl_test.go (about)

     1  // Copyright 2025 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package purl_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/google/go-cmp/cmp"
    21  	archivemeta "github.com/google/osv-scalibr/extractor/filesystem/language/java/archive/metadata"
    22  	"github.com/google/osv-scalibr/extractor/filesystem/language/java/javalockfile"
    23  	mavenpurl "github.com/google/osv-scalibr/extractor/filesystem/language/java/purl"
    24  	"github.com/google/osv-scalibr/purl"
    25  )
    26  
    27  func TestMakePackageURL(t *testing.T) {
    28  	tests := []struct {
    29  		desc     string
    30  		version  string
    31  		metadata any
    32  		want     *purl.PackageURL
    33  	}{
    34  		{
    35  			desc:    "lowercase_name",
    36  			version: "1.0.0",
    37  			metadata: &javalockfile.Metadata{
    38  				ArtifactID: "name",
    39  			},
    40  			want: &purl.PackageURL{
    41  				Type:    purl.TypeMaven,
    42  				Name:    "name",
    43  				Version: "1.0.0",
    44  			},
    45  		},
    46  		{
    47  			desc:    "mixed_case_name",
    48  			version: "1.0.0",
    49  			metadata: &javalockfile.Metadata{
    50  				ArtifactID: "Name",
    51  			},
    52  			want: &purl.PackageURL{
    53  				Type:    purl.TypeMaven,
    54  				Name:    "name",
    55  				Version: "1.0.0",
    56  			},
    57  		},
    58  		{
    59  			desc:    "group_id",
    60  			version: "1.0.0",
    61  			metadata: &javalockfile.Metadata{
    62  				GroupID:    "id",
    63  				ArtifactID: "name",
    64  			},
    65  			want: &purl.PackageURL{
    66  				Type:      purl.TypeMaven,
    67  				Namespace: "id",
    68  				Name:      "name",
    69  				Version:   "1.0.0",
    70  			},
    71  		},
    72  		{
    73  			desc:    "mixed_case_group_id",
    74  			version: "1.0.0",
    75  			metadata: &javalockfile.Metadata{
    76  				GroupID:    "Id",
    77  				ArtifactID: "name",
    78  			},
    79  			want: &purl.PackageURL{
    80  				Type:      purl.TypeMaven,
    81  				Namespace: "id",
    82  				Name:      "name",
    83  				Version:   "1.0.0",
    84  			},
    85  		},
    86  		{
    87  			desc:    "archive_metadata",
    88  			version: "1.0.0",
    89  			metadata: &archivemeta.Metadata{
    90  				GroupID:    "id",
    91  				ArtifactID: "name",
    92  			},
    93  			want: &purl.PackageURL{
    94  				Type:      purl.TypeMaven,
    95  				Namespace: "id",
    96  				Name:      "name",
    97  				Version:   "1.0.0",
    98  			},
    99  		},
   100  		{
   101  			desc:    "type_classifier",
   102  			version: "1.0.0",
   103  			metadata: &javalockfile.Metadata{
   104  				ArtifactID: "name",
   105  				Classifier: "classifier",
   106  				Type:       "type",
   107  			},
   108  			want: &purl.PackageURL{
   109  				Type:    purl.TypeMaven,
   110  				Name:    "name",
   111  				Version: "1.0.0",
   112  				Qualifiers: purl.QualifiersFromMap(map[string]string{
   113  					"classifier": "classifier",
   114  					"type":       "type",
   115  				}),
   116  			},
   117  		},
   118  	}
   119  
   120  	for _, tt := range tests {
   121  		t.Run(tt.desc, func(t *testing.T) {
   122  			got := mavenpurl.MakePackageURL(tt.version, tt.metadata)
   123  			if diff := cmp.Diff(tt.want, got); diff != "" {
   124  				t.Errorf("mavenpurl.MakePackageURL(%v, %v): unexpected diff (-want +got):\n%s", tt.version, tt.metadata, diff)
   125  			}
   126  		})
   127  	}
   128  }