GusTransport { public EndpointInfo getEndpointInfo() throws GetAuthenticationTokenFailureException { try { return endpointInfoCache.get(DUMMY_CACHE_KEY); } catch (ExecutionException | UncheckedExecutionException | ExecutionError e) { Throwable cause = e.getCause(); if (cause instanceof GetAuthenticationTokenFailureRuntimeException) { throw new GetAuthenticationTokenFailureException(cause.getMessage(), cause.getCause()); } else { throw new GetAuthenticationTokenFailureException("Getting auth token failed", e); } } } GusTransport(Optional<String> proxyHost, Optional<Integer> proxyPort, Optional<String> proxyUsername,
                        Optional<String> proxyPassword, String authEndpoint, String authClientId, String authClientSecret,
                        String authUsername, String authPassword, EndpointInfo defaultEndpointInfo,
                        long tokenCacheRefreshPeriodMillis, int connectionPoolMaxSize, int connectionPoolMaxPerRoute);  GusTransport(Optional<String> proxyHost, Optional<Integer> proxyPort, Optional<String> proxyUsername, Optional<String> proxyPassword, String authEndpoint,
                        String authClientId, String authClientSecret, String authUsername, String authPassword,
                        EndpointInfo defaultEndpointInfo, int connectionPoolMaxSize, int connectionPoolMaxPerRoute);  GusTransport(String proxyHost, String proxyPort, String proxyUsername, String proxyPassword, String authEndpoint,
                        String authClientId, String authClientSecret, String authUsername, String authPassword,
                        EndpointInfo defaultEndpointInfo, int connectionPoolMaxSize, int connectionPoolMaxPerRoute); CloseableHttpClient getHttpClient(); EndpointInfo getEndpointInfo(); EndpointInfo getEndpointInfo(boolean refresh); static final String NO_TOKEN; }
@Test public void getEndpointInfo_testFalseRefresh() throws Exception { int times = 10; boolean refresh = false; gusTransport = createGusTransportHappyCase(); EndpointInfo ei = gusTransport.getEndpointInfo(); assertEquals(TEST_INSTANCE_URL, ei.getEndPoint()); assertEquals(TEST_TOKEN, ei.getToken()); for (int i = 0; i < times; i++) { EndpointInfo ei2 = gusTransport.getEndpointInfo(refresh); assertSame(ei, ei2); } verify(httpClient, times(1)).execute(any()); verify(httpResponse, times(1)).getStatusLine(); verify(httpResponseStatusLine, times(1)).getStatusCode(); verify(httpResponse, times(1)).getEntity(); }
@Test public void getEndpointInfo_testTrueRefresh() throws Exception { int times = 10; boolean refresh = true; gusTransport = createGusTransportHappyCase(); EndpointInfo ei = gusTransport.getEndpointInfo(); assertEquals(TEST_INSTANCE_URL, ei.getEndPoint()); assertEquals(TEST_TOKEN, ei.getToken()); for (int i = 1; i < times; i++) { EndpointInfo ei2 = gusTransport.getEndpointInfo(refresh); assertEquals(ei, ei2); } verify(httpClient, times(times)).execute(any()); verify(httpResponse, times(times)).getStatusLine(); verify(httpResponseStatusLine, times(times)).getStatusCode(); verify(httpResponse, times(times)).getEntity(); }
@Test public void getEndpointInfo_testFailToInitEndpointOnConstructionWithBadResponse() throws Exception { mockCacheInitExpectations(); when(httpResponseStatusLine.getStatusCode()).thenReturn(401); gusTransport = createGusTransport(); EndpointInfo ei = gusTransport.getEndpointInfo(); assertEquals(DEFAULT_ENDPOINT, ei.getEndPoint()); assertEquals(GusTransport.NO_TOKEN, ei.getToken()); verify(httpClient, times(1)).execute(any()); verify(httpResponse, times(1)).getStatusLine(); verify(httpResponseStatusLine, times(1)).getStatusCode(); verify(httpResponse, times(1)).getEntity(); }
@Test public void getEndpointInfo_testFailToRefreshEndpointWithBadResponse() throws Exception { boolean refresh = true; mockCacheInitExpectations(); when(httpResponseStatusLine.getStatusCode()).thenReturn(200) .thenReturn(401); when(EntityUtils.toString(httpResponseEntity)) .thenReturn("{\"instance_url\": \"" + TEST_INSTANCE_URL +"\", \"access_token\": \"" + TEST_TOKEN + "\"}") .thenReturn("bad response"); gusTransport = createGusTransport(); EndpointInfo ei = gusTransport.getEndpointInfo(); assertEquals(TEST_INSTANCE_URL, ei.getEndPoint()); assertEquals(TEST_TOKEN, ei.getToken()); EndpointInfo ei2 = gusTransport.getEndpointInfo(refresh); assertSame(ei, ei2); verify(httpClient, times(2)).execute(any()); verify(httpResponse, times(2)).getStatusLine(); verify(httpResponseStatusLine, times(2)).getStatusCode(); verify(httpResponse, times(2)).getEntity(); }