Skip to content

Commit 61b6033

Browse files
committed
integrationstest med wiremock
1 parent 5de28bf commit 61b6033

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,40 @@ void shouldIgnoreMessagesWithBlankContent() throws InterruptedException {
244244
assertThat(messageAdded).isFalse();
245245
assertThat(model.getMessages()).isEmpty();
246246
}
247+
@Test
248+
void shouldRejectAllInvalidIncomingMessages() throws InterruptedException {
249+
NtfyConnectionSpy connectionSpy = new NtfyConnectionSpy();
250+
HelloModel model = new HelloModel(connectionSpy);
251+
252+
CountDownLatch latch = new CountDownLatch(1);
253+
model.getMessages().addListener((ListChangeListener<NtfyMessageDto>) c -> {
254+
while (c.next()) {
255+
if (c.wasAdded()) latch.countDown();
256+
}
257+
});
258+
259+
connectionSpy.simulateIncoming(new NtfyMessageDto("id1", 1, "message", "room", ""));
260+
connectionSpy.simulateIncoming(new NtfyMessageDto("id2", 2, "message", "room", " "));
261+
connectionSpy.simulateIncoming(null);
262+
263+
boolean messageAdded = latch.await(500, TimeUnit.MILLISECONDS);
264+
assertThat(messageAdded).isFalse();
265+
assertThat(model.getMessages()).isEmpty();
266+
}
267+
//integration test
268+
@Test
269+
void shouldCommunicateWithMockedServer(WireMockRuntimeInfo wmInfo) throws InterruptedException {
270+
NtfyConnectionImpl connection = new NtfyConnectionImpl("http://localhost:" + wmInfo.getHttpPort());
271+
HelloModel model = new HelloModel(connection);
272+
model.setMessageToSend("Hello World");
273+
274+
stubFor(post("/mytopic").willReturn(ok()));
275+
276+
CountDownLatch latch = new CountDownLatch(1);
277+
model.sendMessageAsync(success -> latch.countDown());
278+
279+
assertThat(latch.await(1, TimeUnit.SECONDS)).isTrue();
280+
verify(postRequestedFor(urlEqualTo("/mytopic"))
281+
.withRequestBody(matching("Hello World")));
282+
}
247283
}

0 commit comments

Comments
 (0)