ParseConfigController { public Task<ParseConfig> getAsync(String sessionToken) { final ParseRESTCommand command = ParseRESTConfigCommand.fetchConfigCommand(sessionToken); return command.executeAsync(restClient).onSuccessTask(new Continuation<JSONObject, Task<ParseConfig>>() { @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; } }); } }); } ParseConfigController(ParseHttpClient restClient,
                                 ParseCurrentConfigController currentConfigController); Task<ParseConfig> getAsync(String sessionToken);  }
@Test public void testGetAsyncSuccess() throws Exception { final Date date = new Date(); final ParseFile file = new ParseFile( new ParseFile.State.Builder().name("image.png").url("http: final ParseGeoPoint geoPoint = new ParseGeoPoint(44.484, 26.029); final List<Object> list = new ArrayList<Object>() {{ add("foo"); add("bar"); add("baz"); }}; final Map<String, Object> map = new HashMap<String, Object>() {{ put("first", "foo"); put("second", "bar"); put("third", "baz"); }}; final Map<String, Object> sampleConfigParameters = new HashMap<String, Object>() {{ put("string", "value"); put("int", 42); put("double", 0.2778); put("trueBool", true); put("falseBool", false); put("date", date); put("file", file); put("geoPoint", geoPoint); put("array", list); put("object", map); }}; JSONObject responseJson = new JSONObject() {{ put("params", NoObjectsEncoder.get().encode(sampleConfigParameters)); }}; ParseHttpClient restClient = mockParseHttpClientWithResponse(responseJson, 200, "OK"); ParseCurrentConfigController currentConfigController = mockParseCurrentConfigController(); ParseConfigController configController = new ParseConfigController(restClient, currentConfigController); Task<ParseConfig> configTask = configController.getAsync(null); ParseConfig config = ParseTaskUtils.wait(configTask); verify(restClient, times(1)).execute(any(ParseHttpRequest.class)); verify(currentConfigController, times(1)).setCurrentConfigAsync(eq(config)); Map<String, Object> paramsAgain = config.getParams(); assertEquals(10, paramsAgain.size()); assertEquals("value", paramsAgain.get("string")); assertEquals(42, paramsAgain.get("int")); assertEquals(0.2778, paramsAgain.get("double")); assertTrue((Boolean) paramsAgain.get("trueBool")); assertFalse((Boolean) paramsAgain.get("falseBool")); assertEquals(date, paramsAgain.get("date")); ParseFile fileAgain = (ParseFile) paramsAgain.get("file"); assertEquals(file.getUrl(), fileAgain.getUrl()); assertEquals(file.getName(), fileAgain.getName()); ParseGeoPoint geoPointAgain = (ParseGeoPoint) paramsAgain.get("geoPoint"); assertEquals(geoPoint.getLatitude(), geoPointAgain.getLatitude(), 0.0000001); assertEquals(geoPoint.getLongitude(), geoPointAgain.getLongitude(), 0.0000001); List<Object> listAgain = (List<Object>) paramsAgain.get("array"); assertArrayEquals(list.toArray(), listAgain.toArray()); Map<String, Object> mapAgain = (Map<String, Object>) paramsAgain.get("object"); assertEquals(map.size(), mapAgain.size()); for (Map.Entry<String, Object> entry : map.entrySet()) { assertEquals(entry.getValue(), mapAgain.get(entry.getKey())); } }
@Test public void testGetAsyncFailureWithConnectionFailure() throws Exception { ParseRequest.setDefaultInitialRetryDelay(1L); 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); configTask.waitForCompletion(); 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, times(0)).setCurrentConfigAsync(any(ParseConfig.class)); }