@Override public HttpResponse get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return doGet(); }
@Override public void setStatusCode(int code) throws IllegalStateException { this.statusLine.setStatusCode(code); }
public void setStatusCode(int code) { this.response.setStatusCode(code); }
@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 public void testResolveInstrumentationKeyWithPendingStatus() { //setup MockProfileFetcher mockFetcher = new MockProfileFetcher(); mockFetcher.setAppIdToReturn("appId"); mockFetcher.setResultStatus(ProfileFetcherResultTaskStatus.PENDING); InstrumentationKeyResolver.INSTANCE.setProfileFetcher(mockFetcher); //run String appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey("ikey"); //validate Assert.assertNull(appId); Assert.assertEquals(1, mockFetcher.callCount()); //mimic calling resolver again after some time mockFetcher.setResultStatus(ProfileFetcherResultTaskStatus.COMPLETE); appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey("ikey"); Assert.assertEquals("cid-v1:appId", appId); //fetcher will be called again since the task was pending (i.e. not yet in the cache) Assert.assertEquals(2, mockFetcher.callCount()); }
@Test public void testResolveInstrumentationKey() { //setup MockProfileFetcher mockFetcher = new MockProfileFetcher(); mockFetcher.setAppIdToReturn("id1"); InstrumentationKeyResolver.INSTANCE.setProfileFetcher(mockFetcher); //run String appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey("ikey"); //validate Assert.assertEquals("cid-v1:id1", appId); Assert.assertEquals(1, mockFetcher.callCount()); }
public MockHttpAsyncClientWrapper() { this.entity = new MockHttpEntity(); this.response = new MockHttpResponse(this.entity, 200); this.task = new MockHttpTask(this.response); this.mockClient = mock(CloseableHttpAsyncClient.class); when(mockClient.execute(any(HttpUriRequest.class), any(FutureCallback.class))).thenReturn(this.task); }
@Test public void testInstrumentationKeyIsResolvedDuringModuleInit() { Assert.assertEquals(0, mockProfileFetcher.callCount()); String ikey = TelemetryConfiguration.getActive().getInstrumentationKey(); //calling resolver now will actually retrieve the appId from the completed task mockProfileFetcher.setResultStatus(ProfileFetcherResultTaskStatus.COMPLETE); mockProfileFetcher.setAppIdToReturn("someAppId"); String appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey(ikey); Assert.assertEquals(1, mockProfileFetcher.callCount()); Assert.assertEquals("cid-v1:someAppId", appId); //calling it again should retrieve appId from cache (i.e. fetcher call count remains 2) appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey(ikey); Assert.assertEquals(1, mockProfileFetcher.callCount()); Assert.assertEquals("cid-v1:someAppId", appId); }
@Before public void testInitialize() { // initialize mock profile fetcher (for resolving ikeys to appIds) mockProfileFetcher = new MockProfileFetcher(); mockProfileFetcher.setResultStatus(ProfileFetcherResultTaskStatus.PENDING); InstrumentationKeyResolver.INSTANCE.setProfileFetcher(mockProfileFetcher); InstrumentationKeyResolver.INSTANCE.clearCache(); defaultModule = new WebRequestTrackingTelemetryModule(); defaultModule.initialize(TelemetryConfiguration.getActive()); channel.reset(); }
public void setTaskAsComplete() { this.task.setIsDone(true); }
@Before public void testInitialize() { // initialize mock profile fetcher (for resolving ikeys to appIds) mockProfileFetcher = new MockProfileFetcher(); InstrumentationKeyResolver.INSTANCE.setProfileFetcher(mockProfileFetcher); InstrumentationKeyResolver.INSTANCE.clearCache(); }
public void setFailureOn(boolean fail) { this.task.setFailureOn(fail); }
public void setAppId(String appId) { this.entity.setContent(appId); }
public MockHttpResponse(HttpEntity entity, int statusCode) { this.entity = entity; this.statusLine = new MockStatusLine(new ProtocolVersion("http", 1, 1), statusCode, "reason"); }
@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."); }
@Test public void testResolveInstrumentationKeyWithFailedStatus() { //setup MockProfileFetcher mockFetcher = new MockProfileFetcher(); mockFetcher.setAppIdToReturn("appId"); mockFetcher.setResultStatus(ProfileFetcherResultTaskStatus.FAILED); InstrumentationKeyResolver.INSTANCE.setProfileFetcher(mockFetcher); //run String appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey("ikey"); //validate Assert.assertNull(appId); Assert.assertEquals(1, mockFetcher.callCount()); //mimic calling resolver again after some time mockFetcher.setResultStatus(ProfileFetcherResultTaskStatus.COMPLETE); appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey("ikey"); Assert.assertEquals("cid-v1:appId", appId); //fetcher will be called again since the previous attempt failed Assert.assertEquals(2, mockFetcher.callCount()); }
@Test public void testIkeyResolvedFromCache() { //setup MockProfileFetcher mockFetcher = new MockProfileFetcher(); mockFetcher.setAppIdToReturn("appId"); InstrumentationKeyResolver.INSTANCE.setProfileFetcher(mockFetcher); //run Assert.assertEquals(0, mockFetcher.callCount()); InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey("ikey"); Assert.assertEquals(1, mockFetcher.callCount()); //resolving the same ikey should not generate new call to fetcher InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey("ikey"); Assert.assertEquals(1, mockFetcher.callCount()); //resolving another ikey increases call count InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey("ikey2"); Assert.assertEquals(2, mockFetcher.callCount()); } }
@Override public HttpResponse get() throws InterruptedException, ExecutionException { return doGet(); }
public void setTaskAsPending() { this.task.setIsDone(false); }
@Before public void testInitialize() { // initialize mock profile fetcher (for resolving ikeys to appIds) mockProfileFetcher = new MockProfileFetcher(); InstrumentationKeyResolver.INSTANCE.setProfileFetcher(mockProfileFetcher); InstrumentationKeyResolver.INSTANCE.clearCache(); }