From 30f32fb4d470eaabff3428ace910fc740abf80c7 Mon Sep 17 00:00:00 2001
From: Ivan Olexyn <ivan@olexyn.com>
Date: Tue, 21 Apr 2020 20:34:33 +0200
Subject: [PATCH] + separate the request from the preparation which can be
 shared between mock and misp.

---
 mispclient/src/core/ClientServlet.java | 123 ++++++++++++++++---------
 mispmock/src/core/ClientMock.java      |  53 ++++-------
 2 files changed, 99 insertions(+), 77 deletions(-)

diff --git a/mispclient/src/core/ClientServlet.java b/mispclient/src/core/ClientServlet.java
index 69d2662..8b155e2 100644
--- a/mispclient/src/core/ClientServlet.java
+++ b/mispclient/src/core/ClientServlet.java
@@ -14,8 +14,8 @@ public class ClientServlet extends HttpServlet {
     protected static final String MISP_BRIDGE_URL = "http://localhost:9090/mispbridge/core";
     protected static final String APP_URL = "http://localhost:9090";
 
-    public static final int AVAILABLE_RIDES_OVERHEAD_TRIGGER = 16;
-    public static final int AVAILABLE_RIDES_OVERHEAD = 32;
+    public static final int AVAILABLE_RIDES_OVERHEAD_TRIGGER = 32;
+    public static final int AVAILABLE_RIDES_OVERHEAD = 64;
 
 
     public final Map<Long, Ride> available = new HashMap<>();
@@ -33,77 +33,116 @@ public class ClientServlet extends HttpServlet {
 
 
     /**
-     * # send POST (Ride)
-     * Generated by Loop
+     * Generated by Loop.
+     * Prepare payload for the request.
+     * Process the parsed response.
      */
-    void sendPostRide(Ride ride) throws IOException, ServletException, InterruptedException {
+    final void sendPostRide() throws IOException, ServletException, InterruptedException {
 
-        HttpURLConnection connection = ConnectionHelper.make("POST", MISP_BRIDGE_URL);
+        final Ride ride = new Ride();
 
+        synchronized (available) {
+            available.put(ride.getID(), ride);
+        }
+
+        final Ride parsedRide = doSendPostRide(ride);
+
+        synchronized (available) {
+            available.remove(ride.getID());
+            ride.setRequest(parsedRide.getRequest());
+        }
+
+        synchronized (booked) {
+            booked.put(ride.getID(), ride);
+        }
+        sendGetRequest(ride);
+    }
+
+
+    /**
+     * Send POST (Ride).
+     * Parse response.
+     */
+    protected Ride doSendPostRide(Ride ride) throws IOException, ServletException, InterruptedException {
         // send POST (Ride)
-        available.put(ride.getID(), ride);
+        final HttpURLConnection connection = ConnectionHelper.make("POST", MISP_BRIDGE_URL);
+
         connection.setDoOutput(true);
         DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
         outputStream.writeBytes(ride.json());
         outputStream.flush();
         outputStream.close();
 
-        // handle OK (Ride)
-        // remove Ride from AvailableRides
-        // add Ride to ReservedRides
-        if (connection.getResponseCode() == 200) {
-            Ride parsedRide = ConnectionHelper.parseRide(connection);
-            if (parsedRide.equals(ride)) {
-                ride.setState(State.BOOKED);
-            }
-        }
+        return ConnectionHelper.parseRide(connection);
     }
 
 
     /**
-     * # send GET (Request) to App
+     * Prepare payload for the request.
+     * Process the parsed response.
      */
-    void sendGetRequest(Ride ride) throws IOException, ServletException, InterruptedException {
-
-        // send FOO
-        // TODO make sure as many as possible tyes of requests can be forwarded.
-
-        // handle OK (Data)
-        // remove Ride from PendingRequests
-        // add Ride to PendingData
-        // send GET (Ride)(Data)
-        String data = "DATA";
-        ride.setData(data);
-        ride.setState(State.LOADED);
-        sendGetRideRequestData(ride);
+    final void sendGetRequest(Ride ride) throws IOException, ServletException, InterruptedException {
+
+        Ride parsedRide = doSendGetRequest(ride);
+
+        ride.setData(parsedRide.getData());
+
+        synchronized (booked) {
+            booked.remove(ride.getID());
+        }
+        synchronized (loaded) {
+            loaded.put(ride.getID(), ride);
+        }
 
+        sendGetRideRequestData(ride);
     }
 
 
     /**
-     * # send GET (Ride)(Request)(Data)
+     * Send GET (Request) to App.
+     * Parse response.
      */
-    void sendGetRideRequestData(Ride oldRide) throws IOException, ServletException, InterruptedException {
+    protected Ride doSendGetRequest(Ride ride) throws IOException, InterruptedException {
 
-        HttpURLConnection connection = ConnectionHelper.make("GET", MISP_BRIDGE_URL);
+        // send GET (Ride)
+        final HttpURLConnection connection = ConnectionHelper.make("GET", APP_URL);
 
-        // send GET (Ride)(Request)(Data)
         connection.setDoOutput(true);
         DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
-        outputStream.writeBytes(oldRide.json());
+        outputStream.writeBytes(ride.getRequest());
         outputStream.flush();
         outputStream.close();
 
-        // handle OK (RIDE)
-        if (connection.getResponseCode() == 200) {
-            Ride shellIdRide = ConnectionHelper.parseRide(connection);
-            if (shellIdRide.getID() != null) {
-                loaded.remove(oldRide.getID());
-            }
+        return ConnectionHelper.parseRide(connection);
+    }
+
+
+    /**
+     * Prepare payload for the request.
+     * Process the parsed response.
+     */
+    final protected void sendGetRideRequestData(Ride ride) throws IOException, InterruptedException {
+        doSendGetRideRequest(ride);
+        synchronized (loaded) {
+            loaded.remove(ride.getID());
         }
     }
 
 
+    /**
+     * Send GET (Ride)(Request)(Data).
+     * Parse response.
+     */
+    protected void doSendGetRideRequest(Ride ride) throws IOException, InterruptedException {
+
+        HttpURLConnection connection = ConnectionHelper.make("GET", MISP_BRIDGE_URL);
+
+        connection.setDoOutput(true);
+        DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
+        outputStream.writeBytes(ride.json());
+        outputStream.flush();
+        outputStream.close();
+    }
 }
 
 
@@ -125,7 +164,7 @@ class PostRideRunnable implements Runnable {
         while (true) {
             if (clientServlet.available.size() < ClientServlet.AVAILABLE_RIDES_OVERHEAD_TRIGGER) {
                 for (int i = 0; i < ClientServlet.AVAILABLE_RIDES_OVERHEAD; i++) {
-                    try {clientServlet.sendPostRide(new Ride());} catch (IOException | ServletException | InterruptedException e) { e.printStackTrace(); }
+                    try {clientServlet.sendPostRide();} catch (IOException | ServletException | InterruptedException e) { e.printStackTrace(); }
                 }
             }
         }
diff --git a/mispmock/src/core/ClientMock.java b/mispmock/src/core/ClientMock.java
index 5acb9ca..0f4ea77 100644
--- a/mispmock/src/core/ClientMock.java
+++ b/mispmock/src/core/ClientMock.java
@@ -20,17 +20,12 @@ public class ClientMock extends ClientServlet {
         this.mockSet = mockSet;
     }
 
-
+    /**
+     * Send POST (Ride).
+     * Parse response.
+     */
     @Override
-    void sendPostRide(Ride ride) throws IOException, ServletException, InterruptedException {
-
-
-
-
-        synchronized (available){
-            available.put(ride.getID(),ride);
-        }
-
+    protected Ride doSendPostRide(Ride ride) throws IOException, ServletException, InterruptedException {
 
         // Mock Exchange
         final ExchangeMock exchange = new ExchangeMock();
@@ -43,27 +38,23 @@ public class ClientMock extends ClientServlet {
             exchange.notify();
             mockSet.bridgeMock.doPost(exchange.request, exchange.response);
             exchange.wait();
-
-            // handle OK (Ride)(Request)
-            Ride parsedRide = new Ride(exchange.response.getContentAsString());
-            ride.setRequest(parsedRide.getRequest());
-
             exchange.notify();
         }
-        available.remove(ride.getID());
-        booked.put(ride.getID(),ride);
-        sendGetRequest(ride);
+
+        // handle OK (Ride)(Request)
+        return new Ride(exchange.response.getContentAsString());
     }
 
 
     /**
-     * # send GET (Request) to App
+     * Send GET (Request) to App.
+     * Parse response.
      */
     @Override
-    void sendGetRequest(Ride ride) throws IOException, ServletException, InterruptedException {
+    protected Ride doSendGetRequest(Ride ride) throws IOException, InterruptedException {
 
         // Mock Exchange
-        ExchangeMock exchange = new ExchangeMock();
+        final ExchangeMock exchange = new ExchangeMock();
 
         exchange.request.setMethod("GET");
         exchange.request.setContentType("application/json");
@@ -73,28 +64,24 @@ public class ClientMock extends ClientServlet {
             // Mock GET (Request)
             exchange.notify();
             mockSet.appMock.doGet(exchange.request, exchange.response);
-            //exchange.wait();
 
             // handle OK (Data)
-            Ride parsedRide = new Ride(exchange.response.getContentAsString());
-            ride.setData(parsedRide.getData());
             exchange.notify();
         }
-        booked.remove(ride.getID());
-        loaded.put(ride.getID(),ride);
-        sendGetRideRequestData(ride);
+
+        return new Ride(exchange.response.getContentAsString());
     }
 
 
     /**
-     * # send GET (Ride)(Request)(Data)
+     * Send GET (Ride)(Request)(Data).
+     * Parse response.
      */
     @Override
-    void sendGetRideRequestData(Ride ride) throws IOException, ServletException, InterruptedException {
+    protected void doSendGetRideRequest(Ride ride) throws IOException, InterruptedException {
 
         // Mock Exchange
-        ExchangeMock exchange = new ExchangeMock();
-
+        final ExchangeMock exchange = new ExchangeMock();
         exchange.request.setMethod("GET");
         exchange.request.setContentType("application/json");
         exchange.request.setContent(ride.json().getBytes());
@@ -104,10 +91,6 @@ public class ClientMock extends ClientServlet {
             exchange.notify();
             mockSet.bridgeMock.doGet(exchange.request, exchange.response);
             exchange.wait();
-
-            // handle OK (Ride)
-            Ride parsedRide = new Ride(exchange.response.getContentAsString());
-            loaded.remove(parsedRide.getID());
             exchange.notify();
         }
     }