SAMZA-1307 - Fix ZkKeyBuilder null checks for pathPrefix
authorNavina Ramesh <navina@apache.org>
Wed, 24 May 2017 06:33:09 +0000 (23:33 -0700)
committernramesh <nramesh@linkedin.com>
Wed, 24 May 2017 06:33:09 +0000 (23:33 -0700)
Author: Navina Ramesh <navina@apache.org>

Reviewers: Jagadish V <jagadish@apache.org>

Closes #202 from navina/SAMZA-1307

samza-core/src/main/java/org/apache/samza/zk/ZkKeyBuilder.java
samza-core/src/test/java/org/apache/samza/zk/TestZkKeyBuilder.java

index 7452a97..7e4e0d6 100644 (file)
@@ -19,7 +19,6 @@
 
 package org.apache.samza.zk;
 
-import org.apache.samza.SamzaException;
 import com.google.common.base.Strings;
 
 /**
@@ -53,10 +52,11 @@ public class ZkKeyBuilder {
   static final String JOBMODEL_GENERATION_PATH = "JobModelGeneration";
 
   public ZkKeyBuilder(String pathPrefix) {
-    if (Strings.isNullOrEmpty(pathPrefix)) {
-      throw new SamzaException("Zk PathPrefix cannot be null or empty!");
+    if (pathPrefix != null && !pathPrefix.trim().isEmpty()) {
+      this.pathPrefix = pathPrefix.trim();
+    } else {
+      throw new IllegalArgumentException("Zk PathPrefix cannot be null or empty!");
     }
-    this.pathPrefix = pathPrefix.trim();
   }
 
   public String getRootPath() {
@@ -87,7 +87,7 @@ public class ZkKeyBuilder {
   }
 
   public String getJobModelPathPrefix() {
-    return String.format("%s/%s/jobModels", getRootPath(), JOBMODEL_GENERATION_PATH, pathPrefix);
+    return String.format("%s/%s/jobModels", getRootPath(), JOBMODEL_GENERATION_PATH);
   }
 
   public String getJobModelPath(String jobModelVersion) {
index 3134cfa..6d0bc0b 100644 (file)
  */
 package org.apache.samza.zk;
 
-import org.apache.samza.SamzaException;
 import org.junit.Assert;
 import org.junit.Test;
 
 public class TestZkKeyBuilder {
 
-  @Test
-  public void pathPrefixCannotBeNullOrEmpty() {
-    try {
-      new ZkKeyBuilder("");
-      Assert.fail("Key Builder was created with empty path prefix!");
-      new ZkKeyBuilder(null);
-      Assert.fail("Key Builder was created with null path prefix!");
-    } catch (SamzaException e) {
-      // Expected
-    }
+  @Test(expected = IllegalArgumentException.class)
+  public void pathPrefixCannotBeNull() {
+    new ZkKeyBuilder(null);
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void pathPrefixCannotBeEmpty() {
+    new ZkKeyBuilder("    ");
   }
 
   @Test
@@ -53,7 +50,6 @@ public class TestZkKeyBuilder {
 
   @Test
   public void testJobModelPath() {
-
     ZkKeyBuilder builder = new ZkKeyBuilder("test");
 
     Assert.assertEquals("/test/" + ZkKeyBuilder.JOBMODEL_GENERATION_PATH + "/jobModelVersion", builder.getJobModelVersionPath());