Skip to content

Commit d3464f7

Browse files
committed
rewrite after feedback
1 parent 3aeaad1 commit d3464f7

3 files changed

Lines changed: 34 additions & 23 deletions

File tree

src/main/java/com/example/NtfyConnectionImpl.java

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,21 @@ public class NtfyConnectionImpl implements NtfyConnection {
1818
private final ObjectMapper mapper = new ObjectMapper();
1919

2020
public NtfyConnectionImpl() {
21-
Dotenv dotenv = Dotenv.load();
22-
hostName = Objects.requireNonNull(dotenv.get("HOST_NAME"));
21+
String loadedHostName = null;
22+
try {
23+
Dotenv dotenv = Dotenv.load();
24+
loadedHostName = dotenv.get("HOST_NAME");
25+
} catch (Exception e) {
26+
System.err.println("WARNING: Could not load .env file for HOST_NAME. Using fallback.");
27+
}
28+
29+
this.hostName = (loadedHostName != null)
30+
? loadedHostName
31+
: "http://localhost:8080";
32+
33+
if (this.hostName.equals("http://localhost:8080")) {
34+
System.out.println("DEBUG: NtfyConnectionImpl running in test/fallback mode.");
35+
}
2336
}
2437

2538
public NtfyConnectionImpl(String hostName) {
@@ -30,21 +43,23 @@ public NtfyConnectionImpl(String hostName) {
3043
public boolean send(String message) {
3144
HttpRequest httpRequest = HttpRequest.newBuilder()
3245
.POST(HttpRequest.BodyPublishers.ofString(message))
33-
//.header("Cache", "no")
3446
.uri(URI.create(hostName + "/mytopic"))
3547
.build();
36-
try {
37-
//Todo: handle long blocking send requests to not freeze the JavaFX thread
38-
//1. Use thread send message?
39-
//2. Use async?
40-
var reponse = http.send(httpRequest, HttpResponse.BodyHandlers.discarding());
41-
return true;
42-
} catch (IOException e) {
43-
System.out.println("Error sending message");
44-
} catch (InterruptedException e) {
45-
System.out.println("Interruped sending message");
46-
}
47-
return false;
48+
49+
http.sendAsync(httpRequest, HttpResponse.BodyHandlers.discarding())
50+
.thenAccept(response -> {
51+
if (response.statusCode() >= 200 && response.statusCode() < 300) {
52+
System.out.println("Message sent successfully.");
53+
} else {
54+
System.err.println("Error while sending: " + response.statusCode());
55+
}
56+
})
57+
.exceptionally(e -> {
58+
System.err.println("Network issue: " + e.getMessage());
59+
return null;
60+
});
61+
62+
return true;
4863
}
4964

5065
@Override

src/main/java/module-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module hellofx {
1+
module chatapp {
22
requires javafx.controls;
33
requires javafx.fxml;
44
requires com.fasterxml.jackson.annotation;

src/test/java/com/example/ChatModelTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
55
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
6-
import org.junit.jupiter.api.DisplayName;
76
import org.junit.jupiter.api.Test;
87

98
import static com.github.tomakehurst.wiremock.client.WireMock.*;
@@ -24,7 +23,6 @@ class ChatModelTest {
2423

2524

2625
@Test
27-
@DisplayName("När sendMessage anropas, ska rätt meddelande skickas till anslutningen")
2826
void sendMessage_callsConnectionWithCorrectMessage() {
2927

3028
var spy = new NtfyConnectionSpy();
@@ -41,7 +39,7 @@ void sendMessage_callsConnectionWithCorrectMessage() {
4139

4240

4341
@Test
44-
@DisplayName("När ett meddelande tas emot via anslutningen, ska det läggas till i Modelns lista")
42+
4543
void receiveMessage_addsToObservableList() throws Exception {
4644

4745
var spy = new NtfyConnectionSpy();
@@ -65,18 +63,16 @@ void receiveMessage_addsToObservableList() throws Exception {
6563
}
6664

6765
@Test
68-
@DisplayName("sendMessage ska skicka en POST-förfrågan till WireMock-servern med rätt body")
69-
void sendMessageToFakeServer_viaNtfyConnectionImpl(WireMockRuntimeInfo wmRuntimeInfo) {
66+
void sendMessageToFakeServer_viaNtfyConnectionImpl(WireMockRuntimeInfo wmRuntimeInfo) throws InterruptedException {
7067
var con = new NtfyConnectionImpl("http://localhost:" + wmRuntimeInfo.getHttpPort());
7168
var model = new ChatModel(con);
7269
String messageToSend = "Test Body Content";
7370

74-
7571
stubFor(post("/mytopic").willReturn(ok()));
7672

77-
7873
model.sendMessage(messageToSend);
7974

75+
Thread.sleep(500);
8076

8177
verify(postRequestedFor(urlEqualTo("/mytopic"))
8278
.withRequestBody(matching(messageToSend)));

0 commit comments

Comments
 (0)