Search in sources :

Example 6 with FacebookUser

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

the class TestReferences method testSecondDatastore.

private void testSecondDatastore(Datastore datastore) {
    assertNull(datastore.find(FacebookUser.class).filter(eq("id", 1)).first());
    assertNull(datastore.find(FacebookUser.class).filter(eq("id", 3)).first());
    final FacebookUser db2FoundUser = datastore.find(FacebookUser.class).filter(eq("id", 2)).first();
    assertNotNull(db2FoundUser);
    assertNotNull(datastore.find(FacebookUser.class).filter(eq("id", 4)).first());
    assertEquals(db2FoundUser.friends.size(), 1, "Should find 1 friend");
    assertEquals(db2FoundUser.friends.get(0).id, 4, "Should find the right friend");
}
Also used : FacebookUser(dev.morphia.test.models.FacebookUser)

Example 7 with FacebookUser

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

the class TestReferences method testMultipleDatabasesSingleThreaded.

@Test
public void testMultipleDatabasesSingleThreaded() {
    getMapper().map(List.of(FacebookUser.class));
    final Datastore ds1 = createDatastore(getMongoClient(), "db1");
    final Datastore ds2 = createDatastore(getMongoClient(), "db2");
    final FacebookUser db1Friend = new FacebookUser(3, "DB1 FaceBook Friend");
    ds1.save(db1Friend);
    final FacebookUser db1User = new FacebookUser(1, "DB1 FaceBook User");
    db1User.friends.add(db1Friend);
    ds1.save(db1User);
    final FacebookUser db2Friend = new FacebookUser(4, "DB2 FaceBook Friend");
    ds2.save(db2Friend);
    final FacebookUser db2User = new FacebookUser(2, "DB2 FaceBook User");
    db2User.friends.add(db2Friend);
    ds2.save(db2User);
    testFirstDatastore(ds1);
    testSecondDatastore(ds2);
    for (int i = 1; i <= 4; i++) {
        assertNull(getDs().find(FacebookUser.class).filter(eq("id", i)).first());
    }
}
Also used : Datastore(dev.morphia.Datastore) Morphia.createDatastore(dev.morphia.Morphia.createDatastore) FacebookUser(dev.morphia.test.models.FacebookUser) Test(org.testng.annotations.Test)

Example 8 with FacebookUser

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

the class TestReferences method testFirstDatastore.

private void testFirstDatastore(Datastore datastore) {
    final FacebookUser user = datastore.find(FacebookUser.class).filter(eq("id", 1)).first();
    assertNotNull(user);
    assertNotNull(datastore.find(FacebookUser.class).filter(eq("id", 3)).first());
    assertEquals(user.friends.size(), 1, "Should find 1 friend");
    assertEquals(user.friends.get(0).id, 3, "Should find the right friend");
    assertNull(datastore.find(FacebookUser.class).filter(eq("id", 2)).first());
    assertNull(datastore.find(FacebookUser.class).filter(eq("id", 4)).first());
}
Also used : FacebookUser(dev.morphia.test.models.FacebookUser)

Example 9 with FacebookUser

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

the class TestDatastore method testRefresh.

@Test
public void testRefresh() {
    FacebookUser steve = getDs().save(new FacebookUser(1, "Steve"));
    assertEquals(steve.loginCount, 0);
    UpdateResult loginCount = getDs().find(FacebookUser.class).update(inc("loginCount", 10)).execute();
    assertEquals(loginCount.getModifiedCount(), 1);
    getDs().refresh(steve);
    assertEquals(steve.loginCount, 10);
    loginCount = getDs().find(FacebookUser.class).update(set("username", "Mark"), set("loginCount", 1)).execute();
    assertEquals(loginCount.getModifiedCount(), 1);
    getDs().refresh(steve);
    assertEquals(steve.loginCount, 1);
    assertEquals(steve.username, "Mark");
}
Also used : UpdateResult(com.mongodb.client.result.UpdateResult) FacebookUser(dev.morphia.test.models.FacebookUser) Test(org.testng.annotations.Test)

Example 10 with FacebookUser

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

the class TestDatastore method testUpdateWithCollation.

@Test
public void testUpdateWithCollation() {
    getDs().save(asList(new FacebookUser(1, "John Doe"), new FacebookUser(2, "john doe")));
    final Update<FacebookUser> update = getDs().find(FacebookUser.class).filter(eq("username", "john doe")).update(inc("loginCount"));
    UpdateResult results = update.execute();
    assertEquals(results.getModifiedCount(), 1);
    assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 1)).iterator(new FindOptions().limit(1)).next().loginCount, 0);
    assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 2)).iterator(new FindOptions().limit(1)).next().loginCount, 1);
    results = update.execute(new UpdateOptions().multi(true).collation(Collation.builder().locale("en").collationStrength(SECONDARY).build()));
    assertEquals(results.getModifiedCount(), 2);
    assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 1)).iterator(new FindOptions().limit(1)).next().loginCount, 1);
    assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 2)).iterator(new FindOptions().limit(1)).next().loginCount, 2);
}
Also used : FindOptions(dev.morphia.query.FindOptions) UpdateResult(com.mongodb.client.result.UpdateResult) UpdateOptions(dev.morphia.UpdateOptions) 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