Search in sources :

Example 11 with FacebookUser

use of dev.morphia.test.models.FacebookUser in project morphia by mongodb.

the class TestQuery method testKeyList.

@Test
public void testKeyList() {
    final Rectangle rect = new Rectangle(1000, 1);
    Rectangle rectangle = getDs().save(rect);
    assertEquals(rectangle.getId(), rect.getId());
    final FacebookUser fbUser1 = new FacebookUser(1, "scott");
    final FacebookUser fbUser2 = new FacebookUser(2, "tom");
    final FacebookUser fbUser3 = new FacebookUser(3, "oli");
    final FacebookUser fbUser4 = new FacebookUser(4, "frank");
    final List<FacebookUser> users = getDs().save(asList(fbUser1, fbUser2, fbUser3, fbUser4));
    assertEquals(fbUser1.getId(), 1);
    final List<Key<FacebookUser>> fbUserKeys = new ArrayList<>();
    for (FacebookUser user : users) {
        fbUserKeys.add(getMapper().getKey(user));
    }
    assertEquals(fbUserKeys.get(0).getId(), fbUser1.getId());
    assertEquals(fbUserKeys.get(1).getId(), fbUser2.getId());
    assertEquals(fbUserKeys.get(2).getId(), fbUser3.getId());
    assertEquals(fbUserKeys.get(3).getId(), fbUser4.getId());
    final Keys k1 = new Keys(getMapper().getKey(rectangle), fbUserKeys);
    final Keys keys = getDs().save(k1);
    assertEquals(k1.getId(), keys.getId());
    final Datastore datastore = getDs();
    final Keys k1Loaded = datastore.find(Keys.class).filter(eq("_id", k1.getId())).first();
    for (Key<FacebookUser> key : k1Loaded.getUsers()) {
        assertNotNull(key.getId());
    }
    assertNotNull(k1Loaded.getRect().getId());
}
Also used : Datastore(dev.morphia.Datastore) Keys(dev.morphia.test.models.Keys) Rectangle(dev.morphia.test.models.Rectangle) ArrayList(java.util.ArrayList) Key(dev.morphia.Key) FacebookUser(dev.morphia.test.models.FacebookUser) Test(org.testng.annotations.Test)

Aggregations

FacebookUser (dev.morphia.test.models.FacebookUser)11 Test (org.testng.annotations.Test)9 FindOptions (dev.morphia.query.FindOptions)3 ArrayList (java.util.ArrayList)3 UpdateResult (com.mongodb.client.result.UpdateResult)2 Datastore (dev.morphia.Datastore)2 ModifyOptions (dev.morphia.ModifyOptions)2 Arrays.asList (java.util.Arrays.asList)2 List (java.util.List)2 Document (org.bson.Document)2 DBRef (com.mongodb.DBRef)1 Key (dev.morphia.Key)1 Morphia.createDatastore (dev.morphia.Morphia.createDatastore)1 UpdateOptions (dev.morphia.UpdateOptions)1 FindAndDeleteOptions (dev.morphia.query.FindAndDeleteOptions)1 Keys (dev.morphia.test.models.Keys)1 Rectangle (dev.morphia.test.models.Rectangle)1