private ParseCurrentConfigController mockParseCurrentConfigControllerWithException( Exception exception) { ParseCurrentConfigController controller = mock(ParseCurrentConfigController.class); when(controller.getCurrentConfigAsync()) .thenReturn(Task.<ParseConfig>forError(exception)); return controller; } }
private ParseCurrentConfigController mockParseCurrentConfigControllerWithResponse( final ParseConfig result) { ParseCurrentConfigController controller = mock(ParseCurrentConfigController.class); when(controller.getCurrentConfigAsync()) .thenReturn(Task.forResult(result)); return controller; }
/** * Retrieves the most recently-fetched configuration object, either from memory or * disk if necessary. * * @return The most recently-fetched {@code ParseConfig} if it exists, else an empty * {@code ParseConfig} */ public static ParseConfig getCurrentConfig() { try { return ParseTaskUtils.wait(getConfigController().getCurrentConfigController() .getCurrentConfigAsync() ); } catch (ParseException e) { // In order to have backward compatibility, we swallow the exception silently. return new ParseConfig(); } }
@Test public void testGetCurrentConfigAsyncSuccessCurrentConfigAlreadySet() throws Exception { File configFile = new File(temporaryFolder.getRoot(), "config"); ParseCurrentConfigController currentConfigController = new ParseCurrentConfigController(configFile); ParseConfig config = new ParseConfig(); currentConfigController.currentConfig = config; Task<ParseConfig> getTask = currentConfigController.getCurrentConfigAsync(); ParseTaskUtils.wait(getTask); ParseConfig configAgain = getTask.getResult(); // Verify we get the same ParseConfig when currentConfig is set assertSame(config, configAgain); }
@Test public void testGetCurrentConfigAsyncSuccessCurrentConfigNotSetDiskConfigNotExist() throws Exception { File configFile = new File(temporaryFolder.getRoot(), "config"); ParseCurrentConfigController currentConfigController = new ParseCurrentConfigController(configFile); // Verify before set, disk config does not exist and in memory config is null assertFalse(configFile.exists()); assertNull(currentConfigController.currentConfig); Task<ParseConfig> getTask = currentConfigController.getCurrentConfigAsync(); ParseTaskUtils.wait(getTask); ParseConfig config = getTask.getResult(); // Verify after set, in memory config is set and the config we get is empty assertSame(config, currentConfigController.currentConfig); assertEquals(0, config.getParams().size()); }
@Test public void testGetCurrentConfigAsyncSuccessCurrentConfigNotSetDiskConfigExist() throws Exception { File configFile = new File(temporaryFolder.getRoot(), "config"); ParseCurrentConfigController currentConfigController = new ParseCurrentConfigController(configFile); // Save sample ParseConfig to disk final Map<String, Object> sampleConfigParameters = new HashMap<String, Object>() {{ put("string", "value"); }}; JSONObject sampleConfigJson = new JSONObject() {{ put("params", NoObjectsEncoder.get().encode(sampleConfigParameters)); }}; ParseConfig diskConfig = ParseConfig.decode(sampleConfigJson, ParseDecoder.get()); currentConfigController.saveToDisk(diskConfig); // Verify before set, disk config exist and in memory config is null assertTrue(configFile.exists()); assertNull(currentConfigController.currentConfig); Task<ParseConfig> getTask = currentConfigController.getCurrentConfigAsync(); ParseTaskUtils.wait(getTask); ParseConfig config = getTask.getResult(); // Verify after set, in memory config is set and value is correct assertSame(config, currentConfigController.currentConfig); assertEquals("value", config.get("string")); assertEquals(1, config.getParams().size()); }