|
1 | 1 | package com.example; |
2 | 2 |
|
3 | | -import org.junit.jupiter.api.BeforeEach; |
| 3 | +import com.github.tomakehurst.wiremock.client.WireMock; |
| 4 | +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; |
| 5 | +import com.github.tomakehurst.wiremock.junit5.WireMockTest; |
| 6 | +import org.junit.jupiter.api.DisplayName; |
4 | 7 | import org.junit.jupiter.api.Test; |
5 | 8 |
|
6 | | -import java.io.File; |
7 | | -import java.io.IOException; |
8 | | -import java.nio.file.Files; |
9 | | - |
10 | | -import static org.assertj.core.api.Assertions.assertThat; |
| 9 | +import static com.github.tomakehurst.wiremock.client.WireMock.*; |
| 10 | +import static org.assertj.core.api.Assertions.*; |
11 | 11 |
|
| 12 | +@WireMockTest |
12 | 13 | class HelloModelTest { |
13 | 14 |
|
14 | | - private HelloModel model; |
15 | | - private NtfyConnectionSpy connectionSpy; |
| 15 | + @Test |
| 16 | + @DisplayName("Model should pass the text to the connection when sending a message") |
| 17 | + void modelDelegatesSendToConnection() { |
| 18 | + var fakeConnection = new NtfyConnectionSpy(); |
| 19 | + var model = new HelloModel(fakeConnection); |
| 20 | + |
| 21 | + model.setMessageToSend("Ping!"); |
| 22 | + model.sendMessage(); |
| 23 | + |
| 24 | + assertThat(fakeConnection.message) |
| 25 | + .as("Message should be forwarded to NtfyConnection") |
| 26 | + .isEqualTo("Ping!"); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + @DisplayName("Integration: sending a message should hit /mytopic on a mock server") |
| 31 | + void sendMessageReachesMockServer(WireMockRuntimeInfo wm) { |
| 32 | + var baseUrl = "http://localhost:" + wm.getHttpPort(); |
| 33 | + var con = new NtfyConnectionImpl(baseUrl); |
| 34 | + var model = new HelloModel(con); |
| 35 | + |
| 36 | + stubFor(post("/mytopic").willReturn(aResponse().withStatus(200))); |
16 | 37 |
|
17 | | - @BeforeEach |
18 | | - void setUp() { |
19 | | - connectionSpy = new NtfyConnectionSpy(); |
20 | | - model = new HelloModel(connectionSpy); |
| 38 | + model.setMessageToSend("Hello from test"); |
| 39 | + model.sendMessage(); |
| 40 | + |
| 41 | + verify(postRequestedFor(urlPathEqualTo("/mytopic")) |
| 42 | + .withRequestBody(matching(".*Hello from test.*"))); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + @DisplayName("Model should contain an initial message after construction") |
| 47 | + void initialMessagesListContainsWelcomeMessage() { |
| 48 | + var spy = new NtfyConnectionSpy(); |
| 49 | + var model = new HelloModel(spy); |
| 50 | + |
| 51 | + assertThat(model.getMessages()) |
| 52 | + .as("Model should initialize with one default message") |
| 53 | + .hasSize(1); |
21 | 54 | } |
22 | 55 |
|
23 | 56 | @Test |
24 | | - void sendMessage_callsConnection() { |
25 | | - model.sendMessage("Hej"); |
26 | | - assertThat(connectionSpy.getLastMessage()).isEqualTo("Hej!"); |
| 57 | + @DisplayName("Message field should reset to empty after successful send") |
| 58 | + void messageFieldClearsAfterSend() { |
| 59 | + var dummyConnection = new NtfyConnectionSpy(); |
| 60 | + var model = new HelloModel(dummyConnection); |
| 61 | + |
| 62 | + model.setMessageToSend("Reset me!"); |
| 63 | + model.sendMessage(); |
| 64 | + |
| 65 | + assertThat(model.getMessageToSend()) |
| 66 | + .as("Message input should be cleared after send") |
| 67 | + .isEmpty(); |
27 | 68 | } |
28 | 69 |
|
29 | 70 | @Test |
30 | | - void sendFile_callsConnectionWithData() throws IOException { |
31 | | - File tempFile = File.createTempFile("testfile", ".txt"); |
32 | | - Files.writeString(tempFile.toPath(), "Hello World"); |
| 71 | + @DisplayName("Sending blank or empty text should not call the connection") |
| 72 | + void emptyMessageShouldNotBeSent() { |
| 73 | + var spy = new NtfyConnectionSpy(); |
| 74 | + var model = new HelloModel(spy); |
33 | 75 |
|
34 | | - model.sendFile(tempFile); |
| 76 | + model.setMessageToSend(" "); // blank message |
| 77 | + model.sendMessage(); |
35 | 78 |
|
36 | | - assertThat(connectionSpy.getLastFileName()).isEqualTo(tempFile.getName()); |
37 | | - assertThat(connectionSpy.getLastFileData()).isEqualTo(Files.readAllBytes(tempFile.toPath())); |
| 79 | + assertThat(spy.message) |
| 80 | + .as("Connection should not be called with blank input") |
| 81 | + .isNullOrEmpty(); |
38 | 82 | } |
39 | 83 | } |
| 84 | + |
0 commit comments