package whitesource import ( "testing" "github.com/SAP/jenkins-library/pkg/mock" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestExecuteScanMTA(t *testing.T) { const pomXML = ` 4.0.0 my-artifact-id jar ` config := ScanOptions{ ScanType: "mta", OrgToken: "org-token", UserToken: "user-token", ProductName: "mock-product", ProductVersion: "product-version", ProjectName: "mock-project", } t.Parallel() t.Run("happy path MTA", func(t *testing.T) { // init utilsMock := NewScanUtilsMock() utilsMock.AddFile("pom.xml", []byte(pomXML)) utilsMock.AddFile("package.json", []byte(`{"name":"my-module-name"}`)) scan := newTestScan(&config) // test err := scan.ExecuteMTAScan(&config, utilsMock) // assert require.NoError(t, err) expectedCalls := []mock.ExecCall{ { Exec: "mvn", Params: []string{ "--file", "pom.xml", "-Dorg.whitesource.orgToken=org-token", "-Dorg.whitesource.product=mock-product", "-Dorg.whitesource.checkPolicies=true", "-Dorg.whitesource.failOnError=true", "-Dorg.whitesource.forceUpdate=true", "-Dorg.whitesource.aggregateProjectName=mock-project", "-Dorg.whitesource.aggregateModules=true", "-Dorg.whitesource.userKey=user-token", "-Dorg.whitesource.productVersion=product-version", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "org.whitesource:whitesource-maven-plugin:19.5.1:update", }, }, { Exec: "npm", Params: []string{ "ls", }, }, { Exec: "npx", Params: []string{ "whitesource", "run", }, }, } assert.Equal(t, expectedCalls, utilsMock.Calls) assert.True(t, utilsMock.HasWrittenFile(whiteSourceConfig)) assert.True(t, utilsMock.HasRemovedFile(whiteSourceConfig)) assert.Equal(t, expectedCalls, utilsMock.Calls) }) t.Run("MTA with only maven modules", func(t *testing.T) { // init utilsMock := NewScanUtilsMock() utilsMock.AddFile("pom.xml", []byte(pomXML)) scan := newTestScan(&config) // test err := scan.ExecuteMTAScan(&config, utilsMock) // assert require.NoError(t, err) expectedCalls := []mock.ExecCall{ { Exec: "mvn", Params: []string{ "--file", "pom.xml", "-Dorg.whitesource.orgToken=org-token", "-Dorg.whitesource.product=mock-product", "-Dorg.whitesource.checkPolicies=true", "-Dorg.whitesource.failOnError=true", "-Dorg.whitesource.forceUpdate=true", "-Dorg.whitesource.aggregateProjectName=mock-project", "-Dorg.whitesource.aggregateModules=true", "-Dorg.whitesource.userKey=user-token", "-Dorg.whitesource.productVersion=product-version", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "org.whitesource:whitesource-maven-plugin:19.5.1:update", }, }, } assert.Equal(t, expectedCalls, utilsMock.Calls) assert.False(t, utilsMock.HasWrittenFile(whiteSourceConfig)) assert.Equal(t, expectedCalls, utilsMock.Calls) }) t.Run("MTA with only NPM modules", func(t *testing.T) { // init utilsMock := NewScanUtilsMock() utilsMock.AddFile("package.json", []byte(`{"name":"my-module-name"}`)) scan := newTestScan(&config) // test err := scan.ExecuteMTAScan(&config, utilsMock) // assert require.NoError(t, err) expectedCalls := []mock.ExecCall{ { Exec: "npm", Params: []string{ "ls", }, }, { Exec: "npx", Params: []string{ "whitesource", "run", }, }, } assert.Equal(t, expectedCalls, utilsMock.Calls) assert.True(t, utilsMock.HasWrittenFile(whiteSourceConfig)) assert.True(t, utilsMock.HasRemovedFile(whiteSourceConfig)) assert.Equal(t, expectedCalls, utilsMock.Calls) }) t.Run("MTA with neither Maven nor NPM modules results in error", func(t *testing.T) { // init utilsMock := NewScanUtilsMock() scan := newTestScan(&config) // test err := scan.ExecuteMTAScan(&config, utilsMock) // assert assert.EqualError(t, err, "neither Maven nor NPM modules found, no scan performed") }) }