public void testGetEntities() throws Exception { final CountDownLatch latch = new CountDownLatch(1); EntityUtils.getEntities( getContext(), 0, 100, new EntityListListener() { @Override public void onList(ListResult<Entity> entities) { addResult(0, entities); latch.countDown(); } @Override public void onError(SocializeException error) { error.printStackTrace(); latch.countDown(); } }); latch.await(20, TimeUnit.SECONDS); ListResult<Entity> after = getResult(0); assertNotNull(after); assertTrue(after.size() >= 2); // Make sure it contains what we expect. JSONObject json = TestUtils.getJSON(getContext(), "entities.json"); JSONArray jsonArray = json.getJSONArray("items"); JSONObject jsonObject0 = (JSONObject) jsonArray.get(0); JSONObject jsonObject1 = (JSONObject) jsonArray.get(1); String key0 = jsonObject0.getString("key"); String key1 = jsonObject1.getString("key"); List<Entity> items = after.getItems(); int found = 0; for (Entity entity : items) { if (entity.getKey().equals(key0) || entity.getKey().equals(key1)) { found++; } if (found >= 2) { break; } } assertEquals(2, found); }
private ListResult<T> doListTypeRequest( HttpUriRequest request, ActionType type, boolean isJSONResponse) throws SocializeException { List<T> results = null; List<ActionError> errors = null; HttpEntity entity = null; ListResult<T> result = null; if (!clientFactory.isDestroyed()) { try { HttpClient client = clientFactory.getClient(); if (logger != null && logger.isDebugEnabled()) { logger.debug("Request: " + request.getMethod() + " " + request.getRequestLine().getUri()); } HttpResponse response = executeRequest(client, request); if (logger != null && logger.isDebugEnabled()) { logger.debug("Response: " + response.getStatusLine().getStatusCode()); } entity = response.getEntity(); if (httpUtils.isHttpError(response)) { if (sessionPersister != null && httpUtils.isAuthError(response)) { sessionPersister.delete(context); } String msg = ioUtils.readSafe(entity.getContent()); throw new SocializeApiError(httpUtils, response.getStatusLine().getStatusCode(), msg); } else { result = new ListResult<T>(); if (isJSONResponse) { // Read the json just for logging String json = ioUtils.readSafe(entity.getContent()); if (logger != null && logger.isDebugEnabled()) { logger.debug("JSON Response: " + json); } if (!StringUtils.isEmpty(json)) { JSONObject object = jsonParser.parseObject(json); if (object.has(JSON_ATTR_ERRORS) && !object.isNull(JSON_ATTR_ERRORS)) { JSONArray errorList = object.getJSONArray(JSON_ATTR_ERRORS); int length = errorList.length(); errors = new ArrayList<ActionError>(length); for (int i = 0; i < length; i++) { JSONObject jsonObject = errorList.getJSONObject(i); ActionError error = errorFactory.fromJSON(jsonObject); errors.add(error); } result.setErrors(errors); } if (object.has(JSON_ATTR_ITEMS) && !object.isNull(JSON_ATTR_ITEMS)) { JSONArray list = object.getJSONArray(JSON_ATTR_ITEMS); int length = list.length(); results = new ArrayList<T>(length); for (int i = 0; i < length; i++) { results.add(fromJSON(list.getJSONObject(i), type)); } result.setItems(results); } if (object.has(JSON_ATTR_COUNT) && !object.isNull(JSON_ATTR_COUNT)) { result.setTotalCount(object.getInt(JSON_ATTR_COUNT)); } } } } } catch (Throwable e) { throw SocializeException.wrap(e); } finally { closeEntity(entity); } return result; } else { if (logger != null) { logger.warn("Attempt to access HttpClientFactory that was already destroyed"); } return null; } }
public void testGetEntitiesWithSort() throws Exception { final Activity context = getContext(); // 3 entities (these are created from python script) String[] keys = {"http://entityA.com", "http://entityB.com", "http://entityC.com"}; // Get by normal means final CountDownLatch getLatch = new CountDownLatch(1); EntityUtils.getEntities( context, SortOrder.CREATION_DATE, new EntityListListener() { @Override public void onList(ListResult<Entity> entities) { addResult(0, entities); getLatch.countDown(); } @Override public void onError(SocializeException error) { error.printStackTrace(); getLatch.countDown(); } }, keys); assertTrue(getLatch.await(20, TimeUnit.SECONDS)); ListResult<Entity> after = getResult(0); assertNotNull(after); assertEquals(3, after.size()); // Check the order.. should be C/B/A List<Entity> items = after.getItems(); assertEquals(keys[2], items.get(0).getKey()); assertEquals(keys[1], items.get(1).getKey()); assertEquals(keys[0], items.get(2).getKey()); // Now get by activity final CountDownLatch activityLatch = new CountDownLatch(1); EntityUtils.getEntities( context, SortOrder.TOTAL_ACTIVITY, new EntityListListener() { @Override public void onList(ListResult<Entity> entities) { addResult(1, entities); activityLatch.countDown(); } @Override public void onError(SocializeException error) { error.printStackTrace(); activityLatch.countDown(); } }, keys); assertTrue(activityLatch.await(20, TimeUnit.SECONDS)); ListResult<Entity> sorted = getResult(1); assertNotNull(sorted); assertEquals(3, sorted.size()); // Check the order.. should be A/B/C List<Entity> sortedItems = sorted.getItems(); assertEquals(keys[0], sortedItems.get(0).getKey()); assertEquals(keys[1], sortedItems.get(1).getKey()); assertEquals(keys[2], sortedItems.get(2).getKey()); }