github.com/jaylevin/jenkins-library@v1.230.4/cmd/integrationArtifactDeploy_test.go (about) 1 package cmd 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "io/ioutil" 8 "net/http" 9 "testing" 10 11 "github.com/SAP/jenkins-library/pkg/cpi" 12 piperhttp "github.com/SAP/jenkins-library/pkg/http" 13 "github.com/SAP/jenkins-library/pkg/mock" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 type integrationArtifactDeployMockUtils struct { 18 *mock.ExecMockRunner 19 } 20 21 func newIntegrationArtifactDeployTestsUtils() integrationArtifactDeployMockUtils { 22 utils := integrationArtifactDeployMockUtils{ 23 ExecMockRunner: &mock.ExecMockRunner{}, 24 } 25 return utils 26 } 27 28 func TestRunIntegrationArtifactDeploy(t *testing.T) { 29 t.Parallel() 30 31 t.Run("Successfull Integration Flow Deploy Test", func(t *testing.T) { 32 apiServiceKey := `{ 33 "oauth": { 34 "url": "https://demo", 35 "clientid": "demouser", 36 "clientsecret": "******", 37 "tokenurl": "https://demo/oauth/token" 38 } 39 }` 40 41 config := integrationArtifactDeployOptions{ 42 APIServiceKey: apiServiceKey, 43 IntegrationFlowID: "flow1", 44 } 45 46 httpClient := httpMockCpis{CPIFunction: "", ResponseBody: ``, TestType: "PositiveAndDeployIntegrationDesigntimeArtifactResBody"} 47 48 err := runIntegrationArtifactDeploy(&config, nil, &httpClient) 49 50 if assert.NoError(t, err) { 51 52 t.Run("check url", func(t *testing.T) { 53 assert.Equal(t, "https://demo/api/v1/IntegrationRuntimeArtifacts('flow1')", httpClient.URL) 54 }) 55 56 t.Run("check method", func(t *testing.T) { 57 assert.Equal(t, "GET", httpClient.Method) 58 }) 59 } 60 }) 61 62 t.Run("Trigger Failure for Integration Flow Deployment", func(t *testing.T) { 63 64 apiServiceKey := `{ 65 "oauth": { 66 "url": "https://demo", 67 "clientid": "demouser", 68 "clientsecret": "******", 69 "tokenurl": "https://demo/oauth/token" 70 } 71 }` 72 73 config := integrationArtifactDeployOptions{ 74 APIServiceKey: apiServiceKey, 75 IntegrationFlowID: "flow1", 76 } 77 78 httpClient := httpMockCpis{CPIFunction: "FailIntegrationDesigntimeArtifactDeployment", ResponseBody: ``, TestType: "Negative"} 79 80 err := runIntegrationArtifactDeploy(&config, nil, &httpClient) 81 82 if assert.Error(t, err) { 83 84 t.Run("check url", func(t *testing.T) { 85 assert.Equal(t, "https://demo/api/v1/DeployIntegrationDesigntimeArtifact?Id='flow1'&Version='Active'", httpClient.URL) 86 }) 87 88 t.Run("check method", func(t *testing.T) { 89 assert.Equal(t, "POST", httpClient.Method) 90 }) 91 } 92 }) 93 94 t.Run("Failed Integration Flow Deploy Test", func(t *testing.T) { 95 96 apiServiceKey := `{ 97 "oauth": { 98 "url": "https://demo", 99 "clientid": "demouser", 100 "clientsecret": "******", 101 "tokenurl": "https://demo/oauth/token" 102 } 103 }` 104 105 config := integrationArtifactDeployOptions{ 106 APIServiceKey: apiServiceKey, 107 IntegrationFlowID: "flow1", 108 } 109 110 httpClient := httpMockCpis{CPIFunction: "", ResponseBody: ``, TestType: "NegativeAndDeployIntegrationDesigntimeArtifactResBody"} 111 112 err := runIntegrationArtifactDeploy(&config, nil, &httpClient) 113 114 assert.EqualError(t, err, "{\"message\": \"java.lang.IllegalStateException: No credentials for 'smtp' found\"}") 115 }) 116 117 t.Run("Successfull GetIntegrationArtifactDeployStatus Test", func(t *testing.T) { 118 clientOptions := piperhttp.ClientOptions{} 119 clientOptions.Token = fmt.Sprintf("Bearer %s", "Demo") 120 apiServiceKey := `{ 121 "oauth": { 122 "url": "https://demo", 123 "clientid": "demouser", 124 "clientsecret": "******", 125 "tokenurl": "https://demo/oauth/token" 126 } 127 }` 128 129 config := integrationArtifactDeployOptions{ 130 APIServiceKey: apiServiceKey, 131 IntegrationFlowID: "flow1", 132 } 133 134 httpClient := httpMockCpis{CPIFunction: "GetIntegrationArtifactDeployStatus", Options: clientOptions, ResponseBody: ``, TestType: "PositiveAndDeployIntegrationDesigntimeArtifactResBody"} 135 136 resp, err := getIntegrationArtifactDeployStatus(&config, &httpClient, "https://demo") 137 138 assert.Equal(t, "STARTED", resp) 139 140 assert.NoError(t, err) 141 }) 142 143 t.Run("Successfull GetIntegrationArtifactDeployError Test", func(t *testing.T) { 144 clientOptions := piperhttp.ClientOptions{} 145 clientOptions.Token = fmt.Sprintf("Bearer %s", "Demo") 146 apiServiceKey := `{ 147 "oauth": { 148 "url": "https://demo", 149 "clientid": "demouser", 150 "clientsecret": "******", 151 "tokenurl": "https://demo/oauth/token" 152 } 153 }` 154 155 config := integrationArtifactDeployOptions{ 156 APIServiceKey: apiServiceKey, 157 IntegrationFlowID: "flow1", 158 } 159 160 httpClient := httpMockCpis{CPIFunction: "GetIntegrationArtifactDeployErrorDetails", Options: clientOptions, ResponseBody: ``, TestType: "PositiveAndGetDeployedIntegrationDesigntimeArtifactErrorResBody"} 161 162 resp, err := getIntegrationArtifactDeployError(&config, &httpClient, "https://demo") 163 164 assert.Equal(t, "{\"message\": \"java.lang.IllegalStateException: No credentials for 'smtp' found\"}", resp) 165 166 assert.NoError(t, err) 167 }) 168 } 169 170 type httpMockCpis struct { 171 Method string 172 URL string 173 Header map[string][]string 174 ResponseBody string 175 Options piperhttp.ClientOptions 176 StatusCode int 177 CPIFunction string 178 TestType string 179 } 180 181 func (c *httpMockCpis) SetOptions(options piperhttp.ClientOptions) { 182 c.Options = options 183 } 184 185 func (c *httpMockCpis) SendRequest(method string, url string, r io.Reader, header http.Header, cookies []*http.Cookie) (*http.Response, error) { 186 187 c.Method = method 188 c.URL = url 189 190 if r != nil { 191 _, err := ioutil.ReadAll(r) 192 193 if err != nil { 194 return nil, err 195 } 196 } 197 198 if c.Options.Token == "" { 199 c.ResponseBody = "{\r\n\t\t\t\"access_token\": \"demotoken\",\r\n\t\t\t\"token_type\": \"Bearer\",\r\n\t\t\t\"expires_in\": 3600,\r\n\t\t\t\"scope\": \"\"\r\n\t\t}" 200 c.StatusCode = 200 201 res := http.Response{ 202 StatusCode: c.StatusCode, 203 Header: c.Header, 204 Body: ioutil.NopCloser(bytes.NewReader([]byte(c.ResponseBody))), 205 } 206 return &res, nil 207 } 208 if c.CPIFunction == "" { 209 c.CPIFunction = cpi.GetCPIFunctionNameByURLCheck(url, method, c.TestType) 210 resp, error := cpi.GetCPIFunctionMockResponse(c.CPIFunction, c.TestType) 211 c.CPIFunction = "" 212 return resp, error 213 } 214 215 return cpi.GetCPIFunctionMockResponse(c.CPIFunction, c.TestType) 216 }