Related to issue 38888: Failing SS Build
authorMartin Taal <martin.taal@openbravo.com>
Mon, 03 Dec 2018 17:12:20 +0100
changeset 7939 5b50ecaeffde
parent 7938 e74b669c41d7
child 7940 602137242a48
Related to issue 38888: Failing SS Build
Do a request to the server to remove batches in error
src-test/org/openbravo/test/mobile/retail/multiserver/MultiserverUtils.java
--- a/src-test/org/openbravo/test/mobile/retail/multiserver/MultiserverUtils.java	Thu Jul 05 14:44:47 2018 -0400
+++ b/src-test/org/openbravo/test/mobile/retail/multiserver/MultiserverUtils.java	Mon Dec 03 17:12:20 2018 +0100
@@ -19,10 +19,17 @@
 
 package org.openbravo.test.mobile.retail.multiserver;
 
+import java.io.OutputStream;
+import java.io.StringWriter;
+import java.net.HttpURLConnection;
+import java.net.URL;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
 
+import org.apache.commons.io.IOUtils;
+import org.json.JSONException;
+import org.json.JSONObject;
 import org.openbravo.test.mobile.core.utils.ConfigurationProperties;
 import org.openbravo.test.mobile.core.utils.OBWait;
 import org.openbravo.test.mobile.core.utils.TestLogger;
@@ -46,6 +53,8 @@
 
   public static void waitForSynchronization(final ServerType storeServerType) {
 
+    removeAllBatchesInError(storeServerType);
+
     // wait for the store to sync
     waitForOutgoingSynced(waitMillis, 10, storeServerType, ServerType.CENTRAL);
     // wait for central to sync
@@ -63,16 +72,7 @@
       return;
     }
 
-    final String toServerKey;
-    if (toServerType.equals(ServerType.STORE1)) {
-      toServerKey = "Store1";
-    } else if (toServerType.equals(ServerType.STORE2)) {
-      toServerKey = "Store2";
-    } else if (toServerType.equals(ServerType.CENTRAL)) {
-      toServerKey = "Central";
-    } else {
-      throw new IllegalStateException("Server type " + toServerType + " not supported here");
-    }
+    final String toServerKey = getServerKey(toServerType);
     
     final String sqlOutgoing = "select COUNT(1) AS count from sym_outgoing_batch where node_id = '"
         + toServerKey + "' and status not in ('OK', 'IG')";
@@ -135,6 +135,18 @@
     };
   }
 
+  private static String getServerKey(ServerType server) {
+    if (server.equals(ServerType.STORE1)) {
+      return "Store1";
+    } else if (server.equals(ServerType.STORE2)) {
+      return "Store2";
+    } else if (server.equals(ServerType.CENTRAL)) {
+      return "Central";
+    } else {
+      throw new IllegalStateException("Server type " + server + " not supported here");
+    }
+  }
+
   public static void activateSynchronizedMode(final ServerType server) {
     TestLogger.getLogger().info("Activating Syncronized mode");
     final String sql = String
@@ -164,4 +176,81 @@
         DatabaseHelperInsertUpdateOrDelete.UNKNOWN_AFFECTED_ROWS, ServerType.CENTRAL);
   }
 
+  public static void removeAllBatchesInError(ServerType server) {
+    removeBatchesInError(server);
+    removeBatchesInError(ServerType.CENTRAL);
+  }
+
+  public static void removeBatchesInError(final ServerType server) {
+    final String serverKey = getServerKey(server);
+    final String sqlLogging = "select * from sym_outgoing_batch where node_id = '" + serverKey
+        + "' and status = ('ER') ORDER BY node_id";
+
+    new DatabaseHelperSelect() {
+      @Override
+      protected void yieldResultSet(final ResultSet rs) throws SQLException {
+        final String batchId = rs.getString("batch_id");
+        final String nodeId = rs.getString("node_id");
+        final JSONObject json = new JSONObject();
+        try {
+          json.put("inpnodeid", nodeId);
+          json.put("inpbatchid", batchId);
+        } catch (JSONException e) {
+          throw new RuntimeException(e);
+        }
+        doKernelActionRequest(server, "org.openbravo.replication.symmetricds.process",
+            json.toString());
+      }
+    }.execute(sqlLogging, 100000, false, server, RecordsCount.LESS_OR_EQUAL_THAN_EXPECTED);
+
+  }
+
+  public static void doKernelActionRequest(ServerType server, String action, String content) {
+    try {
+      final String baseUrl;
+      final String context;
+      if (ServerType.STORE1.equals(server) || ServerType.STORE2.equals(server)) {
+        baseUrl = ConfigurationProperties.INSTANCE.getOpenbravoBaseURL();
+        context = ConfigurationProperties.INSTANCE.getOpenbravoContext();
+      } else {
+        baseUrl = ConfigurationProperties.INSTANCE.getCentralOpenbravoUrl();
+        context = ConfigurationProperties.INSTANCE.getCentralOpenbravoContext();
+      }
+      
+      String wsPart = "/org.openbravo.client.kernel?_action=" + action + "&l=Openbravo&p=openbravo";
+      String method = "POST";
+      String completeUrl = baseUrl + context + "/" + wsPart;
+      TestLogger.getLogger().info("Request to " + server + " " + completeUrl);
+
+      final HttpURLConnection hc = createConnection(completeUrl, method);
+      if (content != null) {
+        final OutputStream os = hc.getOutputStream();
+        os.write(content.getBytes("UTF-8"));
+        os.flush();
+        os.close();
+      }
+      hc.connect();
+
+      final StringWriter writer = new StringWriter();
+      IOUtils.copy(hc.getInputStream(), writer, "utf-8");
+      String retContent = writer.toString();
+      TestLogger.getLogger().info("Request to " + server + " " + action + "-->" + retContent);
+    } catch (Throwable t) {
+      throw new RuntimeException("Request to " + server + " " + action, t);
+    }
+  }
+
+  private static HttpURLConnection createConnection(String url, String method) throws Exception {
+    final URL connUrl = new URL(url);
+    final HttpURLConnection hc = (HttpURLConnection) connUrl.openConnection();
+    hc.setRequestMethod(method);
+    hc.setAllowUserInteraction(false);
+    hc.setDefaultUseCaches(false);
+    hc.setDoOutput(true);
+    hc.setDoInput(true);
+    hc.setInstanceFollowRedirects(true);
+    hc.setUseCaches(false);
+    return hc;
+  }
+
 }