static LineMessagingClientBuilder builder(String channelToken) { return builder(FixedChannelTokenSupplier.of(channelToken)); }
private void reply(final List<Message> messages) { final ReplyEvent replyEvent = (ReplyEvent) originalEvent; lineMessagingClient.replyMessage(new ReplyMessage(replyEvent.getReplyToken(), messages)) .whenComplete(this::logging); // DO NOT BLOCK HERE, otherwise, next message processing will be BLOCKED. }
@Override public void execute() { final String richMenuId = checkNotNull(arguments.getRichMenuId(), "--rich-menu-id= is not set."); final BotApiResponse botApiResponse = getUnchecked(lineMessagingClient.deleteRichMenu(richMenuId)); log.info("Successfully finished. {}", botApiResponse); } }
@Test public void testBuilderWithChannelTokenSupplier() throws InterruptedException { lineMessagingClient = LineMessagingClient.builder(() -> "MOCKED_TOKEN") .apiEndPoint("http://localhost:" + mockWebServer.getPort()) .build(); // Do lineMessagingClient.getProfile("TEST"); // Verify final RecordedRequest recordedRequest = mockWebServer.takeRequest(); assertThat(recordedRequest.getHeader("Authorization")) .isEqualTo("Bearer MOCKED_TOKEN"); }
@Test(timeout = ASYNC_TEST_TIMEOUT) public void forChannelTokenSupplier() throws Exception { // Do when(channelTokenSupplier.get()).thenReturn("1st"); lineMessagingClient.getMessageContent("TEST"); // Verify final RecordedRequest request1st = mockWebServer.takeRequest(); assertThat(request1st.getHeaders().toMultimap()) .containsEntry("Authorization", singletonList("Bearer 1st")); // Do again with another channel token. when(channelTokenSupplier.get()).thenReturn("2nd"); lineMessagingClient.getMessageContent("TEST"); // Verify final RecordedRequest request2nd = mockWebServer.takeRequest(); assertThat(request2nd.getHeaders().toMultimap()) .containsEntry("Authorization", singletonList("Bearer 2nd")); }
@Test public void relativeRequestTest() throws Exception { final UserProfileResponse profileResponseMock = new UserProfileResponse("name", "userId", "https://line.me/picture_url", "Status message"); mockWebServer.enqueue(new MockResponse() .setResponseCode(200) .setBody(new ObjectMapper() .writeValueAsString(profileResponseMock))); // Do final UserProfileResponse actualResponse = target.getProfile("USER_TOKEN").get(); // Verify final RecordedRequest recordedRequest = mockWebServer.takeRequest(); assertThat(recordedRequest.getPath()) .isEqualTo("/CanContainsRelative/v2/bot/profile/USER_TOKEN"); assertThat(actualResponse).isEqualTo(profileResponseMock); } }
@Override public void execute() { final String richMenuId = checkNotNull(arguments.getRichMenuId(), "--rich-menu-id= is not set."); final RichMenuResponse richMenuResponse = getUnchecked(lineMessagingClient.getRichMenu(richMenuId)); log.info("Successfully finished."); log.info("response = {}", richMenuResponse); } }
@Override public void execute() { final RichMenu richMenu = payloadProvider.read(RichMenu.class); final RichMenuIdResponse richMenuIdResponse = getUnchecked(lineMessagingClient.createRichMenu(richMenu)); log.info("Successfully finished. {}", richMenuIdResponse); } }
@Test(timeout = ASYNC_TEST_TIMEOUT) public void relativeRequestTest() throws Exception { final String apiEndPoint = "http://" + mockWebServer.getHostName() + ':' + mockWebServer.getPort() + "/CanContainsRelative/"; lineMessagingClient = LineMessagingClient .builder("SECRET") .apiEndPoint(apiEndPoint) .build(); final UserProfileResponse profileResponseMock = new UserProfileResponse("name", "userId", "https://line.me/picture_url", "Status message"); mockWebServer.enqueue(new MockResponse() .setResponseCode(200) .setBody(new ObjectMapper() .writeValueAsString(profileResponseMock))); // Do final UserProfileResponse actualResponse = lineMessagingClient.getProfile("USER_TOKEN").get(); // Verify final RecordedRequest recordedRequest = mockWebServer.takeRequest(); assertThat(recordedRequest.getPath()) .isEqualTo("/CanContainsRelative/v2/bot/profile/USER_TOKEN"); assertThat(actualResponse).isEqualTo(profileResponseMock); }
@Test(timeout = ASYNC_TEST_TIMEOUT) public void status400BadRequestTest() throws Exception { // Mocking mocking(400, ERROR_RESPONSE); // Expect expectedException.expect(ExecutionException.class); expectedException.expectCause(isA(BadRequestException.class)); expectedException.expectCause(errorResponseIs(ERROR_RESPONSE)); // Do lineMessagingClient.getMessageContent("TOKEN").get(); }
@Test public void testBuildWithFixedToken() throws InterruptedException { lineMessagingClient = new LineMessagingClientBuilder() .channelToken("MOCKED_TOKEN") .apiEndPoint("http://localhost:" + mockWebServer.getPort()) .build(); // Do lineMessagingClient.getProfile("TEST"); // Verify final RecordedRequest recordedRequest = mockWebServer.takeRequest(); assertThat(recordedRequest.getHeader("Authorization")) .isEqualTo("Bearer MOCKED_TOKEN"); }
/** * Expose {@link LineMessagingClient} as {@link Bean}. */ @Bean public LineMessagingClient lineMessagingClient( final ChannelTokenSupplier channelTokenSupplier) { return LineMessagingClient .builder(channelTokenSupplier) .apiEndPoint(lineBotProperties.getApiEndPoint()) .connectTimeout(lineBotProperties.getConnectTimeout()) .readTimeout(lineBotProperties.getReadTimeout()) .writeTimeout(lineBotProperties.getWriteTimeout()) .build(); }
private void reply(final List<Message> messages) { final ReplyEvent replyEvent = (ReplyEvent) originalEvent; lineMessagingClient.replyMessage(new ReplyMessage(replyEvent.getReplyToken(), messages)) .whenComplete(this::logging); // DO NOT BLOCK HERE, otherwise, next message processing will be BLOCKED. }
@Test(timeout = ASYNC_TEST_TIMEOUT) public void status200WithoutBodyTest() throws Exception { // Mocking mockWebServer.enqueue(new MockResponse().setResponseCode(200)); // Do final BotApiResponse botApiResponse = lineMessagingClient.deleteRichMenu("RICH_MENU_ID").get(); assertThat(botApiResponse).isEqualTo(SUCCESS); }
@Test(timeout = ASYNC_TEST_TIMEOUT) public void status401UnauthorizedTest() throws Exception { // Mocking mocking(401, ERROR_RESPONSE); // Expect expectedException.expect(ExecutionException.class); expectedException.expectCause(isA(UnauthorizedException.class)); expectedException.expectCause(errorResponseIs(ERROR_RESPONSE)); // Do lineMessagingClient.getMessageContent("TOKEN").get(); }
@Override protected LineMessagingClient createLineMessagingClient(final MockWebServer mockWebServer) { return LineMessagingClient.builder(channelTokenSupplier) .apiEndPoint("http://localhost:" + mockWebServer.getPort()) .build(); } }
@Before public void setUp() { target = targetFactory.createForEvent(EVENT); when(lineMessagingClient.replyMessage(any())) .thenReturn(CompletableFuture.completedFuture(new BotApiResponse("success", null))); }
@Test(timeout = ASYNC_TEST_TIMEOUT) public void status200WithBodyTest() throws Exception { // Mocking mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody("{}")); // Do final BotApiResponse botApiResponse = lineMessagingClient.deleteRichMenu("RICH_MENU_ID").get(); assertThat(botApiResponse).isEqualTo(SUCCESS); } }
@Test(timeout = ASYNC_TEST_TIMEOUT) public void status403ForbiddenTest() throws Exception { // Mocking mocking(403, ERROR_RESPONSE); // Expect expectedException.expect(ExecutionException.class); expectedException.expectCause(isA(ForbiddenException.class)); expectedException.expectCause(errorResponseIs(ERROR_RESPONSE)); // Do lineMessagingClient.getMessageContent("TOKEN").get(); }
protected LineMessagingClient createLineMessagingClient(final MockWebServer mockWebServer) { return LineMessagingClient.builder("token") .apiEndPoint("http://localhost:" + mockWebServer.getPort()) .build(); }