github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/whitesource/scanMaven_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package whitesource
     5  
     6  import (
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/SAP/jenkins-library/pkg/mock"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestExecuteScanMaven(t *testing.T) {
    16  	t.Parallel()
    17  	t.Run("maven modules are aggregated", func(t *testing.T) {
    18  		// init
    19  		const pomXML = `<?xml version="1.0" encoding="UTF-8"?>
    20  <project xmlns="http://maven.apache.org/POM/4.0.0"
    21           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    22           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    23      <modelVersion>4.0.0</modelVersion>
    24      <artifactId>my-artifact-id</artifactId>
    25      <packaging>jar</packaging>
    26  </project>
    27  `
    28  		config := ScanOptions{
    29  			ScanType:       "maven",
    30  			OrgToken:       "org-token",
    31  			UserToken:      "user-token",
    32  			ProductName:    "mock-product",
    33  			ProductVersion: "product-version",
    34  			ProjectName:    "mock-project",
    35  		}
    36  		utilsMock := NewScanUtilsMock()
    37  		utilsMock.AddFile("pom.xml", []byte(pomXML))
    38  		scan := newTestScan(&config)
    39  		// test
    40  		err := scan.ExecuteMavenScan(&config, utilsMock)
    41  		// assert
    42  		require.NoError(t, err)
    43  		expectedCalls := []mock.ExecCall{
    44  			{
    45  				Exec: "mvn",
    46  				Params: []string{
    47  					"--file",
    48  					"pom.xml",
    49  					"-Dorg.whitesource.orgToken=org-token",
    50  					"-Dorg.whitesource.product=mock-product",
    51  					"-Dorg.whitesource.checkPolicies=true",
    52  					"-Dorg.whitesource.failOnError=true",
    53  					"-Dorg.whitesource.forceUpdate=true",
    54  					"-Dorg.whitesource.aggregateProjectName=mock-project",
    55  					"-Dorg.whitesource.aggregateModules=true",
    56  					"-Dorg.whitesource.userKey=user-token",
    57  					"-Dorg.whitesource.productVersion=product-version",
    58  					"-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn",
    59  					"--batch-mode",
    60  					"org.whitesource:whitesource-maven-plugin:19.5.1:update",
    61  				},
    62  			},
    63  		}
    64  		assert.Equal(t, expectedCalls, utilsMock.Calls)
    65  	})
    66  	t.Run("maven modules are separate projects", func(t *testing.T) {
    67  		// init
    68  		const rootPomXML = `<?xml version="1.0" encoding="UTF-8"?>
    69  <project xmlns="http://maven.apache.org/POM/4.0.0"
    70           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    71           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    72      <modelVersion>4.0.0</modelVersion>
    73      <artifactId>my-artifact-id</artifactId>
    74      <packaging>jar</packaging>
    75  	<modules>
    76  		<module>sub</module>
    77  	</modules>
    78  </project>
    79  `
    80  		const modulePomXML = `<?xml version="1.0" encoding="UTF-8"?>
    81  <project xmlns="http://maven.apache.org/POM/4.0.0"
    82           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    83           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    84      <modelVersion>4.0.0</modelVersion>
    85      <artifactId>my-artifact-id-sub</artifactId>
    86      <packaging>jar</packaging>
    87  </project>
    88  `
    89  		config := ScanOptions{
    90  			ScanType:       "maven",
    91  			OrgToken:       "org-token",
    92  			UserToken:      "user-token",
    93  			ProductName:    "mock-product",
    94  			ProductVersion: "product-version",
    95  			ProductToken:   "product-version",
    96  		}
    97  		utilsMock := NewScanUtilsMock()
    98  		utilsMock.AddFile("pom.xml", []byte(rootPomXML))
    99  		utilsMock.AddFile(filepath.Join("sub", "pom.xml"), []byte(modulePomXML))
   100  		scan := newTestScan(&config)
   101  		// test
   102  		err := scan.ExecuteMavenScan(&config, utilsMock)
   103  		// assert
   104  		require.NoError(t, err)
   105  		expectedCalls := []mock.ExecCall{
   106  			{
   107  				Exec: "mvn",
   108  				Params: []string{
   109  					"--file",
   110  					"pom.xml",
   111  					"-Dorg.whitesource.orgToken=org-token",
   112  					"-Dorg.whitesource.product=mock-product",
   113  					"-Dorg.whitesource.checkPolicies=true",
   114  					"-Dorg.whitesource.failOnError=true",
   115  					"-Dorg.whitesource.forceUpdate=true",
   116  					"-Dorg.whitesource.userKey=user-token",
   117  					"-Dorg.whitesource.productVersion=product-version",
   118  					"-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn",
   119  					"--batch-mode",
   120  					"org.whitesource:whitesource-maven-plugin:19.5.1:update",
   121  				},
   122  			},
   123  		}
   124  		assert.Equal(t, expectedCalls, utilsMock.Calls)
   125  		require.Len(t, scan.ScannedProjects(), 2)
   126  		_, existsRoot := scan.ProjectByName("my-artifact-id - product-version")
   127  		_, existsModule := scan.ProjectByName("my-artifact-id-sub - product-version")
   128  		assert.True(t, existsRoot)
   129  		assert.True(t, existsModule)
   130  	})
   131  	t.Run("pom.xml does not exist", func(t *testing.T) {
   132  		// init
   133  		config := ScanOptions{
   134  			ScanType:    "maven",
   135  			OrgToken:    "org-token",
   136  			UserToken:   "user-token",
   137  			ProductName: "mock-product",
   138  		}
   139  		utilsMock := NewScanUtilsMock()
   140  		scan := newTestScan(&config)
   141  		// test
   142  		err := scan.ExecuteMavenScan(&config, utilsMock)
   143  		// assert
   144  		assert.EqualError(t, err,
   145  			"for scanning with type 'maven', the file 'pom.xml' must exist in the project root")
   146  		assert.Len(t, utilsMock.Calls, 0)
   147  	})
   148  }