IGNITE-3345 Added human readable error text and negative tests.

Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/7e8aa946
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/7e8aa946
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/7e8aa946

Branch: refs/heads/ignite-3345
Commit: 7e8aa9468d3f81c4d1002a79bdaf5cf94d02f326
Parents: be64ed8
Author: Alexey Kuznetsov <akuznet...@apache.org>
Authored: Mon Feb 19 16:50:09 2018 +0700
Committer: Alexey Kuznetsov <akuznet...@apache.org>
Committed: Mon Feb 19 16:50:09 2018 +0700

----------------------------------------------------------------------
 .../JettyRestProcessorAbstractSelfTest.java     |  99 +++++++++++----
 .../http/jetty/GridJettyRestHandler.java        | 123 ++++++++++---------
 2 files changed, 139 insertions(+), 83 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/7e8aa946/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorAbstractSelfTest.java
----------------------------------------------------------------------
diff --git 
a/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorAbstractSelfTest.java
 
b/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorAbstractSelfTest.java
index 65d8a67..6d39b59 100644
--- 
a/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorAbstractSelfTest.java
+++ 
b/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorAbstractSelfTest.java
@@ -144,6 +144,8 @@ import org.apache.ignite.testframework.GridTestUtils;
 
 import static org.apache.ignite.IgniteSystemProperties.IGNITE_JETTY_PORT;
 import static org.apache.ignite.internal.IgniteVersionUtils.VER_STR;
+import static 
org.apache.ignite.internal.processors.rest.GridRestResponse.STATUS_FAILED;
+import static 
org.apache.ignite.internal.processors.rest.GridRestResponse.STATUS_SUCCESS;
 
 /**
  * Tests for Jetty REST protocol.
@@ -2023,9 +2025,10 @@ public abstract class JettyRestProcessorAbstractSelfTest 
extends AbstractRestPro
      * @param type Key and value type.
      * @param k Key to put.
      * @param v Value to put.
+     * @param status Expected operation status to check.
      * @throws Exception If failed.
      */
-    private void putTypedValue(String type, String k, String v) throws 
Exception {
+    private void putTypedValue(String type, String k, String v, int status) 
throws Exception {
         String ret = content("test_typed_access", GridRestCommand.CACHE_PUT,
             "keyType", type,
             "valueType", type,
@@ -2034,6 +2037,15 @@ public abstract class JettyRestProcessorAbstractSelfTest 
extends AbstractRestPro
         );
 
         info("Command result: " + ret);
+
+        JsonNode json = JSON_MAPPER.readTree(ret);
+
+        assertEquals(status, json.get("successStatus").asInt());
+
+        if (status == STATUS_SUCCESS)
+            assertTrue(json.get("error").isNull());
+        else
+            assertTrue(json.get("error").asText().startsWith("Failed to 
convert value to specified type [type="));
     }
 
     /**
@@ -2041,8 +2053,8 @@ public abstract class JettyRestProcessorAbstractSelfTest 
extends AbstractRestPro
      */
     public void testTypedPut() throws Exception {
         // Test boolean type.
-        putTypedValue("boolean", "true", "false");
-        putTypedValue("java.lang.Boolean", "false", "true");
+        putTypedValue("boolean", "true", "false", STATUS_SUCCESS);
+        putTypedValue("java.lang.Boolean", "false", "true", STATUS_SUCCESS);
 
         IgniteCache<Boolean, Boolean> cBool = typedCache();
 
@@ -2050,8 +2062,11 @@ public abstract class JettyRestProcessorAbstractSelfTest 
extends AbstractRestPro
         assertEquals(cBool.get(false), Boolean.TRUE);
 
         // Test byte type.
-        putTypedValue("byte", "64", "100");
-        putTypedValue("java.lang.Byte", "-25", "-127");
+        putTypedValue("byte", "64", "100", STATUS_SUCCESS);
+        putTypedValue("java.lang.Byte", "-25", "-127", STATUS_SUCCESS);
+        putTypedValue("byte", "65", "aaa", STATUS_FAILED);
+        putTypedValue("byte", "aaa", "64", STATUS_FAILED);
+        putTypedValue("byte", "aaa", "aaa", STATUS_FAILED);
 
         IgniteCache<Byte, Byte> cByte = typedCache();
 
@@ -2059,8 +2074,11 @@ public abstract class JettyRestProcessorAbstractSelfTest 
extends AbstractRestPro
         assertEquals(cByte.get(Byte.valueOf("-25")), Byte.valueOf("-127"));
 
         // Test short type.
-        putTypedValue("short", "1024", "4096");
-        putTypedValue("java.lang.Short", "-15000", "-16000");
+        putTypedValue("short", "1024", "4096", STATUS_SUCCESS);
+        putTypedValue("java.lang.Short", "-15000", "-16000", STATUS_SUCCESS);
+        putTypedValue("short", "1025", "bbb", STATUS_FAILED);
+        putTypedValue("short", "bbb", "5", STATUS_FAILED);
+        putTypedValue("short", "bbb", "bbb", STATUS_FAILED);
 
         IgniteCache<Short, Short> cShort = typedCache();
 
@@ -2068,9 +2086,12 @@ public abstract class JettyRestProcessorAbstractSelfTest 
extends AbstractRestPro
         assertEquals(cShort.get(Short.valueOf("-15000")), 
Short.valueOf("-16000"));
 
         // Test integer type.
-        putTypedValue("int", "65555", "128256");
-        putTypedValue("Integer", "74555", "200000");
-        putTypedValue("java.lang.Integer", "-200", "-100000");
+        putTypedValue("int", "65555", "128256", STATUS_SUCCESS);
+        putTypedValue("Integer", "74555", "200000", STATUS_SUCCESS);
+        putTypedValue("java.lang.Integer", "-200", "-100000", STATUS_SUCCESS);
+        putTypedValue("int", "0", "ccc", STATUS_FAILED);
+        putTypedValue("int", "ccc", "0", STATUS_FAILED);
+        putTypedValue("int", "ccc", "ccc", STATUS_FAILED);
 
         IgniteCache<Integer, Integer> cInt = typedCache();
 
@@ -2079,8 +2100,11 @@ public abstract class JettyRestProcessorAbstractSelfTest 
extends AbstractRestPro
         assertEquals(cInt.get(-200), Integer.valueOf(-100000));
 
         // Test long type.
-        putTypedValue("long", "3000000", "400000");
-        putTypedValue("java.lang.Long", "-3000000", "-400000");
+        putTypedValue("long", "3000000", "400000", STATUS_SUCCESS);
+        putTypedValue("java.lang.Long", "-3000000", "-400000", STATUS_SUCCESS);
+        putTypedValue("long", "777", "ddd", STATUS_FAILED);
+        putTypedValue("long", "ddd", "777", STATUS_FAILED);
+        putTypedValue("long", "ddd", "ddd", STATUS_FAILED);
 
         IgniteCache<Long, Long> cLong = typedCache();
 
@@ -2088,8 +2112,11 @@ public abstract class JettyRestProcessorAbstractSelfTest 
extends AbstractRestPro
         assertEquals(cLong.get(-3000000L), Long.valueOf(-400000));
 
         // Test float type.
-        putTypedValue("float", "1.5", "2.5");
-        putTypedValue("java.lang.Float", "-7.5", "-8.5");
+        putTypedValue("float", "1.5", "2.5", STATUS_SUCCESS);
+        putTypedValue("java.lang.Float", "-7.5", "-8.5", STATUS_SUCCESS);
+        putTypedValue("float", "1.5", "hhh", STATUS_FAILED);
+        putTypedValue("float", "hhh", "1.5", STATUS_FAILED);
+        putTypedValue("float", "hhh", "hhh", STATUS_FAILED);
 
         IgniteCache<Float, Float> cFloat = typedCache();
 
@@ -2097,8 +2124,11 @@ public abstract class JettyRestProcessorAbstractSelfTest 
extends AbstractRestPro
         assertEquals(cFloat.get(-7.5f), -8.5f);
 
         // Test double type.
-        putTypedValue("double", "5.5", "75.5");
-        putTypedValue("java.lang.Double", "-155.5", "-255.5");
+        putTypedValue("double", "5.5", "75.5", STATUS_SUCCESS);
+        putTypedValue("java.lang.Double", "-155.5", "-255.5", STATUS_SUCCESS);
+        putTypedValue("double", "jjj", "75.5", STATUS_FAILED);
+        putTypedValue("double", "6.5", "jjj", STATUS_FAILED);
+        putTypedValue("double", "jjj", "jjj", STATUS_FAILED);
 
         IgniteCache<Double, Double> cDouble = typedCache();
 
@@ -2106,8 +2136,11 @@ public abstract class JettyRestProcessorAbstractSelfTest 
extends AbstractRestPro
         assertEquals(cDouble.get(-155.5d), -255.5d);
 
         // Test date type.
-        putTypedValue("Date", "2018-02-18", "2017-01-01");
-        putTypedValue("java.sql.Date", "2018-01-01", "2017-02-02");
+        putTypedValue("date", "2018-02-18", "2017-01-01", STATUS_SUCCESS);
+        putTypedValue("java.sql.Date", "2018-01-01", "2017-02-02", 
STATUS_SUCCESS);
+        putTypedValue("date", "xxxx-yy-mm", "2017-01-01", STATUS_FAILED);
+        putTypedValue("date", "2018-03-18", "xxxx-yy-mm", STATUS_FAILED);
+        putTypedValue("date", "xxxx-yy-mm", "xxxx-yy-mm", STATUS_FAILED);
 
         IgniteCache<Date, Date> cDate = typedCache();
 
@@ -2115,8 +2148,11 @@ public abstract class JettyRestProcessorAbstractSelfTest 
extends AbstractRestPro
         assertEquals(cDate.get(Date.valueOf("2018-01-01")), 
Date.valueOf("2017-02-02"));
 
         // Test time type.
-        putTypedValue("Time", "01:01:01", "02:02:02");
-        putTypedValue("java.sql.Time", "03:03:03", "04:04:04");
+        putTypedValue("Time", "01:01:01", "02:02:02", STATUS_SUCCESS);
+        putTypedValue("java.sql.Time", "03:03:03", "04:04:04", STATUS_SUCCESS);
+        putTypedValue("Time", "aa:bb:dd", "02:02:02", STATUS_FAILED);
+        putTypedValue("Time", "01:01:01", "zz:vv:pp", STATUS_FAILED);
+        putTypedValue("Time", "zz:zz:zz", "zz:zz:zz", STATUS_FAILED);
 
         IgniteCache<Time, Time> cTime = typedCache();
 
@@ -2124,8 +2160,11 @@ public abstract class JettyRestProcessorAbstractSelfTest 
extends AbstractRestPro
         assertEquals(cTime.get(Time.valueOf("03:03:03")), 
Time.valueOf("04:04:04"));
 
         // Test timestamp type.
-        putTypedValue("Timestamp", "2018-02-18%2001:01:01", 
"2017-01-01%2002:02:02");
-        putTypedValue("java.sql.timestamp", "2018-01-01%2001:01:01", 
"2018-05-05%2005:05:05");
+        putTypedValue("Timestamp", "2018-02-18%2001:01:01", 
"2017-01-01%2002:02:02", STATUS_SUCCESS);
+        putTypedValue("java.sql.timestamp", "2018-01-01%2001:01:01", 
"2018-05-05%2005:05:05", STATUS_SUCCESS);
+        putTypedValue("timestamp", "error", "2018-03-18%2001:01:01", 
STATUS_FAILED);
+        putTypedValue("timestamp", "2018-03-18%2001:01:01", "error", 
STATUS_FAILED);
+        putTypedValue("timestamp", "error", "error", STATUS_FAILED);
 
         IgniteCache<Timestamp, Timestamp> cTimestamp = typedCache();
 
@@ -2135,11 +2174,14 @@ public abstract class 
JettyRestProcessorAbstractSelfTest extends AbstractRestPro
         // Test UUID type.
         UUID k1 = UUID.fromString("121f5ae8-148d-11e8-b642-0ed5f89f718b");
         UUID v1 = UUID.fromString("64c6c225-b31c-4000-b136-ef14562ac785");
-        putTypedValue("UUID", k1.toString(), v1.toString());
+        putTypedValue("UUID", k1.toString(), v1.toString(), STATUS_SUCCESS);
+        putTypedValue("UUID", "error", v1.toString(), STATUS_FAILED);
+        putTypedValue("UUID", k1.toString(), "error", STATUS_FAILED);
+        putTypedValue("UUID", "error", "error", STATUS_FAILED);
 
         UUID k2 = UUID.randomUUID();
         UUID v2 = UUID.randomUUID();
-        putTypedValue("java.util.UUID", k2.toString(), v2.toString());
+        putTypedValue("java.util.UUID", k2.toString(), v2.toString(), 
STATUS_SUCCESS);
 
         IgniteCache<UUID, UUID> cUUID = typedCache();
 
@@ -2149,11 +2191,14 @@ public abstract class 
JettyRestProcessorAbstractSelfTest extends AbstractRestPro
         // Test IgniteUuid type.
         IgniteUuid ik1 = IgniteUuid.randomUuid();
         IgniteUuid iv1 = IgniteUuid.randomUuid();
+        putTypedValue("IgniteUuid", ik1.toString(), iv1.toString(), 
STATUS_SUCCESS);
+        putTypedValue("IgniteUuid", "error", iv1.toString(), STATUS_FAILED);
+        putTypedValue("IgniteUuid", ik1.toString(), "error", STATUS_FAILED);
+        putTypedValue("IgniteUuid", "error", "error", STATUS_FAILED);
+
         IgniteUuid ik2 = IgniteUuid.randomUuid();
         IgniteUuid iv2 = IgniteUuid.randomUuid();
-
-        putTypedValue("IgniteUuid", ik1.toString(), iv1.toString());
-        putTypedValue("org.apache.ignite.lang.IgniteUuid", ik2.toString(), 
iv2.toString());
+        putTypedValue("org.apache.ignite.lang.IgniteUuid", ik2.toString(), 
iv2.toString(), STATUS_SUCCESS);
 
         IgniteCache<IgniteUuid, IgniteUuid> cIgniteUUID = typedCache();
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/7e8aa946/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java
----------------------------------------------------------------------
diff --git 
a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java
 
b/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java
index f09a818..bc5782c 100644
--- 
a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java
+++ 
b/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java
@@ -36,7 +36,6 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
-import javax.servlet.ServletException;
 import javax.servlet.ServletOutputStream;
 import javax.servlet.ServletRequest;
 import javax.servlet.http.HttpServletRequest;
@@ -265,7 +264,7 @@ public class GridJettyRestHandler extends AbstractHandler {
 
     /** {@inheritDoc} */
     @Override public void handle(String target, Request req, 
HttpServletRequest srvReq, HttpServletResponse res)
-        throws IOException, ServletException {
+        throws IOException {
         if (log.isDebugEnabled())
             log.debug("Handling request [target=" + target + ", req=" + req + 
", srvReq=" + srvReq + ']');
 
@@ -390,64 +389,76 @@ public class GridJettyRestHandler extends AbstractHandler 
{
         }
     }
 
-    private Object convert(String type, Object obj) {
+    /**
+     * @param type Optional value type.
+     * @param obj Object to convert.
+     * @return Converted value.
+     * @throws IgniteCheckedException If failed to convert.
+     */
+    private Object convert(String type, Object obj) throws 
IgniteCheckedException {
         if (F.isEmpty(type) || obj == null)
             return obj;
 
         String s = (String)obj;
 
-        switch (type.toLowerCase()) {
-            case "boolean":
-            case "java.lang.boolean":
-                return Boolean.valueOf(s);
-
-            case "byte":
-            case "java.lang.byte":
-                return Byte.valueOf(s);
-
-            case "short":
-            case "java.lang.short":
-                return Short.valueOf(s);
-
-            case "int":
-            case "integer":
-            case "java.lang.integer":
-                return Integer.valueOf(s);
-
-            case "long":
-            case "java.lang.long":
-                return Long.valueOf(s);
-
-            case "float":
-            case "java.lang.float":
-                return Float.valueOf(s);
-
-            case "double":
-            case "java.lang.double":
-                return Double.valueOf(s);
-
-            case "date":
-            case "java.sql.date":
-                return Date.valueOf(s);
-
-            case "time":
-            case "java.sql.time":
-                return Time.valueOf(s);
-
-            case "timestamp":
-            case "java.sql.timestamp":
-                return Timestamp.valueOf(s);
-
-            case "uuid":
-            case "java.util.uuid":
-                return UUID.fromString(s);
-
-            case "igniteuuid":
-            case "org.apache.ignite.lang.igniteuuid":
-                return IgniteUuid.fromString(s);
-
-            default:
-                // No-op.
+        try {
+            switch (type.toLowerCase()) {
+                case "boolean":
+                case "java.lang.boolean":
+                    return Boolean.valueOf(s);
+
+                case "byte":
+                case "java.lang.byte":
+                    return Byte.valueOf(s);
+
+                case "short":
+                case "java.lang.short":
+                    return Short.valueOf(s);
+
+                case "int":
+                case "integer":
+                case "java.lang.integer":
+                    return Integer.valueOf(s);
+
+                case "long":
+                case "java.lang.long":
+                    return Long.valueOf(s);
+
+                case "float":
+                case "java.lang.float":
+                    return Float.valueOf(s);
+
+                case "double":
+                case "java.lang.double":
+                    return Double.valueOf(s);
+
+                case "date":
+                case "java.sql.date":
+                    return Date.valueOf(s);
+
+                case "time":
+                case "java.sql.time":
+                    return Time.valueOf(s);
+
+                case "timestamp":
+                case "java.sql.timestamp":
+                    return Timestamp.valueOf(s);
+
+                case "uuid":
+                case "java.util.uuid":
+                    return UUID.fromString(s);
+
+                case "igniteuuid":
+                case "org.apache.ignite.lang.igniteuuid":
+                    return IgniteUuid.fromString(s);
+
+                default:
+                    // No-op.
+            }
+        }
+        catch (Throwable e) {
+            throw new IgniteCheckedException("Failed to convert value to 
specified type [type=" + type +
+                ", val=" + s + ", reason=" + e.getClass().getName() + ": " + 
e.getMessage() + "]");
         }
 
         return obj;
@@ -775,7 +786,7 @@ public class GridJettyRestHandler extends AbstractHandler {
      * @param params Parameters map.
      * @return Values.
      */
-    protected List<Object> values(String type, String keyPrefix, Map<String, 
Object> params) {
+    protected List<Object> values(String type, String keyPrefix, Map<String, 
Object> params) throws IgniteCheckedException {
         assert keyPrefix != null;
 
         List<Object> vals = new LinkedList<>();

Reply via email to