github.com/SAP/jenkins-library@v1.362.0/cmd/integrationArtifactDownload_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package cmd
     5  
     6  import (
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestRunIntegrationArtifactDownload(t *testing.T) {
    14  	t.Parallel()
    15  
    16  	t.Run("Successfull Download of Integration flow Artifact", func(t *testing.T) {
    17  		tempDir := t.TempDir()
    18  		apiServiceKey := `{
    19  			"oauth": {
    20  				"url": "https://demo",
    21  				"clientid": "demouser",
    22  				"clientsecret": "******",
    23  				"tokenurl": "https://demo/oauth/token"
    24  			}
    25  		}`
    26  
    27  		config := integrationArtifactDownloadOptions{
    28  			APIServiceKey:          apiServiceKey,
    29  			IntegrationFlowID:      "flow1",
    30  			IntegrationFlowVersion: "1.0.1",
    31  			DownloadPath:           tempDir,
    32  		}
    33  
    34  		httpClient := httpMockCpis{CPIFunction: "IntegrationArtifactDownload", ResponseBody: ``, TestType: "PositiveAndGetetIntegrationArtifactDownloadResBody"}
    35  
    36  		err := runIntegrationArtifactDownload(&config, nil, &httpClient)
    37  		absolutePath := filepath.Join(tempDir, "flow1.zip")
    38  		assert.DirExists(t, tempDir)
    39  		if assert.NoError(t, err) {
    40  			assert.Equal(t, fileExists(absolutePath), true)
    41  			t.Run("check url", func(t *testing.T) {
    42  				assert.Equal(t, "https://demo/api/v1/IntegrationDesigntimeArtifacts(Id='flow1',Version='1.0.1')/$value", httpClient.URL)
    43  			})
    44  
    45  			t.Run("check method", func(t *testing.T) {
    46  				assert.Equal(t, "GET", httpClient.Method)
    47  			})
    48  		}
    49  	})
    50  
    51  	t.Run("Failed case of Integration Flow artifact Download", func(t *testing.T) {
    52  		apiServiceKey := `{
    53  			"oauth": {
    54  				"url": "https://demo",
    55  				"clientid": "demouser",
    56  				"clientsecret": "******",
    57  				"tokenurl": "https://demo/oauth/token"
    58  			}
    59  		}`
    60  
    61  		config := integrationArtifactDownloadOptions{
    62  			APIServiceKey:          apiServiceKey,
    63  			IntegrationFlowID:      "flow1",
    64  			IntegrationFlowVersion: "1.0.1",
    65  			DownloadPath:           "tmp",
    66  		}
    67  
    68  		httpClient := httpMockCpis{CPIFunction: "IntegrationArtifactDownload", ResponseBody: ``, TestType: "Negative"}
    69  
    70  		err := runIntegrationArtifactDownload(&config, nil, &httpClient)
    71  
    72  		assert.EqualError(t, err, "HTTP GET request to https://demo/api/v1/IntegrationDesigntimeArtifacts(Id='flow1',Version='1.0.1')/$value failed with error: Unable to download integration artifact, Response Status code:400")
    73  	})
    74  }