From 65529ea6c1abaaa87e7a1d3dddd61c859673ad75 Mon Sep 17 00:00:00 2001 From: rathnapandi Date: Thu, 7 Dec 2023 10:48:58 -0700 Subject: [PATCH] - add junit tests --- .../apim/lib/StandardImportCLIOptions.java | 12 +- .../lib/cli/CLIAPIUpgradeAccessOptions.java | 156 +++++++++--------- .../api/export/lib/APIComparatorTest.java | 65 +++++--- .../export/lib/ClientAppComparatorTest.java | 46 +++++- 4 files changed, 171 insertions(+), 108 deletions(-) diff --git a/modules/apim-adapter/src/main/java/com/axway/apim/lib/StandardImportCLIOptions.java b/modules/apim-adapter/src/main/java/com/axway/apim/lib/StandardImportCLIOptions.java index 5c1969151..3355ae11e 100644 --- a/modules/apim-adapter/src/main/java/com/axway/apim/lib/StandardImportCLIOptions.java +++ b/modules/apim-adapter/src/main/java/com/axway/apim/lib/StandardImportCLIOptions.java @@ -5,13 +5,13 @@ import com.axway.apim.lib.error.AppException; public class StandardImportCLIOptions extends CLIOptions { - + private final CLIOptions cliOptions; public StandardImportCLIOptions(CLIOptions cliOptions) { this.cliOptions = cliOptions; } - + @Override public Parameters getParams() throws AppException { StandardImportParams params = (StandardImportParams)cliOptions.getParams(); @@ -20,15 +20,15 @@ public Parameters getParams() throws AppException { params.setStageConfig(getValue("stageConfig")); return params; } - + @Override public void addOptions() { cliOptions.addOptions(); - + Option option = new Option("enabledCaches", true, "By default, no cache is used for import actions. However, here you can enable caches if necessary to improve performance. Has no effect, when -gnoreCache is set. More information on the impact: https://bit.ly/3FjXRXE"); option.setArgName("applicationsQuotaCache,*API*"); cliOptions.addOption(option); - + option = new Option("stageConfig", true, "Manually provide the name of the stage configuration file to use instead of derived from the given stage."); option.setArgName("my-staged-config.json"); cliOptions.addOption(option); @@ -38,7 +38,7 @@ public void addOptions() { public void addOption(Option option) { cliOptions.addOption(option); } - + @Override public void parse() throws AppException{ diff --git a/modules/apis/src/main/java/com/axway/apim/api/export/lib/cli/CLIAPIUpgradeAccessOptions.java b/modules/apis/src/main/java/com/axway/apim/api/export/lib/cli/CLIAPIUpgradeAccessOptions.java index 80c324355..fdfa96322 100644 --- a/modules/apis/src/main/java/com/axway/apim/api/export/lib/cli/CLIAPIUpgradeAccessOptions.java +++ b/modules/apis/src/main/java/com/axway/apim/api/export/lib/cli/CLIAPIUpgradeAccessOptions.java @@ -10,89 +10,89 @@ import com.axway.apim.lib.error.AppException; public class CLIAPIUpgradeAccessOptions extends CLIOptions { - - private CLIAPIUpgradeAccessOptions(String[] args) { - super(args); - } - - public static CLIOptions create(String[] args) throws AppException { - CLIOptions cliOptions = new CLIAPIUpgradeAccessOptions(args); - cliOptions = new CLIAPIFilterOptions(cliOptions); - cliOptions = new CoreCLIOptions(cliOptions); - cliOptions.addOptions(); - cliOptions.parse(); - return cliOptions; - } - - @Override - public void addOptions() { - Option option = new Option("refAPIId", true, "Filter the reference API based on the ID."); - option.setRequired(false); - option.setArgName("UUID-ID-OF-THE-REF-API"); - addOption(option); - - option = new Option("refAPIName", true, "Filter the reference API based on the name. Wildcards are supported."); - option.setRequired(false); - option.setArgName("*My-Old-API*"); - addOption(option); - - option = new Option("refAPIVersion", true, "Filter the reference API based on the version."); - option.setRequired(false); - option.setArgName("1.0.0"); - addOption(option); - - option = new Option("refAPIOrg", true, "Filter the reference API based on the organization. Wildcards are supported."); - option.setRequired(false); - option.setArgName("*Org A*"); - addOption(option); - - option = new Option("refAPIDeprecate", true, "If set the old/reference API will be flagged as deprecated. Defaults to false."); - option.setRequired(false); - option.setArgName("true"); - addOption(option); - - option = new Option("refAPIRetire", true, "If set the old/reference API will be retired. Default to false."); - option.setRequired(false); - option.setArgName("true"); - addOption(option); - - option = new Option("refAPIRetireDate", true, "Sets the retirement date of the old API. Supported formats: \"dd.MM.yyyy\", \"dd/MM/yyyy\", \"yyyy-MM-dd\", \"dd-MM-yyyy\""); - option.setRequired(false); - option.setArgName("2021/06/30"); - addOption(option); - } - @Override - public void printUsage(String message, String[] args) { - super.printUsage(message, args); - Console.println("----------------------------------------------------------------------------------------"); - Console.println("Upgrade access for one or more APIs based on the given reference API."); - Console.println("App-Subscriptions and Granted orgs are taken over to all selected APIs based on the reference API."); - Console.println("The reference API must be unique. APIs must be published to be considered."); - Console.println(getBinaryName()+" api upgrade-access -s api-env -refAPIId -id "); - Console.println(getBinaryName()+" api upgrade-access -s api-env -n \"*APIs-to-be-upgraded*\" -refAPIName \"*Name of Ref-API*\""); - Console.println(getBinaryName()+" api upgrade-access -s api-env -n \"*APIs-to-be-upgraded*\" -refAPIName \"*Name of Ref-API*\" -refAPIDeprecate true"); - Console.println(); - Console.println(); - Console.println("For more information and advanced examples please visit:"); - Console.println("https://github.com/Axway-API-Management-Plus/apim-cli/wiki"); - } + private CLIAPIUpgradeAccessOptions(String[] args) { + super(args); + } + + public static CLIOptions create(String[] args) throws AppException { + CLIOptions cliOptions = new CLIAPIUpgradeAccessOptions(args); + cliOptions = new CLIAPIFilterOptions(cliOptions); + cliOptions = new CoreCLIOptions(cliOptions); + cliOptions.addOptions(); + cliOptions.parse(); + return cliOptions; + } + + @Override + public void addOptions() { + Option option = new Option("refAPIId", true, "Filter the reference API based on the ID."); + option.setRequired(false); + option.setArgName("UUID-ID-OF-THE-REF-API"); + addOption(option); + + option = new Option("refAPIName", true, "Filter the reference API based on the name. Wildcards are supported."); + option.setRequired(false); + option.setArgName("*My-Old-API*"); + addOption(option); + + option = new Option("refAPIVersion", true, "Filter the reference API based on the version."); + option.setRequired(false); + option.setArgName("1.0.0"); + addOption(option); + + option = new Option("refAPIOrg", true, "Filter the reference API based on the organization. Wildcards are supported."); + option.setRequired(false); + option.setArgName("*Org A*"); + addOption(option); + + option = new Option("refAPIDeprecate", true, "If set the old/reference API will be flagged as deprecated. Defaults to false."); + option.setRequired(false); + option.setArgName("true"); + addOption(option); + + option = new Option("refAPIRetire", true, "If set the old/reference API will be retired. Default to false."); + option.setRequired(false); + option.setArgName("true"); + addOption(option); + + option = new Option("refAPIRetireDate", true, "Sets the retirement date of the old API. Supported formats: \"dd.MM.yyyy\", \"dd/MM/yyyy\", \"yyyy-MM-dd\", \"dd-MM-yyyy\""); + option.setRequired(false); + option.setArgName("2021/06/30"); + addOption(option); + } + + @Override + public void printUsage(String message, String[] args) { + super.printUsage(message, args); + Console.println("----------------------------------------------------------------------------------------"); + Console.println("Upgrade access for one or more APIs based on the given reference API."); + Console.println("App-Subscriptions and Granted orgs are taken over to all selected APIs based on the reference API."); + Console.println("The reference API must be unique. APIs must be published to be considered."); + Console.println(getBinaryName() + " api upgrade-access -s api-env -refAPIId -id "); + Console.println(getBinaryName() + " api upgrade-access -s api-env -n \"*APIs-to-be-upgraded*\" -refAPIName \"*Name of Ref-API*\""); + Console.println(getBinaryName() + " api upgrade-access -s api-env -n \"*APIs-to-be-upgraded*\" -refAPIName \"*Name of Ref-API*\" -refAPIDeprecate true"); + Console.println(); + Console.println(); + Console.println("For more information and advanced examples please visit:"); + Console.println("https://github.com/Axway-API-Management-Plus/apim-cli/wiki"); + } @Override protected String getAppName() { return "Upgrade access"; } - @Override - public Parameters getParams() throws AppException { - APIUpgradeAccessParams params = new APIUpgradeAccessParams(); - params.setReferenceAPIId(getValue("refAPIId")); - params.setReferenceAPIName(getValue("refAPIName")); - params.setReferenceAPIVersion(getValue("refAPIVersion")); - params.setReferenceAPIOrganization(getValue("refAPIOrg")); - params.setReferenceAPIDeprecate(Boolean.parseBoolean(getValue("refAPIDeprecate"))); - params.setReferenceAPIRetire(Boolean.parseBoolean(getValue("refAPIRetire"))); - params.setReferenceAPIRetirementDate(getValue("refAPIRetireDate")); - return params; - } + @Override + public Parameters getParams() throws AppException { + APIUpgradeAccessParams params = new APIUpgradeAccessParams(); + params.setReferenceAPIId(getValue("refAPIId")); + params.setReferenceAPIName(getValue("refAPIName")); + params.setReferenceAPIVersion(getValue("refAPIVersion")); + params.setReferenceAPIOrganization(getValue("refAPIOrg")); + params.setReferenceAPIDeprecate(Boolean.parseBoolean(getValue("refAPIDeprecate"))); + params.setReferenceAPIRetire(Boolean.parseBoolean(getValue("refAPIRetire"))); + params.setReferenceAPIRetirementDate(getValue("refAPIRetireDate")); + return params; + } } diff --git a/modules/apis/src/test/java/com/axway/apim/api/export/lib/APIComparatorTest.java b/modules/apis/src/test/java/com/axway/apim/api/export/lib/APIComparatorTest.java index ae55239e2..9a7e0f90b 100644 --- a/modules/apis/src/test/java/com/axway/apim/api/export/lib/APIComparatorTest.java +++ b/modules/apis/src/test/java/com/axway/apim/api/export/lib/APIComparatorTest.java @@ -1,35 +1,29 @@ package com.axway.apim.api.export.lib; -import static org.testng.Assert.assertEquals; - +import com.axway.apim.api.API; +import org.testng.Assert; import org.testng.annotations.Test; -import com.axway.apim.api.API; +import static org.testng.Assert.assertEquals; public class APIComparatorTest { - @Test - public void testCompareAPIWithoutVersion() { - APIComparator comp = new APIComparator(); - API api1 = new API(); - api1.setName("API 1"); - api1.setVersion("1.0.0"); - - API api2 = new API(); - api2.setName("API 1"); - - // Should not lead to a NPE! - int rc = comp.compare(api1, api2); - assertEquals(rc, 0); - } - - @Test - public void compareEmptyAPIs() { + public void testCompareAPIWithoutVersion() { APIComparator comp = new APIComparator(); - int rc = comp.compare(null, null); + API api1 = new API(); + api1.setName("API 1"); + api1.setVersion("1.0.0"); + + API api2 = new API(); + api2.setName("API 1"); + + // Should not lead to a NPE! + int rc = comp.compare(api1, api2); assertEquals(rc, 0); } + + @Test public void compareAPIsWithEmptyName() { APIComparator comp = new APIComparator(); @@ -64,4 +58,33 @@ public void compareAPIsWithNameAndVersion() { assertEquals(rc, 0); } + + @Test + public void compareApi1Empty() { + APIComparator comp = new APIComparator(); + Assert.assertEquals(comp.compare(null, new API()), 0); + } + + @Test + public void compareApi2Empty() { + APIComparator comp = new APIComparator(); + Assert.assertEquals(comp.compare(new API(), null), 0); + } + + @Test + public void compareApi1EmptyName() { + API api = new API(); + api.setName("abc"); + APIComparator comp = new APIComparator(); + Assert.assertEquals(comp.compare(null, api), 0); + } + + @Test + public void compareApi2EmptyName() { + API api = new API(); + api.setName("abc"); + APIComparator comp = new APIComparator(); + Assert.assertEquals(comp.compare(api, null), 0); + } + } diff --git a/modules/apis/src/test/java/com/axway/apim/api/export/lib/ClientAppComparatorTest.java b/modules/apis/src/test/java/com/axway/apim/api/export/lib/ClientAppComparatorTest.java index d74d27b1a..d885c6c90 100644 --- a/modules/apis/src/test/java/com/axway/apim/api/export/lib/ClientAppComparatorTest.java +++ b/modules/apis/src/test/java/com/axway/apim/api/export/lib/ClientAppComparatorTest.java @@ -10,14 +10,14 @@ public class ClientAppComparatorTest { @Test - public void sortEmptyClientApplications(){ + public void sortEmptyClientApplications() { List clientApplicationList = new ArrayList<>(); clientApplicationList.sort(new ClientAppComparator()); Assert.assertTrue(clientApplicationList.isEmpty()); } @Test - public void sortClientApplicationsWithoutName(){ + public void sortClientApplicationsWithoutName() { List clientApplicationList = new ArrayList<>(); ClientApplication clientApplication = new ClientApplication(); clientApplicationList.add(clientApplication); @@ -29,7 +29,7 @@ public void sortClientApplicationsWithoutName(){ @Test - public void sortClientApplicationsWithName(){ + public void sortClientApplicationsWithName() { List clientApplicationList = new ArrayList<>(); ClientApplication clientApplication = new ClientApplication(); clientApplication.setName("xyz"); @@ -44,4 +44,44 @@ public void sortClientApplicationsWithName(){ } + @Test + public void sortClientApplicationsWithNameOne() { + List clientApplicationList = new ArrayList<>(); + ClientApplication clientApplication = new ClientApplication(); + clientApplication.setName("xyz"); + clientApplicationList.add(clientApplication); + clientApplicationList.sort(new ClientAppComparator()); + Assert.assertEquals(clientApplicationList.get(0).getName(), "xyz"); + + } + + @Test + public void compareApp1Empty() { + ClientAppComparator clientAppComparator = new ClientAppComparator(); + Assert.assertEquals(clientAppComparator.compare(null, new ClientApplication()), 0); + } + + @Test + public void compareApp2Empty() { + ClientAppComparator clientAppComparator = new ClientAppComparator(); + Assert.assertEquals(clientAppComparator.compare(new ClientApplication(), null), 0); + } + + @Test + public void compareApp1EmptyName() { + ClientApplication clientApplication = new ClientApplication(); + clientApplication.setName("abc"); + ClientAppComparator clientAppComparator = new ClientAppComparator(); + Assert.assertEquals(clientAppComparator.compare(null, clientApplication), 0); + } + + @Test + public void compareApp2EmptyName() { + ClientApplication clientApplication = new ClientApplication(); + clientApplication.setName("abc"); + ClientAppComparator clientAppComparator = new ClientAppComparator(); + Assert.assertEquals(clientAppComparator.compare(clientApplication, null), 0); + } + + }