github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/integrationArtifactUpload_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/ouraigua/jenkins-library/pkg/mock"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestRunIntegrationArtifactUpload(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	t.Run("Successfull Integration Flow Create Test", func(t *testing.T) {
    18  		filesMock := mock.FilesMock{}
    19  		path := filepath.Join("tempDir", "iflow4.zip")
    20  		filesMock.AddFile(path, []byte("dummy content"))
    21  		exists, err := filesMock.FileExists(path)
    22  		assert.NoError(t, err)
    23  		assert.True(t, exists)
    24  
    25  		apiServiceKey := `{
    26  			"oauth": {
    27  				"url": "https://demo",
    28  				"clientid": "demouser",
    29  				"clientsecret": "******",
    30  				"tokenurl": "https://demo/oauth/token"
    31  			}
    32  		}`
    33  
    34  		config := integrationArtifactUploadOptions{
    35  			APIServiceKey:       apiServiceKey,
    36  			IntegrationFlowName: "flow4",
    37  			IntegrationFlowID:   "flow4",
    38  			PackageID:           "CICD",
    39  			FilePath:            path,
    40  		}
    41  
    42  		httpClient := httpMockCpis{CPIFunction: "", ResponseBody: ``, TestType: "PositiveAndCreateIntegrationDesigntimeArtifactResBody"}
    43  
    44  		err = runIntegrationArtifactUpload(&config, nil, &filesMock, &httpClient)
    45  
    46  		if assert.NoError(t, err) {
    47  
    48  			t.Run("check url", func(t *testing.T) {
    49  				assert.Equal(t, "https://demo/api/v1/IntegrationDesigntimeArtifacts(Id='flow4',Version='Active')", httpClient.URL)
    50  			})
    51  
    52  			t.Run("check method", func(t *testing.T) {
    53  				assert.Equal(t, "PUT", httpClient.Method)
    54  			})
    55  		}
    56  	})
    57  
    58  	t.Run("Successfull Integration Flow Update Test", func(t *testing.T) {
    59  
    60  		files := mock.FilesMock{}
    61  		path := filepath.Join("tempDir", "iflow4.zip")
    62  		files.AddFile(path, []byte("dummy content"))
    63  		exists, err := files.FileExists(path)
    64  		assert.NoError(t, err)
    65  		assert.True(t, exists)
    66  		apiServiceKey := `{
    67  			"oauth": {
    68  				"url": "https://demo",
    69  				"clientid": "demouser",
    70  				"clientsecret": "******",
    71  				"tokenurl": "https://demo/oauth/token"
    72  			}
    73  		}`
    74  		config := integrationArtifactUploadOptions{
    75  			APIServiceKey:       apiServiceKey,
    76  			IntegrationFlowName: "flow4",
    77  			IntegrationFlowID:   "flow4",
    78  			PackageID:           "CICD",
    79  			FilePath:            path,
    80  		}
    81  
    82  		httpClient := httpMockCpis{CPIFunction: "", ResponseBody: ``, TestType: "PositiveAndUpdateIntegrationDesigntimeArtifactResBody"}
    83  
    84  		err = runIntegrationArtifactUpload(&config, nil, &files, &httpClient)
    85  
    86  		if assert.NoError(t, err) {
    87  
    88  			t.Run("check url", func(t *testing.T) {
    89  				assert.Equal(t, "https://demo/api/v1/IntegrationDesigntimeArtifacts", httpClient.URL)
    90  			})
    91  
    92  			t.Run("check method", func(t *testing.T) {
    93  				assert.Equal(t, "POST", httpClient.Method)
    94  			})
    95  		}
    96  	})
    97  
    98  	t.Run("Failed case of Integration Flow Get Test", func(t *testing.T) {
    99  
   100  		apiServiceKey := `{
   101  			"oauth": {
   102  				"url": "https://demo",
   103  				"clientid": "demouser",
   104  				"clientsecret": "******",
   105  				"tokenurl": "https://demo/oauth/token"
   106  			}
   107  		}`
   108  		config := integrationArtifactUploadOptions{
   109  			APIServiceKey:       apiServiceKey,
   110  			IntegrationFlowName: "flow4",
   111  			IntegrationFlowID:   "flow4",
   112  			PackageID:           "CICD",
   113  			FilePath:            "path",
   114  		}
   115  
   116  		httpClient := httpMockCpis{CPIFunction: "", ResponseBody: ``, TestType: "NegativeAndGetIntegrationDesigntimeArtifactResBody"}
   117  
   118  		err := runIntegrationArtifactUpload(&config, nil, nil, &httpClient)
   119  		assert.Error(t, err)
   120  	})
   121  
   122  	t.Run("Failed case of Integration Flow Update Test", func(t *testing.T) {
   123  		files := mock.FilesMock{}
   124  		path := filepath.Join("tempDir", "iflow4.zip")
   125  		files.AddFile(path, []byte("dummy content"))
   126  		exists, err := files.FileExists(path)
   127  		assert.NoError(t, err)
   128  		assert.True(t, exists)
   129  
   130  		apiServiceKey := `{
   131  			"oauth": {
   132  				"url": "https://demo",
   133  				"clientid": "demouser",
   134  				"clientsecret": "******",
   135  				"tokenurl": "https://demo/oauth/token"
   136  			}
   137  		}`
   138  		config := integrationArtifactUploadOptions{
   139  			APIServiceKey:       apiServiceKey,
   140  			IntegrationFlowName: "flow4",
   141  			IntegrationFlowID:   "flow4",
   142  			PackageID:           "CICD",
   143  			FilePath:            path,
   144  		}
   145  
   146  		httpClient := httpMockCpis{CPIFunction: "", ResponseBody: ``, TestType: "NegativeAndCreateIntegrationDesigntimeArtifactResBody"}
   147  
   148  		err = runIntegrationArtifactUpload(&config, nil, &files, &httpClient)
   149  		assert.EqualError(t, err, "HTTP PUT request to https://demo/api/v1/IntegrationDesigntimeArtifacts(Id='flow4',Version='Active') failed with error: : 401 Unauthorized")
   150  	})
   151  
   152  	t.Run("Failed case of Integration Flow Create Test", func(t *testing.T) {
   153  		filesMock := mock.FilesMock{}
   154  		path := filepath.Join("tempDir", "iflow4.zip")
   155  		filesMock.AddFile(path, []byte("dummy content"))
   156  		exists, err := filesMock.FileExists(path)
   157  		assert.NoError(t, err)
   158  		assert.True(t, exists)
   159  
   160  		apiServiceKey := `{
   161  			"oauth": {
   162  				"url": "https://demo",
   163  				"clientid": "demouser",
   164  				"clientsecret": "******",
   165  				"tokenurl": "https://demo/oauth/token"
   166  			}
   167  		}`
   168  		config := integrationArtifactUploadOptions{
   169  			APIServiceKey:       apiServiceKey,
   170  			IntegrationFlowName: "flow4",
   171  			IntegrationFlowID:   "flow4",
   172  			PackageID:           "CICD",
   173  			FilePath:            path,
   174  		}
   175  
   176  		httpClient := httpMockCpis{CPIFunction: "", ResponseBody: ``, TestType: "NegativeAndUpdateIntegrationDesigntimeArtifactResBody"}
   177  
   178  		err = runIntegrationArtifactUpload(&config, nil, &filesMock, &httpClient)
   179  		assert.EqualError(t, err, "HTTP POST request to https://demo/api/v1/IntegrationDesigntimeArtifacts failed with error: : 401 Unauthorized")
   180  	})
   181  }