@Override public ProfileFetcherResult fetchAppProfile(String instrumentationKey) throws ExecutionException { ++callCounter; if (throwException) { throw new ExecutionException("No doughnuts for you.", null); } return new ProfileFetcherResult(this.appId, this.status); }
private String processResult(ProfileFetcherResult result, String instrumentationKey) { String appId = null; switch (result.getStatus()) { case PENDING: InternalLogger.INSTANCE.trace("InstrumentationKeyResolver - pending resolution of instrumentation key: %s", instrumentationKey); break; case FAILED: InternalLogger.INSTANCE.error("InstrumentationKeyResolver - failed to resolve instrumentation key: %s", instrumentationKey); break; case COMPLETE: InternalLogger.INSTANCE.trace("InstrumentationKeyResolver - successfully resolved instrumentation key: %s", instrumentationKey); appId = String.format(CorrelationIdFormat, result.getAppId()); break; default: InternalLogger.INSTANCE.error("InstrumentationKeyResolver - unexpected status. Instrumentation key: %s", instrumentationKey); break; } return appId; } }
private String processResult(ProfileFetcherResult result, String instrumentationKey) { String appId = null; switch (result.getStatus()) { case PENDING: InternalLogger.INSTANCE.trace("InstrumentationKeyResolver - pending resolution of instrumentation key: %s", instrumentationKey); break; case FAILED: InternalLogger.INSTANCE.error("InstrumentationKeyResolver - failed to resolve instrumentation key: %s", instrumentationKey); break; case COMPLETE: InternalLogger.INSTANCE.trace("InstrumentationKeyResolver - successfully resolved instrumentation key: %s", instrumentationKey); appId = String.format(CorrelationIdFormat, result.getAppId()); break; default: InternalLogger.INSTANCE.error("InstrumentationKeyResolver - unexpected status. Instrumentation key: %s", instrumentationKey); break; } return appId; } }
ProfileFetcherResult result = new ProfileFetcherResult(null, ProfileFetcherResultTaskStatus.PENDING); return new ProfileFetcherResult(null, ProfileFetcherResultTaskStatus.FAILED); return new ProfileFetcherResult(null, ProfileFetcherResultTaskStatus.FAILED); return new ProfileFetcherResult(appId, ProfileFetcherResultTaskStatus.COMPLETE);
@Test public void testFetchApplicationIdMultipleIkeys() throws InterruptedException, ExecutionException, ParseException, IOException { //setup MockHttpAsyncClientWrapper clientWrapper = new MockHttpAsyncClientWrapper(); clientWrapper.setAppId("AppId"); clientWrapper.setFailureOn(false); CdsProfileFetcher fetcher = new CdsProfileFetcher(); fetcher.setHttpClient(clientWrapper.getClient()); // the first time we try to fetch the profile, we should get a "pending" task status // since the profile fetcher uses asynchronous calls to retrieve the profile from CDS ProfileFetcherResult result = fetcher.fetchAppProfile("ikey"); Assert.assertEquals(ProfileFetcherResultTaskStatus.PENDING, result.getStatus()); Assert.assertNull(result.getAppId()); // call for a second ikey, should also return "pending" result = fetcher.fetchAppProfile("ikey2"); Assert.assertEquals(ProfileFetcherResultTaskStatus.PENDING, result.getStatus()); Assert.assertNull(result.getAppId()); // mimic task completion clientWrapper.setTaskAsComplete(); result = fetcher.fetchAppProfile("ikey"); Assert.assertEquals(ProfileFetcherResultTaskStatus.COMPLETE, result.getStatus()); Assert.assertEquals("AppId", result.getAppId()); clientWrapper.setAppId("AppId2"); result = fetcher.fetchAppProfile("ikey2"); Assert.assertEquals(ProfileFetcherResultTaskStatus.COMPLETE, result.getStatus()); Assert.assertEquals("AppId2", result.getAppId()); }
ProfileFetcherResult result = new ProfileFetcherResult(null, ProfileFetcherResultTaskStatus.PENDING); return new ProfileFetcherResult(null, ProfileFetcherResultTaskStatus.FAILED); return new ProfileFetcherResult(null, ProfileFetcherResultTaskStatus.FAILED); return new ProfileFetcherResult(appId, ProfileFetcherResultTaskStatus.COMPLETE);
@Test public void testFetchApplicationId() throws InterruptedException, ExecutionException, ParseException, IOException { //setup MockHttpAsyncClientWrapper clientWrapper = new MockHttpAsyncClientWrapper(); clientWrapper.setAppId("AppId"); clientWrapper.setFailureOn(false); CdsProfileFetcher fetcher = new CdsProfileFetcher(); fetcher.setHttpClient(clientWrapper.getClient()); // the first time we try to fetch the profile, we might get a "pending" task status // since the profile fetcher uses asynchronous calls to retrieve the profile from CDS // this is mimic'ed with clientWrapper.setTaskAsPending(); clientWrapper.setTaskAsPending(); ProfileFetcherResult result = fetcher.fetchAppProfile("ikey"); Assert.assertEquals(ProfileFetcherResultTaskStatus.PENDING, result.getStatus()); Assert.assertNull(result.getAppId()); // mimic task completion clientWrapper.setTaskAsComplete(); result = fetcher.fetchAppProfile("ikey"); Assert.assertEquals(ProfileFetcherResultTaskStatus.COMPLETE, result.getStatus()); Assert.assertEquals("AppId", result.getAppId()); }
@Test public void testFetchApplicationIdFailureWithNon200StatusCode() throws InterruptedException, ExecutionException, ParseException, IOException { //setup MockHttpAsyncClientWrapper clientWrapper = new MockHttpAsyncClientWrapper(); clientWrapper.setAppId("AppId"); clientWrapper.setFailureOn(false); CdsProfileFetcher fetcher = new CdsProfileFetcher(); fetcher.setHttpClient(clientWrapper.getClient()); // the first time we try to fetch the profile, we might get a "pending" task status // since the profile fetcher uses asynchronous calls to retrieve the profile from CDS // this is mimic'ed with clientWrapper.setTaskAsPending(); clientWrapper.setTaskAsPending(); ProfileFetcherResult result = fetcher.fetchAppProfile("ikey"); Assert.assertEquals(ProfileFetcherResultTaskStatus.PENDING, result.getStatus()); Assert.assertNull(result.getAppId()); // mimic task completion with 404 status code clientWrapper.setTaskAsComplete(); clientWrapper.setStatusCode(404); result = fetcher.fetchAppProfile("ikey"); Assert.assertEquals(ProfileFetcherResultTaskStatus.FAILED, result.getStatus()); Assert.assertNull(result.getAppId()); }
@Test public void testFetchApplicationIdWithTaskCompleteImmediately() throws InterruptedException, ExecutionException, ParseException, IOException { //setup MockHttpAsyncClientWrapper clientWrapper = new MockHttpAsyncClientWrapper(); clientWrapper.setAppId("AppId"); clientWrapper.setFailureOn(false); clientWrapper.setTaskAsComplete(); CdsProfileFetcher fetcher = new CdsProfileFetcher(); fetcher.setHttpClient(clientWrapper.getClient()); // task is completed right away ProfileFetcherResult result = fetcher.fetchAppProfile("ikey"); Assert.assertEquals(ProfileFetcherResultTaskStatus.COMPLETE, result.getStatus()); Assert.assertEquals("AppId", result.getAppId()); }
@Test(expected = ExecutionException.class) public void testFetchApplicationIdFailureWithException() throws InterruptedException, ExecutionException, ParseException, IOException { //setup - mimic timeout from the async http call MockHttpAsyncClientWrapper clientWrapper = new MockHttpAsyncClientWrapper(); clientWrapper.setAppId("AppId"); CdsProfileFetcher fetcher = new CdsProfileFetcher(); fetcher.setHttpClient(clientWrapper.getClient()); // the first time we try to fetch the profile, we should get a "pending" task status // since the profile fetcher uses asynchronous calls to retrieve the profile from CDS ProfileFetcherResult result = fetcher.fetchAppProfile("ikey"); Assert.assertEquals(ProfileFetcherResultTaskStatus.PENDING, result.getStatus()); Assert.assertNull(result.getAppId()); // instruct mock task to fail clientWrapper.setFailureOn(true); clientWrapper.setTaskAsComplete(); result = fetcher.fetchAppProfile("ikey"); Assert.fail("Should not have reached here. Instead, an exception should have been thrown."); }