forked from wso2/product-apim-tooling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevFirst_testUtils.go
More file actions
450 lines (350 loc) · 17 KB
/
devFirst_testUtils.go
File metadata and controls
450 lines (350 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
* Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package testutils
import (
"io/ioutil"
"log"
"path/filepath"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/wso2/product-apim-tooling/import-export-cli/integration/apim"
"github.com/wso2/product-apim-tooling/import-export-cli/integration/base"
"gopkg.in/yaml.v2"
)
func AwsInitProject(t *testing.T, args *AWSInitTestArgs) (string, error) {
//Setup Environment and login to it.
base.SetupEnvWithoutTokenFlag(t, args.SrcAPIM.GetEnvName(), args.SrcAPIM.GetApimURL())
base.Login(t, args.SrcAPIM.GetEnvName(), args.CtlUser.Username, args.CtlUser.Password)
output, err := base.Execute(t, "aws", "init", "-n", args.ApiNameFlag, "-s", args.ApiStageNameFlag)
return output, err
}
func ValidateAWSInitProject(t *testing.T, args *AWSInitTestArgs) {
t.Helper()
output, err := AwsInitProject(t, args)
if err != nil {
log.Fatal(err)
}
//Project initialized
assert.Nil(t, err, "Error testing aws init command")
assert.Contains(t, output, "Project initialized", "Error while executing aws init command")
//Remove Created project and logout
t.Cleanup(func() {
base.RemoveDir(args.ApiNameFlag)
})
return
}
func InitProject(t *testing.T, args *InitTestArgs) (string, error) {
//Setup Environment and login to it.
base.SetupEnvWithoutTokenFlag(t, args.SrcAPIM.GetEnvName(), args.SrcAPIM.GetApimURL())
base.Login(t, args.SrcAPIM.GetEnvName(), args.CtlUser.Username, args.CtlUser.Password)
output, err := base.Execute(t, "init", args.InitFlag)
return output, err
}
func InitProjectWithDefinitionFlag(t *testing.T, args *InitTestArgs) (string, error) {
//Setup Environment and login to it.
base.SetupEnvWithoutTokenFlag(t, args.SrcAPIM.GetEnvName(), args.SrcAPIM.GetApimURL())
base.Login(t, args.SrcAPIM.GetEnvName(), args.CtlUser.Username, args.CtlUser.Password)
output, err := base.Execute(t, "init", args.InitFlag, "--definition", args.DefinitionFlag)
return output, err
}
func importApiFromProject(t *testing.T, projectName, apiName, paramsPath string, client *apim.Client, credentials *Credentials,
isCleanup, isPreserveProvider bool) (string, error) {
projectPath, _ := filepath.Abs(projectName)
params := []string{"import", "api", "-f", projectPath, "-e", client.GetEnvName(), "-k",
"--verbose", "--preserve-provider=" + strconv.FormatBool(isPreserveProvider)}
if paramsPath != "" {
params = append(params, "--params", paramsPath)
}
output, err := base.Execute(t, params...)
base.WaitForIndexing()
if isCleanup {
t.Cleanup(func() {
username, password := apim.RetrieveAdminCredentialsInsteadCreator(credentials.Username, credentials.Password)
client.Login(username, password)
err := client.DeleteAPIByName(apiName)
if err != nil {
t.Fatal(err)
}
base.WaitForIndexing()
})
}
return output, err
}
func importApiFromProjectWithUpdate(t *testing.T, projectName string, client *apim.Client, apiName string, credentials *Credentials,
isCleanup, preserveProvider bool) (string, error) {
projectPath, _ := filepath.Abs(projectName)
output, err := base.Execute(t, "import", "api", "-f", projectPath, "-e", client.GetEnvName(),
"--preserve-provider="+strconv.FormatBool(preserveProvider), "-k", "--update", "--verbose")
base.WaitForIndexing()
if isCleanup {
t.Cleanup(func() {
username, password := apim.RetrieveAdminCredentialsInsteadCreator(credentials.Username, credentials.Password)
client.Login(username, password)
err := client.DeleteAPIByName(apiName)
if err != nil {
t.Fatal(err)
}
base.WaitForIndexing()
})
}
return output, err
}
func ValidateInitializeProject(t *testing.T, args *InitTestArgs) {
t.Helper()
output, err := InitProject(t, args)
if err != nil {
log.Fatal(err)
}
assert.Nil(t, err, "Error while generating Project")
assert.Contains(t, output, "Project initialized", "Project initialization Failed")
//Remove Created project and logout
t.Cleanup(func() {
base.RemoveDir(args.InitFlag)
})
}
//Function to initialize a project using API definition
func ValidateInitializeProjectWithOASFlag(t *testing.T, args *InitTestArgs) {
t.Helper()
output, err := InitProjectWithOasFlag(t, args)
if err != nil {
log.Fatal(err)
}
assert.Nil(t, err, "Error while generating Project")
assert.Containsf(t, output, "Project initialized", "Test initialization Failed with --oas flag")
//Remove Created project and logout
t.Cleanup(func() {
base.RemoveDir(args.InitFlag)
})
}
//Function to initialize a project using API definition
func ValidateInitializeProjectWithOASFlagWithoutCleaning(t *testing.T, args *InitTestArgs) {
t.Helper()
output, err := InitProjectWithOasFlag(t, args)
if err != nil {
log.Fatal(err)
}
assert.Nil(t, err, "Error while generating Project")
assert.Containsf(t, output, "Project initialized", "Test initialization Failed with --oas flag")
}
//Function to initialize a project using API definition using --definition flag
func ValidateInitializeProjectWithDefinitionFlag(t *testing.T, args *InitTestArgs) {
t.Helper()
output, err := InitProjectWithDefinitionFlag(t, args)
if err != nil {
log.Fatal(err)
}
assert.Nil(t, err, "Error while generating Project")
assert.Containsf(t, output, "Project initialized", "Test initialization Failed with --oas flag")
//Remove created project
t.Cleanup(func() {
base.RemoveDir(args.InitFlag)
})
}
func ValidateImportProject(t *testing.T, args *InitTestArgs, paramsPath string, preserveProvider bool) *apim.API {
t.Helper()
result, error := importApiFromProject(t, args.InitFlag, args.APIName, paramsPath, args.SrcAPIM, &args.CtlUser,
true, preserveProvider)
assert.Nil(t, error, "Error while importing Project")
assert.Contains(t, result, "Successfully imported API", "Error while importing Project")
// Get App from env 2
importedAPI := GetAPI(t, args.SrcAPIM, args.APIName, args.CtlUser.Username, args.CtlUser.Password)
base.WaitForIndexing()
//Remove Created project and logout
t.Cleanup(func() {
base.RemoveDir(args.InitFlag)
})
return importedAPI
}
func ValidateAWSProjectImport(t *testing.T, args *AWSInitTestArgs, isPreserveProvider bool) {
t.Helper()
result, error := importApiFromProject(t, args.ApiNameFlag, args.ApiNameFlag, "", args.SrcAPIM, &args.CtlUser, true, isPreserveProvider)
assert.Nil(t, error, "Error while importing Project")
assert.Contains(t, result, "Successfully imported API", "Error while importing Project")
base.WaitForIndexing()
//Remove Created project and logout
t.Cleanup(func() {
base.RemoveDir(args.InitFlag)
})
}
func ValidateImportProjectFailed(t *testing.T, args *InitTestArgs, paramsPath string) {
t.Helper()
result, _ := importApiFromProject(t, args.InitFlag, args.APIName, paramsPath, args.SrcAPIM, &args.CtlUser, false, true)
assert.Contains(t, result, "409", "Test failed because API is imported successfully")
base.WaitForIndexing()
//Remove Created project and logout
t.Cleanup(func() {
base.RemoveDir(args.InitFlag)
})
}
func ValidateImportProjectWithInvalidSwaggerFailed(t *testing.T, args *InitTestArgs, paramsPath string, preserveProvider bool) {
t.Helper()
result, _ := importApiFromProject(t, args.InitFlag, args.APIName, paramsPath, args.SrcAPIM, &args.CtlUser, false, preserveProvider)
assert.Contains(t, result, "400", "Test failed because API is imported successfully")
assert.Contains(t, result, "Error while parsing OpenAPI definition", "Test failed because API is imported successfully")
base.WaitForIndexing()
//Remove Created project and logout
t.Cleanup(func() {
base.RemoveDir(args.InitFlag)
})
}
func ValidateImportUpdateProject(t *testing.T, args *InitTestArgs, preserveProvider bool) *apim.API {
t.Helper()
result, error := importApiFromProjectWithUpdate(t, args.InitFlag, args.SrcAPIM, args.APIName, &args.CtlUser, false, preserveProvider)
assert.Nil(t, error, "Error while generating Project")
assert.Contains(t, result, "Successfully imported API", "Test InitializeProjectWithDefinitionFlag Failed")
base.WaitForIndexing()
// Get App from env 2
importedAPI := GetAPI(t, args.SrcAPIM, args.APIName, args.CtlUser.Username, args.CtlUser.Password)
//Remove Created project and logout
t.Cleanup(func() {
base.RemoveDir(args.InitFlag)
})
return importedAPI
}
func ValidateImportUpdateProjectNotAlreadyImported(t *testing.T, args *InitTestArgs) {
t.Helper()
result, error := importApiFromProjectWithUpdate(t, args.InitFlag, args.SrcAPIM, args.APIName, &args.CtlUser, true, true)
assert.Nil(t, error, "Error while generating Project")
assert.Contains(t, result, "Successfully imported API", "Test InitializeProjectWithDefinitionFlag Failed")
//Remove Created project and logout
t.Cleanup(func() {
base.RemoveDir(args.InitFlag)
})
}
func ValidateExportImportedAPI(t *testing.T, args *InitTestArgs, DevFirstDefaultAPIName string, DevFirstDefaultAPIVersion string) string {
expOutput, expError := exportApiImportedFromProject(t, DevFirstDefaultAPIName, DevFirstDefaultAPIVersion, args.SrcAPIM.GetEnvName())
//Check whether api is exported or not
assert.Nil(t, expError, "Error while Exporting API")
assert.Contains(t, expOutput, "Successfully exported API!", "Error while exporting API")
return expOutput
}
func ValidateAPIWithDocIsExported(t *testing.T, args *InitTestArgs, DevFirstDefaultAPIName, DevFirstDefaultAPIVersion, TestCaseDestPathSuffix string) {
expOutput := ValidateExportImportedAPI(t, args, DevFirstDefaultAPIName, DevFirstDefaultAPIVersion)
//Unzip exported API and check whether the imported doc is in there
exportedPath := base.GetExportedPathFromOutput(expOutput)
relativePath := strings.ReplaceAll(exportedPath, ".zip", "")
base.Unzip(relativePath, exportedPath)
docPathOfExportedApi := relativePath + TestDefaultExtractedFileName + TestCaseDestPathSuffix
//Check whether the file is available
isDocExported := base.IsFileAvailable(t, docPathOfExportedApi)
base.Log("Doc is Exported", isDocExported)
assert.Equal(t, true, isDocExported, "Error while exporting API with document")
t.Cleanup(func() {
//Remove Created project and logout
base.RemoveDir(args.InitFlag)
base.RemoveDir(exportedPath)
base.RemoveDir(relativePath)
})
}
func ValidateAPIWithIconIsExported(t *testing.T, args *InitTestArgs, DevFirstDefaultAPIName string, DevFirstDefaultAPIVersion string) {
expOutput := ValidateExportImportedAPI(t, args, DevFirstDefaultAPIName, DevFirstDefaultAPIVersion)
//Unzip exported API and check whether the imported image(.png) is in there
exportedPath := base.GetExportedPathFromOutput(expOutput)
relativePath := strings.ReplaceAll(exportedPath, ".zip", "")
base.Unzip(relativePath, exportedPath)
iconPathOfExportedApi := relativePath + TestDefaultExtractedFileName + DevFirstSampleCaseDestPngPathSuffix
isIconExported := base.IsFileAvailable(t, iconPathOfExportedApi)
base.Log("Icon is Exported", isIconExported)
assert.Equal(t, true, isIconExported, "Error while exporting API with icon")
t.Cleanup(func() {
//Remove Created project and logout
base.RemoveDir(args.InitFlag)
base.RemoveDir(exportedPath)
base.RemoveDir(relativePath)
})
}
func ValidateAPIWithImageIsExported(t *testing.T, args *InitTestArgs, DevFirstDefaultAPIName string, DevFirstDefaultAPIVersion string) {
expOutput := ValidateExportImportedAPI(t, args, DevFirstDefaultAPIName, DevFirstDefaultAPIVersion)
//Unzip exported API and check whether the imported image(.png) is in there
exportedPath := base.GetExportedPathFromOutput(expOutput)
relativePath := strings.ReplaceAll(exportedPath, ".zip", "")
base.Unzip(relativePath, exportedPath)
imagePathOfExportedApi := relativePath + TestDefaultExtractedFileName + DevFirstUpdatedSampleCaseDestJpegPathSuffix
isIconExported := base.IsFileAvailable(t, imagePathOfExportedApi)
base.Log("Image is Exported", isIconExported)
assert.Equal(t, true, isIconExported, "Error while exporting API with icon")
t.Cleanup(func() {
//Remove Created project and logout
base.RemoveDir(args.InitFlag)
base.RemoveDir(exportedPath)
base.RemoveDir(relativePath)
})
}
func ValidateAPIWithUpdatedSequenceIsExported(t *testing.T, args *InitTestArgs, DevFirstDefaultAPIName, DevFirstDefaultAPIVersion string) {
expOutput := ValidateExportImportedAPI(t, args, DevFirstDefaultAPIName, DevFirstDefaultAPIVersion)
// Unzip exported API and check whether the updated sequence file is in there
exportedPath := base.GetExportedPathFromOutput(expOutput)
relativePath := strings.ReplaceAll(exportedPath, ".zip", "")
base.Unzip(relativePath, exportedPath)
// Check whether the exported operation policy is equivalent to the latest operation policy
exportedApiSequencePath := relativePath + TestDefaultExtractedFileName + DevFirstSampleCaseDestExportedPolicy1PathSuffix
lastUpdatedSequencePath, _ := filepath.Abs(DevFirstUpdatedSampleCasePolicy1Path)
isSequenceUpdated := base.IsFileContentIdentical(exportedApiSequencePath, lastUpdatedSequencePath)
base.Log("Exported operation policy is updated", isSequenceUpdated)
assert.Equal(t, true, isSequenceUpdated, "Error while updating the operation policy of API")
// Check whether the exported operation policy definition is equivalent to the latest operation policy definition
exportedApiSequenceDefinitionPath := relativePath + TestDefaultExtractedFileName + DevFirstSampleCaseDestExportedPolicyDefinition1PathSuffix
lastUpdatedSequenceDefinitionPath, _ := filepath.Abs(DevFirstUpdatedSampleCasePolicyDefinition1Path)
isSequenceDefinitionUpdated := base.IsFileContentIdentical(exportedApiSequenceDefinitionPath, lastUpdatedSequenceDefinitionPath)
base.Log("Exported operation policy definition is updated", isSequenceDefinitionUpdated)
assert.Equal(t, true, isSequenceDefinitionUpdated, "Error while updating the operation policy definition of API")
// Check whether the API definition file has accurate operation policy related metadata
exportedApiYamlFilePath := filepath.Join(relativePath, TestDefaultExtractedFileName, DevFirstSampleCaseApiYamlFilePathSuffix)
exportedApiYaml, err := ioutil.ReadFile(exportedApiYamlFilePath)
if err != nil {
t.Error(err)
}
var api *apim.APIFile
err = yaml.Unmarshal(exportedApiYaml, &api)
if err != nil {
t.Error(err)
}
exportedApiOperations := api.Data.Operations
var selectedOperation apim.APIOperations
for _, operation := range exportedApiOperations {
if strings.EqualFold(operation.Target, TestSampleOperationTarget) {
selectedOperation = operation
}
}
assert.NotNil(t, selectedOperation, "Correct operation is not updated")
assert.Equal(t, selectedOperation.Target, TestSampleOperationTarget, "Exported API does not have the expected operation related metadata. "+
"Target of the operation should be "+TestSampleOperationTarget)
assert.Equal(t, selectedOperation.Verb, TestSampleOperationVerb, "Exported API does not have the expected operation related metadata. "+
"Verb of the operation should be "+TestSampleOperationVerb)
assert.Equal(t, selectedOperation.AuthType, TestSampleOperationAuthType, "Exported API does not have the expected operation related metadata. "+
"AuthType of the operation should be "+TestSampleOperationAuthType)
assert.Equal(t, selectedOperation.ThrottlingPolicy, TestSampleOperationThrottlingPolicy, "Exported API does not have the expected operation related metadata. "+
"ThrottlingPolicy of the operation should be "+TestSampleOperationThrottlingPolicy)
requestOperationPolicy := selectedOperation.OperationPolicies.Request.([]interface{})[0].(map[interface{}]interface{})
assert.Equal(t, requestOperationPolicy["policyName"].(string), TestSamplePolicyName, "Exported API does not have the expected operation policy related metadata. ",
"policyName of the operation policy should be "+TestSamplePolicyName)
assert.Equal(t, requestOperationPolicy["parameters"].(map[interface{}]interface{})[TestSampleOperationPolicyPropertyNameField].(string),
TestSampleOperationPolicyPropertyName, "Exported API does not have the expected operation policy related metadata. ",
TestSampleOperationPolicyPropertyNameField+" of the operation policy should be "+TestSampleOperationPolicyPropertyName)
assert.Equal(t, requestOperationPolicy["parameters"].(map[interface{}]interface{})[TestSampleOperationPolicyPropertyValueField].(string),
TestSampleOperationPolicyPropertyValue, "Exported API does not have the expected operation policy related metadata. ",
TestSampleOperationPolicyPropertyValueField+" of the operation policy should be "+TestSampleOperationPolicyPropertyValue)
t.Cleanup(func() {
// Remove created project and logout
base.RemoveDir(args.InitFlag)
base.RemoveDir(exportedPath)
base.RemoveDir(relativePath)
})
}