github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/pkg/java_test.go (about)

     1  package pkg
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestPomProperties_PkgTypeIndicated(t *testing.T) {
    11  	cases := []struct {
    12  		name          string
    13  		pomProperties JavaPomProperties
    14  		expectedType  Type
    15  	}{
    16  		{
    17  			name: "regular Java package",
    18  			pomProperties: JavaPomProperties{
    19  				Path:       "some path",
    20  				Name:       "some name",
    21  				GroupID:    "some group ID",
    22  				ArtifactID: "some artifact ID",
    23  				Version:    "1",
    24  			},
    25  			expectedType: JavaPkg,
    26  		},
    27  		{
    28  			name: "cloudbees jenkins plugin",
    29  			pomProperties: JavaPomProperties{
    30  				Path:       "some path",
    31  				Name:       "some name",
    32  				GroupID:    "com.cloudbees.jenkins.plugins",
    33  				ArtifactID: "some artifact ID",
    34  				Version:    "1",
    35  			},
    36  			expectedType: JenkinsPluginPkg,
    37  		},
    38  		{
    39  			name: "jenkins.io plugin",
    40  			pomProperties: JavaPomProperties{
    41  				Path:       "some path",
    42  				Name:       "some name",
    43  				GroupID:    "io.jenkins.plugins",
    44  				ArtifactID: "some artifact ID",
    45  				Version:    "1",
    46  			},
    47  			expectedType: JenkinsPluginPkg,
    48  		},
    49  		{
    50  			name: "jenkins-ci.io plugin",
    51  			pomProperties: JavaPomProperties{
    52  				Path:       "some path",
    53  				Name:       "some name",
    54  				GroupID:    "io.jenkins-ci.plugins",
    55  				ArtifactID: "some artifact ID",
    56  				Version:    "1",
    57  			},
    58  			expectedType: JenkinsPluginPkg,
    59  		},
    60  		{
    61  			name: "jenkins-ci.org plugin",
    62  			pomProperties: JavaPomProperties{
    63  				Path:       "some path",
    64  				Name:       "some name",
    65  				GroupID:    "org.jenkins-ci.plugins",
    66  				ArtifactID: "some artifact ID",
    67  				Version:    "1",
    68  			},
    69  			expectedType: JenkinsPluginPkg,
    70  		},
    71  		{
    72  			name: "jenkins.org plugin",
    73  			pomProperties: JavaPomProperties{
    74  				Path:       "some path",
    75  				Name:       "some name",
    76  				GroupID:    "org.jenkins.plugins",
    77  				ArtifactID: "some artifact ID",
    78  				Version:    "1",
    79  			},
    80  			expectedType: JenkinsPluginPkg,
    81  		},
    82  		{
    83  			name: "jenkins plugin prefix",
    84  			pomProperties: JavaPomProperties{
    85  				Path:       "some path",
    86  				Name:       "some name",
    87  				GroupID:    "com.cloudbees.jenkins.plugins.bluesteel",
    88  				ArtifactID: "some artifact ID",
    89  				Version:    "1",
    90  			},
    91  			expectedType: JenkinsPluginPkg,
    92  		},
    93  		{
    94  			name: "jenkins.plugin somewhere in group id",
    95  			pomProperties: JavaPomProperties{
    96  				Path:       "some path",
    97  				Name:       "some name",
    98  				GroupID:    "org.wagoodman.jenkins.plugins.something",
    99  				ArtifactID: "some artifact ID",
   100  				Version:    "1",
   101  			},
   102  			expectedType: JenkinsPluginPkg,
   103  		},
   104  	}
   105  
   106  	for _, tc := range cases {
   107  		t.Run(tc.name, func(t *testing.T) {
   108  			actual := tc.pomProperties.PkgTypeIndicated()
   109  			assert.Equal(t, tc.expectedType, actual)
   110  		})
   111  	}
   112  }
   113  
   114  func Test_legacyJavaManifest_toNewManifest(t *testing.T) {
   115  	tests := []struct {
   116  		name string
   117  		lm   legacyJavaManifest
   118  		want JavaManifest
   119  	}{
   120  		{
   121  			name: "empty",
   122  			lm:   legacyJavaManifest{},
   123  			want: JavaManifest{},
   124  		},
   125  		{
   126  			name: "main sections are sorted",
   127  			lm: legacyJavaManifest{
   128  				Main: map[string]string{
   129  					"a key": "a value",
   130  					"b key": "b value",
   131  					"c key": "c value",
   132  				},
   133  			},
   134  			want: JavaManifest{Main: KeyValues{
   135  				{
   136  					Key:   "a key",
   137  					Value: "a value",
   138  				},
   139  				{
   140  					Key:   "b key",
   141  					Value: "b value",
   142  				},
   143  				{
   144  					Key:   "c key",
   145  					Value: "c value",
   146  				},
   147  			}},
   148  		},
   149  		{
   150  			name: "named sections have their name in the result",
   151  			lm: legacyJavaManifest{
   152  				NamedSections: map[string]map[string]string{
   153  					"a section": {
   154  						"a key": "a value",
   155  						"b key": "b value",
   156  						"c key": "c value",
   157  					},
   158  					"b section": {
   159  						"d key": "d value",
   160  						"e key": "e value",
   161  						"f key": "f value",
   162  					},
   163  				},
   164  			},
   165  			want: JavaManifest{Sections: []KeyValues{
   166  				{
   167  					{
   168  						Key:   "Name",
   169  						Value: "a section",
   170  					},
   171  					{
   172  						Key:   "a key",
   173  						Value: "a value",
   174  					},
   175  					{
   176  						Key:   "b key",
   177  						Value: "b value",
   178  					},
   179  					{
   180  						Key:   "c key",
   181  						Value: "c value",
   182  					},
   183  				},
   184  				{
   185  					{
   186  						Key:   "Name",
   187  						Value: "b section",
   188  					},
   189  					{
   190  						Key:   "d key",
   191  						Value: "d value",
   192  					},
   193  					{
   194  						Key:   "e key",
   195  						Value: "e value",
   196  					},
   197  					{
   198  						Key:   "f key",
   199  						Value: "f value",
   200  					},
   201  				},
   202  			}},
   203  		},
   204  	}
   205  	for _, tt := range tests {
   206  		t.Run(tt.name, func(t *testing.T) {
   207  			if diff := cmp.Diff(tt.want, tt.lm.toNewManifest()); diff != "" {
   208  				t.Errorf("unexpected diff in manifest (-want +got):\n%s", diff)
   209  			}
   210  		})
   211  	}
   212  }