falcon-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From venkat...@apache.org
Subject [2/3] git commit: FALCON-459 Lineage resource API fails with NPE for bad query params. Contributed by Sowmya Ramesh
Date Mon, 16 Jun 2014 05:43:39 GMT
FALCON-459 Lineage resource API fails with NPE for bad query params. Contributed by Sowmya
Ramesh


Project: http://git-wip-us.apache.org/repos/asf/incubator-falcon/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-falcon/commit/662adb21
Tree: http://git-wip-us.apache.org/repos/asf/incubator-falcon/tree/662adb21
Diff: http://git-wip-us.apache.org/repos/asf/incubator-falcon/diff/662adb21

Branch: refs/heads/master
Commit: 662adb2154cbd47bbdaebceae529013ed37d125f
Parents: ab07b44
Author: Venkatesh Seetharam <venkatesh@apache.org>
Authored: Mon Jun 16 11:11:11 2014 +0530
Committer: Venkatesh Seetharam <venkatesh@apache.org>
Committed: Mon Jun 16 11:11:11 2014 +0530

----------------------------------------------------------------------
 CHANGES.txt                                     |  3 ++
 .../metadata/LineageMetadataResource.java       | 42 ++++++++++++++------
 .../metadata/LineageMetadataResourceTest.java   | 21 ++++++++++
 3 files changed, 54 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-falcon/blob/662adb21/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 418d337..b24fc4b 100755
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -16,6 +16,9 @@ Trunk (Unreleased)
   OPTIMIZATIONS
 
   BUG FIXES
+   FALCON-459 Lineage resource API fails with NPE for bad query params
+   (Sowmya Ramesh via Venkatesh Seetharam)
+
    FALCON-456 Custom DistCp conflict with core DistCp in container classpath
    fails falcon workflows (Venkatesh Seetharam)
 

http://git-wip-us.apache.org/repos/asf/incubator-falcon/blob/662adb21/prism/src/main/java/org/apache/falcon/resource/metadata/LineageMetadataResource.java
----------------------------------------------------------------------
diff --git a/prism/src/main/java/org/apache/falcon/resource/metadata/LineageMetadataResource.java
b/prism/src/main/java/org/apache/falcon/resource/metadata/LineageMetadataResource.java
index 93b4c04..cf6b6b1 100644
--- a/prism/src/main/java/org/apache/falcon/resource/metadata/LineageMetadataResource.java
+++ b/prism/src/main/java/org/apache/falcon/resource/metadata/LineageMetadataResource.java
@@ -38,6 +38,7 @@ import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.apache.commons.lang.StringUtils;
 
 import javax.ws.rs.DefaultValue;
 import javax.ws.rs.GET;
@@ -143,6 +144,7 @@ public class LineageMetadataResource {
     public Response getVertex(@PathParam("id") final String vertexId) {
         checkIfMetadataMappingServiceIsEnabled();
         LOG.info("Get vertex for vertexId= {}", vertexId);
+        validateInputs("Invalid argument: vertex id passed is null or empty.", vertexId);
         try {
             Vertex vertex = findVertex(vertexId);
 
@@ -182,6 +184,7 @@ public class LineageMetadataResource {
                                         final String relationships) {
         checkIfMetadataMappingServiceIsEnabled();
         LOG.info("Get vertex for vertexId= {}", vertexId);
+        validateInputs("Invalid argument: vertex id passed is null or empty.", vertexId);
         try {
             Vertex vertex = findVertex(vertexId);
 
@@ -281,6 +284,7 @@ public class LineageMetadataResource {
                                 @QueryParam("value") final String value) {
         checkIfMetadataMappingServiceIsEnabled();
         LOG.info("Get vertices for property key= {}, value= {}", key, value);
+        validateInputs("Invalid argument: key or value passed is null or empty.", key, value);
         try {
             JSONObject response = buildJSONResponse(getGraph().getVertices(key, value));
             return Response.ok(response).build();
@@ -305,6 +309,8 @@ public class LineageMetadataResource {
                                    @PathParam("direction") String direction) {
         checkIfMetadataMappingServiceIsEnabled();
         LOG.info("Get vertex edges for vertexId= {}, direction= {}", vertexId, direction);
+        // Validate vertex id. Direction is validated in VertexQueryArguments.
+        validateInputs("Invalid argument: vertex id or direction passed is null or empty.",
vertexId, direction);
         try {
             Vertex vertex = findVertex(vertexId);
 
@@ -394,6 +400,7 @@ public class LineageMetadataResource {
     public Response getEdge(@PathParam("id") final String edgeId) {
         checkIfMetadataMappingServiceIsEnabled();
         LOG.info("Get vertex for edgeId= {}", edgeId);
+        validateInputs("Invalid argument: edge id passed is null or empty.", edgeId);
         try {
             Edge edge = getGraph().getEdge(edgeId);
             if (edge == null) {
@@ -439,6 +446,17 @@ public class LineageMetadataResource {
         }
     }
 
+    private static void validateInputs(String errorMsg, String... inputs) {
+        for (String input : inputs) {
+            if (StringUtils.isEmpty(input)) {
+                throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
+                        .entity(errorMsg)
+                        .type("text/plain")
+                        .build());
+            }
+        }
+    }
+
     private enum ReturnType {VERTICES, EDGES, COUNT, VERTEX_IDS}
 
     public static final String OUT_E = "outE";
@@ -464,51 +482,51 @@ public class LineageMetadataResource {
         private final boolean countOnly;
 
         public VertexQueryArguments(String directionSegment) {
-            if (directionSegment.equals(OUT_E)) {
+            if (OUT_E.equals(directionSegment)) {
                 returnType = ReturnType.EDGES;
                 queryDirection = Direction.OUT;
                 countOnly = false;
-            } else if (directionSegment.equals(IN_E)) {
+            } else if (IN_E.equals(directionSegment)) {
                 returnType = ReturnType.EDGES;
                 queryDirection = Direction.IN;
                 countOnly = false;
-            } else if (directionSegment.equals(BOTH_E)) {
+            } else if (BOTH_E.equals(directionSegment)) {
                 returnType = ReturnType.EDGES;
                 queryDirection = Direction.BOTH;
                 countOnly = false;
-            } else if (directionSegment.equals(OUT)) {
+            } else if (OUT.equals(directionSegment)) {
                 returnType = ReturnType.VERTICES;
                 queryDirection = Direction.OUT;
                 countOnly = false;
-            } else if (directionSegment.equals(IN)) {
+            } else if (IN.equals(directionSegment)) {
                 returnType = ReturnType.VERTICES;
                 queryDirection = Direction.IN;
                 countOnly = false;
-            } else if (directionSegment.equals(BOTH)) {
+            } else if (BOTH.equals(directionSegment)) {
                 returnType = ReturnType.VERTICES;
                 queryDirection = Direction.BOTH;
                 countOnly = false;
-            } else if (directionSegment.equals(BOTH_COUNT)) {
+            } else if (BOTH_COUNT.equals(directionSegment)) {
                 returnType = ReturnType.COUNT;
                 queryDirection = Direction.BOTH;
                 countOnly = true;
-            } else if (directionSegment.equals(IN_COUNT)) {
+            } else if (IN_COUNT.equals(directionSegment)) {
                 returnType = ReturnType.COUNT;
                 queryDirection = Direction.IN;
                 countOnly = true;
-            } else if (directionSegment.equals(OUT_COUNT)) {
+            } else if (OUT_COUNT.equals(directionSegment)) {
                 returnType = ReturnType.COUNT;
                 queryDirection = Direction.OUT;
                 countOnly = true;
-            } else if (directionSegment.equals(BOTH_IDS)) {
+            } else if (BOTH_IDS.equals(directionSegment)) {
                 returnType = ReturnType.VERTEX_IDS;
                 queryDirection = Direction.BOTH;
                 countOnly = false;
-            } else if (directionSegment.equals(IN_IDS)) {
+            } else if (IN_IDS.equals(directionSegment)) {
                 returnType = ReturnType.VERTEX_IDS;
                 queryDirection = Direction.IN;
                 countOnly = false;
-            } else if (directionSegment.equals(OUT_IDS)) {
+            } else if (OUT_IDS.equals(directionSegment)) {
                 returnType = ReturnType.VERTEX_IDS;
                 queryDirection = Direction.OUT;
                 countOnly = false;

http://git-wip-us.apache.org/repos/asf/incubator-falcon/blob/662adb21/prism/src/test/java/org/apache/falcon/resource/metadata/LineageMetadataResourceTest.java
----------------------------------------------------------------------
diff --git a/prism/src/test/java/org/apache/falcon/resource/metadata/LineageMetadataResourceTest.java
b/prism/src/test/java/org/apache/falcon/resource/metadata/LineageMetadataResourceTest.java
index e4d06a5..26c24c7 100644
--- a/prism/src/test/java/org/apache/falcon/resource/metadata/LineageMetadataResourceTest.java
+++ b/prism/src/test/java/org/apache/falcon/resource/metadata/LineageMetadataResourceTest.java
@@ -215,6 +215,18 @@ public class LineageMetadataResourceTest {
     }
 
     @Test
+    public void testGetVerticesWithInvalidKeyValue() throws Exception {
+        LineageMetadataResource resource = new LineageMetadataResource();
+        try {
+            resource.getVertices(null, null);
+        } catch(WebApplicationException e) {
+            Assert.assertEquals(e.getResponse().getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
+            Assert.assertEquals(e.getResponse().getEntity().toString(),
+                    "Invalid argument: key or value passed is null or empty.");
+        }
+    }
+
+    @Test
     public void testVertexEdgesForIdAndDirectionOut() throws Exception {
         String processInstance = PROCESS_ENTITY_NAME + "/2014-01-01T01:00Z";
         Vertex vertex = service.getGraph().getVertices(
@@ -265,6 +277,15 @@ public class LineageMetadataResourceTest {
         verifyVertexEdgesCount(vertexId, LineageMetadataResource.BOTH_IDS, expectedSize);
     }
 
+
+
+    @Test (expectedExceptions = WebApplicationException.class)
+    public void testVertexEdgesForIdAndInvalidDirection() throws Exception {
+        LineageMetadataResource resource = new LineageMetadataResource();
+        resource.getVertexEdges("0", "blah");
+        Assert.fail("The API call should have thrown an exception");
+    }
+
     private void verifyVertexEdges(String vertexId, String direction,
                                    int expectedSize, List<String> expected) {
         LineageMetadataResource resource = new LineageMetadataResource();


Mime
View raw message