@Before public void testInitialize() { // initialize mock profile fetcher (for resolving ikeys to appIds) mockProfileFetcher = new MockProfileFetcher(); InstrumentationKeyResolver.INSTANCE.setProfileFetcher(mockProfileFetcher); InstrumentationKeyResolver.INSTANCE.clearCache(); }
@Before public void testInitialize() { // initialize mock profile fetcher (for resolving ikeys to appIds) mockProfileFetcher = new MockProfileFetcher(); InstrumentationKeyResolver.INSTANCE.setProfileFetcher(mockProfileFetcher); InstrumentationKeyResolver.INSTANCE.clearCache(); }
@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()); }
@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(); }
@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 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()); } }
@Test public void testResolveInstrumentationKeyWhenExceptionThrown() { //setup MockProfileFetcher mockFetcher = new MockProfileFetcher(); mockFetcher.setAppIdToReturn("appId"); mockFetcher.setExceptionOn(true); InstrumentationKeyResolver.INSTANCE.setProfileFetcher(mockFetcher); //run String appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey("ikey"); //validate no exception is thrown back to the caller and appId is null Assert.assertNull(appId); Assert.assertEquals(1, mockFetcher.callCount()); //mimic calling resolver again after some time mockFetcher.setExceptionOn(false); mockFetcher.setResultStatus(ProfileFetcherResultTaskStatus.PENDING); appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey("ikey"); //result is still null since fetcher returns a task in pending state Assert.assertNull(appId); //fetcher will be called again since the previous attempt failed Assert.assertEquals(2, mockFetcher.callCount()); //mimic final call which returns the completed task mockFetcher.setResultStatus(ProfileFetcherResultTaskStatus.COMPLETE); appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey("ikey"); Assert.assertEquals("cid-v1:appId", appId); Assert.assertEquals(3, mockFetcher.callCount()); }