MParticleApiClientImpl extends MParticleBaseClientImpl implements MParticleApiClient { @Override public void fetchConfig() throws IOException, MPConfigException { fetchConfig(false); } MParticleApiClientImpl(ConfigManager configManager, SharedPreferences sharedPreferences, Context context); @Override void fetchConfig(); void fetchConfig(boolean force); JSONObject fetchAudiences(); int sendMessageBatch(String message); @Override AliasNetworkResponse sendAliasRequest(String message); void setCookies(JSONObject serverCookies); JSONObject getCookies();  }
@Test @PrepareForTest({URL.class, MParticleApiClientImpl.class, MPUtility.class}) public void testFetchConfigSuccess() throws Exception { setup(); PowerMockito.mockStatic(MPUtility.class); Mockito.when(MPUtility.hmacSha256Encode(Mockito.anyString(), Mockito.anyString())).thenReturn("encoded"); Mockito.when(mockConnection.getResponseCode()).thenReturn(200); JSONObject response = new JSONObject(); response.put("test", "value"); Mockito.when(MPUtility.getJsonResponse(mockConnection)).thenReturn(response); ArgumentCaptor<JSONObject> captor = ArgumentCaptor.forClass(JSONObject.class); client.fetchConfig(); Mockito.verify(configManager).updateConfig(captor.capture()); assertEquals(response, captor.getValue()); }
@Test @PrepareForTest({URL.class, MParticleApiClientImpl.class, MPUtility.class}) public void testFetchConfigFailure() throws Exception { setup(); PowerMockito.mockStatic(MPUtility.class); Mockito.when(MPUtility.hmacSha256Encode(Mockito.anyString(), Mockito.anyString())).thenReturn("encoded"); Mockito.when(mockConnection.getResponseCode()).thenReturn(400); Exception e = null; try { client.fetchConfig(); } catch (MParticleApiClientImpl.MPConfigException cfe) { e = cfe; } assertNotNull(e); }
@Test @PrepareForTest({URL.class, MParticleApiClientImpl.class, MPUtility.class}) public void testConfigDelay() throws Exception { setup(); PowerMockito.mockStatic(MPUtility.class); Mockito.when(MPUtility.hmacSha256Encode(Mockito.anyString(), Mockito.anyString())).thenReturn("encoded"); Mockito.when(mockConnection.getResponseCode()).thenReturn(400); Exception e = null; try { client.fetchConfig(); } catch (MParticleApiClientImpl.MPConfigException cfe) { e = cfe; } assertNotNull(e); e = null; try { client.fetchConfig(); } catch (MParticleApiClientImpl.MPConfigException cfe) { e = cfe; } assertNull(e); e = null; try { client.fetchConfig(true); } catch (MParticleApiClientImpl.MPConfigException cfe) { e = cfe; } assertNotNull(e); }
@Test @PrepareForTest({URL.class, MParticleApiClientImpl.class, MPUtility.class}) public void testConfigRequestWhileThrottled() throws Exception { setup(); PowerMockito.mockStatic(MPUtility.class); for (MParticleBaseClientImpl.Endpoint endpoint: MParticleBaseClientImpl.Endpoint.values()) { client.getRequestHandler().setNextRequestTime(endpoint, System.currentTimeMillis() + 1000); } Mockito.when(mockConnection.getResponseCode()).thenReturn(200); JSONObject response = new JSONObject(); response.put("test", "value"); Mockito.when(MPUtility.getJsonResponse(mockConnection)).thenReturn(response); ArgumentCaptor<JSONObject> captor = ArgumentCaptor.forClass(JSONObject.class); client.fetchConfig(); Mockito.verify(configManager).updateConfig(captor.capture()); assertEquals(response, captor.getValue()); }