ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anovi...@apache.org
Subject ignite git commit: IGNITE-3277 Fixed tests.
Date Fri, 17 Jun 2016 05:00:53 GMT
Repository: ignite
Updated Branches:
  refs/heads/ignite-3277 73689ae5d -> 14eed58b1


IGNITE-3277 Fixed tests.


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

Branch: refs/heads/ignite-3277
Commit: 14eed58b16ec615328a901076e2b783864ec85bf
Parents: 73689ae
Author: Andrey Novikov <anovikov@apache.org>
Authored: Fri Jun 17 10:57:19 2016 +0700
Committer: Andrey Novikov <anovikov@apache.org>
Committed: Fri Jun 17 10:57:19 2016 +0700

----------------------------------------------------------------------
 .../client/ClientDefaultCacheSelfTest.java      | 115 ++++++++++++-------
 .../JettyRestProcessorAbstractSelfTest.java     |  12 +-
 .../http/jetty/GridJettyRestHandler.java        |  10 +-
 3 files changed, 86 insertions(+), 51 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/14eed58b/modules/clients/src/test/java/org/apache/ignite/internal/client/ClientDefaultCacheSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/clients/src/test/java/org/apache/ignite/internal/client/ClientDefaultCacheSelfTest.java
b/modules/clients/src/test/java/org/apache/ignite/internal/client/ClientDefaultCacheSelfTest.java
index f910b7d..6697ea6 100644
--- a/modules/clients/src/test/java/org/apache/ignite/internal/client/ClientDefaultCacheSelfTest.java
+++ b/modules/clients/src/test/java/org/apache/ignite/internal/client/ClientDefaultCacheSelfTest.java
@@ -17,20 +17,28 @@
 
 package org.apache.ignite.internal.client;
 
-import java.io.BufferedReader;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.io.LineNumberReader;
 import java.net.URL;
 import java.net.URLConnection;
+import java.net.URLEncoder;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Map;
 import java.util.UUID;
 import org.apache.ignite.cache.CacheAtomicityMode;
 import org.apache.ignite.cache.CacheMode;
 import org.apache.ignite.configuration.CacheConfiguration;
 import org.apache.ignite.configuration.ConnectorConfiguration;
 import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.processors.rest.GridRestCommand;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.SB;
 import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
 import org.apache.ignite.spi.discovery.tcp.ipfinder.TcpDiscoveryIpFinder;
 import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
@@ -58,7 +66,7 @@ public class ClientDefaultCacheSelfTest extends GridCommonAbstractTest {
     private static final int HTTP_PORT = 8081;
 
     /** Url address to send HTTP request. */
-    private static final String TEST_URL = "http://" + HOST + ":" + HTTP_PORT + "/ignite";
+    private static final String TEST_URL = "http://" + HOST + ":" + HTTP_PORT + "/ignite?";
 
     /** Used to sent request charset. */
     private static final String CHARSET = StandardCharsets.UTF_8.name();
@@ -66,6 +74,9 @@ public class ClientDefaultCacheSelfTest extends GridCommonAbstractTest {
     /** Name of node local cache. */
     private static final String LOCAL_CACHE = "local";
 
+    /** JSON to java mapper. */
+    private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
+
     /** {@inheritDoc} */
     @Override protected void beforeTestsStarted() throws Exception {
         System.setProperty(IGNITE_JETTY_PORT, String.valueOf(HTTP_PORT));
@@ -103,77 +114,95 @@ public class ClientDefaultCacheSelfTest extends GridCommonAbstractTest
{
 
         cfg.setDiscoverySpi(disco);
 
-        CacheConfiguration cLocal = new CacheConfiguration();
+        CacheConfiguration cLoc = new CacheConfiguration();
 
-        cLocal.setName(LOCAL_CACHE);
+        cLoc.setName(LOCAL_CACHE);
 
-        cLocal.setCacheMode(CacheMode.LOCAL);
+        cLoc.setCacheMode(CacheMode.LOCAL);
 
-        cLocal.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
+        cLoc.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
 
-        cfg.setCacheConfiguration(defaultCacheConfiguration(), cLocal);
+        cfg.setCacheConfiguration(defaultCacheConfiguration(), cLoc);
 
         return cfg;
     }
 
     /**
-     * Builds list of connection strings with few different ports.
-     * Used to avoid possible failures in case of port range active.
-     *
-     * @param startPort Port to start list from.
-     * @return List of client connection strings.
-     */
-    private Collection<String> getServerList(int startPort) {
-        Collection<String> srvs = new ArrayList<>();
-
-        for (int i = startPort; i < startPort + 10; i++)
-            srvs.add(HOST + ":" + i);
-
-        return srvs;
-    }
-
-    /*
      * Send HTTP request to Jetty server of node and process result.
      *
-     * @param query Send query parameters.
+     * @param params Command parameters.
      * @return Processed response string.
+     * @throws IOException If failed.
      */
-    private String sendHttp(String query) {
-        String res = "No result";
+    private String content(Map<String, String> params) throws IOException {
+        SB sb = new SB(TEST_URL);
+
+        for (Map.Entry<String, String> e : params.entrySet())
+            sb.a(e.getKey()).a('=').a(e.getValue()).a('&');
+
+        String qry = sb.toString();
 
         try {
-            URLConnection connection = new URL(TEST_URL + "?" + query).openConnection();
+            URL url = new URL(qry);
 
-            connection.setRequestProperty("Accept-Charset", CHARSET);
+            URLConnection conn = url.openConnection();
 
-            BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+            conn.setRequestProperty("Accept-Charset", CHARSET);
 
-            res = r.readLine();
+            InputStream in = conn.getInputStream();
 
-            r.close();
+            StringBuilder buf = new StringBuilder(256);
+
+            try (LineNumberReader rdr = new LineNumberReader(new InputStreamReader(in, "UTF-8")))
{
+                for (String line = rdr.readLine(); line != null; line = rdr.readLine())
+                    buf.append(line);
+            }
+
+            return buf.toString();
         }
         catch (IOException e) {
-            error("Failed to send HTTP request: " + TEST_URL + "?" + query, e);
+            error("Failed to send HTTP request: " + TEST_URL + "?" + qry, e);
+
+            throw e;
         }
+    }
+
+    /**
+     * @param content Content to check.
+     */
+    private JsonNode jsonResponse(String content) throws IOException {
+        assertNotNull(content);
+        assertFalse(content.isEmpty());
+
+        JsonNode node = JSON_MAPPER.readTree(content);
+
+        assertFalse(node.get("affinityNodeId").asText().isEmpty());
+        assertEquals(0, node.get("successStatus").asInt());
+        assertTrue(node.get("error").asText().isEmpty());
+        assertTrue(node.get("sessionToken").asText().isEmpty());
 
-        // Cut node id from response.
-        return res.substring(res.indexOf("\"response\""));
+        return node.get("response");
     }
 
     /**
      * Json format string in cache should not transform to Json object on get request.
      */
-    public void testSkipString2JsonTransformation() {
+    public void testSkipString2JsonTransformation() throws Exception {
+        String val = "{\"v\":\"my Value\",\"t\":1422559650154}";
+
         // Put to cache JSON format string value.
-        assertEquals("Incorrect query response", "\"response\":true,\"sessionToken\":\"\",\"successStatus\":0}",
-            sendHttp("cmd=put&cacheName=" + LOCAL_CACHE +
-                "&key=a&val=%7B%22v%22%3A%22my%20Value%22%2C%22t%22%3A1422559650154%7D"));
+        String ret = content(F.asMap("cmd", GridRestCommand.CACHE_PUT.key(), "cacheName",
LOCAL_CACHE,
+            "key", "a", "val", URLEncoder.encode(val, CHARSET)));
+
+        JsonNode res = jsonResponse(ret);
+
+        assertEquals("Incorrect put response", true, res.asBoolean());
 
         // Escape '\' symbols disappear from response string on transformation to JSON object.
-        assertEquals(
-            "Incorrect query response",
-            "\"response\":\"{\\\"v\\\":\\\"my Value\\\",\\\"t\\\":1422559650154}\"," +
-                "\"sessionToken\":\"\",\"successStatus\":0}",
-            sendHttp("cmd=get&cacheName=" + LOCAL_CACHE + "&key=a"));
+        ret = content(F.asMap("cmd", GridRestCommand.CACHE_GET.key(), "cacheName", LOCAL_CACHE,
"key", "a"));
+
+        res = jsonResponse(ret);
+
+        assertEquals("Incorrect get response", val, res.asText());
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/14eed58b/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 66d74b8..81b49f9 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
@@ -26,6 +26,7 @@ import java.io.UnsupportedEncodingException;
 import java.net.URL;
 import java.net.URLConnection;
 import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -113,8 +114,11 @@ public abstract class JettyRestProcessorAbstractSelfTest extends AbstractRestPro
     /** Grid count. */
     private static final int GRID_CNT = 3;
 
-    /** Default encoding. */
-    private static final String CHARSET = "UTF-8";
+    /** Url address to send HTTP request. */
+    private final String TEST_URL = "http://" + LOC_HOST + ":" + restPort() + "/ignite?";
+
+    /** Used to sent request charset. */
+    private static final String CHARSET = StandardCharsets.UTF_8.name();
 
     /** JSON to java mapper. */
     private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
@@ -164,9 +168,7 @@ public abstract class JettyRestProcessorAbstractSelfTest extends AbstractRestPro
      * @throws Exception If failed.
      */
     protected String content(Map<String, String> params) throws Exception {
-        SB sb = new SB();
-
-        sb.a("http://").a(LOC_HOST).a(":").a(restPort()).a("/ignite?");
+        SB sb = new SB(TEST_URL);
 
         for (Map.Entry<String, String> e : params.entrySet())
             sb.a(e.getKey()).a('=').a(e.getValue()).a('&');

http://git-wip-us.apache.org/repos/asf/ignite/blob/14eed58b/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 ac53fb0..fcbfd6d 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
@@ -42,6 +42,7 @@ import java.io.InputStreamReader;
 import java.io.LineNumberReader;
 import java.lang.reflect.Method;
 import java.net.InetSocketAddress;
+import java.nio.charset.StandardCharsets;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -101,6 +102,9 @@ import static org.apache.ignite.internal.processors.rest.GridRestResponse.STATUS
  * {@code /ignite?cmd=cmdName&param1=abc&param2=123}
  */
 public class GridJettyRestHandler extends AbstractHandler {
+    /** Used to sent request charset. */
+    private static final String CHARSET = StandardCharsets.UTF_8.name();
+
     /** Logger. */
     private final IgniteLogger log;
     /** Authentication checker. */
@@ -223,7 +227,7 @@ public class GridJettyRestHandler extends AbstractHandler {
         InputStream in = getClass().getResourceAsStream("rest.html");
 
         if (in != null) {
-            LineNumberReader rdr = new LineNumberReader(new InputStreamReader(in, "UTF-8"));
+            LineNumberReader rdr = new LineNumberReader(new InputStreamReader(in, CHARSET));
 
             try {
                 StringBuilder buf = new StringBuilder(2048);
@@ -232,7 +236,7 @@ public class GridJettyRestHandler extends AbstractHandler {
                     buf.append(line);
 
                     if (!line.endsWith(" "))
-                        buf.append(" ");
+                        buf.append(' ');
                 }
 
                 dfltPage = buf.toString();
@@ -500,7 +504,7 @@ public class GridJettyRestHandler extends AbstractHandler {
                 try {
                     PropertyDescriptor[] props = Introspector.getBeanInfo(beanCls).getPropertyDescriptors();
 
-                    if (props == null || (props.length == 1 && props[0].getName().equals("class")))
+                    if (props == null || (props.length == 1 && "class".equals(props[0].getName())))
                         return LESS_NAMING_SERIALIZER;
                 }
                 catch (IntrospectionException ignore) {


Mime
View raw message