usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From g...@apache.org
Subject [2/7] incubator-usergrid git commit: [USERGRID-303]Updated 'order by' tests to work with new test framework
Date Wed, 07 Jan 2015 22:47:20 GMT
[USERGRID-303]Updated 'order by' tests to work with new test framework


Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/28117282
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/28117282
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/28117282

Branch: refs/heads/USERGRID-313
Commit: 281172822bc3053220e2b38d1af4b61dfa217ffb
Parents: dc625ac
Author: ryan bridges <rbridges@apigee.com>
Authored: Wed Jan 7 09:14:14 2015 -0500
Committer: ryan bridges <rbridges@apigee.com>
Committed: Wed Jan 7 14:44:29 2015 -0500

----------------------------------------------------------------------
 .../rest/applications/queries/OrderByTest.java  | 270 ++++++++++---------
 1 file changed, 140 insertions(+), 130 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/28117282/stack/rest/src/test/java/org/apache/usergrid/rest/applications/queries/OrderByTest.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/queries/OrderByTest.java
b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/queries/OrderByTest.java
index f00912c..3ba5787 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/queries/OrderByTest.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/queries/OrderByTest.java
@@ -17,21 +17,19 @@
 package org.apache.usergrid.rest.applications.queries;
 
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import org.apache.commons.lang.ArrayUtils;
+import org.apache.usergrid.rest.test.resource2point0.AbstractRestIT;
+import org.apache.usergrid.rest.test.resource2point0.model.Collection;
+import org.apache.usergrid.rest.test.resource2point0.model.Entity;
+import org.apache.usergrid.rest.test.resource2point0.model.QueryParameters;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-import com.fasterxml.jackson.databind.JsonNode;
 import java.io.IOException;
-import org.junit.Rule;
-import org.junit.Test;
-import org.apache.usergrid.rest.AbstractRestIT;
-import org.apache.usergrid.rest.TestContextSetup;
-import org.apache.usergrid.rest.test.resource.CustomCollection;
+import java.util.LinkedHashMap;
 
 import static org.junit.Assert.assertEquals;
-import static org.apache.usergrid.utils.MapUtils.hashMap;
 
 
 /**
@@ -41,132 +39,144 @@ import static org.apache.usergrid.utils.MapUtils.hashMap;
  * @since 4.0
  */
 public class OrderByTest extends AbstractRestIT {
-
-    @Rule
-    public TestContextSetup context = new TestContextSetup( this );
-
-
-    @Test
-    // USERGRID-1400
-    public void orderByShouldNotAffectResults() throws IOException {
-
-        CustomCollection activities = context.customCollection( "activities" );
-
-        long created = 0;
-        Map actor = hashMap( "displayName", "Erin" );
-        Map props = new HashMap();
-        props.put( "actor", actor );
-        props.put( "verb", "go" );
-        props.put( "content", "bragh" );
-        for ( int i = 0; i < 20; i++ ) {
-            props.put( "ordinal", i );
-            JsonNode activity = activities.create( props );
-            if ( i == 5 ) {
-                created = activity.findValue( "created" ).longValue();
-            }
-        }
-
-        refreshIndex(context.getOrgName(), context.getAppName());
-
-        String query = "select * where created > " + created;
-        JsonNode node = activities.withQuery( query ).get();
-        assertEquals( 10, node.get( "entities" ).size() );
-
-        query = query + " order by created desc";
-        node = activities.withQuery( query ).get();
-        assertEquals( 10, node.get( "entities" ).size() );
+  private static Logger log = LoggerFactory.getLogger(OrderByTest.class);
+
+  /**
+   * Inserts a number of entities. Query a subset of entities
+   * with unspecified 'order by' and then ordered by 'created'
+   * to ensure the result is unchanged.
+   * 1. Insert entities
+   * 2. Query without 'order by'
+   * 3. Query with 'order by'
+   * 4. Ensure the same entities are returned
+   * @throws IOException
+   */
+  @Test
+  // USERGRID-1400
+  public void orderByShouldNotAffectResults() throws IOException {
+
+    long created = 0;
+    Entity actor = new Entity();
+    actor.put("displayName", "Erin");
+    Entity props = new Entity();
+    props.put("actor", actor);
+    props.put("verb", "go");
+    props.put("content", "bragh");
+    //1. Insert entities
+    for (int i = 0; i < 20; i++) {
+      props.put("ordinal", i);
+      Entity activity = this.app().collection("activity").post(props);
+      log.info("Created", activity.get("created").toString());
+      if (i == 5) {
+        created = Long.parseLong(activity.get("created").toString());
+      }
     }
 
+    refreshIndex();
+    //2. Query without 'order by'
+    String query = "select * where created > " + created;
+    QueryParameters params = new QueryParameters().setQuery(query);
+    Collection activitiesWithoutOrderBy = this.app().collection("activities").get(params);
+    assertEquals(10, activitiesWithoutOrderBy.response.getEntityCount());
+    //3. Query with 'order by'
+    query = query + " order by created desc";
+    params.setQuery(query);
+    Collection activitiesWithOrderBy = this.app().collection("activities").get(params);
+    assertEquals(10, activitiesWithOrderBy.response.getEntityCount());
+    //4. Ensure the same entities are returned
+    while(activitiesWithoutOrderBy.hasNext() && activitiesWithOrderBy.hasNext())
{
+      Entity activityWithoutOrderBy = activitiesWithoutOrderBy.next();
+      Entity activityWithOrderBy = activitiesWithOrderBy.next();
+      assertEquals(activityWithoutOrderBy.get("uuid").toString(), activityWithOrderBy.get("uuid").toString());
+    }
+  }
 
-    @Test
-    // USERGRID-1520
-    public void orderByComesBeforeLimitResult() throws IOException {
-
-        CustomCollection activities = context.customCollection( "activities" );
-
-        Map actor = hashMap( "displayName", "Erin" );
-        Map props = new HashMap();
-        int checkResultsNum = 0;
-
-        props.put( "actor", actor );
-        props.put( "verb", "go" );
-        props.put( "content", "bragh" );
-
-        for ( int i = 0; i < 20; i++ ) {
-            props.put( "ordinal", i );
-            JsonNode activity = activities.create( props );
-        }
-
-        refreshIndex(context.getOrgName(), context.getAppName());
-
-        String query = "select * where created > " + 1 + " order by created desc";
-
-        JsonNode incorrectNode = activities.withQuery( query ).withLimit( 5 ).get();
-
-        assertEquals( 5, incorrectNode.get( "entities" ).size() );
 
-        while ( checkResultsNum < 5 ) {
-            assertEquals( activities.entityIndex( query, checkResultsNum ),
-                    activities.entityIndexLimit( query, 5, checkResultsNum ) );
-            checkResultsNum++;
-        }
+  /**
+   * Ensure successful query when 'limit' and 'order by' are specified
+   * 1. Insert entities
+   * 2. Query a subset of the entities, specifying order and limit
+   * 3. Ensure the correct number of results are returned
+   * @throws IOException
+   */
+  @Test
+  // USERGRID-1520
+  public void orderByComesBeforeLimitResult() throws IOException {
+
+    Entity actor = new Entity();
+    actor.put("displayName", "Erin");
+    Entity props = new Entity();
+    props.put("actor", actor);
+    props.put("verb", "go");
+    props.put("content", "bragh");
+    //1. Insert entities
+    for (int i = 0; i < 20; i++) {
+      props.put("ordinal", i);
+      this.app().collection("activity").post(props);
     }
 
-  /*
-   * public JsonNode entityIndex(JsonNode container, int index) { return
-   * container.get("entities").get(index); }
+    refreshIndex();
+    //2. Query a subset of the entities, specifying order and limit
+    String query = "select * where created > " + 1 + " order by created desc";
+    QueryParameters params = new QueryParameters().setQuery(query).setLimit(5);
+    Collection activities = this.app().collection("activities").get(params);
+    //3. Ensure the correct number of results are returned
+    assertEquals(5, activities.response.getEntityCount());
+
+  }
+
+  /**
+   * Ensure that results are returned in the correct descending order, when specified
+   * 1. Insert a number of entities and add them to an array
+   * 2. Query for the entities in descending order
+   * 3. Validate that the order is correct
+   * @throws IOException
    */
+  @Test
+  // USERGRID-1521
+  public void orderByReturnCorrectResults() throws IOException {
+
+    int size = 20;
+    Entity[] activities = new Entity[size];
+
+    Entity actor = new Entity();
+    actor.put("displayName", "Erin");
+    Entity props = new Entity();
+    props.put("actor", actor);
+    props.put("verb", "go");
+    props.put("content", "bragh");
+    //1. Insert a number of entities and add them to an array
+    for (int i = 0; i < size; i++) {
+      props.put("ordinal", i);
+      Entity e = this.app().collection("activity").post(props);
+      activities[i] = e;
+      log.info(String.valueOf(e.get("uuid").toString()));
+      log.info(String.valueOf(Long.parseLong(activities[0].get("created").toString())));
+    }
 
-
-    @Test
-    // USERGRID-1521
-    public void orderByReturnCorrectResults() throws IOException {
-
-        CustomCollection activities = context.customCollection( "activities" );
-
-        int size = 200;
-
-        Map<String, String> actor = hashMap( "displayName", "Erin" );
-        Map<String, Object> props = new HashMap<String, Object>();
-
-        props.put( "actor", actor );
-        props.put( "verb", "go" );
-        props.put( "content", "bragh" );
-
-        List<JsonNode> activites = new ArrayList<JsonNode>( size );
-
-        for ( int i = 0; i < size; i++ ) {
-            props.put( "ordinal", i );
-            JsonNode activity = activities.create( props ).get( "entities" ).get( 0 );
-            activites.add( activity );
-        }
-
-        refreshIndex(context.getOrgName(), context.getAppName());
-
-        long lastCreated = activites.get( activites.size() - 1 ).get( "created" ).asLong();
-
-        String errorQuery = String.format( "select * where created <= %d order by created
desc", lastCreated );
-        String cursor = null;
-        int index = size - 1;
-
-        do {
-            JsonNode response = activities.withQuery( errorQuery ).get();
-            JsonNode cursorNode = response.get( "cursor" );
-
-            cursor = cursorNode != null ? cursorNode.asText() : null;
-
-            JsonNode entities = response.get( "entities" );
-
-            int returnSize = entities.size();
-
-            for ( int i = 0; i < returnSize; i++, index-- ) {
-                assertEquals( activites.get( index ), entities.get( i ) );
-            }
-
-            activities = activities.withCursor( cursor );
-        }
-        while ( cursor != null && cursor.length() > 0 );
-
-        assertEquals( "Paged to last result", -1, index );
+    refreshIndex();
+
+
+     ArrayUtils.reverse(activities);
+    long lastCreated = Long.parseLong(activities[0].get("created").toString());
+    //2. Query for the entities in descending order
+    String errorQuery = String.format("select * where created <= %d order by created desc",
lastCreated);
+    String cursor = null;
+    int index = size - 1;
+
+    QueryParameters params = new QueryParameters().setQuery(errorQuery);
+    Collection activitiesResponse = this.app().collection("activities").get(params);
+    //3. Validate that the order is correct
+    do {
+      int returnSize = activitiesResponse.response.getEntityCount();
+      //loop through the current page of results
+      for (int i = 0; i < returnSize; i++, index--) {
+        assertEquals(((LinkedHashMap<String, Object>) activitiesResponse.response.getEntities().get(i)).get("uuid").toString(),
+            ((Entity) activities[index]).get("uuid").toString());
+      }
+      //grab the next page of results
+      activitiesResponse = this.app().getNextPage(activitiesResponse, true);
     }
+    while (activitiesResponse.getCursor() != null);
+  }
 }


Mime
View raw message