From 979f24679ad6f8ee9e8f8c0c0b913a3504bedfdb Mon Sep 17 00:00:00 2001
From: alaskowski <alaskowski@ethz.ch>
Date: Mon, 3 Apr 2023 09:01:47 +0200
Subject: [PATCH] SSDM-13251: Added simple implementations of
 TwoPhaseTransactionAPI methods to server-data-store java client

---
 .../ethz/sis/afsclient/client/AfsClient.java  | 18 +++++---
 .../sis/afsclient/client/AfsClientTest.java   | 45 ++++++++++++++++---
 2 files changed, 53 insertions(+), 10 deletions(-)

diff --git a/api-data-store-server-java/src/main/java/ch/ethz/sis/afsclient/client/AfsClient.java b/api-data-store-server-java/src/main/java/ch/ethz/sis/afsclient/client/AfsClient.java
index fa151f65157..f2eed8819c9 100644
--- a/api-data-store-server-java/src/main/java/ch/ethz/sis/afsclient/client/AfsClient.java
+++ b/api-data-store-server-java/src/main/java/ch/ethz/sis/afsclient/client/AfsClient.java
@@ -187,31 +187,39 @@ public final class AfsClient implements PublicAPI
     @Override
     public void begin(final UUID transactionId) throws Exception
     {
-
+        validateSessionToken();
+        Map<String, Object> parameters =
+                Map.of("transactionId", transactionId,
+                        "sessionToken", getSessionToken());
+        request("POST", "begin", Map.of(), jsonObjectMapper.writeValue(parameters));
     }
 
     @Override
     public Boolean prepare() throws Exception
     {
-        return null;
+        validateSessionToken();
+        return request("POST", "prepare", Map.of());
     }
 
     @Override
     public void commit() throws Exception
     {
-
+        validateSessionToken();
+        request("POST", "commit", Map.of());
     }
 
     @Override
     public void rollback() throws Exception
     {
-
+        validateSessionToken();
+        request("POST", "rollback", Map.of());
     }
 
     @Override
     public List<UUID> recover() throws Exception
     {
-        return null;
+        validateSessionToken();
+        return request("POST", "recover", Map.of());
     }
 
     private <T> T request(@NonNull final String httpMethod, @NonNull final String apiMethod,
diff --git a/api-data-store-server-java/src/test/java/ch/ethz/sis/afsclient/client/AfsClientTest.java b/api-data-store-server-java/src/test/java/ch/ethz/sis/afsclient/client/AfsClientTest.java
index 654a2a554b5..1d8b56026d2 100644
--- a/api-data-store-server-java/src/test/java/ch/ethz/sis/afsclient/client/AfsClientTest.java
+++ b/api-data-store-server-java/src/test/java/ch/ethz/sis/afsclient/client/AfsClientTest.java
@@ -5,6 +5,8 @@ import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.CoreMatchers.containsString;
 
 import java.net.URI;
+import java.util.List;
+import java.util.UUID;
 
 import org.junit.*;
 
@@ -179,28 +181,61 @@ public class AfsClientTest
     }
 
     @Test
-    public void testBegin()
+    public void begin_methodIsPost() throws Exception
     {
+        login();
+
+        UUID transactionId = UUID.randomUUID();
+
+        httpServer.setNextResponse("{\"result\": null}");
+        afsClient.begin(transactionId);
+
+        assertEquals("POST", httpServer.getHttpExchange().getRequestMethod());
     }
 
     @Test
-    public void testPrepare()
+    public void prepare_methodIsPost() throws Exception
     {
+        login();
+
+        httpServer.setNextResponse("{\"result\": true}");
+        Boolean result = afsClient.prepare();
+
+        assertEquals("POST", httpServer.getHttpExchange().getRequestMethod());
+        assertTrue(result);
     }
 
     @Test
-    public void testCommit()
+    public void commit_methodIsPost() throws Exception
     {
+        login();
+        httpServer.setNextResponse("{\"result\": null}");
+
+        afsClient.commit();
+        assertEquals("POST", httpServer.getHttpExchange().getRequestMethod());
     }
 
     @Test
-    public void testRollback()
+    public void rollback_methodIsPost() throws Exception
     {
+        login();
+
+        httpServer.setNextResponse("{\"result\": null}");
+
+        afsClient.rollback();
+        assertEquals("POST", httpServer.getHttpExchange().getRequestMethod());
     }
 
     @Test
-    public void testRecover()
+    public void recover_methodIsPost() throws Exception
     {
+        login();
+
+        httpServer.setNextResponse("{\"result\": null}");
+
+        List<UUID> result = afsClient.recover();
+        assertEquals("POST", httpServer.getHttpExchange().getRequestMethod());
+        assertNull(result);
     }
 
     private void login() throws Exception
-- 
GitLab