RetentionExpirationExporterApp { @SuppressWarnings("PMD.SystemPrintln") @SuppressFBWarnings(value = "VA_FORMAT_STRING_USES_NEWLINE", justification = "We will use the standard carriage return character.") ToolsCommonConstants.ReturnValue parseCommandLineArguments(String[] args, ApplicationContext applicationContext) { try { namespaceOpt = argParser.addArgument("n", "namespace", true, "Namespace.", true); businessObjectDefinitionNameOpt = argParser.addArgument("b", "businessObjectDefinitionName", true, "Business object definition.", true); localOutputFileOpt = argParser.addArgument("o", "localOutputFile", true, "The path to files on your local file system.", true); regServerHostOpt = argParser.addArgument("H", "regServerHost", true, "Registration Service hostname.", true); Option regServerPortOpt = argParser.addArgument("P", "regServerPort", true, "Registration Service port.", true); udcServerHostOpt = argParser.addArgument("c", "udcServerHost", true, "UDC application server hostname.", true); Option sslOpt = argParser.addArgument("s", "ssl", true, "Enable or disable SSL (HTTPS).", false); usernameOpt = argParser.addArgument("u", "username", true, "The username for HTTPS client authentication.", false); passwordOpt = argParser.addArgument("w", "password", true, "The password used for HTTPS client authentication.", false); enableEnvVariablesOpt = argParser .addArgument("E", "enableEnvVariables", true, "The enableEnvVariables used for HTTPS client authentication through environment provided var.", false); trustSelfSignedCertificateOpt = argParser.addArgument("C", "trustSelfSignedCertificate", true, "If set to true, makes HTTPS client trust self-signed certificate.", false); disableHostnameVerificationOpt = argParser.addArgument("d", "disableHostnameVerification", true, "If set to true, turns off hostname verification.", false); Option helpOpt = argParser.addArgument("h", "help", false, "Display usage information and exit.", false); Option versionOpt = argParser.addArgument("v", "version", false, "Display version information and exit.", false); argParser.parseArguments(args, false); if (argParser.getBooleanValue(helpOpt)) { System.out.println(argParser.getUsageInformation()); return ToolsCommonConstants.ReturnValue.SUCCESS; } if (argParser.getBooleanValue(versionOpt)) { BuildInformation buildInformation = applicationContext.getBean(BuildInformation.class); System.out.println(String .format(ToolsCommonConstants.BUILD_INFO_STRING_FORMAT, buildInformation.getBuildDate(), buildInformation.getBuildNumber(), buildInformation.getBuildOs(), buildInformation.getBuildUser())); return ToolsCommonConstants.ReturnValue.SUCCESS; } argParser.parseArguments(args, true); useSsl = argParser.getStringValueAsBoolean(sslOpt, false); trustSelfSignedCertificate = argParser.getStringValueAsBoolean(trustSelfSignedCertificateOpt, false); disableHostnameVerification = argParser.getStringValueAsBoolean(disableHostnameVerificationOpt, false); if (useSsl) { if (StringUtils.isBlank(argParser.getStringValue(usernameOpt))) { throw new ParseException("Username is required when SSL is enabled."); } ToolsArgumentHelper.validateCliEnvArgument(argParser, passwordOpt, enableEnvVariablesOpt); } regServerPort = argParser.getIntegerValue(regServerPortOpt); } catch (ParseException ex) { LOGGER.error("Error parsing command line arguments: " + ex.getMessage() + "\n" + argParser.getUsageInformation()); return ToolsCommonConstants.ReturnValue.FAILURE; } return null; } RetentionExpirationExporterApp(); @SuppressWarnings("PMD.DoNotCallSystemExit") // Using System.exit is allowed for an actual application to exit. static void main(String[] args);  }
@Test public void testParseCommandLineArgumentsHelpOpt() { String output = runTestGetSystemOut(() -> { String[] arguments = {"--help"}; assertEquals(ToolsCommonConstants.ReturnValue.SUCCESS, retentionExpirationExporterApp.parseCommandLineArguments(arguments, applicationContext)); }); assertTrue("Incorrect usage information returned.", output.startsWith("usage: " + RetentionExpirationExporterApp.APPLICATION_NAME)); }
@Test public void testParseCommandLineArgumentsNone() { assertEquals(ToolsCommonConstants.ReturnValue.FAILURE, retentionExpirationExporterApp.parseCommandLineArguments(new String[] {}, applicationContext)); }
@Test public void testParseCommandLineArgumentsSslTrueAndNoPassword() { String[] arguments = {"--namespace", NAMESPACE, "--businessObjectDefinitionName", BUSINESS_OBJECT_DEFINITION_NAME, "--localOutputFile", LOCAL_OUTPUT_FILE, "--regServerHost", WEB_SERVICE_HOSTNAME, "--regServerPort", WEB_SERVICE_HTTPS_PORT.toString(), "--udcServerHost", UDC_SERVICE_HOSTNAME, "--ssl", "true", "--username", WEB_SERVICE_HTTPS_USERNAME, "--trustSelfSignedCertificate", "true", "--disableHostnameVerification", "true"}; assertEquals(ToolsCommonConstants.ReturnValue.FAILURE, retentionExpirationExporterApp.parseCommandLineArguments(arguments, applicationContext)); }
@Test public void testParseCommandLineArgumentsSslTrueAndNoUsername() { String[] arguments = {"--namespace", NAMESPACE, "--businessObjectDefinitionName", BUSINESS_OBJECT_DEFINITION_NAME, "--localOutputFile", LOCAL_OUTPUT_FILE, "--regServerHost", WEB_SERVICE_HOSTNAME, "--regServerPort", WEB_SERVICE_HTTPS_PORT.toString(), "--udcServerHost", UDC_SERVICE_HOSTNAME, "--ssl", "true", "--password", WEB_SERVICE_HTTPS_PASSWORD, "--trustSelfSignedCertificate", "true", "--disableHostnameVerification", "true"}; assertEquals(ToolsCommonConstants.ReturnValue.FAILURE, retentionExpirationExporterApp.parseCommandLineArguments(arguments, applicationContext)); }
@Test public void testParseCommandLineArgumentsEnableEnvVarFalse() { environmentVariables.set("HERD_PASSWORD", WEB_SERVICE_HTTPS_PASSWORD); String[] arguments = {"--namespace", NAMESPACE, "--businessObjectDefinitionName", BUSINESS_OBJECT_DEFINITION_NAME, "--localOutputFile", LOCAL_OUTPUT_FILE, "--regServerHost", WEB_SERVICE_HOSTNAME, "--regServerPort", WEB_SERVICE_HTTPS_PORT.toString(), "--udcServerHost", UDC_SERVICE_HOSTNAME, "--ssl", "true", "--username", WEB_SERVICE_HTTPS_USERNAME, "-E", "false", "--trustSelfSignedCertificate", "true", "--disableHostnameVerification", "true"}; assertEquals(ToolsCommonConstants.ReturnValue.FAILURE, retentionExpirationExporterApp.parseCommandLineArguments(arguments, applicationContext)); environmentVariables.clear("HERD_PASSWORD"); }
@Test public void testParseCommandLineArgumentsSslTrueEnvPassword() { environmentVariables.set("HERD_PASSWORD", WEB_SERVICE_HTTPS_PASSWORD); String[] arguments = {"--namespace", NAMESPACE, "--businessObjectDefinitionName", BUSINESS_OBJECT_DEFINITION_NAME, "--localOutputFile", LOCAL_OUTPUT_FILE, "--regServerHost", WEB_SERVICE_HOSTNAME, "--regServerPort", WEB_SERVICE_HTTPS_PORT.toString(), "--udcServerHost", UDC_SERVICE_HOSTNAME, "--ssl", "true", "--username", WEB_SERVICE_HTTPS_USERNAME, "-E", "true", "--trustSelfSignedCertificate", "true", "--disableHostnameVerification", "true"}; assertNull(retentionExpirationExporterApp.parseCommandLineArguments(arguments, applicationContext)); environmentVariables.clear("HERD_PASSWORD"); }
@Test public void testParseCommandLineArgumentsVersionOpt() { String output = runTestGetSystemOut(() -> { String[] arguments = {"--version"}; assertEquals(ToolsCommonConstants.ReturnValue.SUCCESS, retentionExpirationExporterApp.parseCommandLineArguments(arguments, applicationContext)); }); BuildInformation buildInformation = applicationContext.getBean(BuildInformation.class); assertEquals("output", String .format(DataBridgeApp.BUILD_INFO_STRING_FORMAT, buildInformation.getBuildDate(), buildInformation.getBuildNumber(), buildInformation.getBuildOs(), buildInformation.getBuildUser()), output); }
@Test public void testParseShortCommandLineArgumentsSuccess() { String[] arguments = {"-n", NAMESPACE, "-b", BUSINESS_OBJECT_DEFINITION_NAME, "-o", LOCAL_OUTPUT_FILE, "-H", WEB_SERVICE_HOSTNAME, "-P", WEB_SERVICE_HTTPS_PORT.toString(), "-c", UDC_SERVICE_HOSTNAME, "-s", "true", "-u", WEB_SERVICE_HTTPS_USERNAME, "-w", WEB_SERVICE_HTTPS_PASSWORD, "-C", "true", "-d", "true"}; assertNull(retentionExpirationExporterApp.parseCommandLineArguments(arguments, applicationContext)); }