Skip to content

Commit f175e86

Browse files
authored
Merge branch: Tests --> Main.
Adds Unit tests for HelloModel
2 parents a206aeb + 00bdec2 commit f175e86

File tree

4 files changed

+112
-4
lines changed

4 files changed

+112
-4
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ public void sendMessage() {
5252
}
5353

5454
public void receiveMessage() {
55-
connection.receive(m->Platform.runLater(()->messages.add(m)));
55+
connection.receive(m-> {
56+
try {
57+
Platform.runLater(()->messages.add(m));
58+
} catch (IllegalStateException e) {
59+
messages.add(m);
60+
}
61+
62+
});
5663
}
5764

5865
public void sendFile(File file) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public boolean sendFile(File file){
8989
.PUT(HttpRequest.BodyPublishers.ofFile(file.toPath()))
9090
.build();
9191

92-
http.sendAsync(request, HttpResponse.BodyHandlers.discarding());
92+
http.send(request, HttpResponse.BodyHandlers.discarding());
9393
return true;
9494

9595
} catch (Exception e) {

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

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import org.junit.jupiter.api.DisplayName;
77
import org.junit.jupiter.api.Test;
88

9+
import java.io.File;
10+
import java.io.IOException;
11+
912
import static com.github.tomakehurst.wiremock.client.WireMock.*;
1013
import static org.assertj.core.api.Assertions.assertThat;
1114

@@ -33,13 +36,96 @@ void sendMessageToFakeServer(WireMockRuntimeInfo wmRuntimeInfo) {
3336
var con = new NtfyConnectionImpl("http://localhost:" + wmRuntimeInfo.getHttpPort());
3437
var model = new HelloModel(con);
3538
model.setMessageToSend("Hello World");
36-
stubFor(post("/mytopic").willReturn(ok()));
39+
stubFor(post("/adam").willReturn(ok()));
3740

3841
model.sendMessage();
3942

4043
// Verify call made to server
41-
verify(postRequestedFor(urlEqualTo("/mytopic"))
44+
verify(postRequestedFor(urlEqualTo("/adam"))
4245
.withRequestBody(matching("Hello World")));
4346
}
4447

48+
@Test
49+
@DisplayName("Given file when calling sendFile then connection.sendFile should be called")
50+
void sendFileCallConnectionSendFile() {
51+
var spy = new NtfyConnectionSpy();
52+
var model = new HelloModel(spy);
53+
54+
File file = new File("dummy.txt");
55+
model.sendFile(file);
56+
57+
assertThat(spy.sentFile).isEqualTo(file);
58+
}
59+
60+
@Test
61+
@DisplayName("sendFile should upload file to the server using PUT")
62+
void sendFileToFakeServer(WireMockRuntimeInfo wmRuntimeInfo) throws Exception {
63+
var con = new NtfyConnectionImpl("http://localhost:" + wmRuntimeInfo.getHttpPort());
64+
var model = new HelloModel(con);
65+
66+
File temp = File.createTempFile("upload_test", ".txt");
67+
68+
stubFor(put("/adam").willReturn(ok()));
69+
70+
model.sendFile(temp);
71+
72+
verify(putRequestedFor(urlEqualTo("/adam")));
73+
74+
}
75+
76+
@Test
77+
@DisplayName("Model should receive messages when connection invokes handler")
78+
void receiveMessageShouldAddToModelViewHander(){
79+
var spy = new NtfyConnectionSpy();
80+
var model = new HelloModel(spy);
81+
82+
NtfyMessageDto incoming = new NtfyMessageDto(
83+
"123",
84+
1000,
85+
0,
86+
"message",
87+
"adam",
88+
"this is a test hehehe",
89+
null
90+
);
91+
92+
spy.simulateIncoming(incoming);
93+
94+
assertThat(model.getMessages()).containsExactly(incoming);
95+
}
96+
97+
@Test
98+
@DisplayName("Model constructor should register receive handler on connection")
99+
void constructorShouldRegisterHandler() {
100+
101+
var spy = new NtfyConnectionSpy();
102+
var model = new HelloModel(spy);
103+
104+
assertThat(spy.handler).isNotNull();
105+
}
106+
107+
@Test
108+
@DisplayName("messageToSendProperty should update when setting message")
109+
void messagePropertyShouldUpdate() {
110+
111+
var spy = new NtfyConnectionSpy();
112+
var model = new HelloModel(spy);
113+
114+
model.setMessageToSend("Hello!");
115+
116+
assertThat(model.getMessageToSend()).isEqualTo("Hello!");
117+
}
118+
119+
@Test
120+
@DisplayName("getGreeting should return the expected greeting text")
121+
void greetingShouldBeCorrect() {
122+
123+
var model = new HelloModel(new NtfyConnectionSpy());
124+
125+
assertThat(model.getGreeting()).isEqualTo("Chat Client by Adam");
126+
}
127+
128+
129+
130+
45131
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
11
package com.example;
22

3+
import java.io.File;
34
import java.util.function.Consumer;
45

56
public class NtfyConnectionSpy implements NtfyConnection {
67

78
String message;
9+
File sentFile;
10+
Consumer<NtfyMessageDto> handler;
11+
812
@Override
913
public boolean send(String message) {
1014
this.message = message;
1115
return true;
1216
}
1317

18+
public boolean sendFile(File file){
19+
this.sentFile = file;
20+
return true;
21+
}
22+
1423
@Override
1524
public void receive(Consumer<NtfyMessageDto> messageHandler) {
25+
this.handler = messageHandler;
26+
}
1627

28+
public void simulateIncoming(NtfyMessageDto message){
29+
if (handler !=null){
30+
handler.accept(message);
31+
}
1732
}
1833
}

0 commit comments

Comments
 (0)