github.com/xgoffin/jenkins-library@v1.154.0/cmd/integrationArtifactTriggerIntegrationTest_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 integrationArtifactTriggerIntegrationTestMockUtils struct {
    14  	*mock.ExecMockRunner
    15  	*mock.FilesMock
    16  }
    17  
    18  func newIntegrationArtifactTriggerIntegrationTestTestsUtils() integrationArtifactTriggerIntegrationTestMockUtils {
    19  	utils := integrationArtifactTriggerIntegrationTestMockUtils{
    20  		ExecMockRunner: &mock.ExecMockRunner{},
    21  		FilesMock:      &mock.FilesMock{},
    22  	}
    23  	return utils
    24  }
    25  
    26  func TestRunIntegrationArtifactTriggerIntegrationTest(t *testing.T) {
    27  	t.Run("MessageBodyPath good but no ContentType (ERROR) callIFlowURL", func(t *testing.T) {
    28  		//init
    29  		iFlowServiceKey := `{
    30  			"oauth": {
    31  				"url": "https://demo",
    32  				"clientid": "demouser",
    33  				"clientsecret": "******",
    34  				"tokenurl": "https://demo/oauth/token"
    35  			}
    36  		}`
    37  		config := integrationArtifactTriggerIntegrationTestOptions{
    38  			IntegrationFlowServiceKey: iFlowServiceKey,
    39  			IntegrationFlowID:         "CPI_IFlow_Call_using_Cert",
    40  			MessageBodyPath:           "/file.txt",
    41  			ContentType:               "",
    42  		}
    43  
    44  		utils := newIntegrationArtifactTriggerIntegrationTestTestsUtils()
    45  		utils.AddFile("file.txt", []byte("dummycontent"))
    46  		httpClient := httpMockCpis{CPIFunction: "TriggerIntegrationTest", ResponseBody: ``, TestType: "Positive"}
    47  
    48  		//test
    49  		err := callIFlowURL(&config, nil, utils, &httpClient, "")
    50  
    51  		//assert
    52  		assert.Error(t, err)
    53  	})
    54  
    55  	t.Run("MessageBodyPath and ContentType good but file missing (ERROR) callIFlowURL", func(t *testing.T) {
    56  		//init
    57  		iFlowServiceKey := `{
    58  			"oauth": {
    59  				"url": "https://demo",
    60  				"clientid": "demouser",
    61  				"clientsecret": "******",
    62  				"tokenurl": "https://demo/oauth/token"
    63  			}
    64  		}`
    65  		config := integrationArtifactTriggerIntegrationTestOptions{
    66  			IntegrationFlowServiceKey: iFlowServiceKey,
    67  			IntegrationFlowID:         "CPI_IFlow_Call_using_Cert",
    68  			MessageBodyPath:           "test.txt",
    69  			ContentType:               "txt",
    70  		}
    71  
    72  		utils := newIntegrationArtifactTriggerIntegrationTestTestsUtils()
    73  		//no file created here. error expected
    74  		httpClient := httpMockCpis{CPIFunction: "TriggerIntegrationTest", ResponseBody: ``, TestType: "Positive"}
    75  
    76  		//test
    77  		err := callIFlowURL(&config, nil, utils, &httpClient, "")
    78  
    79  		//assert
    80  		assert.Error(t, err)
    81  	})
    82  
    83  	t.Run("MessageBodyPath, ContentType, and file good (SUCCESS) callIFlowURL", func(t *testing.T) {
    84  		dir, err := ioutil.TempDir("", "")
    85  		defer os.RemoveAll(dir) // clean up
    86  		assert.NoError(t, err, "Error when creating temp dir")
    87  		//init
    88  		iFlowServiceKey := `{
    89  			"oauth": {
    90  				"url": "https://demo",
    91  				"clientid": "demouser",
    92  				"clientsecret": "******",
    93  				"tokenurl": "https://demo/oauth/token"
    94  			}
    95  		}`
    96  		config := integrationArtifactTriggerIntegrationTestOptions{
    97  			IntegrationFlowServiceKey: iFlowServiceKey,
    98  			IntegrationFlowID:         "CPI_IFlow_Call_using_Cert",
    99  			MessageBodyPath:           filepath.Join(dir, "test.txt"),
   100  			ContentType:               "txt",
   101  		}
   102  
   103  		utils := newIntegrationArtifactTriggerIntegrationTestTestsUtils()
   104  		utils.AddFile(config.MessageBodyPath, []byte("dummycontent1")) //have to add a file here to see in utils
   105  		ioutil.WriteFile(config.MessageBodyPath, []byte("dummycontent2"), 0755)
   106  		httpClient := httpMockCpis{CPIFunction: "TriggerIntegrationTest", ResponseBody: ``, TestType: "Positive"}
   107  
   108  		//test
   109  		err = callIFlowURL(&config, nil, utils, &httpClient, "https://my-service.com/endpoint")
   110  
   111  		//assert
   112  		assert.NoError(t, err)
   113  		assert.Equal(t, "POST", httpClient.Method)
   114  		assert.Equal(t, "https://my-service.com/endpoint", httpClient.URL)
   115  	})
   116  
   117  	t.Run("No MessageBodyPath still works (SUCCESS) callIFlowURL", func(t *testing.T) {
   118  		//init
   119  		iFlowServiceKey := `{
   120  			"oauth": {
   121  				"url": "https://demo",
   122  				"clientid": "demouser",
   123  				"clientsecret": "******",
   124  				"tokenurl": "https://demo/oauth/token"
   125  			}
   126  		}`
   127  		config := integrationArtifactTriggerIntegrationTestOptions{
   128  			IntegrationFlowServiceKey: iFlowServiceKey,
   129  			IntegrationFlowID:         "CPI_IFlow_Call_using_Cert",
   130  			MessageBodyPath:           "",
   131  			ContentType:               "txt",
   132  		}
   133  
   134  		utils := newIntegrationArtifactTriggerIntegrationTestTestsUtils()
   135  		//utils.AddFile(config.MessageBodyPath, []byte("dummycontent1")) //have to add a file here to see in utils
   136  		//ioutil.WriteFile(config.MessageBodyPath, []byte("dummycontent2"), 0755)
   137  		httpClient := httpMockCpis{CPIFunction: "TriggerIntegrationTest", ResponseBody: ``, TestType: "Positive"}
   138  
   139  		//test
   140  		err := callIFlowURL(&config, nil, utils, &httpClient, "https://my-service.com/endpoint")
   141  
   142  		//assert
   143  		assert.NoError(t, err)
   144  		assert.Equal(t, "GET", httpClient.Method)
   145  		assert.Equal(t, "https://my-service.com/endpoint", httpClient.URL)
   146  
   147  	})
   148  
   149  	t.Run("nil fileBody (SUCCESS) callIFlowURL", func(t *testing.T) {
   150  		dir, err := ioutil.TempDir("", "")
   151  		defer os.RemoveAll(dir) // clean up
   152  		assert.NoError(t, err, "Error when creating temp dir")
   153  		//init
   154  		iFlowServiceKey := `{
   155  			"oauth": {
   156  				"url": "https://demo",
   157  				"clientid": "demouser",
   158  				"clientsecret": "******",
   159  				"tokenurl": "https://demo/oauth/token"
   160  			}
   161  		}`
   162  		config := integrationArtifactTriggerIntegrationTestOptions{
   163  			IntegrationFlowServiceKey: iFlowServiceKey,
   164  			IntegrationFlowID:         "CPI_IFlow_Call_using_Cert",
   165  			MessageBodyPath:           filepath.Join(dir, "test.txt"),
   166  			ContentType:               "txt",
   167  		}
   168  
   169  		utils := newIntegrationArtifactTriggerIntegrationTestTestsUtils()
   170  		utils.AddFile(config.MessageBodyPath, []byte(nil)) //have to add a file here to see in utils
   171  		ioutil.WriteFile(config.MessageBodyPath, []byte(nil), 0755)
   172  		httpClient := httpMockCpis{CPIFunction: "TriggerIntegrationTest", ResponseBody: ``, TestType: "Positive"}
   173  
   174  		//test
   175  		err = callIFlowURL(&config, nil, utils, &httpClient, "")
   176  
   177  		//assert
   178  		assert.NoError(t, err)
   179  	})
   180  }