@Test public void testRefreshTokenWithNoRefreshToken() { accessTokenCache.saveAccessToken(ACCESS_TOKEN); LineApiResponse<RefreshTokenResult> refreshTokenResponse = LineApiResponse.createAsSuccess(new RefreshTokenResult( "newAccessToken", 999L, null, Collections.emptyList())); doReturn(refreshTokenResponse) .when(internalOauthApiClient) .refreshToken(anyString(), any(InternalAccessToken.class)); LineApiResponse<LineAccessToken> actualResponse = target.refreshAccessToken(); verify(internalOauthApiClient, times(1)).refreshToken(CHANNEL_ID, ACCESS_TOKEN); assertTrue(actualResponse.isSuccess()); assertNotNull(actualResponse.getResponseData()); assertEquals("newAccessToken", actualResponse.getResponseData().getTokenString()); assertEquals(999L, actualResponse.getResponseData().getExpiresInMillis()); assertTrue(actualResponse.getResponseData().getIssuedClientTimeMillis() > 0); InternalAccessToken newAccessToken = accessTokenCache.getAccessToken(); assertNotNull(newAccessToken); assertEquals("newAccessToken", newAccessToken.getAccessToken()); assertEquals(999L, newAccessToken.getExpiresInMillis()); assertTrue(newAccessToken.getIssuedClientTimeMillis() > 0); assertEquals(ACCESS_TOKEN.getRefreshToken(), newAccessToken.getRefreshToken()); }
@Override @NonNull public LineApiResponse<LineAccessToken> refreshAccessToken() { InternalAccessToken accessToken; try { accessToken = accessTokenCache.getAccessToken(); } catch (Exception exception) { return LineApiResponse.createAsError( LineApiResponseCode.INTERNAL_ERROR, new LineApiError("get access token fail:" + exception.getMessage()) ); } if (accessToken == null || TextUtils.isEmpty(accessToken.getRefreshToken())) { return LineApiResponse.createAsError( LineApiResponseCode.INTERNAL_ERROR, new LineApiError("access token or refresh token is not found.")); } LineApiResponse<RefreshTokenResult> response = oauthApiClient.refreshToken(channelId, accessToken); if (!response.isSuccess()) { return LineApiResponse.createAsError( response.getResponseCode(), response.getErrorData()); } RefreshTokenResult refreshTokenResult = response.getResponseData(); String refreshToken = TextUtils.isEmpty(refreshTokenResult.getRefreshToken()) ? accessToken.getRefreshToken() : refreshTokenResult.getRefreshToken(); InternalAccessToken newToken = new InternalAccessToken( refreshTokenResult.getAccessToken(), refreshTokenResult.getExpiresInMillis(), System.currentTimeMillis() , refreshToken); try { accessTokenCache.saveAccessToken(newToken); } catch(Exception exception) { return LineApiResponse.createAsError( LineApiResponseCode.INTERNAL_ERROR, new LineApiError("save access token fail:" + exception.getMessage()) ); } return LineApiResponse.createAsSuccess(new LineAccessToken( newToken.getAccessToken(), newToken.getExpiresInMillis(), newToken.getIssuedClientTimeMillis())); }
LineApiClientImpl implements LineApiClient { @Override @NonNull public LineApiResponse<LineAccessToken> refreshAccessToken() { InternalAccessToken accessToken; try { accessToken = accessTokenCache.getAccessToken(); } catch (Exception exception) { return LineApiResponse.createAsError( LineApiResponseCode.INTERNAL_ERROR, new LineApiError("get access token fail:" + exception.getMessage()) ); } if (accessToken == null || TextUtils.isEmpty(accessToken.getRefreshToken())) { return LineApiResponse.createAsError( LineApiResponseCode.INTERNAL_ERROR, new LineApiError("access token or refresh token is not found.")); } LineApiResponse<RefreshTokenResult> response = oauthApiClient.refreshToken(channelId, accessToken); if (!response.isSuccess()) { return LineApiResponse.createAsError( response.getResponseCode(), response.getErrorData()); } RefreshTokenResult refreshTokenResult = response.getResponseData(); String refreshToken = TextUtils.isEmpty(refreshTokenResult.getRefreshToken()) ? accessToken.getRefreshToken() : refreshTokenResult.getRefreshToken(); InternalAccessToken newToken = new InternalAccessToken( refreshTokenResult.getAccessToken(), refreshTokenResult.getExpiresInMillis(), System.currentTimeMillis() , refreshToken); try { accessTokenCache.saveAccessToken(newToken); } catch(Exception exception) { return LineApiResponse.createAsError( LineApiResponseCode.INTERNAL_ERROR, new LineApiError("save access token fail:" + exception.getMessage()) ); } return LineApiResponse.createAsSuccess(new LineAccessToken( newToken.getAccessToken(), newToken.getExpiresInMillis(), newToken.getIssuedClientTimeMillis())); } }
LineApiClientImpl implements LineApiClient { @Override @NonNull public LineApiResponse<LineAccessToken> refreshAccessToken() { InternalAccessToken accessToken; try { accessToken = accessTokenCache.getAccessToken(); } catch (Exception exception) { return LineApiResponse.createAsError( LineApiResponseCode.INTERNAL_ERROR, new LineApiError("get access token fail:" + exception.getMessage()) ); } if (accessToken == null || TextUtils.isEmpty(accessToken.getRefreshToken())) { return LineApiResponse.createAsError( LineApiResponseCode.INTERNAL_ERROR, new LineApiError("access token or refresh token is not found.")); } LineApiResponse<RefreshTokenResult> response = oauthApiClient.refreshToken(channelId, accessToken); if (!response.isSuccess()) { return LineApiResponse.createAsError( response.getResponseCode(), response.getErrorData()); } RefreshTokenResult refreshTokenResult = response.getResponseData(); String refreshToken = TextUtils.isEmpty(refreshTokenResult.getRefreshToken()) ? accessToken.getRefreshToken() : refreshTokenResult.getRefreshToken(); InternalAccessToken newToken = new InternalAccessToken( refreshTokenResult.getAccessToken(), refreshTokenResult.getExpiresInMillis(), System.currentTimeMillis() , refreshToken); try { accessTokenCache.saveAccessToken(newToken); } catch(Exception exception) { return LineApiResponse.createAsError( LineApiResponseCode.INTERNAL_ERROR, new LineApiError("save access token fail:" + exception.getMessage()) ); } return LineApiResponse.createAsSuccess(new LineAccessToken( newToken.getAccessToken(), newToken.getExpiresInMillis(), newToken.getIssuedClientTimeMillis())); } LineApiClientImpl(
            @NonNull String channelId,
            @NonNull LineAuthenticationApiClient oauthApiClient,
            @NonNull TalkApiClient talkApiClient,
            @NonNull AccessTokenCache accessTokenCache); }
LineApiClientImpl implements LineApiClient { @Override @NonNull public LineApiResponse<LineAccessToken> refreshAccessToken() { InternalAccessToken accessToken; try { accessToken = accessTokenCache.getAccessToken(); } catch (Exception exception) { return LineApiResponse.createAsError( LineApiResponseCode.INTERNAL_ERROR, new LineApiError("get access token fail:" + exception.getMessage()) ); } if (accessToken == null || TextUtils.isEmpty(accessToken.getRefreshToken())) { return LineApiResponse.createAsError( LineApiResponseCode.INTERNAL_ERROR, new LineApiError("access token or refresh token is not found.")); } LineApiResponse<RefreshTokenResult> response = oauthApiClient.refreshToken(channelId, accessToken); if (!response.isSuccess()) { return LineApiResponse.createAsError( response.getResponseCode(), response.getErrorData()); } RefreshTokenResult refreshTokenResult = response.getResponseData(); String refreshToken = TextUtils.isEmpty(refreshTokenResult.getRefreshToken()) ? accessToken.getRefreshToken() : refreshTokenResult.getRefreshToken(); InternalAccessToken newToken = new InternalAccessToken( refreshTokenResult.getAccessToken(), refreshTokenResult.getExpiresInMillis(), System.currentTimeMillis() , refreshToken); try { accessTokenCache.saveAccessToken(newToken); } catch(Exception exception) { return LineApiResponse.createAsError( LineApiResponseCode.INTERNAL_ERROR, new LineApiError("save access token fail:" + exception.getMessage()) ); } return LineApiResponse.createAsSuccess(new LineAccessToken( newToken.getAccessToken(), newToken.getExpiresInMillis(), newToken.getIssuedClientTimeMillis())); } LineApiClientImpl(
            @NonNull String channelId,
            @NonNull LineAuthenticationApiClient oauthApiClient,
            @NonNull TalkApiClient talkApiClient,
            @NonNull AccessTokenCache accessTokenCache); @Override @NonNull LineApiResponse<?> logout(); @Override @NonNull LineApiResponse<LineAccessToken> refreshAccessToken(); @Override @NonNull LineApiResponse<LineCredential> verifyToken(); @Override @NonNull LineApiResponse<LineAccessToken> getCurrentAccessToken(); @Override @TokenAutoRefresh @NonNull LineApiResponse<LineProfile> getProfile(); @Override @TokenAutoRefresh @NonNull LineApiResponse<LineFriendshipStatus> getFriendshipStatus(); @Override @TokenAutoRefresh @NonNull LineApiResponse<GetFriendsResponse> getFriends(
            @NonNull FriendSortField sort,
            @Nullable String nextPageRequestToken); @Override @TokenAutoRefresh @NonNull LineApiResponse<GetFriendsResponse> getFriends(
            @NonNull FriendSortField sort,
            @Nullable String nextPageRequestToken,
            boolean isForOttShareMessage); @Override @TokenAutoRefresh @NonNull LineApiResponse<GetFriendsResponse> getFriendsApprovers(
            FriendSortField sort,
            @Nullable String nextPageRequestToken); @Override @TokenAutoRefresh @NonNull LineApiResponse<GetGroupsResponse> getGroups(@Nullable String nextPageRequestToken); @Override @TokenAutoRefresh @NonNull LineApiResponse<GetGroupsResponse> getGroups(
            @Nullable String nextPageRequestToken,
            boolean isForOttShareMessage); @Override @TokenAutoRefresh @NonNull LineApiResponse<GetFriendsResponse> getGroupApprovers(
            @NonNull String groupId,
            @Nullable String nextPageRequestToken); @Override @TokenAutoRefresh @NonNull LineApiResponse<String> sendMessage(
            @NonNull String targetUserId,
            @NonNull List<MessageData> messages); @Override @TokenAutoRefresh @NonNull LineApiResponse<List<SendMessageResponse>> sendMessageToMultipleUsers(
            @NonNull List<String> targetUserIds,
            @NonNull List<MessageData> messages); @Override @TokenAutoRefresh @NonNull LineApiResponse<List<SendMessageResponse>> sendMessageToMultipleUsers(
            @NonNull List<String> targetUserIds,
            @NonNull List<MessageData> messages,
            boolean isOttUsed); @NonNull LineApiResponse<Boolean> getOpenChatAgreementStatus(); @NonNull LineApiResponse<OpenChatRoomInfo> createOpenChatRoom(@NonNull OpenChatParameters openChatParameters); @NonNull LineApiResponse<Boolean> joinOpenChatRoom(@NonNull String roomId, @NonNull String displayName); @NonNull LineApiResponse<OpenChatRoomStatus> getOpenChatRoomStatus(@NonNull String roomId); @NonNull LineApiResponse<MembershipStatus> getOpenChatMembershipStatus(@NonNull String roomId); @NonNull LineApiResponse<OpenChatRoomJoinType> getOpenChatRoomJoinType(@NonNull String roomId); }
LineApiClientImpl implements LineApiClient { @Override @NonNull public LineApiResponse<LineAccessToken> refreshAccessToken() { InternalAccessToken accessToken; try { accessToken = accessTokenCache.getAccessToken(); } catch (Exception exception) { return LineApiResponse.createAsError( LineApiResponseCode.INTERNAL_ERROR, new LineApiError("get access token fail:" + exception.getMessage()) ); } if (accessToken == null || TextUtils.isEmpty(accessToken.getRefreshToken())) { return LineApiResponse.createAsError( LineApiResponseCode.INTERNAL_ERROR, new LineApiError("access token or refresh token is not found.")); } LineApiResponse<RefreshTokenResult> response = oauthApiClient.refreshToken(channelId, accessToken); if (!response.isSuccess()) { return LineApiResponse.createAsError( response.getResponseCode(), response.getErrorData()); } RefreshTokenResult refreshTokenResult = response.getResponseData(); String refreshToken = TextUtils.isEmpty(refreshTokenResult.getRefreshToken()) ? accessToken.getRefreshToken() : refreshTokenResult.getRefreshToken(); InternalAccessToken newToken = new InternalAccessToken( refreshTokenResult.getAccessToken(), refreshTokenResult.getExpiresInMillis(), System.currentTimeMillis() , refreshToken); try { accessTokenCache.saveAccessToken(newToken); } catch(Exception exception) { return LineApiResponse.createAsError( LineApiResponseCode.INTERNAL_ERROR, new LineApiError("save access token fail:" + exception.getMessage()) ); } return LineApiResponse.createAsSuccess(new LineAccessToken( newToken.getAccessToken(), newToken.getExpiresInMillis(), newToken.getIssuedClientTimeMillis())); } LineApiClientImpl(
            @NonNull String channelId,
            @NonNull LineAuthenticationApiClient oauthApiClient,
            @NonNull TalkApiClient talkApiClient,
            @NonNull AccessTokenCache accessTokenCache); @Override @NonNull LineApiResponse<?> logout(); @Override @NonNull LineApiResponse<LineAccessToken> refreshAccessToken(); @Override @NonNull LineApiResponse<LineCredential> verifyToken(); @Override @NonNull LineApiResponse<LineAccessToken> getCurrentAccessToken(); @Override @TokenAutoRefresh @NonNull LineApiResponse<LineProfile> getProfile(); @Override @TokenAutoRefresh @NonNull LineApiResponse<LineFriendshipStatus> getFriendshipStatus(); @Override @TokenAutoRefresh @NonNull LineApiResponse<GetFriendsResponse> getFriends(
            @NonNull FriendSortField sort,
            @Nullable String nextPageRequestToken); @Override @TokenAutoRefresh @NonNull LineApiResponse<GetFriendsResponse> getFriends(
            @NonNull FriendSortField sort,
            @Nullable String nextPageRequestToken,
            boolean isForOttShareMessage); @Override @TokenAutoRefresh @NonNull LineApiResponse<GetFriendsResponse> getFriendsApprovers(
            FriendSortField sort,
            @Nullable String nextPageRequestToken); @Override @TokenAutoRefresh @NonNull LineApiResponse<GetGroupsResponse> getGroups(@Nullable String nextPageRequestToken); @Override @TokenAutoRefresh @NonNull LineApiResponse<GetGroupsResponse> getGroups(
            @Nullable String nextPageRequestToken,
            boolean isForOttShareMessage); @Override @TokenAutoRefresh @NonNull LineApiResponse<GetFriendsResponse> getGroupApprovers(
            @NonNull String groupId,
            @Nullable String nextPageRequestToken); @Override @TokenAutoRefresh @NonNull LineApiResponse<String> sendMessage(
            @NonNull String targetUserId,
            @NonNull List<MessageData> messages); @Override @TokenAutoRefresh @NonNull LineApiResponse<List<SendMessageResponse>> sendMessageToMultipleUsers(
            @NonNull List<String> targetUserIds,
            @NonNull List<MessageData> messages); @Override @TokenAutoRefresh @NonNull LineApiResponse<List<SendMessageResponse>> sendMessageToMultipleUsers(
            @NonNull List<String> targetUserIds,
            @NonNull List<MessageData> messages,
            boolean isOttUsed); @NonNull LineApiResponse<Boolean> getOpenChatAgreementStatus(); @NonNull LineApiResponse<OpenChatRoomInfo> createOpenChatRoom(@NonNull OpenChatParameters openChatParameters); @NonNull LineApiResponse<Boolean> joinOpenChatRoom(@NonNull String roomId, @NonNull String displayName); @NonNull LineApiResponse<OpenChatRoomStatus> getOpenChatRoomStatus(@NonNull String roomId); @NonNull LineApiResponse<MembershipStatus> getOpenChatMembershipStatus(@NonNull String roomId); @NonNull LineApiResponse<OpenChatRoomJoinType> getOpenChatRoomJoinType(@NonNull String roomId);  }