ASTERIXDB-1356: report file not found

Change-Id: Ia12bf84adfd8442103567df42ac22e5750e1360f
Reviewed-on: https://asterix-gerrit.ics.uci.edu/771
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Murtadha Hubail <hubailmor@gmail.com>
diff --git a/asterix-app/src/test/resources/runtimets/queries/load/file-not-found/file-not-found.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/load/file-not-found/file-not-found.1.ddl.aql
new file mode 100644
index 0000000..37a8f14
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/load/file-not-found/file-not-found.1.ddl.aql
@@ -0,0 +1,29 @@
+/*
+ * 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.
+ */
+/**
+ * Test loading from a file that does not exist.
+ * Expected result: fail - File not found.
+ */
+
+drop dataverse broken if exists;
+create dataverse broken;
+use dataverse broken;
+
+create type xtype as closed { id: int32 };
+create dataset X(xtype) primary key id;
diff --git a/asterix-app/src/test/resources/runtimets/queries/load/file-not-found/file-not-found.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/load/file-not-found/file-not-found.2.update.aql
new file mode 100644
index 0000000..c26ffd5
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/load/file-not-found/file-not-found.2.update.aql
@@ -0,0 +1,30 @@
+/*
+ * 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.
+ */
+/**
+ * Test loading from a file that does not exist.
+ * Expected result: fail - File not found.
+ */
+
+use dataverse broken;
+
+load dataset X using localfs(
+  ("path"="asterix_nc1://bla"),
+  ("format"="delimited-text"),
+  ("delimiter"="|")
+);
diff --git a/asterix-app/src/test/resources/runtimets/testsuite.xml b/asterix-app/src/test/resources/runtimets/testsuite.xml
index e4e12f3..0ce4103 100644
--- a/asterix-app/src/test/resources/runtimets/testsuite.xml
+++ b/asterix-app/src/test/resources/runtimets/testsuite.xml
@@ -6343,6 +6343,12 @@
                 <!-- <expected-error>org.apache.hyracks.api.exceptions.HyracksException</expected-error> -->
             </compilation-unit>
         </test-case>
+        <test-case FilePath="load">
+            <compilation-unit name="file-not-found">
+              <output-dir compare="Text">none</output-dir>
+              <expected-error>org.apache.hyracks.api.exceptions.HyracksDataException: bla: path not found</expected-error>
+            </compilation-unit>
+        </test-case>
         <test-case FilePath="user-defined-functions">
             <compilation-unit name="query-issue244">
                 <output-dir compare="Text">query-issue244</output-dir>
diff --git a/asterix-external-data/src/main/java/org/apache/asterix/external/input/stream/LocalFSInputStream.java b/asterix-external-data/src/main/java/org/apache/asterix/external/input/stream/LocalFSInputStream.java
index 2519177..3c3b8fb 100644
--- a/asterix-external-data/src/main/java/org/apache/asterix/external/input/stream/LocalFSInputStream.java
+++ b/asterix-external-data/src/main/java/org/apache/asterix/external/input/stream/LocalFSInputStream.java
@@ -38,7 +38,7 @@
     private byte lastByte;
     private File currentFile;
 
-    public LocalFSInputStream(FileSystemWatcher watcher) throws IOException {
+    public LocalFSInputStream(FileSystemWatcher watcher) {
         this.watcher = watcher;
     }
 
diff --git a/asterix-external-data/src/main/java/org/apache/asterix/external/input/stream/factory/LocalFSInputStreamFactory.java b/asterix-external-data/src/main/java/org/apache/asterix/external/input/stream/factory/LocalFSInputStreamFactory.java
index 712ffbe..ae012f3 100644
--- a/asterix-external-data/src/main/java/org/apache/asterix/external/input/stream/factory/LocalFSInputStreamFactory.java
+++ b/asterix-external-data/src/main/java/org/apache/asterix/external/input/stream/factory/LocalFSInputStreamFactory.java
@@ -19,7 +19,6 @@
 package org.apache.asterix.external.input.stream.factory;
 
 import java.io.File;
-import java.io.IOException;
 import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Map;
@@ -72,11 +71,7 @@
             }
             watcher = new FileSystemWatcher(inputResources, expression, isFeed);
         }
-        try {
-            return new LocalFSInputStream(watcher);
-        } catch (IOException e) {
-            throw new HyracksDataException(e);
-        }
+        return new LocalFSInputStream(watcher);
     }
 
     @Override
diff --git a/asterix-external-data/src/main/java/org/apache/asterix/external/util/FileSystemWatcher.java b/asterix-external-data/src/main/java/org/apache/asterix/external/util/FileSystemWatcher.java
index b15d097..ea5cc8f 100644
--- a/asterix-external-data/src/main/java/org/apache/asterix/external/util/FileSystemWatcher.java
+++ b/asterix-external-data/src/main/java/org/apache/asterix/external/util/FileSystemWatcher.java
@@ -93,6 +93,10 @@
                         register(dirPath);
                     }
                     resume();
+                } else {
+                    if (files.isEmpty()) {
+                        throw new HyracksDataException(path + ": no files found");
+                    }
                 }
             }
         } catch (IOException e) {
diff --git a/asterix-external-data/src/main/java/org/apache/asterix/external/util/LocalFileSystemUtils.java b/asterix-external-data/src/main/java/org/apache/asterix/external/util/LocalFileSystemUtils.java
index d6e9463..16dd1e9 100644
--- a/asterix-external-data/src/main/java/org/apache/asterix/external/util/LocalFileSystemUtils.java
+++ b/asterix-external-data/src/main/java/org/apache/asterix/external/util/LocalFileSystemUtils.java
@@ -29,19 +29,20 @@
 import java.util.LinkedList;
 import java.util.regex.Pattern;
 
+import org.apache.hyracks.api.exceptions.HyracksDataException;
+
 public class LocalFileSystemUtils {
 
-    //TODO: replace this method by FileUtils.iterateFilesAndDirs(.)
     public static void traverse(final LinkedList<File> files, File root, final String expression,
             final LinkedList<Path> dirs) throws IOException {
-        if (!Files.exists(root.toPath())) {
-            return;
+        final Path path = root.toPath();
+        if (!Files.exists(path)) {
+            throw new HyracksDataException(path + ": path not found");
         }
-        if (!Files.isDirectory(root.toPath())) {
-            validateAndAdd(root.toPath(), expression, files);
+        if (!Files.isDirectory(path)) {
+            validateAndAdd(path, expression, files);
         }
-        //FileUtils.iterateFilesAndDirs(directory, fileFilter, dirFilter)
-        Files.walkFileTree(root.toPath(), new SimpleFileVisitor<Path>() {
+        Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
             @Override
             public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes attrs) throws IOException {
                 if (!Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {