@Test public void testKeywordsAndQuery() throws Exception { logger.info("testKeywordsOrQuery"); UUID applicationId = createApplication("testOrganization", "testKeywordsAndQuery"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("title", "Galactians 2"); properties.put("keywords", "Hot, Space Invaders, Classic"); Entity firstGame = em.create("game", properties); properties = new LinkedHashMap<String, Object>(); properties.put("title", "Bunnies Extreme"); properties.put("keywords", "Hot, New"); Entity secondGame = em.create("game", properties); properties = new LinkedHashMap<String, Object>(); properties.put("title", "Hot Shots Extreme"); properties.put("keywords", "Action, New"); Entity thirdGame = em.create("game", properties); Query query = Query.fromQL("select * where keywords contains 'new' and title contains 'extreme'"); Results r = em.searchCollection(em.getApplicationRef(), "games", query); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); assertEquals(2, r.size()); assertEquals(secondGame.getUuid(), r.getEntities().get(0).getUuid()); assertEquals(thirdGame.getUuid(), r.getEntities().get(1).getUuid()); }
@Test public void testSecondarySorts() throws Exception { logger.info("testSecondarySorts"); UUID applicationId = createApplication("testOrganization", "testSecondarySorts"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); for (int i = alphabet.length - 1; i >= 0; i--) { String name = alphabet[i]; Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("name", name); properties.put("group", i / 3); properties.put("reverse_name", alphabet[alphabet.length - 1 - i]); em.create("item", properties); } Query query = Query.fromQL("group = 1 order by name desc"); Results r = em.searchCollection(em.getApplicationRef(), "items", query); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); int i = 6; for (Entity entity : r.getEntities()) { i--; assertEquals(1L, entity.getProperty("group")); assertEquals(alphabet[i], entity.getProperty("name")); } assertEquals(3, i); }
@Test public void userFirstNameSearch() throws Exception { UUID applicationId = createApplication("testOrganization", "testFirstName"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); String firstName = "firstName" + UUIDUtils.newTimeUUID(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("username", "edanuff"); properties.put("email", "*****@*****.**"); properties.put("firstname", firstName); Entity user = em.create("user", properties); assertNotNull(user); // EntityRef Query query = new Query(); query.addEqualityFilter("firstname", firstName); Results r = em.searchCollection(em.getApplicationRef(), "users", query); assertTrue(r.size() > 0); Entity returned = r.getEntities().get(0); assertEquals(user.getUuid(), returned.getUuid()); // update the username String newFirstName = "firstName" + UUIDUtils.newTimeUUID(); user.setProperty("firstname", newFirstName); em.update(user); // search with the old username, should be no results query = new Query(); query.addEqualityFilter("firstname", firstName); r = em.searchCollection(em.getApplicationRef(), "users", query); assertEquals(0, r.size()); // search with the new username, should be results. query = new Query(); query.addEqualityFilter("firstname", newFirstName); r = em.searchCollection(em.getApplicationRef(), "users", query); assertTrue(r.size() > 0); returned = r.getEntities().get(0); assertEquals(user.getUuid(), returned.getUuid()); }
@Test public void andQuery() throws Exception { UUID applicationId = createApplication("testOrganization", "andQuery"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("keywords", "blah,test,game"); properties.put("title", "Solitaire"); Entity game1 = em.create("game", properties); assertNotNull(game1); properties = new LinkedHashMap<String, Object>(); properties.put("keywords", "random,test"); properties.put("title", "Hearts"); Entity game2 = em.create("game", properties); assertNotNull(game2); // overlap Query query = Query.fromQL("select * where keywords contains 'test' AND keywords contains 'random'"); Results r = em.searchCollection(em.getApplicationRef(), "games", query); assertEquals(1, r.size()); // disjoint query = Query.fromQL("select * where keywords contains 'random' AND keywords contains 'blah'"); r = em.searchCollection(em.getApplicationRef(), "games", query); assertEquals(0, r.size()); // same each side query = Query.fromQL("select * where keywords contains 'test' AND keywords contains 'test'"); r = em.searchCollection(em.getApplicationRef(), "games", query); assertEquals(2, r.size()); Entity returned = r.getEntities().get(0); assertEquals(game1.getUuid(), returned.getUuid()); returned = r.getEntities().get(1); assertEquals(game2.getUuid(), returned.getUuid()); // one side, left query = Query.fromQL("select * where keywords contains 'test' AND keywords contains 'foobar'"); r = em.searchCollection(em.getApplicationRef(), "games", query); assertEquals(0, r.size()); // one side, right query = Query.fromQL("select * where keywords contains 'foobar' AND keywords contains 'test'"); r = em.searchCollection(em.getApplicationRef(), "games", query); assertEquals(0, r.size()); }
@Test public void pagingWithBoundsCriteria() throws Exception { UUID applicationId = createApplication("testOrganization", "pagingWithBoundsCriteria"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); int size = 40; List<UUID> entityIds = new ArrayList<UUID>(); for (int i = 0; i < size; i++) { Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("index", i); Entity created = em.create("page", properties); entityIds.add(created.getUuid()); } int pageSize = 10; Query query = new Query(); query.setLimit(pageSize); query.addFilter("index >= 10"); query.addFilter("index <= 29"); Results r = null; // check they're all the same before deletion for (int i = 1; i < 3; i++) { r = em.searchCollection(em.getApplicationRef(), "pages", query); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); assertEquals(pageSize, r.size()); for (int j = 0; j < pageSize; j++) { assertEquals(entityIds.get(i * pageSize + j), r.getEntities().get(j).getUuid()); } query.setCursor(r.getCursor()); } r = em.searchCollection(em.getApplicationRef(), "pages", query); assertEquals(0, r.size()); assertNull(r.getCursor()); }
@Test public void groupTitleSearch() throws Exception { UUID applicationId = createApplication("testOrganization", "groupTitleSearch"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); String titleName = "groupName" + UUIDUtils.newTimeUUID(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("title", titleName); properties.put("path", "testPath"); properties.put("name", "testName"); Entity group = em.create("group", properties); assertNotNull(group); // EntityRef Query query = new Query(); query.addEqualityFilter("title", titleName); Results r = em.searchCollection(em.getApplicationRef(), "groups", query); assertTrue(r.size() > 0); Entity returned = r.getEntities().get(0); assertEquals(group.getUuid(), returned.getUuid()); }
@Test public void userLastNameSearch() throws Exception { UUID applicationId = createApplication("testOrganization", "testLastName"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); String lastName = "lastName" + UUIDUtils.newTimeUUID(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("username", "edanuff"); properties.put("email", "*****@*****.**"); properties.put("lastname", lastName); Entity user = em.create("user", properties); assertNotNull(user); // EntityRef Query query = new Query(); query.addEqualityFilter("lastname", lastName); Results r = em.searchCollection(em.getApplicationRef(), "users", query); assertTrue(r.size() > 0); Entity returned = r.getEntities().get(0); assertEquals(user.getUuid(), returned.getUuid()); }
@Test public void testNotQueryAnd() throws Exception { UUID applicationId = createApplication("testOrganization", "testNotQueryAnd"); EntityManager em = emf.getEntityManager(applicationId); Map<String, Object> location = new LinkedHashMap<String, Object>(); location.put("Place", "24 Westminster Avenue, Venice, CA 90291, USA"); location.put("Longitude", -118.47425979999998); location.put("Latitude", 33.9887663); Map<String, Object> recipient = new LinkedHashMap<String, Object>(); recipient.put("TimeRequested", 1359077878l); recipient.put("Username", "fb_536692245"); recipient.put("Location", location); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("Flag", "requested"); properties.put("Recipient", recipient); em.create("loveobject", properties); location = new LinkedHashMap<String, Object>(); location.put( "Place", "Via Pietro Maroncelli, 48, 62012 Santa Maria Apparente Province of Macerata, Italy"); location.put("Longitude", 13.693080199999999); location.put("Latitude", 43.2985019); recipient = new LinkedHashMap<String, Object>(); recipient.put("TimeRequested", 1359077878l); recipient.put("Username", "fb_100000787138041"); recipient.put("Location", location); properties = new LinkedHashMap<String, Object>(); properties.put("Flag", "requested"); properties.put("Recipient", recipient); em.create("loveobject", properties); // String s = "select * where Flag = 'requested'"; // String s = // "select * where Flag = 'requested' and NOT Recipient.Username = '******' order by // created asc"; String s = "select * where Flag = 'requested' and NOT Recipient.Username = '******' order by created asc"; Query query = Query.fromQL(s); Results r = em.searchCollection(em.getApplicationRef(), "loveobjects", query); assertTrue(r.size() == 1); String username = (String) ((Map) r.getEntities().get(0).getProperty("Recipient")).get("Username"); // selection results should be a list of lists List<Object> sr = query.getSelectionResults(r); assertTrue(sr.size() == 1); assertEquals("fb_100000787138041", username); }
@Test public void emptyQueryReverse() throws Exception { UUID applicationId = createApplication("testOrganization", "testEmptyQueryReverse"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); String firstName = "firstName" + UUIDUtils.newTimeUUID(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("username", "edanuff"); properties.put("email", "*****@*****.**"); properties.put("firstname", firstName); Entity user = em.create("user", properties); assertNotNull(user); properties = new LinkedHashMap<String, Object>(); properties.put("username", "djacobs"); properties.put("email", "*****@*****.**"); Entity user2 = em.create("user", properties); assertNotNull(user2); // EntityRef Query query = new Query(); query.setReversed(true); Results r = em.searchCollection(em.getApplicationRef(), "users", query); assertEquals(2, r.size()); Entity returned = r.getEntities().get(0); assertEquals(user2.getUuid(), returned.getUuid()); returned = r.getEntities().get(1); assertEquals(user.getUuid(), returned.getUuid()); }
@Test public void runtimeTypeCorrect() throws Exception { UUID applicationId = createApplication("testOrganization", "runtimeTypeCorrect"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); int size = 20; List<User> createdEntities = new ArrayList<User>(); for (int i = 0; i < size; i++) { User user = new User(); user.setEmail(String.format("*****@*****.**", i)); user.setUsername(String.format("test%d", i)); user.setName(String.format("test%d", i)); User created = em.create(user); createdEntities.add(created); } Results r = em.getCollection(em.getApplicationRef(), "users", null, 50, Level.ALL_PROPERTIES, false); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); assertEquals(size, r.size()); // check they're all the same before deletion for (int i = 0; i < size; i++) { assertEquals(createdEntities.get(i).getUuid(), r.getEntities().get(i).getUuid()); assertTrue(r.getEntities().get(i) instanceof User); } }
@Test public void testSubkeys() throws Exception { UUID applicationId = createApplication("testOrganization", "testSubkeys"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("username", "edanuff"); properties.put("email", "*****@*****.**"); Entity user = em.create("user", properties); assertNotNull(user); properties = new LinkedHashMap<String, Object>(); properties.put("actor", hashMap("displayName", "Ed Anuff").map("objectType", "person")); properties.put("verb", "tweet"); properties.put("content", "I ate a sammich"); em.addToCollection(user, "activities", em.create("activity", properties)); properties = new LinkedHashMap<String, Object>(); properties.put("actor", hashMap("displayName", "Ed Anuff").map("objectType", "person")); properties.put("verb", "post"); properties.put("content", "I wrote a blog post"); em.addToCollection(user, "activities", em.create("activity", properties)); properties = new LinkedHashMap<String, Object>(); properties.put("actor", hashMap("displayName", "Ed Anuff").map("objectType", "person")); properties.put("verb", "tweet"); properties.put("content", "I ate another sammich"); em.addToCollection(user, "activities", em.create("activity", properties)); properties = new LinkedHashMap<String, Object>(); properties.put("actor", hashMap("displayName", "Ed Anuff").map("objectType", "person")); properties.put("verb", "post"); properties.put("content", "I wrote another blog post"); em.addToCollection(user, "activities", em.create("activity", properties)); Results r = em.searchCollection(user, "activities", Query.searchForProperty("verb", "post")); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); assertEquals(2, r.size()); }
@Test public void testGroups() throws Exception { UUID applicationId = createApplication("testOrganization", "testGroups"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("username", "edanuff"); properties.put("email", "*****@*****.**"); Entity user1 = em.create("user", properties); assertNotNull(user1); properties = new LinkedHashMap<String, Object>(); properties.put("username", "djacobs"); properties.put("email", "*****@*****.**"); Entity user2 = em.create("user", properties); assertNotNull(user2); properties = new LinkedHashMap<String, Object>(); properties.put("path", "group1"); Entity group = em.create("group", properties); assertNotNull(group); em.addToCollection(group, "users", user1); em.addToCollection(group, "users", user2); properties = new LinkedHashMap<String, Object>(); properties.put("nickname", "ed"); em.updateProperties(new SimpleCollectionRef(group, "users", user1), properties); Results r = em.searchCollection( group, "users", new Query() .addEqualityFilter("member.nickname", "ed") .withResultsLevel(Results.Level.LINKED_PROPERTIES)); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); assertEquals(1, r.size()); em.removeFromCollection(user1, "groups", group); }
public void mergeEntitiesWithMetadata() { if (metadataMerged) { return; } if (metadata == null) { return; } metadataMerged = true; getEntities(); if (entities != null) { for (Entity entity : entities) { entity.clearMetadata(); Map<String, Object> entityMetadata = metadata.get(entity.getUuid()); if (entityMetadata != null) { entity.mergeMetadata(entityMetadata); } } } }
public List<Entity> getEntitiesByType(String type) { if (entitiesByType != null) { return entitiesByType.get(type); } getEntities(); if (entities == null) { return null; } List<Entity> l = null; entitiesByType = new LinkedHashMap<String, List<Entity>>(); for (Entity entity : entities) { l = entitiesByType.get(entity.getType()); if (l == null) { l = new ArrayList<Entity>(); entitiesByType.put(entity.getType(), l); } l.add(entity); } return l; }
@Test public void testCollectionFilters() throws Exception { logger.info("testCollectionFilters"); UUID applicationId = createApplication("testOrganization", "testCollectionFilters"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); for (int i = alphabet.length - 1; i >= 0; i--) { String name = alphabet[i]; Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("name", name); em.create("item", properties); } Query query = Query.fromQL("name < 'delta'"); Results r = em.searchCollection(em.getApplicationRef(), "items", query); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); int i = 0; for (Entity entity : r.getEntities()) { assertEquals(alphabet[i], entity.getProperty("name")); i++; } assertEquals(3, i); query = Query.fromQL("name <= 'delta'"); r = em.searchCollection(em.getApplicationRef(), "items", query); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); i = 0; for (Entity entity : r.getEntities()) { assertEquals(alphabet[i], entity.getProperty("name")); i++; } assertEquals(4, i); query = Query.fromQL("name <= 'foxtrot' and name > 'bravo'"); r = em.searchCollection(em.getApplicationRef(), "items", query); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); i = 2; for (Entity entity : r.getEntities()) { assertEquals(alphabet[i], entity.getProperty("name")); i++; } assertEquals(6, i); query = Query.fromQL("name < 'foxtrot' and name > 'bravo'"); r = em.searchCollection(em.getApplicationRef(), "items", query); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); i = 2; for (Entity entity : r.getEntities()) { assertEquals(alphabet[i], entity.getProperty("name")); i++; } assertEquals(5, i); query = Query.fromQL("name < 'foxtrot' and name >= 'bravo'"); r = em.searchCollection(em.getApplicationRef(), "items", query); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); i = 1; for (Entity entity : r.getEntities()) { assertEquals(alphabet[i], entity.getProperty("name")); i++; } assertEquals(5, i); query = Query.fromQL("name <= 'foxtrot' and name >= 'bravo'"); r = em.searchCollection(em.getApplicationRef(), "items", query); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); i = 1; for (Entity entity : r.getEntities()) { assertEquals(alphabet[i], entity.getProperty("name")); i++; } assertEquals(6, i); query = Query.fromQL("name <= 'foxtrot' and name >= 'bravo' order by name desc"); r = em.searchCollection(em.getApplicationRef(), "items", query); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); i = 6; for (Entity entity : r.getEntities()) { i--; assertEquals(alphabet[i], entity.getProperty("name")); } assertEquals(1, i); query = Query.fromQL("name < 'foxtrot' and name > 'bravo' order by name desc"); r = em.searchCollection(em.getApplicationRef(), "items", query); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); i = 5; for (Entity entity : r.getEntities()) { i--; assertEquals(alphabet[i], entity.getProperty("name")); } assertEquals(2, i); query = Query.fromQL("name < 'foxtrot' and name >= 'bravo' order by name desc"); r = em.searchCollection(em.getApplicationRef(), "items", query); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); i = 5; for (Entity entity : r.getEntities()) { i--; assertEquals(alphabet[i], entity.getProperty("name")); } assertEquals(1, i); query = Query.fromQL("name = 'foxtrot'"); r = em.searchCollection(em.getApplicationRef(), "items", query); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); assertEquals(1, r.size()); long created = r.getEntity().getCreated(); UUID entityId = r.getEntity().getUuid(); query = Query.fromQL("created = " + created); r = em.searchCollection(em.getApplicationRef(), "items", query); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); assertEquals(1, r.size()); assertEquals(entityId, r.getEntity().getUuid()); }
@Test public void pagingAfterDelete() throws Exception { UUID applicationId = createApplication("testOrganization", "pagingAfterDelete"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); int size = 20; List<UUID> entityIds = new ArrayList<UUID>(); for (int i = 0; i < size; i++) { Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("name", "object" + i); Entity created = em.create("objects", properties); entityIds.add(created.getUuid()); } Query query = new Query(); query.setLimit(50); Results r = em.searchCollection(em.getApplicationRef(), "objects", query); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); assertEquals(size, r.size()); // check they're all the same before deletion for (int i = 0; i < size; i++) { assertEquals(entityIds.get(i), r.getEntities().get(i).getUuid()); } // now delete 5 items that will span the 10 pages for (int i = 5; i < 10; i++) { Entity entity = r.getEntities().get(i); em.delete(entity); entityIds.remove(entity.getUuid()); } // now query with paging query = new Query(); r = em.searchCollection(em.getApplicationRef(), "objects", query); assertEquals(10, r.size()); for (int i = 0; i < 10; i++) { assertEquals(entityIds.get(i), r.getEntities().get(i).getUuid()); } // try the next page, set our cursor, it should be the last 5 entities query = new Query(); query.setCursor(r.getCursor()); r = em.searchCollection(em.getApplicationRef(), "objects", query); assertEquals(5, r.size()); for (int i = 10; i < 15; i++) { assertEquals(entityIds.get(i), r.getEntities().get(i - 10).getUuid()); } }
@Test public void orQuery() throws Exception { UUID applicationId = createApplication("testOrganization", "orQuery"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("keywords", "blah,test,game"); properties.put("title", "Solitaire"); Entity game1 = em.create("orquerygame", properties); assertNotNull(game1); properties = new LinkedHashMap<String, Object>(); properties.put("keywords", "random,test"); properties.put("title", "Hearts"); Entity game2 = em.create("orquerygame", properties); assertNotNull(game2); // EntityRef Query query = Query.fromQL("select * where keywords contains 'Random' OR keywords contains 'Game'"); Results r = em.searchCollection(em.getApplicationRef(), "orquerygames", query); assertEquals(2, r.size()); Entity returned = r.getEntities().get(0); assertEquals(game2.getUuid(), returned.getUuid()); returned = r.getEntities().get(1); assertEquals(game1.getUuid(), returned.getUuid()); query = Query.fromQL("select * where( keywords contains 'Random' OR keywords contains 'Game')"); r = em.searchCollection(em.getApplicationRef(), "orquerygames", query); assertEquals(2, r.size()); returned = r.getEntities().get(0); assertEquals(game2.getUuid(), returned.getUuid()); returned = r.getEntities().get(1); assertEquals(game1.getUuid(), returned.getUuid()); // field order shouldn't matter USERGRID-375 query = Query.fromQL("select * where keywords contains 'blah' OR title contains 'blah'"); r = em.searchCollection(em.getApplicationRef(), "orquerygames", query); assertEquals(1, r.size()); returned = r.getEntities().get(0); assertEquals(game1.getUuid(), returned.getUuid()); query = Query.fromQL("select * where title contains 'blah' OR keywords contains 'blah'"); r = em.searchCollection(em.getApplicationRef(), "orquerygames", query); assertEquals(1, r.size()); returned = r.getEntities().get(0); assertEquals(game1.getUuid(), returned.getUuid()); }
@Test public void testCollectionOrdering() throws Exception { logger.info("testCollectionOrdering"); UUID applicationId = createApplication("testOrganization", "testCollectionOrdering"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); for (int i = alphabet.length - 1; i >= 0; i--) { String name = alphabet[i]; Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("name", name); em.create("item", properties); } int i = 0; Query query = Query.fromQL("order by name"); Results r = em.searchCollection(em.getApplicationRef(), "items", query); for (Entity entity : r.getEntities()) { assertEquals(alphabet[i], entity.getProperty("name")); i++; } query = Query.fromQL("order by name").withCursor(r.getCursor()); r = em.searchCollection(em.getApplicationRef(), "items", query); for (Entity entity : r.getEntities()) { assertEquals(alphabet[i], entity.getProperty("name")); i++; } query = Query.fromQL("order by name").withCursor(r.getCursor()); r = em.searchCollection(em.getApplicationRef(), "items", query); for (Entity entity : r.getEntities()) { assertEquals(alphabet[i], entity.getProperty("name")); i++; } assertEquals(alphabet.length, i); i = alphabet.length; query = Query.fromQL("order by name desc"); r = em.searchCollection(em.getApplicationRef(), "items", query); for (Entity entity : r.getEntities()) { i--; assertEquals(alphabet[i], entity.getProperty("name")); } query = Query.fromQL("order by name desc").withCursor(r.getCursor()); r = em.searchCollection(em.getApplicationRef(), "items", query); // logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); for (Entity entity : r.getEntities()) { i--; assertEquals(alphabet[i], entity.getProperty("name")); } query = Query.fromQL("order by name desc").withCursor(r.getCursor()); r = em.searchCollection(em.getApplicationRef(), "items", query); for (Entity entity : r.getEntities()) { i--; assertEquals(alphabet[i], entity.getProperty("name")); } assertEquals(0, i); }
@Test public void testCollection() throws Exception { UUID applicationId = createApplication("testOrganization", "testCollection"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("username", "edanuff"); properties.put("email", "*****@*****.**"); Entity user = em.create("user", properties); assertNotNull(user); properties = new LinkedHashMap<String, Object>(); properties.put( "actor", new LinkedHashMap<String, Object>() { { put("displayName", "Ed Anuff"); put("objectType", "person"); } }); properties.put("verb", "tweet"); properties.put("content", "I ate a sammich"); properties.put("ordinal", 3); Entity activity = em.create("activity", properties); assertNotNull(activity); logger.info("" + activity.getClass()); logger.info(JsonUtils.mapToFormattedJsonString(activity)); activity = em.get(activity.getUuid()); logger.info("" + activity.getClass()); logger.info(JsonUtils.mapToFormattedJsonString(activity)); em.addToCollection(user, "activities", activity); // test queries on the collection properties = new LinkedHashMap<String, Object>(); properties.put( "actor", new LinkedHashMap<String, Object>() { { put("displayName", "Ed Anuff"); put("objectType", "person"); } }); properties.put("verb", "tweet2"); properties.put("content", "I ate a pickle"); properties.put("ordinal", 2); Entity activity2 = em.create("activity", properties); activity2 = em.get(activity2.getUuid()); em.addToCollection(user, "activities", activity2); properties = new LinkedHashMap<String, Object>(); properties.put( "actor", new LinkedHashMap<String, Object>() { { put("displayName", "Ed Anuff"); put("objectType", "person"); } }); properties.put("verb", "tweet2"); properties.put("content", "I ate an apple"); properties.put("ordinal", 1); Entity activity3 = em.create("activity", properties); activity3 = em.get(activity3.getUuid()); em.addToCollection(user, "activities", activity3); // empty query Query query = new Query(); Results r = em.searchCollection(user, "activities", query); assertEquals(3, r.size()); // success // query verb query = new Query().addEqualityFilter("verb", "tweet2"); r = em.searchCollection(user, "activities", query); assertEquals(2, r.size()); // query verb, sort created query = new Query().addEqualityFilter("verb", "tweet2"); query.addSort("created"); r = em.searchCollection(user, "activities", query); assertEquals(2, r.size()); List<Entity> entities = r.getEntities(); assertEquals(entities.get(0).getUuid(), activity2.getUuid()); assertEquals(entities.get(1).getUuid(), activity3.getUuid()); // query verb, sort ordinal query = new Query().addEqualityFilter("verb", "tweet2"); query.addSort("ordinal"); r = em.searchCollection(user, "activities", query); assertEquals(2, r.size()); entities = r.getEntities(); assertEquals(entities.get(0).getUuid(), activity3.getUuid()); assertEquals(entities.get(1).getUuid(), activity2.getUuid()); // empty query, sort content query = new Query(); query.addSort("content"); r = em.searchCollection(user, "activities", query); assertEquals(3, r.size()); entities = r.getEntities(); logger.info(JsonUtils.mapToFormattedJsonString(entities)); assertEquals(entities.get(0).getUuid(), activity2.getUuid()); assertEquals(entities.get(1).getUuid(), activity.getUuid()); assertEquals(entities.get(2).getUuid(), activity3.getUuid()); // empty query, sort verb query = new Query(); query.addSort("verb"); r = em.searchCollection(user, "activities", query); assertEquals(3, r.size()); // empty query, sort ordinal query = new Query(); query.addSort("ordinal"); r = em.searchCollection(user, "activities", query); assertEquals(3, r.size()); entities = r.getEntities(); assertEquals(entities.get(0).getUuid(), activity3.getUuid()); assertEquals(entities.get(1).getUuid(), activity2.getUuid()); assertEquals(entities.get(2).getUuid(), activity.getUuid()); // query ordinal query = new Query().addEqualityFilter("ordinal", 2); r = em.searchCollection(user, "activities", query); assertEquals(1, r.size()); // query ordinal and sort ordinal query = new Query().addEqualityFilter("ordinal", 2); query.addSort("ordinal"); r = em.searchCollection(user, "activities", query); assertEquals(1, r.size()); }