changeset 578:fab4175d7fec

QlikWebsocketClient
author Devel 1
date Fri, 29 Sep 2017 16:04:03 +0200
parents 08e9d364c9d2
children 100d4bff9f82
files stress-tester-qlik/src/main/java/com/passus/st/qlik/Protocol.java stress-tester-qlik/src/main/java/com/passus/st/qlik/QlikWebsocketClient.java
diffstat 2 files changed, 68 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester-qlik/src/main/java/com/passus/st/qlik/Protocol.java	Fri Sep 29 15:04:33 2017 +0200
+++ b/stress-tester-qlik/src/main/java/com/passus/st/qlik/Protocol.java	Fri Sep 29 16:04:03 2017 +0200
@@ -77,16 +77,40 @@
         return (JSONArray) o;
     }
 
+    static Number number(Object o) {
+        if (!(o instanceof Number)) {
+            throw new IllegalArgumentException();
+        }
+        return (Number) o;
+    }
+
+    static Boolean bool(Object o) {
+        if (!(o instanceof Boolean)) {
+            throw new IllegalArgumentException();
+        }
+        return (Boolean) o;
+    }
+
     public static JSONObject parseObject(String json) {
         return jsonObject(JSONValue.parse(json));
     }
 
     public static int getID(JSONObject root) {
         Object id = root.get(ID);
-        if (!(id instanceof Number)) {
-            throw new IllegalArgumentException();
-        }
-        return ((Number) id).intValue();
+        return number(id).intValue();
+    }
+
+    public static int getHandle(JSONObject root) {
+        JSONObject result = jsonObject(root.get(RESULT));
+        JSONObject retValue = jsonObject(result.get("qReturn"));
+        Object handle = retValue.get("qHandle");
+        return number(handle).intValue();
+    }
+
+    public static boolean getReturnedBoolean(JSONObject root) {
+        JSONObject result = jsonObject(root.get(RESULT));
+        Object retValBool = result.get("qReturn");
+        return bool(retValBool);
     }
 
     static List<DocInfo> parseGetDocListResponse(String json) {
--- a/stress-tester-qlik/src/main/java/com/passus/st/qlik/QlikWebsocketClient.java	Fri Sep 29 15:04:33 2017 +0200
+++ b/stress-tester-qlik/src/main/java/com/passus/st/qlik/QlikWebsocketClient.java	Fri Sep 29 16:04:03 2017 +0200
@@ -1,8 +1,10 @@
 package com.passus.st.qlik;
 
+import com.passus.st.qlik.Protocol.DocInfo;
 import java.io.IOException;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.net.URLEncoder;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -49,10 +51,6 @@
             session.getRemote().sendString(msg);
         }
 
-        public void sendMessageNoFuture(JSONObject obj) throws IOException {
-            sendMessage(obj.toJSONString());
-        }
-
         public Future<JSONObject> sendMessage(JSONObject obj) throws IOException {
             int id = Protocol.getID(obj);
             CompletableFuture<JSONObject> future = new CompletableFuture<>();
@@ -69,8 +67,15 @@
         Session session = future.get();
         return session;
     }
-    
-//    private static DocInfo findByDocName()
+
+    private static String findDocIdByDocName(List<Protocol.DocInfo> infos, String docName) {
+        for (Protocol.DocInfo info : infos) {
+            if (docName.equals(info.getDocName())) {
+                return info.getDocId();
+            }
+        }
+        return null;
+    }
 
     static boolean reload(String host, int port, String docName) throws Exception {
         Protocol proto = new Protocol();
@@ -78,29 +83,37 @@
         WebSocketClient client = new WebSocketClient();
         client.start();
 
-        String url = "ws://" + host + ":" + port + "/app/";
-        
-        try (Session session = openSession(client, url, endpoint)) {
-            Future<JSONObject> responseFuture = endpoint.sendMessage(proto.createGetDocList());
-            JSONObject response = responseFuture.get();
-            List<Protocol.DocInfo> infos = Protocol.parseGetDocListResponse(response);
-            for (Protocol.DocInfo info : infos) {
-                
-            }
-        }
+        try {
+            String docId;
+            String url = "ws://" + host + ":" + port + "/app/";
 
-        // sess = sess(url(host,port))
-        // list = sess.getDocList()
-        // info = findByDocName(list,docName)
-        // sess.close()
-        // sess = sess(url(host,port,info.docId))
-        // ignore = sess.OpenDoc(info.docId)
-        // ignore = sess.DoReload()
-        client.stop();
-        throw new UnsupportedOperationException();
+            try (Session session = openSession(client, url, endpoint)) {
+                Future<JSONObject> getDocListFuture = endpoint.sendMessage(proto.createGetDocList());
+                JSONObject getDocListResponse = getDocListFuture.get();
+                List<DocInfo> infos = Protocol.parseGetDocListResponse(getDocListResponse);
+                docId = findDocIdByDocName(infos, docName);
+            }
+
+            if (docId == null) {
+                return false;
+            }
+
+            String appUrl = url + URLEncoder.encode(docId, "UTF-8");
+            try (Session session = openSession(client, appUrl, endpoint)) {
+                Future<JSONObject> openDocFuture = endpoint.sendMessage(proto.createOpenDoc(docId));
+                JSONObject openDocResponse = openDocFuture.get();
+                int handle = Protocol.getHandle(openDocResponse);
+
+                Future<JSONObject> doReloadFuture = endpoint.sendMessage(proto.createDoReload(handle));
+                JSONObject doReloadResponse = doReloadFuture.get();
+                return Protocol.getReturnedBoolean(doReloadResponse);
+            }
+        } finally {
+            client.stop();
+        }
     }
-    
-    public static Future<Boolean> asyncReload() {
+
+    public static Future<Boolean> asyncReload(String host, int port, String docName) {
         throw new UnsupportedOperationException();
     }
 }