Skip to content

Commit 6d1a71d

Browse files
Commit för feedback
1 parent c506a01 commit 6d1a71d

4 files changed

Lines changed: 37 additions & 22 deletions

File tree

src/main/java/com/example/HelloController.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ private void initialize() {
4848
updateSendButtonState();
4949

5050
//Lägger till en lyssnare för att uppdatera knappen vid inmatning av text
51-
messageInput.textProperty().addListener((observable, oldValue, newValue) -> {
52-
updateSendButtonState();
53-
});
51+
messageInput.textProperty().addListener((observable, oldValue, newValue) -> updateSendButtonState());
5452

5553
//Om användaren trycker på Enter eller klickar med musen -> skicka meddelandet
5654
messageInput.setOnAction((event) -> sendMessageToModel());
@@ -105,7 +103,7 @@ protected void updateItem(NtfyMessageDto item, boolean empty) {
105103
chatBox.setItems(model.getMessages());
106104
//Flyttar Platform.runlater till controller på grund av runtimeexeption när tester körs, även för en mer solid MVC
107105
model.getMessages().addListener((ListChangeListener<NtfyMessageDto>) changes -> {
108-
Platform.runLater(() -> chatBox.refresh());
106+
chatBox.refresh();
109107
});
110108
}
111109

@@ -123,8 +121,11 @@ private void updateSendButtonState() {
123121
// Kollar om texten, efter att ha tagit bort ledande/efterföljande mellanslag, är tom.
124122
boolean isTextPresent = !messageInput.getText().trim().isEmpty();
125123

124+
//Nytt villkor för button för att ej kunna skicka meddelanden till servern om ej connectad
125+
boolean isConnected = !disconnectFromServer.isDisabled();
126+
126127
// Sätt disable till TRUE om det INTE finns text.
127-
sendButton.setDisable(!isTextPresent);
128+
sendButton.setDisable(!isTextPresent || !isConnected);
128129
}
129130

130131
//Starta prenumeration via model och uppdaterar button
@@ -133,6 +134,7 @@ public void setConnectToServer() {
133134
model.receiveMessage();
134135
connectToServer.setDisable(true);
135136
disconnectFromServer.setDisable(false);
137+
updateSendButtonState();
136138
}
137139
}
138140

@@ -141,5 +143,6 @@ public void setDisconnectFromServer() {
141143
model.stopSubscription();
142144
connectToServer.setDisable(false);
143145
disconnectFromServer.setDisable(true);
146+
updateSendButtonState();
144147
}
145148
}

src/main/java/com/example/HelloModel.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ public class HelloModel {
2828
//Innehåller meddelandet som ska skickas, kopplat till GUI via SimpleStringProperty
2929
private final StringProperty messageToSend = new SimpleStringProperty();
3030
//Fält för att kunna styra anslutningen
31-
private final Subscription subscription;
31+
private Subscription subscription = null;
3232

3333
//Konstruktorn tar emot nätverkskoppling, antingen ett test via spy eller en riktig via impl
3434
public HelloModel(NtfyConnection connection) {
3535

3636
this.connection = connection;
37-
subscription = receiveMessage(); //subscription startar automatiskt när modellen skapas
37+
//subscription = receiveMessage(); //subscription startar automatiskt när modellen skapas
3838
}
3939

4040
//getter från private, används av controller för att koppla til ListView
@@ -65,8 +65,11 @@ public void sendMessage(String message) {
6565
//Startar en prenumeration på inkommande meddelnaden,
6666
//Returnerar ett Subscription-objekt så den kan stoppas
6767
public Subscription receiveMessage() {
68+
if(subscription != null && subscription.isOpen()) {
69+
return this.subscription;
70+
}
71+
return subscription = connection.receive(messages::add);
6872

69-
return connection.receive(messages::add);
7073

7174
}
7275

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.example;
22

33
import io.github.cdimascio.dotenv.Dotenv;
4+
import javafx.application.Platform;
45
import tools.jackson.databind.ObjectMapper;
56

67
import java.io.IOException;
@@ -71,7 +72,7 @@ public Subscription receive(Consumer<NtfyMessageDto> messageHandler) {
7172
.map(s -> mapper.readValue(s, NtfyMessageDto.class))
7273
.filter(message -> message.event().equals("message"))
7374
.peek(System.out::println)
74-
.forEach(messageHandler));
75+
.forEach(message -> Platform.runLater(()-> messageHandler.accept(message))));
7576
return new Subscription() {
7677
@Override
7778
public void close() {

src/test/java/com/example/HelloModelTest.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,37 +35,44 @@ void sendMessageCallsConnectionWithMessageToSend() {
3535
}
3636

3737
@Test
38-
void receiveMessageFromFakeServer(WireMockRuntimeInfo wireMockRuntimeInfo) throws IOException {
38+
void receiveMessageFromFakeServer(WireMockRuntimeInfo wireMockRuntimeInfo) throws IOException, InterruptedException {
3939
//Arrange
4040
var host = "http://localhost:" + wireMockRuntimeInfo.getHttpPort();
4141
var con = new NtfyConnectionImpl(host);
42+
4243
String fakeMessage = """
4344
{"id":"testID","time":1762935416, "event":"keepalive","topic":"catChat", "message":"Filtreras bort"}
44-
{"id":"testID","time":1762935416, "event":"message","topic":"catChat", "message":"User: Hej"}""";
45+
{"id":"testID","time":1762935416, "event":"message","topic":"catChat", "message":"User: Hej"}
46+
""";
47+
4548

4649
//Simulerar en server
4750
stubFor(get(urlEqualTo("/catChat/json")).willReturn(aResponse()
4851
.withStatus(200)
4952
.withBody(fakeMessage)));
5053

51-
Consumer<NtfyMessageDto> fakeReceiver = Mockito.mock(Consumer.class);
52-
//con.receive(fakeReceiver);
54+
// Consumer<NtfyMessageDto> fakeReceiver = Mockito.mock(Consumer.class);
55+
// //con.receive(fakeReceiver);
56+
//
57+
// ArgumentCaptor<NtfyMessageDto> captor = ArgumentCaptor.forClass(NtfyMessageDto.class);
58+
// Subscription subscription = con.receive(fakeReceiver);
59+
60+
// //Act
61+
62+
var model =new HelloModel(con);
63+
model.receiveMessage();
5364

54-
ArgumentCaptor<NtfyMessageDto> captor = ArgumentCaptor.forClass(NtfyMessageDto.class);
55-
Subscription subscription = con.receive(fakeReceiver);
56-
//Act
65+
Thread.sleep(50);
5766

5867
Awaitility.await()
59-
.atMost(Duration.ofSeconds(4)).untilAsserted(() -> {
60-
Mockito.verify(fakeReceiver, Mockito.times(1))
61-
.accept(captor.capture());
68+
.atMost(Duration.ofSeconds(5)).untilAsserted(() -> {
69+
assertThat(model.getMessages()).hasSize(1);
6270
});
6371

6472
//Assert
65-
String messageReceived = captor.getValue().message();
66-
assertThat(messageReceived).isEqualTo("User: Hej");
73+
assertThat(model.getMessages().getFirst().message()).isEqualTo("User: Hej");
6774

68-
subscription.close(); //Stänger anslutningen
75+
model.stopSubscription(); //Stänger anslutningen
6976
}
7077

7178
@Test
@@ -86,6 +93,7 @@ void messageIsAddedToObservableList() {
8693
var spy = new NtfyConnectionSpy();
8794
var model = new HelloModel(spy);
8895

96+
model.receiveMessage();
8997
var testText = new NtfyMessageDto("id1", 15465823L,"Message", "catChat", "Godmorgon");
9098
spy.simulateIncomingMessage(testText);
9199

0 commit comments

Comments
 (0)