Search in sources :

Example 1 with TokenCredentialCache

use of com.microsoft.azure.sdk.iot.service.auth.TokenCredentialCache in project azure-iot-sdk-java by Azure.

the class TokenCredentialCacheTest method tokenCredentialRenewsExpiredToken.

@Test
public void tokenCredentialRenewsExpiredToken() {
    TokenCredentialCache cache = new TokenCredentialCache(mockTokenCredential);
    new Expectations() {

        {
            mockTokenCredential.getToken((TokenRequestContext) any).block();
            result = mockAccessToken;
            mockAccessToken.getExpiresAt();
            result = OffsetDateTime.MAX;
        }
    };
    AccessToken accessToken = cache.getAccessToken();
    // Token expired one minute ago
    final long milliseconds = System.currentTimeMillis() - (60 * 1000);
    new Expectations() {

        {
            mockAccessToken.getExpiresAt();
            result = Instant.ofEpochMilli(milliseconds).atOffset(ZoneOffset.UTC);
            mockTokenCredential.getToken((TokenRequestContext) any).block();
            result = mockAccessToken2;
        }
    };
    // act
    AccessToken accessToken2 = cache.getAccessToken();
    // assert
    assertEquals(mockAccessToken, accessToken);
    assertEquals(mockAccessToken2, accessToken2);
}
Also used : Expectations(mockit.Expectations) TokenRequestContext(com.azure.core.credential.TokenRequestContext) AccessToken(com.azure.core.credential.AccessToken) TokenCredentialCache(com.microsoft.azure.sdk.iot.service.auth.TokenCredentialCache) Test(org.junit.Test)

Example 2 with TokenCredentialCache

use of com.microsoft.azure.sdk.iot.service.auth.TokenCredentialCache in project azure-iot-sdk-java by Azure.

the class TokenCredentialCacheTest method tokenCredentialDoesNotRenewTooProactively.

@Test
public void tokenCredentialDoesNotRenewTooProactively() {
    TokenCredentialCache cache = new TokenCredentialCache(mockTokenCredential);
    new Expectations() {

        {
            mockTokenCredential.getToken((TokenRequestContext) any).block();
            result = mockAccessToken;
            mockAccessToken.getExpiresAt();
            result = OffsetDateTime.MAX;
        }
    };
    AccessToken accessToken = cache.getAccessToken();
    // 12 minutes from the current time, should not fit within the proactive renewal range, so the cached token shouldn't be renewed
    final long milliseconds = System.currentTimeMillis() + (12 * 60 * 1000);
    new Expectations() {

        {
            mockAccessToken.getExpiresAt();
            result = Instant.ofEpochMilli(milliseconds).atOffset(ZoneOffset.UTC);
        }
    };
    // act
    AccessToken accessToken2 = cache.getAccessToken();
    // assert
    assertEquals(mockAccessToken, accessToken);
    assertEquals(mockAccessToken, accessToken2);
}
Also used : Expectations(mockit.Expectations) TokenRequestContext(com.azure.core.credential.TokenRequestContext) AccessToken(com.azure.core.credential.AccessToken) TokenCredentialCache(com.microsoft.azure.sdk.iot.service.auth.TokenCredentialCache) Test(org.junit.Test)

Example 3 with TokenCredentialCache

use of com.microsoft.azure.sdk.iot.service.auth.TokenCredentialCache in project azure-iot-sdk-java by Azure.

the class TokenCredentialCacheTest method tokenCredentialProactivelyRenewsToken.

@Test
public void tokenCredentialProactivelyRenewsToken() {
    TokenCredentialCache cache = new TokenCredentialCache(mockTokenCredential);
    new Expectations() {

        {
            mockTokenCredential.getToken((TokenRequestContext) any).block();
            result = mockAccessToken;
            mockAccessToken.getExpiresAt();
            result = OffsetDateTime.MAX;
        }
    };
    AccessToken accessToken = cache.getAccessToken();
    // 8 minutes from the current time, should fit within the proactive renewal range
    final long milliseconds = System.currentTimeMillis() + (8 * 60 * 1000);
    new Expectations() {

        {
            mockAccessToken.getExpiresAt();
            result = Instant.ofEpochMilli(milliseconds).atOffset(ZoneOffset.UTC);
            mockTokenCredential.getToken((TokenRequestContext) any).block();
            result = mockAccessToken2;
        }
    };
    // act
    AccessToken accessToken2 = cache.getAccessToken();
    // assert
    assertEquals(mockAccessToken, accessToken);
    assertEquals(mockAccessToken2, accessToken2);
}
Also used : Expectations(mockit.Expectations) TokenRequestContext(com.azure.core.credential.TokenRequestContext) AccessToken(com.azure.core.credential.AccessToken) TokenCredentialCache(com.microsoft.azure.sdk.iot.service.auth.TokenCredentialCache) Test(org.junit.Test)

Example 4 with TokenCredentialCache

use of com.microsoft.azure.sdk.iot.service.auth.TokenCredentialCache in project azure-iot-sdk-java by Azure.

the class TokenCredentialCacheTest method tokenCredentialCachesToken.

@Test
public void tokenCredentialCachesToken() {
    TokenCredentialCache cache = new TokenCredentialCache(mockTokenCredential);
    new Expectations() {

        {
            mockTokenCredential.getToken((TokenRequestContext) any).block();
            result = mockAccessToken;
            mockAccessToken.getExpiresAt();
            result = OffsetDateTime.MAX;
        }
    };
    AccessToken accessToken = cache.getAccessToken();
    AccessToken accessToken2 = cache.getAccessToken();
    assertEquals(mockAccessToken, accessToken);
    assertEquals(mockAccessToken, accessToken2);
}
Also used : Expectations(mockit.Expectations) TokenRequestContext(com.azure.core.credential.TokenRequestContext) AccessToken(com.azure.core.credential.AccessToken) TokenCredentialCache(com.microsoft.azure.sdk.iot.service.auth.TokenCredentialCache) Test(org.junit.Test)

Aggregations

AccessToken (com.azure.core.credential.AccessToken)4 TokenRequestContext (com.azure.core.credential.TokenRequestContext)4 TokenCredentialCache (com.microsoft.azure.sdk.iot.service.auth.TokenCredentialCache)4 Expectations (mockit.Expectations)4 Test (org.junit.Test)4