Search in sources :

Example 1 with Keys

use of dev.morphia.test.models.Keys 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

Datastore (dev.morphia.Datastore)1 Key (dev.morphia.Key)1 FacebookUser (dev.morphia.test.models.FacebookUser)1 Keys (dev.morphia.test.models.Keys)1 Rectangle (dev.morphia.test.models.Rectangle)1 ArrayList (java.util.ArrayList)1 Test (org.testng.annotations.Test)1