github.com/xgoffin/jenkins-library@v1.154.0/cmd/integrationArtifactDownload_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/SAP/jenkins-library/pkg/mock"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  type integrationArtifactDownloadMockUtils struct {
    14  	*mock.ExecMockRunner
    15  	*mock.FilesMock
    16  }
    17  
    18  func newIntegrationArtifactDownloadTestsUtils() integrationArtifactDownloadMockUtils {
    19  	utils := integrationArtifactDownloadMockUtils{
    20  		ExecMockRunner: &mock.ExecMockRunner{},
    21  		FilesMock:      &mock.FilesMock{},
    22  	}
    23  	return utils
    24  }
    25  
    26  func TestRunIntegrationArtifactDownload(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	t.Run("Successfull Download of Integration flow Artifact", func(t *testing.T) {
    30  		tempDir, tmpErr := ioutil.TempDir("", "")
    31  		defer os.RemoveAll(tempDir) // clean up
    32  		assert.NoError(t, tmpErr, "Error when creating temp dir")
    33  		apiServiceKey := `{
    34  			"oauth": {
    35  				"url": "https://demo",
    36  				"clientid": "demouser",
    37  				"clientsecret": "******",
    38  				"tokenurl": "https://demo/oauth/token"
    39  			}
    40  		}`
    41  
    42  		config := integrationArtifactDownloadOptions{
    43  			APIServiceKey:          apiServiceKey,
    44  			IntegrationFlowID:      "flow1",
    45  			IntegrationFlowVersion: "1.0.1",
    46  			DownloadPath:           tempDir,
    47  		}
    48  
    49  		httpClient := httpMockCpis{CPIFunction: "IntegrationArtifactDownload", ResponseBody: ``, TestType: "PositiveAndGetetIntegrationArtifactDownloadResBody"}
    50  
    51  		err := runIntegrationArtifactDownload(&config, nil, &httpClient)
    52  		absolutePath := filepath.Join(tempDir, "flow1.zip")
    53  		assert.DirExists(t, tempDir)
    54  		if assert.NoError(t, err) {
    55  			assert.Equal(t, fileExists(absolutePath), true)
    56  			t.Run("check url", func(t *testing.T) {
    57  				assert.Equal(t, "https://demo/api/v1/IntegrationDesigntimeArtifacts(Id='flow1',Version='1.0.1')/$value", httpClient.URL)
    58  			})
    59  
    60  			t.Run("check method", func(t *testing.T) {
    61  				assert.Equal(t, "GET", httpClient.Method)
    62  			})
    63  		}
    64  	})
    65  
    66  	t.Run("Failed case of Integration Flow artifact Download", func(t *testing.T) {
    67  		apiServiceKey := `{
    68  			"oauth": {
    69  				"url": "https://demo",
    70  				"clientid": "demouser",
    71  				"clientsecret": "******",
    72  				"tokenurl": "https://demo/oauth/token"
    73  			}
    74  		}`
    75  
    76  		config := integrationArtifactDownloadOptions{
    77  			APIServiceKey:          apiServiceKey,
    78  			IntegrationFlowID:      "flow1",
    79  			IntegrationFlowVersion: "1.0.1",
    80  			DownloadPath:           "tmp",
    81  		}
    82  
    83  		httpClient := httpMockCpis{CPIFunction: "IntegrationArtifactDownload", ResponseBody: ``, TestType: "Negative"}
    84  
    85  		err := runIntegrationArtifactDownload(&config, nil, &httpClient)
    86  
    87  		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")
    88  	})
    89  }