@Test public void testGetFromDiskConfigSuccessFileIOException() { File configFile = new File("errorConfigFile"); ParseCurrentConfigController currentConfigController = new ParseCurrentConfigController(configFile); ParseConfig config = currentConfigController.getFromDisk(); assertNull(config); }
private ParseCurrentConfigController mockParseCurrentConfigControllerWithResponse( final ParseConfig result) { ParseCurrentConfigController controller = mock(ParseCurrentConfigController.class); when(controller.getCurrentConfigAsync()) .thenReturn(Task.forResult(result)); return controller; }
@Override public Void call() { synchronized (currentConfigMutex) { currentConfig = config; saveToDisk(config); } return null; } }, ParseExecutors.io());
@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()); }
new ParseCurrentConfigController(configFile); currentConfigController.saveToDisk(config); ParseConfig configAgain = currentConfigController.getFromDisk(); Map<String, Object> paramsAgain = configAgain.getParams(); assertEquals(10, paramsAgain.size());
@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 testSetCurrentConfigAsyncSuccess() throws Exception { File configFile = new File(temporaryFolder.getRoot(), "config"); ParseCurrentConfigController currentConfigController = new ParseCurrentConfigController(configFile); // Verify before set, file is empty and in memory config is null assertFalse(configFile.exists()); assertNull(currentConfigController.currentConfig); ParseConfig config = new ParseConfig(); ParseTaskUtils.wait(currentConfigController.setCurrentConfigAsync(config)); // Verify after set, file exists(saveToDisk is called) and in memory config is set assertTrue(configFile.exists()); assertSame(config, currentConfigController.currentConfig); }
new ParseCurrentConfigController(configFile); currentConfigController.saveToDisk(config);
private ParseCurrentConfigController mockParseCurrentConfigController() { ParseCurrentConfigController currentConfigController = mock(ParseCurrentConfigController.class); when(currentConfigController.setCurrentConfigAsync(any(ParseConfig.class))) .thenReturn(Task.<Void>forResult(null)); return currentConfigController; }
@Test public void testClearCurrentConfigForTestingSuccess() { File configFile = new File(temporaryFolder.getRoot(), "config"); ParseCurrentConfigController currentConfigController = new ParseCurrentConfigController(configFile); currentConfigController.currentConfig = new ParseConfig(); // Verify before set, in memory config is not null assertNotNull(currentConfigController.currentConfig); currentConfigController.clearCurrentConfigForTesting(); // Verify after set, in memory config is null assertNull(currentConfigController.currentConfig); }
public ParseConfigController getConfigController() { if (configController.get() == null) { // TODO(mengyan): Do not rely on Parse global File file = new File(ParsePlugins.get().getParseDir(), FILENAME_CURRENT_CONFIG); ParseCurrentConfigController currentConfigController = new ParseCurrentConfigController(file); configController.compareAndSet(null, new ParseConfigController( ParsePlugins.get().restClient(), currentConfigController)); } return configController.get(); }
@Override public ParseConfig call() { synchronized (currentConfigMutex) { if (currentConfig == null) { ParseConfig config = getFromDisk(); currentConfig = (config != null) ? config : new ParseConfig(); } } return currentConfig; } }, ParseExecutors.io());
@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()); }
@Override public Task<ParseConfig> then(Task<JSONObject> task) { JSONObject result = task.getResult(); final ParseConfig config = ParseConfig.decode(result, ParseDecoder.get()); return currentConfigController.setCurrentConfigAsync(config).continueWith(new Continuation<Void, ParseConfig>() { @Override public ParseConfig then(Task<Void> task) { return config; } }); } });
@Test public void testConstructor() { File configFile = new File(temporaryFolder.getRoot(), "config"); ParseCurrentConfigController currentConfigController = new ParseCurrentConfigController(configFile); assertNull(currentConfigController.currentConfig); }
@Test public void testGetFromDiskSuccessConfigFileNotJsonFile() throws Exception { File configFile = new File(temporaryFolder.getRoot(), "config"); ParseFileUtils.writeStringToFile(configFile, "notJson", "UTF-8"); ParseCurrentConfigController currentConfigController = new ParseCurrentConfigController(configFile); ParseConfig config = currentConfigController.getFromDisk(); assertNull(config); }
@Test public void testGetAsyncFailureWithConnectionFailure() throws Exception { // TODO(mengyan): Remove once we no longer rely on retry logic. ParseRequest.setDefaultInitialRetryDelay(1L); // Make ParseConfigController and call getAsync ParseHttpClient restClient = mock(ParseHttpClient.class); when(restClient.execute(any(ParseHttpRequest.class))).thenThrow(new IOException()); ParseCurrentConfigController currentConfigController = mockParseCurrentConfigController(); ParseConfigController configController = new ParseConfigController(restClient, currentConfigController); Task<ParseConfig> configTask = configController.getAsync(null); // Do not use ParseTaskUtils.wait() since we do not want to throw the exception configTask.waitForCompletion(); // Verify httpClient is tried enough times // TODO(mengyan): Abstract out command runner so we don't have to account for retries. verify(restClient, times(5)).execute(any(ParseHttpRequest.class)); assertTrue(configTask.isFaulted()); Exception error = configTask.getError(); assertThat(error, instanceOf(ParseException.class)); assertEquals(ParseException.CONNECTION_FAILED, ((ParseException) error).getCode()); // Verify currentConfigController is not called verify(currentConfigController, times(0)).setCurrentConfigAsync(any(ParseConfig.class)); }
private ParseCurrentConfigController mockParseCurrentConfigControllerWithException( Exception exception) { ParseCurrentConfigController controller = mock(ParseCurrentConfigController.class); when(controller.getCurrentConfigAsync()) .thenReturn(Task.<ParseConfig>forError(exception)); return controller; } }
verify(currentConfigController, times(1)).setCurrentConfigAsync(eq(config));
/** * 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(); } }