ToolsArgumentHelper { public static void validateCliEnvArgument(ArgumentParser argParser, Option cliOpt, Option enableEnvVariablesOpt) throws ParseException { String optionName = cliOpt.getLongOpt(); String envVarName = getEnvVarName(cliOpt); boolean blankCliOption = StringUtils.isBlank(argParser.getStringValue(cliOpt)); boolean enableEnvVars = argParser.getStringValueAsBoolean(enableEnvVariablesOpt, false); boolean blankEnvVars = StringUtils.isBlank(ToolsArgumentHelper.getEnvValue(cliOpt)); if (blankCliOption && !enableEnvVars || blankCliOption && blankEnvVars) { { throw new ParseException(String.format("Either %s or enableEnvVariables with env variable %s is required.", optionName, envVarName)); } } }  static void validateCliEnvArgument(ArgumentParser argParser, Option cliOpt, Option enableEnvVariablesOpt); static String getCliEnvArgumentValue(ArgumentParser argParser, Option cliOpt, Option enableEnvVariablesOpt);  }
@Test public void validateCliEnvArgumentSuccess() throws ParseException { ArgumentParser argParser = new ArgumentParser("test"); Option passwordOpt = argParser.addArgument("w", "password", true, "The password used for HTTPS client authentication.", false); Option enableEnvVariablesOpt = argParser.addArgument("E", "enableEnvVariables", true, "The enableEnvVariables used for HTTPS client authentication through environment provided variable.", false); String[] argumentsWithCliOpt = {"-w", "cliPassword"}; argParser.parseArguments(argumentsWithCliOpt); ToolsArgumentHelper.validateCliEnvArgument(argParser, passwordOpt, enableEnvVariablesOpt); environmentVariables.set("HERD_PASSWORD", "envPassword"); String[] argumentsWithEnableVarsOpt = {"--enableEnvVariables", "true"}; argParser.parseArguments(argumentsWithEnableVarsOpt); ToolsArgumentHelper.validateCliEnvArgument(argParser, passwordOpt, enableEnvVariablesOpt); environmentVariables.clear("HERD_PASSWORD"); String[] argumentsWithCliAndEnableVarsOpt = {"-w", "cliPassword", "--enableEnvVariables", "true"}; argParser.parseArguments(argumentsWithCliAndEnableVarsOpt); ToolsArgumentHelper.validateCliEnvArgument(argParser, passwordOpt, enableEnvVariablesOpt); }
@Test public void getCliEnvArgumentValueNonBlackCliOption() throws ParseException { String[] argumentsWithCliOpt = {"-w", "cliPassword"}; validateCliEnvArgument(argumentsWithCliOpt, "cliPassword"); }
@Test public void getCliEnvArgumentValueBlackCliOption() throws ParseException { String[] argumentsWithCliOpt = {"-w", ""}; validateCliEnvArgument(argumentsWithCliOpt, null); }
@Test public void getCliEnvArgumentValueEnableEnvVarTrueNonBlackEnvOption() throws ParseException { environmentVariables.set("HERD_PASSWORD", "envPassword"); String[] argumentsWithCliOpt = {"--enableEnvVariables", "true"}; validateCliEnvArgument(argumentsWithCliOpt, "envPassword"); environmentVariables.clear("HERD_PASSWORD"); }
@Test public void getCliEnvArgumentValueEnableEnvVarFalse() throws ParseException { environmentVariables.set("HERD_PASSWORD", "envPassword"); String[] argumentsWithCliOpt = {"--enableEnvVariables", "false"}; validateCliEnvArgument(argumentsWithCliOpt, null); environmentVariables.clear("HERD_PASSWORD"); }
@Test public void getCliEnvArgumentValueEnableEnvVarTrueBlankEnvVar() throws ParseException { environmentVariables.set("HERD_PASSWORD", ""); String[] argumentsWithCliOpt = {"--enableEnvVariables", "true"}; validateCliEnvArgument(argumentsWithCliOpt, null); environmentVariables.clear("HERD_PASSWORD"); }