AuthManager { @WorkerThread @NonNull public String getToken() throws AuthException { String channelId = channel.getId(); if (channelId == null) { throw new AuthException("Unable to create token, channel not created"); } String cachedToken = getCachedToken(channelId); if (cachedToken != null) { return cachedToken; } try { Response<AuthToken> authResponse = apiClient.getToken(channelId); if (authResponse.getResult() != null && authResponse.isSuccessful()) { cache(authResponse.getResult()); return authResponse.getResult().getToken(); } else { throw new AuthException("Failed to generate token. Response: " + authResponse); } } catch (RequestException e) { throw new AuthException("Failed to generate token.", e); } } AuthManager(@NonNull AirshipRuntimeConfig runtimeConfig, @NonNull AirshipChannel channel); @VisibleForTesting  AuthManager(@NonNull AuthApiClient apiClient, @NonNull AirshipChannel channel, @NonNull Clock clock); @WorkerThread @NonNull String getToken(); void tokenExpired(@NonNull String token);  }
@Test public void testGetTokenFromClient() throws RequestException, AuthException { when(mockChannel.getId()).thenReturn("channel id"); when(mockClient.getToken("channel id")) .thenReturn(new Response.Builder<AuthToken>(200) .setResult(new AuthToken("channel id", "some token", 100)) .build()); assertEquals("some token", authManager.getToken()); }
@Test(expected = AuthException.class) public void testGetTokenFailed() throws RequestException, AuthException { when(mockChannel.getId()).thenReturn("channel id"); when(mockClient.getToken("channel id")) .thenReturn(new Response.Builder<AuthToken>(400) .build()); authManager.getToken(); }
@Test public void testGetTokenExpired() throws RequestException, AuthException { clock.currentTimeMillis = 0; when(mockChannel.getId()).thenReturn("channel id"); when(mockClient.getToken("channel id")) .thenReturn(new Response.Builder<AuthToken>(200) .setResult(new AuthToken("channel id", "some token", 100)) .build()) .thenReturn(new Response.Builder<AuthToken>(200) .setResult(new AuthToken("channel id", "some other token", 200)) .build()); assertEquals("some token", authManager.getToken()); clock.currentTimeMillis = 99; assertEquals("some token", authManager.getToken()); clock.currentTimeMillis = 100; assertEquals("some other token", authManager.getToken()); }
@Test public void testGetTokenChannelIdChanged() throws RequestException, AuthException { clock.currentTimeMillis = 0; when(mockChannel.getId()).thenReturn("channel id"); when(mockClient.getToken("channel id")) .thenReturn(new Response.Builder<AuthToken>(200) .setResult(new AuthToken("channel id", "some token", 100)) .build()); assertEquals("some token", authManager.getToken()); when(mockChannel.getId()).thenReturn("other channel id"); when(mockClient.getToken("other channel id")) .thenReturn(new Response.Builder<AuthToken>(200) .setResult(new AuthToken("other channel id", "some other token", 100)) .build()); assertEquals("some other token", authManager.getToken()); }