ReplyByReturnValueConsumer implements Consumer<Object> { @Override public void accept(final Object returnValue) { if (returnValue instanceof CompletableFuture) { ((CompletableFuture<?>) returnValue) .whenComplete(this::whenComplete); } else { acceptResult(returnValue); } }  @Override void accept(final Object returnValue);  }
@Test public void acceptSingleReplyTest() throws Exception { target.accept(new TextMessage("Reply Text")); verify(lineMessagingClient, only()) .replyMessage(new ReplyMessage(EVENT.getReplyToken(), singletonList(new TextMessage("Reply Text")))); }
@Test public void acceptListReplyTest() throws Exception { target.accept(singletonList(new TextMessage("Reply Text"))); verify(lineMessagingClient, only()) .replyMessage(new ReplyMessage(EVENT.getReplyToken(), singletonList(new TextMessage("Reply Text")))); }
@Test public void acceptCompletableSingleReplyTest() throws Exception { final CompletableFuture<TextMessage> returnValue = new CompletableFuture<>(); target.accept(returnValue); returnValue.complete(new TextMessage("Reply Text")); verify(lineMessagingClient, only()) .replyMessage(new ReplyMessage(EVENT.getReplyToken(), singletonList(new TextMessage("Reply Text")))); }
@Test public void acceptCompletableListReplyTest() throws Exception { final CompletableFuture<List<TextMessage>> returnValue = new CompletableFuture<>(); target.accept(returnValue); returnValue.complete(singletonList(new TextMessage("Reply Text"))); verify(lineMessagingClient, only()) .replyMessage(new ReplyMessage(EVENT.getReplyToken(), singletonList(new TextMessage("Reply Text")))); }
@Test public void errorInCompletableLoggingTest() throws Exception { String systemOut = tapSystemOut(() -> { final CompletableFuture<List<TextMessage>> returnValue = new CompletableFuture<>(); target.accept(returnValue); returnValue.completeExceptionally( new GeneralLineMessagingException("EXCEPTION HAPPEN!", null, null)); }); assertThat(systemOut) .contains("EXCEPTION HAPPEN!"); }
@Test public void errorInLineMessagingClientLoggingTest() throws Exception { reset(lineMessagingClient); when(lineMessagingClient.replyMessage(any())) .thenReturn(new CompletableFuture<BotApiResponse>() {{ completeExceptionally(new GeneralLineMessagingException("EXCEPTION HAPPEN!", null, null)); }}); String systemOut = tapSystemOut(() -> { final CompletableFuture<List<TextMessage>> returnValue = new CompletableFuture<>(); target.accept(returnValue); returnValue.complete(singletonList(new TextMessage("Reply Text"))); }); assertThat(systemOut) .contains("failed") .contains("EXCEPTION HAPPEN!"); }