Java Code Examples for com.linecorp.linesdk.LineApiResponse#createAsSuccess()

The following examples show how to use com.linecorp.linesdk.LineApiResponse#createAsSuccess() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LineApiClientImpl.java    From line-sdk-android with Apache License 2.0 6 votes vote down vote up
@Override
@NonNull
public LineApiResponse<LineAccessToken> getCurrentAccessToken() {
    InternalAccessToken internalAccessToken;
    try {
        internalAccessToken = accessTokenCache.getAccessToken();
    } catch (Exception exception) {
        return LineApiResponse.createAsError(
                LineApiResponseCode.INTERNAL_ERROR,
                new LineApiError("get access token fail:" + exception.getMessage())
        );
    }

    if (internalAccessToken == null) {
        return LineApiResponse.createAsError(
                LineApiResponseCode.INTERNAL_ERROR,
                new LineApiError("The cached access token does not exist."));
    }

    return LineApiResponse.createAsSuccess(new LineAccessToken(
            internalAccessToken.getAccessToken(),
            internalAccessToken.getExpiresInMillis(),
            internalAccessToken.getIssuedClientTimeMillis()));
}
 
Example 2
Source File: LineApiClientImplTest.java    From line-sdk-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testRefreshToken() {
    accessTokenCache.saveAccessToken(ACCESS_TOKEN);
    LineApiResponse<RefreshTokenResult> refreshTokenResponse =
            LineApiResponse.createAsSuccess(new RefreshTokenResult(
                    "newAccessToken", 999L, "newRefreshToken", 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());
    InternalAccessToken newAccessToken =
            accessTokenCache.getAccessToken();
    assertNotNull(newAccessToken);
    assertEquals("newAccessToken", newAccessToken.getAccessToken());
    assertEquals(999L, newAccessToken.getExpiresInMillis());
    assertTrue(newAccessToken.getIssuedClientTimeMillis() > 0);
    assertEquals("newRefreshToken", newAccessToken.getRefreshToken());
}
 
Example 3
Source File: LineApiClientImplTest.java    From line-sdk-android with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 4
Source File: TalkApiClientTest.java    From line-sdk-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMessageToMultipleUsersUsingTargetUserIds() {
    final LineApiResponse<?> mockResponse = LineApiResponse.createAsSuccess("");
    givenPostWithJsonApiResponse(mockResponse);

    LineApiResponse<List<SendMessageResponse>> actualResult =
            target.sendMessageToMultipleUsers(
                    ACCESS_TOKEN,
                    Collections.emptyList(),
                    Collections.emptyList(),
                    false);

    assertThat(actualResult, sameInstance(mockResponse));
    verifyApiCallPostWithJson("/message/v3/multisend");
    responseParserInstanceShouldBe(TalkApiClient.MultiSendResponseParser.class);
}
 
Example 5
Source File: TalkApiClientTest.java    From line-sdk-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMessageToMultipleUsersUsingOtt() {
    final LineApiResponse<?> mockResponse = LineApiResponse.createAsSuccess("");
    givenPostWithJsonApiResponse(mockResponse);

    LineApiResponse<List<SendMessageResponse>> actualResult =
            target.sendMessageToMultipleUsers(
                    ACCESS_TOKEN,
                    Collections.emptyList(),
                    Collections.emptyList(),
                    true);

    assertThat(actualResult, sameInstance(mockResponse));

    // Gets ott by target user ids
    verifyApiCallPostWithJson("/" + BASE_PATH_MESSAGE_API + "/" + PATH_OTT_ISSUE);
    responseParserInstanceShouldBe(TalkApiClient.StringParser.class);

    // Send the message using ott
    verifyApiCallPostWithJson("/" + BASE_PATH_MESSAGE_API + "/" + PATH_OTT_SHARE);
    responseParserInstanceShouldBe(TalkApiClient.MultiSendResponseParser.class);
}
 
Example 6
Source File: LineAuthenticationApiClientTest.java    From line-sdk-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testRevokeAccessToken() throws Exception {
    LineApiResponse<?> expectedResponse = LineApiResponse.createAsSuccess(new Object());
    doReturn(expectedResponse).when(httpClient).post(
            any(Uri.class),
            anyMapOf(String.class, String.class),
            anyMapOf(String.class, String.class),
            any(ResponseDataParser.class));

    LineApiResponse<?> actualResponse = target.revokeAccessToken(CHANNEL_ID, ACCESS_TOKEN);

    assertSame(expectedResponse, actualResponse);

    Map<String, String> expectedPostData = new HashMap<>(2);
    expectedPostData.put("access_token", ACCESS_TOKEN.getAccessToken());
    expectedPostData.put("client_id", CHANNEL_ID);

    verify(httpClient, times(1)).post(
            eq(Uri.parse(API_BASE_URL + "/oauth2/v2.1/revoke")),
            eq(Collections.<String, String>emptyMap()),
            eq(expectedPostData),
            any(ResponseDataParser.class));
}
 
Example 7
Source File: LineAuthenticationApiClientTest.java    From line-sdk-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testRevokeRefreshToken() throws Exception {
    LineApiResponse<?> expectedResponse = LineApiResponse.createAsSuccess(new Object());
    doReturn(expectedResponse).when(httpClient).post(
            any(Uri.class),
            anyMapOf(String.class, String.class),
            anyMapOf(String.class, String.class),
            any(ResponseDataParser.class));

    LineApiResponse<?> actualResponse = target.revokeRefreshToken(CHANNEL_ID, ACCESS_TOKEN);

    assertSame(expectedResponse, actualResponse);

    Map<String, String> expectedPostData = new HashMap<>(2);
    expectedPostData.put("refresh_token", ACCESS_TOKEN.getRefreshToken());
    expectedPostData.put("client_id", CHANNEL_ID);

    verify(httpClient, times(1)).post(
            eq(Uri.parse(API_BASE_URL + "/oauth2/v2.1/revoke")),
            eq(Collections.<String, String>emptyMap()),
            eq(expectedPostData),
            any(ResponseDataParser.class));
}
 
Example 8
Source File: LineApiClientImpl.java    From line-sdk-android with Apache License 2.0 5 votes vote down vote up
@NonNull
private LineApiResponse<LineCredential> verifyToken(@NonNull InternalAccessToken accessToken) {
    LineApiResponse<AccessTokenVerificationResult> response =
            oauthApiClient.verifyAccessToken(accessToken);
    if (!response.isSuccess()) {
        return LineApiResponse.createAsError(
                response.getResponseCode(), response.getErrorData());
    }
    AccessTokenVerificationResult verificationResult = response.getResponseData();
    long verifiedClientTimeMillis = System.currentTimeMillis();

    try {
        accessTokenCache.saveAccessToken(
                new InternalAccessToken(
                        accessToken.getAccessToken(),
                        verificationResult.getExpiresInMillis(),
                        verifiedClientTimeMillis,
                        accessToken.getRefreshToken()));
    } catch(Exception exception) {
        return LineApiResponse.createAsError(
                LineApiResponseCode.INTERNAL_ERROR,
                new LineApiError("save access token fail:" + exception.getMessage())
        );
    }

    return LineApiResponse.createAsSuccess(
            new LineCredential(
                    new LineAccessToken(
                            accessToken.getAccessToken(),
                            verificationResult.getExpiresInMillis(),
                            verifiedClientTimeMillis),
                    verificationResult.getScopes()));
}
 
Example 9
Source File: ChannelServiceHttpClient.java    From line-sdk-android with Apache License 2.0 5 votes vote down vote up
@NonNull
private static <T> LineApiResponse<T> getChannelServiceResponse(
        @NonNull HttpURLConnection conn,
        @Nullable ResponseDataParser<T> responseDataParser,
        @NonNull ResponseDataParser<String> errorResponseParser) throws IOException {
    InputStream inputStream = getInputStreamFrom(conn);
    int httpResponseCode = conn.getResponseCode();
    try {
        if (httpResponseCode != HttpURLConnection.HTTP_OK
                && httpResponseCode != HttpURLConnection.HTTP_NO_CONTENT) {
            return LineApiResponse.createAsError(
                    LineApiResponseCode.SERVER_ERROR,
                    new LineApiError(
                            httpResponseCode,
                            errorResponseParser.getResponseData(inputStream)));
        }

        if (responseDataParser == null) {
            return LineApiResponse.createAsSuccess(null);
        }

        return LineApiResponse.createAsSuccess(responseDataParser.getResponseData(inputStream));
    } catch (IOException e) {
        // Evaluates response data parsing error as INTERNAL_ERROR
        return LineApiResponse.createAsError(
                LineApiResponseCode.INTERNAL_ERROR,
                new LineApiError(httpResponseCode, e));
    }
}
 
Example 10
Source File: LineApiClientImplTest.java    From line-sdk-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testLogout() {
    LineApiResponse<?> expectedResponse = LineApiResponse.createAsSuccess(new Object());
    doReturn(expectedResponse)
            .when(internalOauthApiClient)
            .revokeRefreshToken(anyString(), any(InternalAccessToken.class));
    accessTokenCache.saveAccessToken(ACCESS_TOKEN);

    LineApiResponse<?> actualResponse = target.logout();

    assertSame(expectedResponse, actualResponse);
    verify(internalOauthApiClient, times(1)).revokeRefreshToken(CHANNEL_ID, ACCESS_TOKEN);
    assertNull(accessTokenCache.getAccessToken());
}
 
Example 11
Source File: LineApiClientImplTest.java    From line-sdk-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetProfile() {
    LineApiResponse<LineProfile> expectedResponse = LineApiResponse.createAsSuccess(PROFILE);
    doReturn(expectedResponse)
            .when(internalTalkApiClient)
            .getProfile(any(InternalAccessToken.class));
    accessTokenCache.saveAccessToken(ACCESS_TOKEN);

    LineApiResponse<LineProfile> actualResponse = target.getProfile();

    assertSame(expectedResponse, actualResponse);
    verify(internalTalkApiClient, times(1)).getProfile(ACCESS_TOKEN);
}
 
Example 12
Source File: LineApiClientImplTest.java    From line-sdk-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetFriendshipStatus() {
    LineApiResponse<LineFriendshipStatus> expectedResponse =
            LineApiResponse.createAsSuccess(FRIENDSHIP_STATUS);
    doReturn(expectedResponse)
            .when(internalTalkApiClient)
            .getFriendshipStatus(any(InternalAccessToken.class));
    accessTokenCache.saveAccessToken(ACCESS_TOKEN);

    LineApiResponse<LineFriendshipStatus> actualResponse = target.getFriendshipStatus();

    assertSame(expectedResponse, actualResponse);
    verify(internalTalkApiClient, times(1)).getFriendshipStatus(ACCESS_TOKEN);
}
 
Example 13
Source File: LineApiClientImplTest.java    From line-sdk-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testVerifyToken() {
    LineApiResponse<AccessTokenVerificationResult> expectedResponse =
            LineApiResponse.createAsSuccess(new AccessTokenVerificationResult(
                    "1234", 3L, Arrays.asList(Scope.FRIEND, Scope.GROUP)));
    doReturn(expectedResponse)
            .when(internalOauthApiClient)
            .verifyAccessToken(any(InternalAccessToken.class));
    accessTokenCache.saveAccessToken(ACCESS_TOKEN);

    LineApiResponse<LineCredential> actualResponse = target.verifyToken();

    assertTrue(actualResponse.isSuccess());
    verify(internalOauthApiClient, times(1)).verifyAccessToken(ACCESS_TOKEN);

    LineAccessToken verifiedAccessToken = actualResponse.getResponseData().getAccessToken();
    assertEquals(ACCESS_TOKEN.getAccessToken(), verifiedAccessToken.getTokenString());
    assertEquals(3L, verifiedAccessToken.getExpiresInMillis());
    assertTrue(ACCESS_TOKEN.getIssuedClientTimeMillis()
               != verifiedAccessToken.getIssuedClientTimeMillis());
    assertEquals(3L, verifiedAccessToken.getExpiresInMillis());
    assertEquals(Arrays.asList(Scope.FRIEND, Scope.GROUP),
                 actualResponse.getResponseData().getScopes());

    InternalAccessToken newAccessToken = accessTokenCache.getAccessToken();
    assertEquals(ACCESS_TOKEN.getAccessToken(), newAccessToken.getAccessToken());
    assertEquals(3L, newAccessToken.getExpiresInMillis());
    assertTrue(newAccessToken.getIssuedClientTimeMillis()
               != ACCESS_TOKEN.getIssuedClientTimeMillis());
    assertEquals(ACCESS_TOKEN.getRefreshToken(), newAccessToken.getRefreshToken());
}