From 7e23b6d098f10895c1f9ca3547c848e11907ab82 Mon Sep 17 00:00:00 2001
From: alaskowski <alaskowski@ethz.ch>
Date: Mon, 6 Mar 2023 15:05:18 +0100
Subject: [PATCH] SSDM-13251: Created a simple unit test for client

---
 api-data-store-server-java/build.gradle       |  2 -
 api-data-store-server-java/settings.gradle    |  1 -
 .../sis/afsclient/client/AfsClientTest.java   | 82 +++++++++++--------
 .../sis/afsclient/client/DummyHttpServer.java | 72 ++++++++++++++++
 4 files changed, 118 insertions(+), 39 deletions(-)
 create mode 100644 api-data-store-server-java/src/test/java/ch/ethz/sis/afsclient/client/DummyHttpServer.java

diff --git a/api-data-store-server-java/build.gradle b/api-data-store-server-java/build.gradle
index e0b56de7041..340bf18c3b8 100644
--- a/api-data-store-server-java/build.gradle
+++ b/api-data-store-server-java/build.gradle
@@ -24,7 +24,5 @@ dependencies {
     implementation project(':lib-json'),
             'lombok:lombok:1.18.22'
     testImplementation 'junit:junit:4.10'
-//            project(':server-data-store'),
-//            project(':lib-transactional-file-system')
     testRuntimeOnly 'hamcrest:hamcrest-core:1.3'
 }
diff --git a/api-data-store-server-java/settings.gradle b/api-data-store-server-java/settings.gradle
index 568185ea090..7cbef054c8b 100644
--- a/api-data-store-server-java/settings.gradle
+++ b/api-data-store-server-java/settings.gradle
@@ -1,2 +1 @@
 includeFlat 'lib-json'
-//, 'lib-transactional-file-system', 'server-data-store'
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 abcc57347d8..694c329b935 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
@@ -3,99 +3,109 @@ package ch.ethz.sis.afsclient.client;
 import static org.junit.Assert.*;
 
 import java.net.URI;
-import java.util.List;
 
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
-//import ch.ethz.sis.afs.manager.TransactionConnection;
-//import ch.ethz.sis.afsserver.server.Server;
-//import ch.ethz.sis.afsserver.server.observer.impl.DummyServerObserver;
-//import ch.ethz.sis.afsserver.startup.AtomicFileSystemServerParameter;
-//import ch.ethz.sis.shared.startup.Configuration;
-
 public class AfsClientTest
 {
-    
-//    private static Server<TransactionConnection, ?> afsServer;
+
+    private static DummyHttpServer httpServer;
 
     private static AfsClient afsClient;
 
+    private static int httpServerPort;
+
+    private static String httpServerPath;
+
     @BeforeClass
-    public static void classSetUp() throws Exception {
-//        final Configuration configuration = new Configuration(List.of(AtomicFileSystemServerParameter.class),
-//                "src/test/resources/afs-server-config.properties");
-//        final DummyServerObserver dummyServerObserver = new DummyServerObserver();
-//        afsServer = new Server<>(configuration, dummyServerObserver, dummyServerObserver);
-//
-//        final int httpServerPort = configuration.getIntegerProperty(AtomicFileSystemServerParameter.httpServerPort);
-//        final String httpServerPath = configuration.getStringProperty(AtomicFileSystemServerParameter.httpServerUri);
-//        afsClient = new AfsClient(new URI("http", null, "localhost", httpServerPort, httpServerPath, null, null));
+    public static void classSetUp() throws Exception
+    {
+        httpServerPort = 8085;
+        httpServerPath = "/fileserver";
+        httpServer = new DummyHttpServer(httpServerPort, httpServerPath);
+        httpServer.start();
+        afsClient = new AfsClient(
+                new URI("http", null, "localhost", httpServerPort, httpServerPath, null, null));
     }
 
     @AfterClass
-    public static void classTearDown() throws Exception {
-//        afsServer.shutdown(true);
+    public static void classTearDown() throws Exception
+    {
+        httpServer.stop();
     }
 
     @Test
-    public void testLogin() throws Exception {
-//        final String token = afsClient.login("test", "test");
-        String token = "null";
+    public void testLogin() throws Exception
+    {
+        final String token = afsClient.login("test", "test");
         assertNotNull(token);
     }
 
     @Test
-    public void testIsSessionValid() {
+    public void testIsSessionValid()
+    {
     }
 
     @Test
-    public void testLogout() {
+    public void testLogout()
+    {
     }
 
     @Test
-    public void testList() {
+    public void testList()
+    {
     }
 
     @Test
-    public void testRead() {
+    public void testRead()
+    {
     }
 
     @Test
-    public void testWrite() {
+    public void testWrite()
+    {
     }
 
     @Test
-    public void testDelete() {
+    public void testDelete()
+    {
     }
 
     @Test
-    public void testCopy() {
+    public void testCopy()
+    {
     }
 
     @Test
-    public void testMove() {
+    public void testMove()
+    {
     }
 
     @Test
-    public void testBegin() {
+    public void testBegin()
+    {
     }
 
     @Test
-    public void testPrepare() {
+    public void testPrepare()
+    {
     }
 
     @Test
-    public void testCommit() {
+    public void testCommit()
+    {
     }
 
     @Test
-    public void testRollback() {
+    public void testRollback()
+    {
     }
 
     @Test
-    public void testRecover() {
+    public void testRecover()
+    {
     }
 
 }
\ No newline at end of file
diff --git a/api-data-store-server-java/src/test/java/ch/ethz/sis/afsclient/client/DummyHttpServer.java b/api-data-store-server-java/src/test/java/ch/ethz/sis/afsclient/client/DummyHttpServer.java
new file mode 100644
index 00000000000..2450273d9a0
--- /dev/null
+++ b/api-data-store-server-java/src/test/java/ch/ethz/sis/afsclient/client/DummyHttpServer.java
@@ -0,0 +1,72 @@
+/*
+ *  Copyright ETH 2023 Zürich, Scientific IT Services
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+
+package ch.ethz.sis.afsclient.client;
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.InetSocketAddress;
+
+import com.sun.net.httpserver.HttpExchange;
+import com.sun.net.httpserver.HttpHandler;
+import com.sun.net.httpserver.HttpServer;
+
+public final class DummyHttpServer
+{
+    private HttpServer httpServer;
+
+    private final int httpServerPort; // 8085
+
+    private final String httpServerPath; // "/fileserver"
+
+    private static final String DEFAULT_RESPONSE = "{\"result\": \"success\"}";
+
+    private String nextResponse = DEFAULT_RESPONSE;
+
+    public DummyHttpServer(int httpServerPort, String httpServerPath) throws IOException
+    {
+        this.httpServerPort = httpServerPort;
+        this.httpServerPath = httpServerPath;
+        httpServer = HttpServer.create(new InetSocketAddress(httpServerPort), 0);
+        httpServer.createContext(httpServerPath, new HttpHandler()
+        {
+            public void handle(HttpExchange exchange) throws IOException
+            {
+                byte[] response = nextResponse.getBytes();
+                exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length);
+                exchange.getResponseBody().write(response);
+                exchange.close();
+            }
+        });
+    }
+
+    public void start()
+    {
+        httpServer.start();
+    }
+
+    public void stop()
+    {
+        httpServer.stop(0);
+    }
+
+    public void setNextResponse(String response)
+    {
+        this.nextResponse = response;
+    }
+
+}
-- 
GitLab