Skip to content

Commit 5ae8019

Browse files
Merge pull request #954 from wasuradananjith/master
Adding tests for API versioning
2 parents 380b0d9 + 3d9b815 commit 5ae8019

File tree

4 files changed

+102
-18
lines changed

4 files changed

+102
-18
lines changed

import-export-cli/integration/api_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,38 @@ func TestExportImportApiBlocked(t *testing.T) {
616616
}
617617
}
618618

619+
// Import an API with the default version. Change the version and import the same API again.
620+
func TestApiVersioning(t *testing.T) {
621+
for _, user := range testCaseUsers {
622+
t.Run(user.Description, func(t *testing.T) {
623+
624+
dev := GetDevClient()
625+
prod := GetProdClient()
626+
627+
api1 := testutils.AddAPI(t, dev, user.ApiCreator.Username, user.ApiCreator.Password)
628+
629+
args := &testutils.ApiImportExportTestArgs{
630+
ApiProvider: testutils.Credentials{Username: user.ApiCreator.Username, Password: user.ApiCreator.Password},
631+
CtlUser: testutils.Credentials{Username: user.CtlUser.Username, Password: user.CtlUser.Password},
632+
Api: api1,
633+
SrcAPIM: dev,
634+
DestAPIM: prod,
635+
}
636+
637+
testutils.ValidateAPIExport(t, args)
638+
importedAPI := testutils.ValidateAPIImportForMultipleVersions(t, args, "")
639+
640+
api2 := testutils.AddCustomAPI(t, dev, user.ApiCreator.Username, user.ApiCreator.Password,
641+
api1.Name, testutils.APIVersion2, api1.Context)
642+
643+
args.Api = api2
644+
645+
testutils.ValidateAPIExport(t, args)
646+
testutils.ValidateAPIImportForMultipleVersions(t, args, importedAPI.ID)
647+
})
648+
}
649+
}
650+
619651
// Export an API with the life cycle status as Deprecated and import to another environment
620652
func TestExportImportApiDeprecated(t *testing.T) {
621653
for _, user := range testCaseUsers {

import-export-cli/integration/apim/apim.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,20 @@ func (instance *Client) GetTokenURL() string {
156156
}
157157

158158
// GenerateSampleAPIData : Generate sample Pizzashack API object
159-
func (instance *Client) GenerateSampleAPIData(provider string) *API {
159+
func (instance *Client) GenerateSampleAPIData(provider, name, version, context string) *API {
160160
api := API{}
161-
api.Name = base.GenerateRandomString() + "API"
161+
if strings.EqualFold(name, "") {
162+
api.Name = base.GenerateRandomString() + "API"
163+
} else {
164+
api.Name = name
165+
}
162166
api.Description = "This is a simple API for Pizza Shack online pizza delivery store."
163-
api.Context = getContext(provider)
164-
api.Version = "1.0.0"
167+
if strings.EqualFold(context, "") {
168+
api.Context = getContext(provider)
169+
} else {
170+
api.Context = context
171+
}
172+
api.Version = version
165173
api.Provider = provider
166174
api.Transport = []string{"http", "https"}
167175
api.Tags = []string{"pizza"}

import-export-cli/integration/testutils/api_testUtils.go

+55-14
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,23 @@ func GetAPIById(t *testing.T, client *apim.Client, username, password, apiId str
4040
return client.GetAPI(apiId)
4141
}
4242

43-
func AddAPI(t *testing.T, client *apim.Client, username string, password string) *apim.API {
43+
func AddAPI(t *testing.T, client *apim.Client, username, password string) *apim.API {
4444
client.Login(username, password)
45-
api := client.GenerateSampleAPIData(username)
46-
doClean := true
47-
id := client.AddAPI(t, api, username, password, doClean)
45+
api := client.GenerateSampleAPIData(username, "", DevFirstDefaultAPIVersion, "")
46+
id := client.AddAPI(t, api, username, password, true)
47+
api = client.GetAPI(id)
48+
return api
49+
}
50+
51+
func AddCustomAPI(t *testing.T, client *apim.Client, username, password, name, version, context string) *apim.API {
52+
client.Login(username, password)
53+
api := client.GenerateSampleAPIData(username, name, version, context)
54+
id := client.AddAPI(t, api, username, password, true)
4855
api = client.GetAPI(id)
4956
return api
5057
}
5158

52-
func UpdateAPI(t *testing.T, client *apim.Client, api *apim.API, username string, password string) *apim.API {
59+
func UpdateAPI(t *testing.T, client *apim.Client, api *apim.API, username, password string) *apim.API {
5360
client.Login(username, password)
5461
id := client.UpdateAPI(t, api, username, password)
5562
api = client.GetAPI(id)
@@ -139,7 +146,7 @@ func GetGatewayEnvironments(apiRevisions *apim.APIRevisionList) []string {
139146

140147
func AddAPIWithoutCleaning(t *testing.T, client *apim.Client, username string, password string) *apim.API {
141148
client.Login(username, password)
142-
api := client.GenerateSampleAPIData(username)
149+
api := client.GenerateSampleAPIData(username, "", DevFirstDefaultAPIVersion, "")
143150
doClean := false
144151
id := client.AddAPI(t, api, username, password, doClean)
145152
api = client.GetAPI(id)
@@ -148,7 +155,7 @@ func AddAPIWithoutCleaning(t *testing.T, client *apim.Client, username string, p
148155

149156
func AddAPIToTwoEnvs(t *testing.T, client1 *apim.Client, client2 *apim.Client, username string, password string) (*apim.API, *apim.API) {
150157
client1.Login(username, password)
151-
api := client1.GenerateSampleAPIData(username)
158+
api := client1.GenerateSampleAPIData(username, "", DevFirstDefaultAPIVersion, "")
152159
doClean := true
153160
id1 := client1.AddAPI(t, api, username, password, doClean)
154161
api1 := client1.GetAPI(id1)
@@ -354,7 +361,7 @@ func ValidateAllApisOfATenantIsExported(t *testing.T, args *ApiImportExportTestA
354361
})
355362
}
356363

357-
func importAPI(t *testing.T, args *ApiImportExportTestArgs) (string, error) {
364+
func importAPI(t *testing.T, args *ApiImportExportTestArgs, doClean bool) (string, error) {
358365
var fileName string
359366
if args.ImportFilePath == "" {
360367
fileName = base.GetAPIArchiveFilePath(t, args.SrcAPIM.GetEnvName(), args.Api.Name, args.Api.Version)
@@ -378,7 +385,7 @@ func importAPI(t *testing.T, args *ApiImportExportTestArgs) (string, error) {
378385

379386
output, err := base.Execute(t, params...)
380387

381-
if !args.Update {
388+
if !args.Update && doClean {
382389
t.Cleanup(func() {
383390
if strings.EqualFold("DEPRECATED", args.Api.LifeCycleStatus) {
384391
args.CtlUser.Username, args.CtlUser.Password =
@@ -470,7 +477,7 @@ func ValidateAPIExportImport(t *testing.T, args *ApiImportExportTestArgs, apiTyp
470477
// Import api to env 2
471478
base.Login(t, args.DestAPIM.GetEnvName(), args.CtlUser.Username, args.CtlUser.Password)
472479

473-
result, err := importAPI(t, args)
480+
result, err := importAPI(t, args, true)
474481
assert.Nil(t, err, "Error while importing the API")
475482
assert.Contains(t, result, "Successfully imported API", "Error while importing the API")
476483

@@ -503,7 +510,7 @@ func ValidateAPIImportExportForAdvertiseOnlyAPI(t *testing.T, args *ApiImportExp
503510
// Import api to env 2
504511
base.Login(t, args.DestAPIM.GetEnvName(), args.CtlUser.Username, args.CtlUser.Password)
505512

506-
result, err := importAPI(t, args)
513+
result, err := importAPI(t, args, true)
507514
assert.Nil(t, err, "Error while importing the API")
508515
assert.Contains(t, result, "Successfully imported API", "Error while importing the API")
509516

@@ -533,7 +540,7 @@ func ValidateAPIRevisionExportImport(t *testing.T, args *ApiImportExportTestArgs
533540
// Import api to env 2
534541
base.Login(t, args.DestAPIM.GetEnvName(), args.CtlUser.Username, args.CtlUser.Password)
535542

536-
importAPI(t, args)
543+
importAPI(t, args, true)
537544

538545
// Give time for newly imported API to get indexed, or else GetAPI by name will fail
539546
base.WaitForIndexing()
@@ -732,7 +739,7 @@ func GetImportedAPI(t *testing.T, args *ApiImportExportTestArgs) *apim.API {
732739
// Import api to env 2
733740
base.Login(t, args.DestAPIM.GetEnvName(), args.CtlUser.Username, args.CtlUser.Password)
734741

735-
_, err := importAPI(t, args)
742+
_, err := importAPI(t, args, true)
736743

737744
if err != nil {
738745
t.Fatal(err)
@@ -800,7 +807,7 @@ func ValidateAPIImport(t *testing.T, args *ApiImportExportTestArgs) {
800807
// Import api to env 2
801808
base.Login(t, args.DestAPIM.GetEnvName(), args.CtlUser.Username, args.CtlUser.Password)
802809

803-
importAPI(t, args)
810+
importAPI(t, args, true)
804811

805812
// Give time for newly imported API to get indexed, or else GetAPI by name will fail
806813
base.WaitForIndexing()
@@ -812,6 +819,40 @@ func ValidateAPIImport(t *testing.T, args *ApiImportExportTestArgs) {
812819
validateAPIsEqualCrossTenant(t, args.Api, importedAPI)
813820
}
814821

822+
func ValidateAPIImportForMultipleVersions(t *testing.T, args *ApiImportExportTestArgs, firstImportedAPIId string) *apim.API {
823+
824+
t.Helper()
825+
826+
isFirstImport := false
827+
if strings.EqualFold(firstImportedAPIId, "") {
828+
isFirstImport = true
829+
}
830+
831+
// Add env2
832+
base.SetupEnv(t, args.DestAPIM.GetEnvName(), args.DestAPIM.GetApimURL(), args.DestAPIM.GetTokenURL())
833+
834+
// Import api to env 2
835+
base.Login(t, args.DestAPIM.GetEnvName(), args.CtlUser.Username, args.CtlUser.Password)
836+
837+
importAPI(t, args, isFirstImport)
838+
839+
// Give time for newly imported API to get indexed, or else getAPI by name will fail
840+
base.WaitForIndexing()
841+
842+
if !isFirstImport {
843+
args.DestAPIM.DeleteAPI(firstImportedAPIId)
844+
base.WaitForIndexing()
845+
}
846+
847+
// Get App from env 2
848+
importedAPI := GetAPI(t, args.DestAPIM, args.Api.Name, args.ApiProvider.Username, args.ApiProvider.Password)
849+
850+
// Validate env 1 and env 2 API is equal
851+
validateAPIsEqualCrossTenant(t, args.Api, importedAPI)
852+
853+
return importedAPI
854+
}
855+
815856
func ValidateAPIImportFailure(t *testing.T, args *ApiImportExportTestArgs) {
816857
t.Helper()
817858

import-export-cli/integration/testutils/testConstants.go

+3
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,6 @@ const DefaultAPIPolicyVersion = "v1"
216216
const TestAPIPolicyOffset = "0"
217217
const TestAPIPolicyLimit = "5"
218218
const CleanUpFunction = "cleanup"
219+
220+
//Constant for API versioning tests
221+
const APIVersion2 = "2.0.0"

0 commit comments

Comments
 (0)