github.com/SAP/jenkins-library@v1.362.0/cmd/apiProviderDownload_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package cmd 5 6 import ( 7 "testing" 8 9 "github.com/SAP/jenkins-library/pkg/mock" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 type apiProviderDownloadTestUtilsBundle struct { 14 *mock.ExecMockRunner 15 *mock.FilesMock 16 } 17 18 func apiProviderDownloadMockUtilsBundle() *apiProviderDownloadTestUtilsBundle { 19 utilsBundle := apiProviderDownloadTestUtilsBundle{ 20 ExecMockRunner: &mock.ExecMockRunner{}, 21 FilesMock: &mock.FilesMock{}, 22 } 23 return &utilsBundle 24 } 25 26 // Successful API Provider download cases 27 func TestApiProviderDownloadSuccess(t *testing.T) { 28 t.Parallel() 29 t.Run("Successful Download of API Provider", func(t *testing.T) { 30 apiServiceKey := `{ 31 "oauth": { 32 "url": "https://demo", 33 "clientid": "demouser", 34 "clientsecret": "******", 35 "tokenurl": "https://demo/oauth/token" 36 } 37 }` 38 39 config := apiProviderDownloadOptions{ 40 APIServiceKey: apiServiceKey, 41 APIProviderName: "provider1", 42 DownloadPath: "APIProvider.json", 43 } 44 httpClient := httpMockCpis{CPIFunction: "APIProviderDownload", ResponseBody: ``, TestType: "Positive"} 45 utilsMock := apiProviderDownloadMockUtilsBundle() 46 err := runApiProviderDownload(&config, nil, &httpClient, utilsMock) 47 48 if assert.NoError(t, err) { 49 50 t.Run("Assert file download & content", func(t *testing.T) { 51 fileExist := assert.True(t, utilsMock.HasWrittenFile(config.DownloadPath)) 52 if fileExist { 53 providerbyteContent, _ := utilsMock.FileRead(config.DownloadPath) 54 providerContent := string(providerbyteContent) 55 assert.Equal(t, providerContent, "{\n\t\t\t\t\"d\": {\n\t\t\t\t\t\"results\": [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\"__metadata\": {\n\t\t\t\t\t\t\t\t\"id\": \"https://roverpoc.it-accd002.cfapps.sap.hana.ondemand.com:443/api/v1/MessageProcessingLogs('AGAS1GcWkfBv-ZtpS6j7TKjReO7t')\",\n\t\t\t\t\t\t\t\t\"uri\": \"https://roverpoc.it-accd002.cfapps.sap.hana.ondemand.com:443/api/v1/MessageProcessingLogs('AGAS1GcWkfBv-ZtpS6j7TKjReO7t')\",\n\t\t\t\t\t\t\t\t\"type\": \"com.sap.hci.api.MessageProcessingLog\"\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\"MessageGuid\": \"AGAS1GcWkfBv-ZtpS6j7TKjReO7t\",\n\t\t\t\t\t\t\t\"CorrelationId\": \"AGAS1GevYrPodxieoYf4YSY4jd-8\",\n\t\t\t\t\t\t\t\"ApplicationMessageId\": null,\n\t\t\t\t\t\t\t\"ApplicationMessageType\": null,\n\t\t\t\t\t\t\t\"LogStart\": \"/Date(1611846759005)/\",\n\t\t\t\t\t\t\t\"LogEnd\": \"/Date(1611846759032)/\",\n\t\t\t\t\t\t\t\"Sender\": null,\n\t\t\t\t\t\t\t\"Receiver\": null,\n\t\t\t\t\t\t\t\"IntegrationFlowName\": \"flow1\",\n\t\t\t\t\t\t\t\"Status\": \"COMPLETED\",\n\t\t\t\t\t\t\t\"LogLevel\": \"INFO\",\n\t\t\t\t\t\t\t\"CustomStatus\": \"COMPLETED\",\n\t\t\t\t\t\t\t\"TransactionId\": \"aa220151116748eeae69db3e88f2bbc8\"\n\t\t\t\t\t\t}\n\t\t\t\t\t]\n\t\t\t\t}\n\t\t\t}") 56 } 57 }) 58 59 t.Run("Assert API Provider url", func(t *testing.T) { 60 assert.Equal(t, "https://demo/apiportal/api/1.0/Management.svc/APIProviders('provider1')", httpClient.URL) 61 }) 62 63 t.Run("Assert method as GET", func(t *testing.T) { 64 assert.Equal(t, "GET", httpClient.Method) 65 }) 66 } 67 }) 68 } 69 70 // API Provider download failure cases 71 func TestApiProviderDownloadFailure(t *testing.T) { 72 t.Parallel() 73 74 t.Run("Failed case of API Provider Download", func(t *testing.T) { 75 apiServiceKey := `{ 76 "oauth": { 77 "url": "https://demo", 78 "clientid": "demouser", 79 "clientsecret": "******", 80 "tokenurl": "https://demo/oauth/token" 81 } 82 }` 83 config := apiProviderDownloadOptions{ 84 APIServiceKey: apiServiceKey, 85 APIProviderName: "provider1", 86 DownloadPath: "APIProvider.json", 87 } 88 httpClient := httpMockCpis{CPIFunction: "APIProviderDownloadFailure", ResponseBody: ``, TestType: "Negative"} 89 utilsMock := apiProviderDownloadMockUtilsBundle() 90 err := runApiProviderDownload(&config, nil, &httpClient, utilsMock) 91 92 assert.False(t, utilsMock.HasWrittenFile(config.DownloadPath)) 93 94 assert.EqualError(t, err, "HTTP GET request to https://demo/apiportal/api/1.0/Management.svc/APIProviders('provider1') failed with error: Service not Found") 95 }) 96 }