Search in sources :

Example 1 with FacebookUser

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

the class TestDatastore method testFindAndDeleteWithCollation.

@Test
public void testFindAndDeleteWithCollation() {
    getDs().save(asList(new FacebookUser(1, "John Doe"), new FacebookUser(2, "john doe")));
    Query<FacebookUser> query = getDs().find(FacebookUser.class).filter(eq("username", "john doe"));
    assertNotNull(query.findAndDelete());
    assertNull(query.findAndDelete());
    FindAndDeleteOptions options = new FindAndDeleteOptions().collation(Collation.builder().locale("en").collationStrength(SECONDARY).build());
    assertNotNull(query.findAndDelete(options));
    assertNull(query.iterator().tryNext());
}
Also used : FindAndDeleteOptions(dev.morphia.query.FindAndDeleteOptions) FacebookUser(dev.morphia.test.models.FacebookUser) Test(org.testng.annotations.Test)

Example 2 with FacebookUser

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

the class TestDatastore method testFindAndModifyWithOptions.

@Test
public void testFindAndModifyWithOptions() {
    getDs().save(asList(new FacebookUser(1, "John Doe"), new FacebookUser(2, "john doe")));
    FacebookUser result = getDs().find(FacebookUser.class).filter(eq("username", "john doe")).modify(inc("loginCount")).execute();
    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);
    assertEquals(result.loginCount, 0);
    result = getDs().find(FacebookUser.class).filter(eq("username", "john doe")).modify(inc("loginCount")).execute(new ModifyOptions().returnDocument(BEFORE).collation(Collation.builder().locale("en").collationStrength(SECONDARY).build()));
    assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 1)).iterator(new FindOptions().limit(1)).next().loginCount, 1);
    assertEquals(result.loginCount, 0);
    assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 2)).iterator(new FindOptions().limit(1)).next().loginCount, 1);
    result = getDs().find(FacebookUser.class).filter(eq("id", 3L), eq("username", "Jon Snow")).modify(inc("loginCount")).execute(new ModifyOptions().returnDocument(BEFORE).upsert(true));
    assertNull(result);
    FacebookUser user = getDs().find(FacebookUser.class).filter(eq("id", 3)).iterator(new FindOptions().limit(1)).next();
    assertEquals(user.loginCount, 1);
    assertEquals(user.username, "Jon Snow");
    result = getDs().find(FacebookUser.class).filter(eq("id", 4L), eq("username", "Ron Swanson")).modify(inc("loginCount")).execute(new ModifyOptions().returnDocument(AFTER).upsert(true));
    assertNotNull(result);
    user = getDs().find(FacebookUser.class).filter(eq("id", 4)).iterator(new FindOptions().limit(1)).next();
    assertEquals(result.loginCount, 1);
    assertEquals(result.username, "Ron Swanson");
    assertEquals(user.loginCount, 1);
    assertEquals(user.username, "Ron Swanson");
}
Also used : FindOptions(dev.morphia.query.FindOptions) ModifyOptions(dev.morphia.ModifyOptions) FacebookUser(dev.morphia.test.models.FacebookUser) Test(org.testng.annotations.Test)

Example 3 with FacebookUser

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

the class TestDatastore method testFindAndModify.

@Test
public void testFindAndModify() {
    getDs().save(asList(new FacebookUser(1, "John Doe"), new FacebookUser(2, "john doe")));
    Query<FacebookUser> query = getDs().find(FacebookUser.class).filter(eq("username", "john doe"));
    Modify<FacebookUser> modify = query.modify(inc("loginCount"));
    assertEquals(modify.execute().loginCount, 0);
    assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 1)).first().loginCount, 0);
    assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 2)).first().loginCount, 1);
    assertEquals(modify.execute(new ModifyOptions().returnDocument(AFTER)).loginCount, 2);
    assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 1)).first().loginCount, 0);
    assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 2)).first().loginCount, 2);
    assertNull(getDs().find(FacebookUser.class).filter(eq("id", 3L), eq("username", "Jon Snow")).modify(inc("loginCount", 4)).execute(new ModifyOptions().returnDocument(BEFORE).upsert(true)));
    FacebookUser user = getDs().find(FacebookUser.class).filter(eq("id", 3)).first();
    assertEquals(user.loginCount, 4);
    assertEquals(user.username, "Jon Snow");
    FacebookUser results = getDs().find(FacebookUser.class).filter(eq("id", 4L), eq("username", "Ron Swanson")).modify(inc("loginCount")).execute(new ModifyOptions().returnDocument(AFTER).upsert(true));
    assertEquals(results.loginCount, 1);
    assertEquals(results.username, "Ron Swanson");
    user = getDs().find(FacebookUser.class).filter(eq("id", 4)).iterator(new FindOptions().limit(1)).next();
    assertEquals(user.loginCount, 1);
    assertEquals(user.username, "Ron Swanson");
}
Also used : FindOptions(dev.morphia.query.FindOptions) ModifyOptions(dev.morphia.ModifyOptions) FacebookUser(dev.morphia.test.models.FacebookUser) Test(org.testng.annotations.Test)

Example 4 with FacebookUser

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

the class TestReferences method testDBRefSaves.

@Test
public void testDBRefSaves() {
    getMapper().map(List.of(FacebookUser.class));
    FacebookUser tom = new FacebookUser(1, "Tom Anderson");
    tom.friends.addAll(List.of(new FacebookUser(2, "Cameron Winklevoss"), new FacebookUser(3, "Tyler Winklevoss")));
    getDs().save(tom.friends);
    getDs().save(tom);
    Document loaded = getDs().getCollection(FacebookUser.class).withDocumentClass(Document.class).find(new Document("_id", 1)).first();
    ((List<Object>) loaded.get("friends")).forEach(f -> assertEquals(f.getClass(), DBRef.class));
}
Also used : DBRef(com.mongodb.DBRef) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) Document(org.bson.Document) FacebookUser(dev.morphia.test.models.FacebookUser) Test(org.testng.annotations.Test)

Example 5 with FacebookUser

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

the class TestReferences method testIdOnly.

@Test
public void testIdOnly() {
    getMapper().map(List.of(HasIdOnly.class, FacebookUser.class));
    HasIdOnly tom = new HasIdOnly();
    tom.list.addAll(List.of(new FacebookUser(2, "Cameron Winklevoss"), new FacebookUser(3, "Tyler Winklevoss")));
    getDs().save(tom.list);
    getDs().save(tom);
    Document loaded = getDs().getCollection(HasIdOnly.class).withDocumentClass(Document.class).find(new Document()).first();
    ((List<Object>) loaded.get("list")).forEach(f -> assertEquals(f.getClass(), Long.class));
}
Also used : Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) Document(org.bson.Document) 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