refactoring api_test.go, adding helper
diff --git a/api_test.go b/api_test.go index e30b855..74ecf36 100644 --- a/api_test.go +++ b/api_test.go
@@ -22,206 +22,50 @@ Expect(err).ShouldNot(HaveOccurred()) // api products for i := 0; i < 10; i++ { - var rows []common.Row - result := strconv.FormatInt(int64(i), 10) - row := common.Row{ - "id": { - Value: "api_product_" + result, - }, - "api_resources": { - Value: "{/**, /test}", - }, - "environments": { - Value: "{Env_0, Env_1}", - }, - "tenant_id": { - Value: "tenant_id_xxxx", - }, - "_change_selector": { - Value: "Org_0", - }, - } - rows = append(rows, row) - res := insertAPIproducts(rows, txn) + row := generateTestApiProduct(i) + res := insertAPIproducts([]common.Row{row}, txn) Expect(res).Should(BeTrue()) } // developers for i := 0; i < 10; i++ { - var rows []common.Row - result := strconv.FormatInt(int64(i), 10) - row := common.Row{ - "id": { - Value: "developer_id_" + result, - }, - "status": { - Value: "Active", - }, - "email": { - Value: "test@apigee.com", - }, - "first_name": { - Value: "Apigee", - }, - "last_name": { - Value: "Google", - }, - "tenant_id": { - Value: "tenant_id_xxxx", - }, - "_change_selector": { - Value: "Org_0", - }, - } - - rows = append(rows, row) - res := insertDevelopers(rows, txn) + row := generateTestDeveloper(i) + res := insertDevelopers([]common.Row{row}, txn) Expect(res).Should(BeTrue()) } // companies for i := 0; i < 10; i++ { - var rows []common.Row - result := strconv.FormatInt(int64(i), 10) - row := common.Row{ - "id": { - Value: "company_id_" + result, - }, - "status": { - Value: "Active", - }, - "name": { - Value: "Apigee Corporation", - }, - "display_name": { - Value: "Apigee", - }, - "tenant_id": { - Value: "tenant_id_xxxx", - }, - "_change_selector": { - Value: "Org_0", - }, - } - - rows = append(rows, row) - res := insertCompanies(rows, txn) + row := generateTestCompany(i) + res := insertCompanies([]common.Row{row}, txn) Expect(res).Should(BeTrue()) } // company developers for i := 0; i < 10; i++ { - var rows []common.Row - result := strconv.FormatInt(int64(i), 10) - row := common.Row{ - "developer_id": { - Value: "developer_id_" + result, - }, - "tenant_id": { - Value: "tenant_id_0", - }, - "_change_selector": { - Value: "test_org0", - }, - "company_id": { - Value: "company_id_" + result, - }, - } - - rows = append(rows, row) - res := insertCompanyDevelopers(rows, txn) + row := generateTestCompanyDeveloper(i); + res := insertCompanyDevelopers([]common.Row{row}, txn) Expect(res).Should(BeTrue()) } // application var j, k int for i := 0; i < 10; i++ { - resulti := strconv.FormatInt(int64(i), 10) for j = k; j < 10+k; j++ { - var rows []common.Row - resultj := strconv.FormatInt(int64(j), 10) - row := common.Row{ - "id": { - Value: "application_id_" + resultj, - }, - "developer_id": { - Value: "developer_id_" + resulti, - }, - "status": { - Value: "Approved", - }, - "tenant_id": { - Value: "tenant_id_xxxx", - }, - "callback_url": { - Value: "http://apigee.com", - }, - "_change_selector": { - Value: "Org_0", - }, - } - - rows = append(rows, row) - res := insertApplications(rows, txn) + row := generateTestApp(j, i); + res := insertApplications([]common.Row{row}, txn) Expect(res).Should(BeTrue()) } k = j } // app credentials for i := 0; i < 10; i++ { - var rows []common.Row - result := strconv.FormatInt(int64(i), 10) - row := common.Row{ - "id": { - Value: "app_credential_" + result, - }, - "app_id": { - Value: "application_id_" + result, - }, - "status": { - Value: "Approved", - }, - "tenant_id": { - Value: "tenant_id_xxxx", - }, - "callback_url": { - Value: "http://apigee.com", - }, - "_change_selector": { - Value: "Org_0", - }, - } - - rows = append(rows, row) - res := insertCredentials(rows, txn) + row := generateTestAppCreds(i) + res := insertCredentials([]common.Row{row}, txn) Expect(res).Should(BeTrue()) } // api product mapper for i := 0; i < 10; i++ { - var rows []common.Row - result := strconv.FormatInt(int64(i), 10) - var s = "hello"; - row := common.Row{ - "apiprdt_id": { - Value: "api_product_" + result, - }, - "status": { - Value: "Approved", - }, - "app_id": { - Value: "application_id_" + result, - }, - "appcred_id": { - Value: "app_credential_" + result, - }, - "tenant_id": { - Value: "tenant_id_xxxx", - }, - "_change_selector": { - Value: "Org_0", - }, - } - - rows = append(rows, row) - res := insertAPIProductMappers(rows, txn) + row := generateTestApiProductMapper(i) + res := insertAPIProductMappers([]common.Row{row}, txn) Expect(res).Should(BeTrue()) } @@ -274,114 +118,35 @@ Expect(err).ShouldNot(HaveOccurred()) for i := 0; i < 10; i++ { - result := strconv.FormatInt(int64(i), 10) - row := common.Row{ - "apiprdt_id": { - Value: "api_product_" + result, - }, - "status": { - Value: "Approved", - }, - "app_id": { - Value: "application_id_" + result, - }, - "appcred_id": { - Value: "app_credential_" + result, - }, - "tenant_id": { - Value: "tenant_id_xxxx", - }, - "_change_selector": { - Value: "Org_0", - }, - } - + row := generateTestApiProductMapper(i); res := deleteAPIproductMapper(row, txn) Expect(res).Should(BeTrue()) } for i := 0; i < 10; i++ { - srvItems := common.Row{} - result := strconv.FormatInt(int64(i), 10) - - scv := &common.ColumnVal{ - Value: "app_credential_" + result, - Type: 3, - } - srvItems["id"] = scv - - scv = &common.ColumnVal{ - Value: "Org_0", - Type: 3, - } - srvItems["_change_selector"] = scv - - res := deleteObject("APP_CREDENTIAL", srvItems, txn) + row := generateTestAppCreds(i) + res := deleteObject("APP_CREDENTIAL", row, txn) Expect(res).Should(BeTrue()) } for i := 0; i < 100; i++ { - - srvItems := common.Row{} - resultj := strconv.FormatInt(int64(i), 10) - - scv := &common.ColumnVal{ - Value: "application_id_" + resultj, - Type: 1, - } - srvItems["id"] = scv - - scv = &common.ColumnVal{ - Value: "Org_0", - Type: 1, - } - srvItems["_change_selector"] = scv - - res := deleteObject("APP", srvItems, txn) + row := generateTestApp(i, 999) //TODO we use j in above insertions + res := deleteObject("APP", row, txn) Expect(res).Should(BeTrue()) } for i := 0; i < 10; i++ { - srvItems := common.Row{} - result := strconv.FormatInt(int64(i), 10) - - scv := &common.ColumnVal{ - Value: "developer_id_" + result, - Type: 1, - } - srvItems["id"] = scv - - scv = &common.ColumnVal{ - Value: "Org_0", - Type: 1, - } - srvItems["_change_selector"] = scv - - res := deleteObject("DEVELOPER", srvItems, txn) + row := generateTestDeveloper(i) + res := deleteObject("DEVELOPER", row, txn) Expect(res).Should(BeTrue()) } for i := 0; i < 10; i++ { - srvItems := common.Row{} - result := strconv.FormatInt(int64(i), 10) - - scv := &common.ColumnVal{ - Value: "api_product_" + result, - Type: 1, - } - srvItems["id"] = scv - - scv = &common.ColumnVal{ - Value: "Org_0", - Type: 1, - } - srvItems["_change_selector"] = scv - - res := deleteObject("API_PRODUCT", srvItems, txn) + row := generateTestApiProduct(i) + res := deleteObject("API_PRODUCT", row, txn) Expect(res).Should(BeTrue()) } txn.Commit() - }) It("Negative cases for DB Deletes on KMS tables", func() { @@ -389,101 +154,57 @@ txn, err := db.Begin() Expect(err).ShouldNot(HaveOccurred()) - srvItems := common.Row{} - result := "DEADBEEF" + row := generateTestApiProductMapper(999) - scv := &common.ColumnVal{ - Value: "api_product_" + result, - Type: 3, - } - srvItems["apiprdt_id"] = scv - - scv = &common.ColumnVal{ - Value: "application_id_" + result, - Type: 3, - } - srvItems["app_id"] = scv - - scv = &common.ColumnVal{ - Value: "app_credential_" + result, - Type: 3, - } - srvItems["appcred_id"] = scv - - scv = &common.ColumnVal{ - Value: "Org_0", - Type: 3, - } - srvItems["_change_selector"] = scv - - res := deleteAPIproductMapper(srvItems, txn) + res := deleteAPIproductMapper(row, txn) Expect(res).Should(BeFalse()) - res = deleteObject("API_PRODUCT", srvItems, txn) + res = deleteObject("API_PRODUCT", row, txn) Expect(res).Should(BeFalse()) - res = deleteObject("APP_CREDENTIAL", srvItems, txn) + res = deleteObject("APP_CREDENTIAL", row, txn) Expect(res).Should(BeFalse()) - res = deleteObject("DEVELOPER", srvItems, txn) + res = deleteObject("DEVELOPER", row, txn) Expect(res).Should(BeFalse()) - res = deleteObject("APP", srvItems, txn) + res = deleteObject("APP", row, txn) + Expect(res).Should(BeFalse()) + + res = deleteObject("COMPANY", row, txn) + Expect(res).Should(BeFalse()) + + res = deleteObject("COMPANY_DEVELOPER", row, txn) Expect(res).Should(BeFalse()) txn.Rollback() }) + It("Negative cases for DB Inserts/updates on KMS tables", func() { db := getDB() txn, err := db.Begin() Expect(err).ShouldNot(HaveOccurred()) - var rows []common.Row - srvItems := common.Row{} - result := "NOPRODID_BADCASE" - scv := &common.ColumnVal{ - Value: "foobar_" + result, - Type: 1, - } - srvItems[result] = scv - - scv = &common.ColumnVal{ - Value: "{/**, /test}", - Type: 1, - } - srvItems["api_resources"] = scv - - scv = &common.ColumnVal{ - Value: "{Env_1, Env_0}", - Type: 1, - } - srvItems["environments"] = scv - - scv = &common.ColumnVal{ - Value: "Org_0", - Type: 1, - } - srvItems["_change_selector"] = scv - - scv = &common.ColumnVal{ - Value: "tenant_id_xxxx", - Type: 1, - } - srvItems["tenant_id"] = scv - - rows = append(rows, srvItems) - res := insertAPIproducts(rows, txn) + row := generateTestApiProduct(999) + row["id"] = nil + res := insertAPIproducts([]common.Row{row}, txn) Expect(res).Should(BeFalse()) - res = insertApplications(rows, txn) + res = insertApplications([]common.Row{row}, txn) Expect(res).Should(BeFalse()) - res = insertCredentials(rows, txn) + res = insertCredentials([]common.Row{row}, txn) Expect(res).Should(BeFalse()) - res = insertAPIProductMappers(rows, txn) + res = insertAPIProductMappers([]common.Row{row}, txn) + Expect(res).Should(BeFalse()) + + res = insertCompanies([]common.Row{row}, txn) + Expect(res).Should(BeFalse()) + + res = insertCompanyDevelopers([]common.Row{row}, txn) Expect(res).Should(BeFalse()) })
diff --git a/test_helper.go b/test_helper.go new file mode 100644 index 0000000..61f8f00 --- /dev/null +++ b/test_helper.go
@@ -0,0 +1,167 @@ +package apidVerifyApiKey + +import ( + "github.com/apigee-labs/transicator/common" + "strconv" +) + +func convertSuffix(i int) string{ + return strconv.FormatInt(int64(i), 10) +} + +func generateTestApiProduct(suffix int) common.Row{ + return common.Row{ + "id": { + Value: "api_product_" + convertSuffix(suffix), + }, + "api_resources": { + Value: "{/**, /test}", + }, + "environments": { + Value: "{Env_0, Env_1}", + }, + "tenant_id": { + Value: "tenant_id_xxxx", + }, + "_change_selector": { + Value: "Org_0", + }, + } +} + +func generateTestDeveloper(suffix int) common.Row { + return common.Row{ + "id": { + Value: "developer_id_" + convertSuffix(suffix), + }, + "status": { + Value: "Active", + }, + "email": { + Value: "test@apigee.com", + }, + "first_name": { + Value: "Apigee", + }, + "last_name": { + Value: "Google", + }, + "tenant_id": { + Value: "tenant_id_xxxx", + }, + "_change_selector": { + Value: "Org_0", + }, + } +} + +func generateTestCompany(suffix int) common.Row { + return common.Row{ + "id": { + Value: "company_id_" + convertSuffix(suffix), + }, + "status": { + Value: "Active", + }, + "name": { + Value: "Apigee Corporation", + }, + "display_name": { + Value: "Apigee", + }, + "tenant_id": { + Value: "tenant_id_xxxx", + }, + "_change_selector": { + Value: "Org_0", + }, + } +} + +func generateTestCompanyDeveloper(suffix int) common.Row { + return common.Row{ + "developer_id": { + Value: "developer_id_" + convertSuffix(suffix), + }, + "tenant_id": { + Value: "tenant_id_0", + }, + "_change_selector": { + Value: "test_org0", + }, + "company_id": { + Value: "company_id_" + convertSuffix(suffix), + }, + } +} + +func generateTestApp(suffix1, suffix2 int) common.Row { + return common.Row{ + "id": { + Value: "application_id_" + convertSuffix(suffix1), + }, + "developer_id": { + Value: "developer_id_" + convertSuffix(suffix2), + }, + "status": { + Value: "Approved", + }, + "tenant_id": { + Value: "tenant_id_xxxx", + }, + "callback_url": { + Value: "http://apigee.com", + }, + "_change_selector": { + Value: "Org_0", + }, + } +} + +func generateTestAppCreds(suffix int) common.Row { + return common.Row{ + "id": { + Value: "app_credential_" + convertSuffix(suffix), + }, + "app_id": { + Value: "application_id_" + convertSuffix(suffix), + }, + "status": { + Value: "Approved", + }, + "tenant_id": { + Value: "tenant_id_xxxx", + }, + "callback_url": { + Value: "http://apigee.com", + }, + "_change_selector": { + Value: "Org_0", + }, + } +} + +func generateTestApiProductMapper(suffix int) common.Row { + return common.Row{ + "apiprdt_id": { + Value: "api_product_" + convertSuffix(suffix), + }, + "status": { + Value: "Approved", + }, + "app_id": { + Value: "application_id_" + convertSuffix(suffix), + }, + "appcred_id": { + Value: "app_credential_" + convertSuffix(suffix), + }, + "tenant_id": { + Value: "tenant_id_xxxx", + }, + "_change_selector": { + Value: "Org_0", + }, + } +} + +