Testing Method structure

This commit is contained in:
2026-02-19 16:24:57 +01:00
parent 9342bf42c5
commit b1198e89d6
6 changed files with 89 additions and 1 deletions
+7
View File
@@ -25,4 +25,11 @@ dependencies {
implementation 'jakarta.websocket:jakarta.websocket-api:2.1.1'
// WebSocket client runtime
implementation 'org.glassfish.tyrus:tyrus-client:2.1.2'
// For testing
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
}
test {
useJUnitPlatform()
}
@@ -7,6 +7,7 @@ import dev.selimaj.session.types.Message;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.WebSocket;
import java.util.concurrent.CompletableFuture;
public final class Session {
@@ -45,6 +46,13 @@ public final class Session {
listener.notify(ws, method, data);
}
public <Req, Res> CompletableFuture<Res> request(
String method,
Req data,
Class<Res> resType) throws Exception {
return listener.request(ws, method, data, resType);
}
public void close() throws Exception {
ws.sendClose(0, "Session closed");
}
@@ -4,6 +4,7 @@ import java.net.http.WebSocket;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -16,6 +17,7 @@ public class SessionListener implements WebSocket.Listener {
private final ObjectMapper mapper = new ObjectMapper();
private final ConcurrentHashMap<Integer, CompletableFuture<JsonNode>> pending = new ConcurrentHashMap<>();
private final AtomicInteger id = new AtomicInteger();
@Override
public CompletionStage<?> onText(WebSocket ws, CharSequence data, boolean last) {
@@ -73,4 +75,29 @@ public class SessionListener implements WebSocket.Listener {
void notify(WebSocket ws, String method, Object data) throws Exception {
send(ws, new Message.Notification(method, mapper.valueToTree(data)));
}
<Req, Res> CompletableFuture<Res> request(
WebSocket ws,
String method,
Req data,
Class<Res> resType) throws Exception {
int id = this.id.incrementAndGet();
CompletableFuture<JsonNode> fut = new CompletableFuture<>();
pending.put(id, fut);
send(ws, new Message.Request(
id,
method,
mapper.valueToTree(data)));
return fut.thenApply(json -> {
try {
return mapper.treeToValue(json, resType);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
}
@@ -3,5 +3,5 @@ package dev.selimaj.session.types;
import com.fasterxml.jackson.databind.JsonNode;
public interface Method<Req extends JsonNode, Res extends JsonNode, Err extends JsonNode> {
String NAME = "";
String getName();
}
@@ -0,0 +1,18 @@
package dev.selimaj.session;
import com.fasterxml.jackson.databind.node.TextNode;
import dev.selimaj.session.types.Method;
public class Methods {
public static class Data implements Method<TextNode, TextNode, TextNode> {
@Override
public String getName() {
return "data";
}
public static String getNameStatic() {
return "data";
}
}
}
@@ -0,0 +1,28 @@
package dev.selimaj.session;
import org.junit.jupiter.api.Test;
public class SessionTest {
static void testRequest() throws Exception {
Session session = Session.connect("ws://localhost:8080/");
session.request(Methods.Data.getNameStatic(), "", String.class);
System.out.println("test");
Thread.sleep(1000);
}
@Test
void requestTest() throws Exception {
testRequest();
}
public static void main(String[] args) {
try {
testRequest();
} catch (Exception e) {
e.printStackTrace();
}
}
}