HCAT-561 HCatClient should differentiate between "partition not available" and other generic errors.

git-svn-id: https://svn.apache.org/repos/asf/incubator/hcatalog/trunk@1418581 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/CHANGES.txt b/CHANGES.txt
index 28c0018..f3da8c3 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -151,6 +151,8 @@
   OPTIMIZATIONS
 
   BUG FIXES
+  HCAT-561 HCatClient should differentiate between "partition not available" and other generic errors.(mithunr via avandana)
+
   HCAT-563 hcat script should look in the correct dir for the storage handler jars (arpitgupta via khorgath)
 
   HCAT-559 Fix trivial build warnings in HCat trunk (mithunr via avandana)
diff --git a/webhcat/java-client/src/main/java/org/apache/hcatalog/api/ConnectionFailureException.java b/webhcat/java-client/src/main/java/org/apache/hcatalog/api/ConnectionFailureException.java
index 4b10a01..6b4c9ab 100644
--- a/webhcat/java-client/src/main/java/org/apache/hcatalog/api/ConnectionFailureException.java
+++ b/webhcat/java-client/src/main/java/org/apache/hcatalog/api/ConnectionFailureException.java
@@ -28,8 +28,8 @@
     private static final long serialVersionUID = 1L;
 
     /**
-     * @param message
-     * @param cause
+     * @param message Exception message.
+     * @param cause The wrapped Throwable that caused this exception.
      */
     public ConnectionFailureException(String message, Throwable cause) {
         super(message, cause);
diff --git a/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatClientHMSImpl.java b/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatClientHMSImpl.java
index 73345da..ffd618d 100644
--- a/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatClientHMSImpl.java
+++ b/webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatClientHMSImpl.java
@@ -79,7 +79,7 @@
                 db = new HCatDatabase(hiveDB);
             }
         } catch (NoSuchObjectException exp) {
-            throw new HCatException(
+            throw new ObjectNotFoundException(
                 "NoSuchObjectException while fetching database", exp);
         } catch (MetaException exp) {
             throw new HCatException("MetaException while fetching database",
@@ -120,7 +120,7 @@
             hmsClient.dropDatabase(checkDB(dbName), true, ifExists, isCascade);
         } catch (NoSuchObjectException e) {
             if (!ifExists) {
-                throw new HCatException(
+                throw new ObjectNotFoundException(
                     "NoSuchObjectException while dropping db.", e);
             }
         } catch (InvalidOperationException e) {
@@ -162,7 +162,7 @@
             throw new ConnectionFailureException(
                 "TException while fetching table.", e);
         } catch (NoSuchObjectException e) {
-            throw new HCatException(
+            throw new ObjectNotFoundException(
                 "NoSuchObjectException while fetching table.", e);
         }
         return table;
@@ -184,7 +184,7 @@
         } catch (MetaException e) {
             throw new HCatException("MetaException while creating table.", e);
         } catch (NoSuchObjectException e) {
-            throw new HCatException(
+            throw new ObjectNotFoundException(
                 "NoSuchObjectException while creating table.", e);
         } catch (TException e) {
             throw new ConnectionFailureException(
@@ -210,7 +210,7 @@
             throw new HCatException("MetaException while updating table schema.", e);
         }
         catch (NoSuchObjectException e) {
-            throw new HCatException(
+            throw new ObjectNotFoundException(
                     "NoSuchObjectException while updating table schema.", e);
         }
         catch (TException e) {
@@ -243,7 +243,7 @@
                 throw new HCatException(
                     "MetaException in create table like command.", e);
             } catch (NoSuchObjectException e) {
-                throw new HCatException(
+                throw new ObjectNotFoundException(
                     "NoSuchObjectException in create table like command.",
                     e);
             } catch (TException e) {
@@ -260,7 +260,7 @@
             hmsClient.dropTable(checkDB(dbName), tableName, true, ifExists);
         } catch (NoSuchObjectException e) {
             if (!ifExists) {
-                throw new HCatException(
+                throw new ObjectNotFoundException(
                     "NoSuchObjectException while dropping table.", e);
             }
         } catch (MetaException e) {
@@ -295,7 +295,7 @@
             throw new ConnectionFailureException(
                 "TException while renaming table", e);
         } catch (NoSuchObjectException e) {
-            throw new HCatException(
+            throw new ObjectNotFoundException(
                 "NoSuchObjectException while renaming table", e);
         } catch (InvalidOperationException e) {
             throw new HCatException(
@@ -314,7 +314,7 @@
                 hcatPtns.add(new HCatPartition(ptn));
             }
         } catch (NoSuchObjectException e) {
-            throw new HCatException(
+            throw new ObjectNotFoundException(
                 "NoSuchObjectException while retrieving partition.", e);
         } catch (MetaException e) {
             throw new HCatException(
@@ -345,7 +345,7 @@
             throw new ConnectionFailureException(
                 "TException while retrieving partition.", e);
         } catch (NoSuchObjectException e) {
-            throw new HCatException(
+            throw new ObjectNotFoundException(
                 "NoSuchObjectException while retrieving partition.", e);
         }
         return partition;
@@ -377,7 +377,7 @@
             throw new ConnectionFailureException(
                 "TException while adding partition.", e);
         } catch (NoSuchObjectException e) {
-            throw new HCatException("The table " + partInfo.getTableName()
+            throw new ObjectNotFoundException("The table " + partInfo.getTableName()
                 + " is could not be found.", e);
         }
     }
@@ -393,7 +393,7 @@
                 ifExists);
         } catch (NoSuchObjectException e) {
             if (!ifExists) {
-                throw new HCatException(
+                throw new ObjectNotFoundException(
                     "NoSuchObjectException while dropping partition.", e);
             }
         } catch (MetaException e) {
@@ -419,7 +419,7 @@
             throw new HCatException("MetaException while fetching partitions.",
                 e);
         } catch (NoSuchObjectException e) {
-            throw new HCatException(
+            throw new ObjectNotFoundException(
                 "NoSuchObjectException while fetching partitions.", e);
         } catch (TException e) {
             throw new ConnectionFailureException(
@@ -439,7 +439,7 @@
             throw new HCatException(
                 "MetaException while marking partition for event.", e);
         } catch (NoSuchObjectException e) {
-            throw new HCatException(
+            throw new ObjectNotFoundException(
                 "NoSuchObjectException while marking partition for event.",
                 e);
         } catch (UnknownTableException e) {
@@ -475,7 +475,7 @@
             throw new HCatException(
                 "MetaException while checking partition for event.", e);
         } catch (NoSuchObjectException e) {
-            throw new HCatException(
+            throw new ObjectNotFoundException(
                 "NoSuchObjectException while checking partition for event.",
                 e);
         } catch (UnknownTableException e) {
@@ -585,7 +585,7 @@
             throw new ConnectionFailureException(
                 "TException while retrieving existing table.", e1);
         } catch (NoSuchObjectException e1) {
-            throw new HCatException(
+            throw new ObjectNotFoundException(
                 "NoSuchObjectException while retrieving existing table.",
                 e1);
         }
@@ -667,7 +667,7 @@
             throw new ConnectionFailureException(
                 "TException while adding partition.", e);
         } catch (NoSuchObjectException e) {
-            throw new HCatException("The table "
+            throw new ObjectNotFoundException("The table "
                 + partInfoList.get(0).getTableName()
                 + " is could not be found.", e);
         }
diff --git a/webhcat/java-client/src/main/java/org/apache/hcatalog/api/ObjectNotFoundException.java b/webhcat/java-client/src/main/java/org/apache/hcatalog/api/ObjectNotFoundException.java
new file mode 100644
index 0000000..d4e6133
--- /dev/null
+++ b/webhcat/java-client/src/main/java/org/apache/hcatalog/api/ObjectNotFoundException.java
@@ -0,0 +1,38 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hcatalog.api;
+
+import org.apache.hcatalog.common.HCatException;
+
+/**
+ * This exception is thrown when a Database, Table or Partition
+ * specified in an HCatalog query is not found.
+ */
+public class ObjectNotFoundException extends HCatException {
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * @param message Exception message.
+     * @param cause The wrapped Throwable that caused this exception.
+     */
+    public ObjectNotFoundException(String message, Throwable cause) {
+        super(message, cause);
+    }
+}
diff --git a/webhcat/java-client/src/test/java/org/apache/hcatalog/api/TestHCatClient.java b/webhcat/java-client/src/test/java/org/apache/hcatalog/api/TestHCatClient.java
index 5cdcf1a..a161eca 100644
--- a/webhcat/java-client/src/test/java/org/apache/hcatalog/api/TestHCatClient.java
+++ b/webhcat/java-client/src/test/java/org/apache/hcatalog/api/TestHCatClient.java
@@ -46,8 +46,10 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertArrayEquals;
 
 public class TestHCatClient {
     private static final Logger LOG = LoggerFactory.getLogger(TestHCatClient.class);
@@ -402,4 +404,77 @@
             assertTrue("Unexpected exception: " + exception.getMessage(), false);
         }
     }
+
+    @Test
+    public void testObjectNotFoundException() throws Exception {
+        try {
+
+            HCatClient client = HCatClient.create(new  Configuration(hcatConf));
+            String dbName = "testObjectNotFoundException_DBName";
+            String tableName = "testObjectNotFoundException_TableName";
+            client.dropDatabase(dbName, true, HCatClient.DropDBMode.CASCADE);
+
+            try {    // Test that fetching a non-existent db-name yields ObjectNotFound.
+                client.getDatabase(dbName);
+                assertTrue("Expected ObjectNotFoundException.", false);
+            } catch(Exception exception) {
+                LOG.info("Got exception: ", exception);
+                assertTrue("Expected ObjectNotFoundException. Got:" + exception.getClass(),
+                        exception instanceof ObjectNotFoundException);
+            }
+
+            client.createDatabase(HCatCreateDBDesc.create(dbName).build());
+
+            try {   // Test that fetching a non-existent table-name yields ObjectNotFound.
+                client.getTable(dbName, tableName);
+                assertTrue("Expected ObjectNotFoundException.", false);
+            } catch(Exception exception) {
+                LOG.info("Got exception: ", exception);
+                assertTrue("Expected ObjectNotFoundException. Got:" + exception.getClass(),
+                        exception instanceof ObjectNotFoundException);
+            }
+
+            String partitionColumn = "part";
+
+            List<HCatFieldSchema> columns = Arrays.asList(new HCatFieldSchema("col", Type.STRING, ""));
+            ArrayList<HCatFieldSchema> partitionColumns = new ArrayList<HCatFieldSchema>(
+                    Arrays.asList(new HCatFieldSchema(partitionColumn, Type.STRING, "")));
+            client.createTable(HCatCreateTableDesc.create(dbName, tableName, columns)
+                    .partCols(partitionColumns)
+                    .build());
+
+            Map<String, String> partitionSpec = new HashMap<String, String>();
+            partitionSpec.put(partitionColumn, "foobar");
+            try {  // Test that fetching a non-existent partition yields ObjectNotFound.
+                client.getPartition(dbName, tableName, partitionSpec);
+                assertTrue("Expected ObjectNotFoundException.", false);
+            } catch(Exception exception) {
+                LOG.info("Got exception: ", exception);
+                assertTrue("Expected ObjectNotFoundException. Got:" + exception.getClass(),
+                        exception instanceof ObjectNotFoundException);
+            }
+
+            client.addPartition(HCatAddPartitionDesc.create(dbName, tableName, "", partitionSpec).build());
+
+            // Test that listPartitionsByFilter() returns an empty-set, if the filter selects no partitions.
+            assertEquals("Expected empty set of partitions.",
+                    0, client.listPartitionsByFilter(dbName, tableName, partitionColumn + " < 'foobar'").size());
+
+            try {  // Test that listPartitionsByFilter() throws HCatException if the partition-key is incorrect.
+                partitionSpec.put("NonExistentKey", "foobar");
+                client.getPartition(dbName, tableName, partitionSpec);
+                assertTrue("Expected HCatException.", false);
+            } catch(Exception exception) {
+                LOG.info("Got exception: ", exception);
+                assertTrue("Expected HCatException. Got:" + exception.getClass(),
+                        exception instanceof HCatException);
+                assertFalse("Did not expect ObjectNotFoundException.", exception instanceof ObjectNotFoundException);
+            }
+
+        }
+        catch (Throwable t) {
+            LOG.error("Unexpected exception!", t);
+            assertTrue("Unexpected exception! " + t.getMessage(), false);
+        }
+    }
 }