Repository: metron Updated Branches: refs/heads/master 76947ad52 -> 6017c5974
METRON-1180: Make Stellar Shell accept zookeeper quorum as a CSV list and not require a port closes apache/incubator-metron#751 Project: http://git-wip-us.apache.org/repos/asf/metron/repo Commit: http://git-wip-us.apache.org/repos/asf/metron/commit/6017c597 Tree: http://git-wip-us.apache.org/repos/asf/metron/tree/6017c597 Diff: http://git-wip-us.apache.org/repos/asf/metron/diff/6017c597 Branch: refs/heads/master Commit: 6017c5974a868f94ef86b9493ee0d244ffd2df65 Parents: 76947ad Author: cstella <[email protected]> Authored: Wed Sep 13 13:09:51 2017 -0700 Committer: cstella <[email protected]> Committed: Wed Sep 13 13:09:51 2017 -0700 ---------------------------------------------------------------------- .../shell/StellarShellOptionsValidator.java | 50 +++++++++++--------- .../shell/StellarShellOptionsValidatorTest.java | 30 +++++------- 2 files changed, 38 insertions(+), 42 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/metron/blob/6017c597/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/shell/StellarShellOptionsValidator.java ---------------------------------------------------------------------- diff --git a/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/shell/StellarShellOptionsValidator.java b/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/shell/StellarShellOptionsValidator.java index 97f5b70..ab92401 100644 --- a/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/shell/StellarShellOptionsValidator.java +++ b/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/common/shell/StellarShellOptionsValidator.java @@ -26,6 +26,8 @@ import java.net.UnknownHostException; import java.util.function.Predicate; import java.util.regex.Matcher; import java.util.regex.Pattern; + +import com.google.common.base.Splitter; import org.apache.commons.cli.CommandLine; import org.apache.commons.lang3.StringUtils; import org.apache.commons.validator.routines.InetAddressValidator; @@ -73,30 +75,32 @@ public class StellarShellOptionsValidator { /** * Zookeeper argument should be in the form [HOST|IP]:PORT. * - * @param z the zookeeper url fragment + * @param zMulti the zookeeper url fragment */ - private static void validateZookeeperOption(String z) throws IllegalArgumentException { - - Matcher matcher = validPortPattern.matcher(z); - if (!matcher.matches()) { - throw new IllegalArgumentException(String.format("Zookeeper option must have port: %s", z)); - } - - if (matcher.groupCount() != 2) { - throw new IllegalArgumentException( - String.format("Zookeeper Option must be in the form of [HOST|IP]:PORT %s", z)); - } - String name = matcher.group(1); - Integer port = Integer.parseInt(matcher.group(2)); - - if (!hostnameValidator.test(name) && !inetAddressValidator.isValid(name)) { - throw new IllegalArgumentException( - String.format("Zookeeper Option %s is not a valid host name or ip address %s", name, z)); - } - - if(port == 0 || port > 65535){ - throw new IllegalArgumentException( - String.format("Zookeeper Option %s port is not valid",z)); + private static void validateZookeeperOption(String zMulti) throws IllegalArgumentException { + for(String z : Splitter.on(",").split(zMulti)) { + Matcher matcher = validPortPattern.matcher(z); + boolean hasPort = z.contains(":"); + if (hasPort && !matcher.matches()) { + throw new IllegalArgumentException(String.format("Zookeeper option must have valid port: %s", z)); + } + + if (hasPort && matcher.groupCount() != 2) { + throw new IllegalArgumentException( + String.format("Zookeeper Option must be in the form of [HOST|IP]:PORT %s", z)); + } + String name = hasPort?matcher.group(1):z; + Integer port = hasPort?Integer.parseInt(matcher.group(2)):null; + + if (!hostnameValidator.test(name) && !inetAddressValidator.isValid(name)) { + throw new IllegalArgumentException( + String.format("Zookeeper Option %s is not a valid host name or ip address %s", name, z)); + } + + if (hasPort && (port == 0 || port > 65535)) { + throw new IllegalArgumentException( + String.format("Zookeeper Option %s port is not valid", z)); + } } } http://git-wip-us.apache.org/repos/asf/metron/blob/6017c597/metron-stellar/stellar-common/src/test/java/org/apache/metron/stellar/common/shell/StellarShellOptionsValidatorTest.java ---------------------------------------------------------------------- diff --git a/metron-stellar/stellar-common/src/test/java/org/apache/metron/stellar/common/shell/StellarShellOptionsValidatorTest.java b/metron-stellar/stellar-common/src/test/java/org/apache/metron/stellar/common/shell/StellarShellOptionsValidatorTest.java index 52fef96..67a8ae7 100644 --- a/metron-stellar/stellar-common/src/test/java/org/apache/metron/stellar/common/shell/StellarShellOptionsValidatorTest.java +++ b/metron-stellar/stellar-common/src/test/java/org/apache/metron/stellar/common/shell/StellarShellOptionsValidatorTest.java @@ -37,9 +37,10 @@ public class StellarShellOptionsValidatorTest { @Test public void validateOptions() throws Exception { String[] validZHostArg = new String[]{"-z", "localhost:8888"}; + String[] validZHostArgNoPort = new String[]{"-z", "localhost"}; + String[] validZIPArgNoPort = new String[]{"-z", "10.10.10.3"}; + String[] validZHostArgList = new String[]{"-z", "localhost:8888,localhost:2181,localhost"}; String[] validZIPArg = new String[]{"-z", "10.10.10.3:9999"}; - String[] invalidZNoPortArg = new String[]{"-z", "youtube.com"}; - String[] invalidZIPNoPortArg = new String[]{"-z", "10.10.10.3"}; String[] invalidZNameArg = new String[]{"-z", "!!!@!!@!:8882"}; String[] invalidZIPArg = new String[]{"-z", "11111.22222.10.3:3332"}; String[] invalidZMissingNameArg = new String[]{"-z", ":8882"}; @@ -88,27 +89,18 @@ public class StellarShellOptionsValidatorTest { commandLine = parser.parse(options, validPFileArg); StellarShellOptionsValidator.validateOptions(commandLine); + commandLine = parser.parse(options, validZHostArgNoPort); + StellarShellOptionsValidator.validateOptions(commandLine); + + commandLine = parser.parse(options, validZHostArgList); + StellarShellOptionsValidator.validateOptions(commandLine); + + commandLine = parser.parse(options, validZIPArgNoPort); + StellarShellOptionsValidator.validateOptions(commandLine); // these should not boolean thrown = false; - try { - commandLine = parser.parse(options, invalidZNoPortArg); - StellarShellOptionsValidator.validateOptions(commandLine); - } catch (IllegalArgumentException e) { - thrown = true; - } - Assert.assertTrue("Did not catch failure for not providing port with host name ", thrown); - thrown = false; - - try { - commandLine = parser.parse(options, invalidZIPNoPortArg); - StellarShellOptionsValidator.validateOptions(commandLine); - } catch (IllegalArgumentException e) { - thrown = true; - } - Assert.assertTrue("Did not catch failure for not providing port with ip address ", thrown); - thrown = false; try { commandLine = parser.parse(options, invalidZNameArg);
