/** * Fetches a new configuration object from the server. * * @return The {@code ParseConfig} that was fetched. * @throws ParseException Throws an exception if the server is inaccessible. */ public static ParseConfig get() throws ParseException { return ParseTaskUtils.wait(getInBackground()); }
/** * Fetches a new configuration object from the server in a background thread. This is preferable * to using {@link #get()}, unless your code is already running from a background thread. * * @param callback callback.done(config, e) is called when the fetch completes. */ public static void getInBackground(ConfigCallback callback) { ParseTaskUtils.callbackOnMainThreadAsync(getInBackground(), callback); }
@Test public void testGetInBackgroundWithCallbackSuccess() throws Exception { final Map<String, Object> params = new HashMap<>(); params.put("string", "value"); ParseConfig config = new ParseConfig(params); ParseConfigController controller = mockParseConfigControllerWithResponse(config); ParseCorePlugins.getInstance().registerConfigController(controller); final Semaphore done = new Semaphore(0); ParseConfig.getInBackground(new ConfigCallback() { @Override public void done(ParseConfig config, ParseException e) { assertEquals(1, config.params.size()); assertEquals("value", config.params.get("string")); done.release(); } }); // Make sure the callback is called assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS)); verify(controller, times(1)).getAsync(anyString()); }
@Test public void testGetInBackgroundWithCallbackFail() throws Exception { ParseException exception = new ParseException(ParseException.CONNECTION_FAILED, "error"); ParseConfigController controller = mockParseConfigControllerWithException(exception); ParseCorePlugins.getInstance().registerConfigController(controller); final Semaphore done = new Semaphore(0); ParseConfig.getInBackground(new ConfigCallback() { @Override public void done(ParseConfig config, ParseException e) { assertEquals(ParseException.CONNECTION_FAILED, e.getCode()); assertEquals("error", e.getMessage()); done.release(); } }); // Make sure the callback is called assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS)); verify(controller, times(1)).getAsync(anyString()); }
@Test public void testGetInBackgroundSuccess() throws Exception { final Map<String, Object> params = new HashMap<>(); params.put("string", "value"); ParseConfig config = new ParseConfig(params); ParseConfigController controller = mockParseConfigControllerWithResponse(config); ParseCorePlugins.getInstance().registerConfigController(controller); Task<ParseConfig> configTask = ParseConfig.getInBackground(); ParseTaskUtils.wait(configTask); ParseConfig configAgain = configTask.getResult(); verify(controller, times(1)).getAsync(anyString()); assertEquals(1, configAgain.params.size()); assertEquals("value", configAgain.params.get("string")); }
@SuppressWarnings("ThrowableResultOfMethodCallIgnored") @Test public void testGetInBackgroundFail() throws Exception { ParseException exception = new ParseException(ParseException.CONNECTION_FAILED, "error"); ParseConfigController controller = mockParseConfigControllerWithException(exception); ParseCorePlugins.getInstance().registerConfigController(controller); Task<ParseConfig> configTask = ParseConfig.getInBackground(); configTask.waitForCompletion(); verify(controller, times(1)).getAsync(anyString()); assertThat(configTask.getError(), instanceOf(ParseException.class)); assertEquals(ParseException.CONNECTION_FAILED, ((ParseException) configTask.getError()).getCode()); assertEquals("error", configTask.getError().getMessage()); }
@NonNull @CheckReturnValue public static Single<ParseConfig> getConfig() { return RxTask.single(() -> ParseConfig.getInBackground()); }