#ignite-961: add test for get rest protocol.
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/b6c68eb5 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/b6c68eb5 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/b6c68eb5 Branch: refs/heads/ignite-1121 Commit: b6c68eb560d91127e27648d91f4bd2709f0bd4fd Parents: a7f664e Author: ivasilinets <ivasilin...@gridgain.com> Authored: Fri Jul 17 16:09:15 2015 +0300 Committer: ivasilinets <ivasilin...@gridgain.com> Committed: Fri Jul 17 16:09:15 2015 +0300 ---------------------------------------------------------------------- .../JettyRestProcessorAbstractSelfTest.java | 251 ++++++++++++++++++- .../http/jetty/GridJettyRestHandler.java | 34 ++- 2 files changed, 269 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b6c68eb5/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 e1425ff..38bfef5 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 @@ -477,7 +477,164 @@ abstract class JettyRestProcessorAbstractSelfTest extends AbstractRestProcessorS /** * @throws Exception If failed. */ + public void testContainesKey() throws Exception { + grid(0).cache(null).put("key0", "val0"); + + String ret = content(F.asMap("cmd", "containskey", "key", "key0")); + + assertNotNull(ret); + assertTrue(!ret.isEmpty()); + + jsonEquals(ret, cachePattern(true, true)); + } + + /** + * @throws Exception If failed. + */ + public void testContainesKeys() throws Exception { + grid(0).cache(null).put("key0", "val0"); + grid(0).cache(null).put("key1", "val1"); + + String ret = content(F.asMap("cmd", "containskeys", "k1", "key0", "k2", "key1")); + + assertNotNull(ret); + assertTrue(!ret.isEmpty()); + + jsonEquals(ret, cacheBulkPattern(true, true)); + } + + /** + * @throws Exception If failed. + */ public void testGetAndPut() throws Exception { + grid(0).cache(null).put("key0", "val0"); + + String ret = content(F.asMap("cmd", "getandput", "key", "key0", "val", "val1")); + + assertNotNull(ret); + assertTrue(!ret.isEmpty()); + + jsonEquals(ret, cachePattern("val0", true)); + + assertEquals("val1", grid(0).cache(null).get("key0")); + } + + /** + * @throws Exception If failed. + */ + public void testGetAndPutIfAbsent() throws Exception { + grid(0).cache(null).put("key0", "val0"); + + String ret = content(F.asMap("cmd", "getandputifabsent", "key", "key0", "val", "val1")); + + assertNotNull(ret); + assertTrue(!ret.isEmpty()); + + jsonEquals(ret, cachePattern("val0", true)); + + assertEquals("val0", grid(0).cache(null).get("key0")); + } + + /** + * @throws Exception If failed. + */ + public void testPutIfAbsent2() throws Exception { + String ret = content(F.asMap("cmd", "putifabsent", "key", "key0", "val", "val1")); + + assertNotNull(ret); + assertTrue(!ret.isEmpty()); + + jsonEquals(ret, cachePattern(true, true)); + + assertEquals("val1", grid(0).cache(null).get("key0")); + } + + /** + * @throws Exception If failed. + */ + public void testRemoveValue() throws Exception { + grid(0).cache(null).put("key0", "val0"); + + String ret = content(F.asMap("cmd", "rmvvalue", "key", "key0", "val", "val1")); + + assertNotNull(ret); + assertTrue(!ret.isEmpty()); + + jsonEquals(ret, cachePattern(false, true)); + + assertEquals("val0", grid(0).cache(null).get("key0")); + + ret = content(F.asMap("cmd", "rmvvalue", "key", "key0", "val", "val0")); + + assertNotNull(ret); + assertTrue(!ret.isEmpty()); + + jsonEquals(ret, cachePattern(true, true)); + + assertNull(grid(0).cache(null).get("key0")); + } + + /** + * @throws Exception If failed. + */ + public void testGetAndRemove() throws Exception { + grid(0).cache(null).put("key0", "val0"); + + String ret = content(F.asMap("cmd", "getandrmv", "key", "key0")); + + assertNotNull(ret); + assertTrue(!ret.isEmpty()); + + jsonEquals(ret, cachePattern("val0", true)); + + assertNull(grid(0).cache(null).get("key0")); + } + + /** + * @throws Exception If failed. + */ + public void testReplaceValue() throws Exception { + grid(0).cache(null).put("key0", "val0"); + + String ret = content(F.asMap("cmd", "repval", "key", "key0", "val", "val1", "val2", "val2")); + + assertNotNull(ret); + assertTrue(!ret.isEmpty()); + + jsonEquals(ret, cachePattern(false, true)); + + assertEquals("val0", grid(0).cache(null).get("key0")); + + ret = content(F.asMap("cmd", "repval", "key", "key0", "val", "val1", "val2", "val0")); + + assertNotNull(ret); + assertTrue(!ret.isEmpty()); + + jsonEquals(ret, cachePattern(true, true)); + + assertEquals("val1", grid(0).cache(null).get("key0")); + } + + /** + * @throws Exception If failed. + */ + public void testGetAndReplace() throws Exception { + grid(0).cache(null).put("key0", "val0"); + + String ret = content(F.asMap("cmd", "getandreplace", "key", "key0", "val", "val1")); + + assertNotNull(ret); + assertTrue(!ret.isEmpty()); + + jsonEquals(ret, cachePattern("val0", true)); + + assertEquals("val1", grid(0).cache(null).get("key0")); + } + + /** + * @throws Exception If failed. + */ + public void testGetAndPutPost() throws Exception { String val = "{\"key\":\"key0\", \"val\":\"val0\"}"; String ret = makePostRequest(F.asMap("cmd", "getandput"), val); @@ -1054,26 +1211,63 @@ abstract class JettyRestProcessorAbstractSelfTest extends AbstractRestProcessorS /** * @throws Exception If failed. */ + public void testRunScriptPost() throws Exception { + String f = "function(param){return param;}"; + String ret = makePostRequest(F.asMap("cmd", "runscript", "func", URLEncoder.encode(f)), "{\"arg\":\"hello\"}"); + + assertNotNull(ret); + assertTrue(!ret.isEmpty()); + + jsonEquals(ret, stringPattern("hello", true)); + } + + /** + * @throws Exception If failed. + */ public void testRunScript() throws Exception { - String f = "function(){return ignite.name();}"; - String ret = makePostRequest(F.asMap("cmd", "runscript", "func", URLEncoder.encode(f)), "{\"arg\":[]}"); + String f = "function(param){return param;}"; + String ret = content(F.asMap("cmd", "runscript", "func", URLEncoder.encode(f), "arg", "hello")); assertNotNull(ret); assertTrue(!ret.isEmpty()); - jsonEquals(ret, stringPattern(getTestGridName(1), true)); + jsonEquals(ret, stringPattern("hello", true)); } /** * @throws Exception If failed. */ - public void testRunAffinityScript() throws Exception { - String f = "function(){return ignite.name();}"; - String ret = makePostRequest(F.asMap("cmd", "affrun", "func", URLEncoder.encode(f)), "{\"arg\":[],\"key\":\"key0\"}"); + public void testRunAffinityScriptPost() throws Exception { + ClusterNode node = grid(0).affinity(null).mapKeyToNode("key0"); + + Ignite ignite = null; + + for (int i = 0; i < GRID_CNT; ++i) { + if (grid(i).localNode().equals(node)) + ignite = grid(i); + } + + assertNotNull(ignite); + + String f = "function(expName){"+ + "if (expName.toString() !== ignite.name().toString()) {" + + "throw \"Not correct name.\"" + + "}" + + "return ignite.name();}"; + + String ret = makePostRequest(F.asMap("cmd", "affrun", "func", URLEncoder.encode(f)), + "{\"arg\":\"" + ignite.name() + "\",\"key\":\"key0\"}"); assertNotNull(ret); assertTrue(!ret.isEmpty()); + jsonEquals(ret, stringPattern(ignite.name(), true)); + } + + /** + * @throws Exception If failed. + */ + public void testRunAffinityScript() throws Exception { ClusterNode node = grid(0).affinity(null).mapKeyToNode("key0"); Ignite ignite = null; @@ -1085,13 +1279,25 @@ abstract class JettyRestProcessorAbstractSelfTest extends AbstractRestProcessorS assertNotNull(ignite); + String f = "function(expName){"+ + "if (expName != ignite.name()) {" + + "throw \"Not correct name.\"" + + "}" + + "return ignite.name();}"; + + String ret = content(F.asMap("cmd", "affrun", "func", URLEncoder.encode(f), + "key", "key0", "arg", ignite.name())); + + assertNotNull(ret); + assertTrue(!ret.isEmpty()); + jsonEquals(ret, stringPattern(ignite.name(), true)); } /** * @throws Exception If failed. */ - public void testMapReduceScript() throws Exception { + public void testMapReduceScriptPost() throws Exception { String map = "function(nodes, arg) {" + "var words = arg.split(\" \");" + "for (var i = 0; i < words.length; i++) {" + @@ -1122,6 +1328,37 @@ abstract class JettyRestProcessorAbstractSelfTest extends AbstractRestProcessorS /** * @throws Exception If failed. */ + public void testMapReduceScript() throws Exception { + String map = "function(nodes, arg) {" + + "var words = arg.split(\" \");" + + "for (var i = 0; i < words.length; i++) {" + + "var f = function(word) {" + + "return word.length;" + + "};" + + "emit(f, words[i], nodes[i % nodes.length]);" + + "}"+ + "};"; + + String reduce = "function(results) {"+ + "var sum = 0;"+ + "for (var i = 0; i < results.size(); ++i) {"+ + "sum += results.get(i).intValue();"+ + "}" + + "return sum;" + + "};"; + + String ret = content(F.asMap("cmd", "excmapreduce", "map", URLEncoder.encode(map), + "reduce", URLEncoder.encode(reduce), "arg", URLEncoder.encode("Hello world!"))); + + assertNotNull(ret); + assertTrue(!ret.isEmpty()); + + jsonEquals(ret, integerPattern(11, true)); + } + + /** + * @throws Exception If failed. + */ public void testQuery() throws Exception { grid(0).cache(null).put("1", "1"); grid(0).cache(null).put("2", "2"); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b6c68eb5/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 d0ae096..1acab51 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 @@ -496,7 +496,8 @@ public class GridJettyRestHandler extends AbstractHandler { restReq0.cacheFlags(intValue("cacheFlags", params, 0)); restReq0.ttl(longValue("exp", params, null)); - if (cmd == CACHE_GET_ALL || cmd == CACHE_PUT_ALL || cmd == CACHE_REMOVE_ALL) { + if (cmd == CACHE_GET_ALL || cmd == CACHE_PUT_ALL || cmd == CACHE_REMOVE_ALL || + cmd == CACHE_CONTAINS_KEYS) { List<Object> keys = values("k", params); List<Object> vals = values("v", params); @@ -580,8 +581,12 @@ public class GridJettyRestHandler extends AbstractHandler { restReq0.script((String)params.get("func")); - Map o = parseRequest(req); - restReq0.argument(ctx.scripting().toJavaObject(o.get("arg"))); + if (req.getHeader("Content-Type") != null && req.getHeader("Content-Type").contains("json")) { + Map o = parseRequest(req); + restReq0.argument(ctx.scripting().toJavaObject(o.get("arg"))); + } + else + restReq0.argument(params.get("arg")); restReq = restReq0; @@ -594,11 +599,17 @@ public class GridJettyRestHandler extends AbstractHandler { restReq0.script((String)params.get("func")); restReq0.cacheName((String) params.get("cacheName")); - Map o = parseRequest(req); - restReq0.argument(ctx.scripting().toJavaObject(o.get("arg"))); + if (req.getHeader("Content-Type") != null && req.getHeader("Content-Type").contains("json")) { + Map o = parseRequest(req); + restReq0.argument(ctx.scripting().toJavaObject(o.get("arg"))); - Object cacheObj = ctx.scripting().toJavaObject(o.get("key")); - restReq0.affinityKey(cacheObj); + Object cacheObj = ctx.scripting().toJavaObject(o.get("key")); + restReq0.affinityKey(cacheObj); + } + else { + restReq0.argument(params.get("arg")); + restReq0.affinityKey(params.get("key")); + } restReq = restReq0; @@ -610,8 +621,13 @@ public class GridJettyRestHandler extends AbstractHandler { restReq0.mapFunction((String) params.get("map")); - Map o = parseRequest(req); - restReq0.argument(ctx.scripting().toJavaObject(o.get("arg"))); + + if (req.getHeader("Content-Type") != null && req.getHeader("Content-Type").contains("json")) { + Map o = parseRequest(req); + restReq0.argument(ctx.scripting().toJavaObject(o.get("arg"))); + } + else + restReq0.argument(params.get("arg")); restReq0.reduceFunction((String) params.get("reduce"));