1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/cmd/apiProviderDownload_test.go
maheshsrikrishnan 64a00c540a
New piper step APIProviderDownload contribution (#3349)
* added store file function in cpi common utils

Change-Id: Ia429a2792266e082d139025a71799d21c30a7df9

* Added api provider steps

Change-Id: Icd2829a91db4c53d0de2330822d2b33933973868

* Update apiProviderDownload.yaml

* fixed yaml JLINT issue

Change-Id: Iac974abc30fa00e68c0177072b93716b0af5e0c5

* Removed trailing spaces

Change-Id: I927e9314fce6e9cab68d6b97577c7c96bb2bddad

* Resolved common steps groovy script conflict

Change-Id: I3ad144b618e1c77953aaeccaa5bf7309aff77ca9

* Change for conflict resolution

Change-Id: Ic955833eca844f090b7983f99f9d3649ebb981c7

* Fixed method name and its corresponding implementation

Change-Id: I465c1f1d5306bb978386de9efca3c521e385b89c

* Moved re-usable function to commonUtils package

Change-Id: Ide06462b01caeb2bf438ad7661e01c15bf8e8e24

* Changed the implementation to use existing writeFile method

* Fixed review comments on documentation and test structuring

Change-Id: Ifebd2f4b50754b2097b2d564fb3cc37c433ef6c9

* Fixed documentation alignment issues

* Fixed spaces issue

Change-Id: I834bd94e01bce72e7f81ab49ba32671c91c66ca9

* Documentation removed extra spaces

Change-Id: I9a639d76ed9b81c870f18349504044bb70753b52

* Fixed doc build issue

Change-Id: I96c3e15e73834b64f8b8e3432ce59f6b037f93fd

* Fixed documentation build issues

Change-Id: I7fca2ba69bc7b7298ee300ccd1ae16a6238dc96b

* Re-generated code for fixing build failure

Change-Id: I22b7ee6162f643d9f3b60f6a33eb7858927182a0

* Adopted file utils & mock

Change-Id: Ic46462003527f41df64395a5a615c19bf374e8ef

* Removed ioutil call in the test & adopted error variable names

* Removed commented lines

Change-Id: I99a12e39bc04323e9c19f1409d97eeca267e6fdb

* Added test for asserting file download and adopted error variables

Change-Id: I49463a3b75987bf68f5261d45602d2d7bd960a05

* Added download path assertion positive & negative case

Change-Id: Ieee461c3973b9dfa8f395dc936e4241ff9694c7b

* Modified tests with DownloadPath variable

Change-Id: Iaf14c9ea1a8242b6c8d8e9e4fac8c23d9c1b3a74

* Added testcase to validate file content

Change-Id: I21aed481b433450c3b536dbb29d45291f61848d8

* Refactored test for file content check to avoid failures

Change-Id: I3b4fe9a0de678f437fd4cc0a8203ae9434d9fa8e

* Removed auto-generated comments

Change-Id: I86c4ac3e7e4476a75d6cbed58826ec1f3278d7d2

* Fixed documentation review comments

Change-Id: I4faf31473b53fc53a5517d418c343bf7320eec55

* Fixed documentation indentation

Change-Id: I386f046cf4e10ee6deb5a81fcfc8c430c97086c8

* Fix build issue

Change-Id: I61a829cabaf03ffd5e77cddc594486a650118fa3
2022-03-09 13:07:23 +01:00

94 lines
3.9 KiB
Go

package cmd
import (
"testing"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/stretchr/testify/assert"
)
type apiProviderDownloadTestUtilsBundle struct {
*mock.ExecMockRunner
*mock.FilesMock
}
func apiProviderDownloadMockUtilsBundle() *apiProviderDownloadTestUtilsBundle {
utilsBundle := apiProviderDownloadTestUtilsBundle{
ExecMockRunner: &mock.ExecMockRunner{},
FilesMock: &mock.FilesMock{},
}
return &utilsBundle
}
//Successful API Provider download cases
func TestApiProviderDownloadSuccess(t *testing.T) {
t.Parallel()
t.Run("Successful Download of API Provider", func(t *testing.T) {
apiServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := apiProviderDownloadOptions{
APIServiceKey: apiServiceKey,
APIProviderName: "provider1",
DownloadPath: "APIProvider.json",
}
httpClient := httpMockCpis{CPIFunction: "APIProviderDownload", ResponseBody: ``, TestType: "Positive"}
utilsMock := apiProviderDownloadMockUtilsBundle()
err := runApiProviderDownload(&config, nil, &httpClient, utilsMock)
if assert.NoError(t, err) {
t.Run("Assert file download & content", func(t *testing.T) {
fileExist := assert.True(t, utilsMock.HasWrittenFile(config.DownloadPath))
if fileExist {
providerbyteContent, _ := utilsMock.FileRead(config.DownloadPath)
providerContent := string(providerbyteContent)
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}")
}
})
t.Run("Assert API Provider url", func(t *testing.T) {
assert.Equal(t, "https://demo/apiportal/api/1.0/Management.svc/APIProviders('provider1')", httpClient.URL)
})
t.Run("Assert method as GET", func(t *testing.T) {
assert.Equal(t, "GET", httpClient.Method)
})
}
})
}
//API Provider download failure cases
func TestApiProviderDownloadFailure(t *testing.T) {
t.Parallel()
t.Run("Failed case of API Provider Download", func(t *testing.T) {
apiServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := apiProviderDownloadOptions{
APIServiceKey: apiServiceKey,
APIProviderName: "provider1",
DownloadPath: "APIProvider.json",
}
httpClient := httpMockCpis{CPIFunction: "APIProviderDownloadFailure", ResponseBody: ``, TestType: "Negative"}
utilsMock := apiProviderDownloadMockUtilsBundle()
err := runApiProviderDownload(&config, nil, &httpClient, utilsMock)
assert.False(t, utilsMock.HasWrittenFile(config.DownloadPath))
assert.EqualError(t, err, "HTTP GET request to https://demo/apiportal/api/1.0/Management.svc/APIProviders('provider1') failed with error: Service not Found")
})
}