From bc593431a72eca91cc1ef7933cecf5bc75c08d95 Mon Sep 17 00:00:00 2001 From: rathnapandi Date: Thu, 31 Oct 2024 11:23:11 -0700 Subject: [PATCH] - add junit test and refactor import cli common code --- .../apim/lib/StandardImportCLIOptions.java | 61 +------------------ .../lib/StandardImportCLIOptionsTest.java | 21 +++++++ .../lib/cli/CLIAPIImportOptions.java | 7 +-- .../appimport/lib/AppImportCLIOptions.java | 4 +- .../organization/lib/OrgImportCLIOptions.java | 12 ++-- .../lib/APIManagerSetupImportCLIOptions.java | 12 ++-- .../users/lib/cli/UserImportCLIOptions.java | 5 +- 7 files changed, 48 insertions(+), 74 deletions(-) create mode 100644 modules/apim-adapter/src/test/java/com/axway/lib/StandardImportCLIOptionsTest.java 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 3355ae11e..9ff62d201 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 @@ -2,30 +2,10 @@ import org.apache.commons.cli.Option; -import com.axway.apim.lib.error.AppException; +public class StandardImportCLIOptions { -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(); - - params.setEnabledCaches(getValue("enabledCaches")); - 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"); + public void addOptions(CLIOptions cliOptions) { + 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 -ignoreCache is set. More information on the impact: https://bit.ly/3FjXRXE"); option.setArgName("applicationsQuotaCache,*API*"); cliOptions.addOption(option); @@ -34,39 +14,4 @@ public void addOptions() { cliOptions.addOption(option); } - @Override - public void addOption(Option option) { - cliOptions.addOption(option); - } - - - @Override - public void parse() throws AppException{ - cliOptions.parse(); - } - - @Override - public String getValue(String key) { - return cliOptions.getValue(key); - } - - @Override - public void printUsage(String message, String[] args) { - cliOptions.printUsage(message, args); - } - - @Override - public void showReturnCodes() { - cliOptions.showReturnCodes(); - } - - @Override - public boolean hasOption(String key) { - return cliOptions.hasOption(key); - } - - @Override - public EnvironmentProperties getEnvProperties() { - return cliOptions.getEnvProperties(); - } } diff --git a/modules/apim-adapter/src/test/java/com/axway/lib/StandardImportCLIOptionsTest.java b/modules/apim-adapter/src/test/java/com/axway/lib/StandardImportCLIOptionsTest.java new file mode 100644 index 000000000..7bea19db2 --- /dev/null +++ b/modules/apim-adapter/src/test/java/com/axway/lib/StandardImportCLIOptionsTest.java @@ -0,0 +1,21 @@ +package com.axway.lib; + +import com.axway.apim.lib.CLIOptions; +import com.axway.apim.lib.StandardImportCLIOptions; +import com.axway.lib.utils.SampleCLIOptions; +import org.testng.Assert; +import org.testng.annotations.Test; + +public class StandardImportCLIOptionsTest { + + @Test + public void testStandardImportCLIOptions() throws Exception { + String[] args = {"-enabledCaches", "applicationsQuotaCache", "-stageConfig", "my-staged-config.json"}; + CLIOptions options = SampleCLIOptions.create(args); + StandardImportCLIOptions standardImportCLIOptions = new StandardImportCLIOptions(); + standardImportCLIOptions.addOptions(options); + options.parse(); + Assert.assertTrue(options.hasOption("enabledCaches")); + Assert.assertTrue(options.hasOption("stageConfig")); + } +} diff --git a/modules/apis/src/main/java/com/axway/apim/apiimport/lib/cli/CLIAPIImportOptions.java b/modules/apis/src/main/java/com/axway/apim/apiimport/lib/cli/CLIAPIImportOptions.java index 9b5472317..e8ffdbbbc 100644 --- a/modules/apis/src/main/java/com/axway/apim/apiimport/lib/cli/CLIAPIImportOptions.java +++ b/modules/apis/src/main/java/com/axway/apim/apiimport/lib/cli/CLIAPIImportOptions.java @@ -22,7 +22,6 @@ private CLIAPIImportOptions(String[] args) { public static CLIOptions create(String[] args) throws AppException { CLIOptions cliOptions = new CLIAPIImportOptions(args); - cliOptions = new StandardImportCLIOptions(cliOptions); cliOptions = new CoreCLIOptions(cliOptions); cliOptions.addOptions(); cliOptions.parse(); @@ -98,8 +97,7 @@ public void addOptions() { addOption(option); new OptionsCommon().addDeprecateAndRetired(this); - - + new StandardImportCLIOptions().addOptions(this); } @Override @@ -128,8 +126,9 @@ protected String getAppName() { @Override public Parameters getParams() { APIImportParams params = new APIImportParams(); + params.setEnabledCaches(getValue("enabledCaches")); + params.setStageConfig(getValue("stageConfig")); params.setConfig(getValue("config")); - params.setStageConfig(getValue("stagedConfig")); params.setApiDefinition(getValue("apidefinition")); params.setForceUpdate(hasOption("forceUpdate")); params.setChangeOrganization(hasOption("changeOrganization")); diff --git a/modules/apps/src/main/java/com/axway/apim/appimport/lib/AppImportCLIOptions.java b/modules/apps/src/main/java/com/axway/apim/appimport/lib/AppImportCLIOptions.java index 6e745c4cc..1c9cecd1a 100644 --- a/modules/apps/src/main/java/com/axway/apim/appimport/lib/AppImportCLIOptions.java +++ b/modules/apps/src/main/java/com/axway/apim/appimport/lib/AppImportCLIOptions.java @@ -16,7 +16,6 @@ private AppImportCLIOptions(String[] args) { public static CLIOptions create(String[] args) throws AppException { CLIOptions cliOptions = new AppImportCLIOptions(args); - cliOptions = new StandardImportCLIOptions(cliOptions); cliOptions = new CoreCLIOptions(cliOptions); cliOptions.addOptions(); cliOptions.parse(); @@ -30,6 +29,7 @@ public void addOptions() { option.setRequired(true); option.setArgName("app_config.json"); addOption(option); + new StandardImportCLIOptions().addOptions(this); } @Override @@ -55,6 +55,8 @@ protected String getAppName() { @Override public Parameters getParams() { AppImportParams params = new AppImportParams(); + params.setEnabledCaches(getValue("enabledCaches")); + params.setStageConfig(getValue("stageConfig")); params.setConfig(getValue("c")); return params; } diff --git a/modules/organizations/src/main/java/com/axway/apim/organization/lib/OrgImportCLIOptions.java b/modules/organizations/src/main/java/com/axway/apim/organization/lib/OrgImportCLIOptions.java index d7f58bd3c..8ccd9137f 100644 --- a/modules/organizations/src/main/java/com/axway/apim/organization/lib/OrgImportCLIOptions.java +++ b/modules/organizations/src/main/java/com/axway/apim/organization/lib/OrgImportCLIOptions.java @@ -13,10 +13,9 @@ public class OrgImportCLIOptions extends CLIOptions { private OrgImportCLIOptions(String[] args) { super(args); } - + public static CLIOptions create(String[] args) throws AppException { CLIOptions cliOptions = new OrgImportCLIOptions(args); - cliOptions = new StandardImportCLIOptions(cliOptions); cliOptions = new CoreCLIOptions(cliOptions); cliOptions.addOptions(); cliOptions.parse(); @@ -30,11 +29,12 @@ public void addOptions() { option.setRequired(true); option.setArgName("org_config.json"); addOption(option); - } + new StandardImportCLIOptions().addOptions(this); + } @Override public void printUsage(String message, String[] args) { - super.printUsage(message, args); + super.printUsage(message, args); Console.println("----------------------------------------------------------------------------------------"); Console.println("How to imports organizations using the JSON-Config format"); Console.println("Import an organization using enviornment properties: env.api-env.properties:"); @@ -50,10 +50,12 @@ public void printUsage(String message, String[] args) { protected String getAppName() { return "Organization-Import"; } - + @Override public Parameters getParams() { OrgImportParams params = new OrgImportParams(); + params.setEnabledCaches(getValue("enabledCaches")); + params.setStageConfig(getValue("stageConfig")); params.setConfig(getValue("config")); return params; } diff --git a/modules/settings/src/main/java/com/axway/apim/setup/lib/APIManagerSetupImportCLIOptions.java b/modules/settings/src/main/java/com/axway/apim/setup/lib/APIManagerSetupImportCLIOptions.java index bd16a0252..0507ecc67 100644 --- a/modules/settings/src/main/java/com/axway/apim/setup/lib/APIManagerSetupImportCLIOptions.java +++ b/modules/settings/src/main/java/com/axway/apim/setup/lib/APIManagerSetupImportCLIOptions.java @@ -13,10 +13,9 @@ public class APIManagerSetupImportCLIOptions extends CLIOptions { private APIManagerSetupImportCLIOptions(String[] args) { super(args); } - + public static CLIOptions create(String[] args) throws AppException { CLIOptions cliOptions = new APIManagerSetupImportCLIOptions(args); - cliOptions = new StandardImportCLIOptions(cliOptions); cliOptions = new CoreCLIOptions(cliOptions); cliOptions.addOptions(); cliOptions.parse(); @@ -29,11 +28,12 @@ public void addOptions() { option.setRequired(true); option.setArgName("api-manager.json"); addOption(option); - } + new StandardImportCLIOptions().addOptions(this); + } @Override public void printUsage(String message, String[] args) { - super.printUsage(message, args); + super.printUsage(message, args); Console.println("----------------------------------------------------------------------------------------"); Console.println("How to import API-Manager configuration"); Console.println("Import the API-Manager configuration:"); @@ -48,10 +48,12 @@ public void printUsage(String message, String[] args) { protected String getAppName() { return "API-Manager Config-Import"; } - + @Override public StandardImportParams getParams() { StandardImportParams params = new StandardImportParams(); + params.setEnabledCaches(getValue("enabledCaches")); + params.setStageConfig(getValue("stageConfig")); params.setConfig(getValue("config")); return params; } diff --git a/modules/users/src/main/java/com/axway/apim/users/lib/cli/UserImportCLIOptions.java b/modules/users/src/main/java/com/axway/apim/users/lib/cli/UserImportCLIOptions.java index cb463f9c9..4a06e6eac 100644 --- a/modules/users/src/main/java/com/axway/apim/users/lib/cli/UserImportCLIOptions.java +++ b/modules/users/src/main/java/com/axway/apim/users/lib/cli/UserImportCLIOptions.java @@ -17,7 +17,6 @@ private UserImportCLIOptions(String[] args) { public static CLIOptions create(String[] args) throws AppException { CLIOptions cliOptions = new UserImportCLIOptions(args); - cliOptions = new StandardImportCLIOptions(cliOptions); cliOptions = new CoreCLIOptions(cliOptions); cliOptions.addOptions(); cliOptions.parse(); @@ -31,6 +30,8 @@ public void addOptions() { option.setRequired(true); option.setArgName("user_config.json"); addOption(option); + new StandardImportCLIOptions().addOptions(this); + } @Override @@ -56,6 +57,8 @@ protected String getAppName() { @Override public Parameters getParams() { UserImportParams params = new UserImportParams(); + params.setEnabledCaches(getValue("enabledCaches")); + params.setStageConfig(getValue("stageConfig")); params.setConfig(getValue("config")); return params; }