Search in sources :

Example 1 with FacebookUser

use of org.mongodb.morphia.TestDatastore.FacebookUser in project morphia by mongodb.

the class MapReduceOptionsTest method mapReduceCommand.

@Test
@SuppressWarnings("deprecation")
public void mapReduceCommand() {
    Query<FacebookUser> query = getDs().find(FacebookUser.class);
    MapReduceOptions<FacebookUser> options = new MapReduceOptions<FacebookUser>().bypassDocumentValidation(true).collation(Collation.builder().locale("en").build()).finalize("i'm a finalize function").jsMode(true).limit(42).map("i'm a map function").maxTimeMS(42000).outputCollection("output collection").outputDB("output db").outputType(OutputType.INLINE).query(query).readPreference(ReadPreference.primaryPreferred()).reduce("i'm a reduce function").scope(new Document("key", "value").append("key2", "value2")).verbose(true);
    MapReduceCommand command = options.toCommand(getMorphia().getMapper());
    assertTrue(command.getBypassDocumentValidation());
    assertEquals(Collation.builder().locale("en").build(), command.getCollation());
    assertTrue(command.getJsMode());
    assertEquals(42, command.getLimit());
    assertEquals("i'm a map function", command.getMap());
    assertEquals(42000, command.getMaxTime(TimeUnit.MILLISECONDS));
    assertEquals("output collection", command.getOutputTarget());
    assertEquals("output db", command.getOutputDB());
    assertEquals(query.getQueryObject(), command.getQuery());
    assertEquals(query.getSortObject(), command.getSort());
    assertEquals(ReadPreference.primaryPreferred(), command.getReadPreference());
    assertEquals("i'm a map function", command.getMap());
    assertEquals("i'm a reduce function", command.getReduce());
    assertEquals("i'm a finalize function", command.getFinalize());
    assertEquals(new Document("key", "value").append("key2", "value2"), command.getScope());
    assertTrue(command.isVerbose());
}
Also used : MapReduceCommand(com.mongodb.MapReduceCommand) Document(org.bson.Document) FacebookUser(org.mongodb.morphia.TestDatastore.FacebookUser) Test(org.junit.Test)

Example 2 with FacebookUser

use of org.mongodb.morphia.TestDatastore.FacebookUser in project morphia by mongodb.

the class TestQuery method testKeyListLookups.

@Test
public void testKeyListLookups() {
    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 Iterable<Key<FacebookUser>> fbKeys = getDs().save(asList(fbUser1, fbUser2, fbUser3, fbUser4));
    assertEquals(1, fbUser1.getId());
    final List<Key<FacebookUser>> fbUserKeys = new ArrayList<Key<FacebookUser>>();
    for (final Key<FacebookUser> key : fbKeys) {
        fbUserKeys.add(key);
    }
    assertEquals(fbUser1.getId(), fbUserKeys.get(0).getId());
    assertEquals(fbUser2.getId(), fbUserKeys.get(1).getId());
    assertEquals(fbUser3.getId(), fbUserKeys.get(2).getId());
    assertEquals(fbUser4.getId(), fbUserKeys.get(3).getId());
    final KeysKeysKeys k1 = new KeysKeysKeys(null, fbUserKeys);
    final Key<KeysKeysKeys> k1Key = getDs().save(k1);
    assertEquals(k1.getId(), k1Key.getId());
    final KeysKeysKeys k1Reloaded = getDs().get(k1);
    final KeysKeysKeys k1Loaded = getDs().getByKey(KeysKeysKeys.class, k1Key);
    assertNotNull(k1Reloaded);
    assertNotNull(k1Loaded);
    for (final Key<FacebookUser> key : k1Loaded.getUsers()) {
        assertNotNull(key.getId());
    }
    assertEquals(4, k1Loaded.getUsers().size());
    final List<FacebookUser> fbUsers = getDs().getByKeys(FacebookUser.class, k1Loaded.getUsers());
    assertEquals(4, fbUsers.size());
    for (final FacebookUser fbUser : fbUsers) {
        assertNotNull(fbUser);
        assertNotNull(fbUser.getId());
        assertNotNull(fbUser.getUsername());
    }
}
Also used : KeysKeysKeys(org.mongodb.morphia.TestDatastore.KeysKeysKeys) ArrayList(java.util.ArrayList) Key(org.mongodb.morphia.Key) FacebookUser(org.mongodb.morphia.TestDatastore.FacebookUser) Test(org.junit.Test)

Example 3 with FacebookUser

use of org.mongodb.morphia.TestDatastore.FacebookUser in project morphia by mongodb.

the class TestQuery method testKeyList.

@Test
public void testKeyList() {
    final Rectangle rect = new Rectangle(1000, 1);
    final Key<Rectangle> rectKey = getDs().save(rect);
    assertEquals(rectKey.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 Iterable<Key<FacebookUser>> fbKeys = getDs().save(asList(fbUser1, fbUser2, fbUser3, fbUser4));
    assertEquals(1, fbUser1.getId());
    final List<Key<FacebookUser>> fbUserKeys = new ArrayList<Key<FacebookUser>>();
    for (final Key<FacebookUser> key : fbKeys) {
        fbUserKeys.add(key);
    }
    assertEquals(fbUser1.getId(), fbUserKeys.get(0).getId());
    assertEquals(fbUser2.getId(), fbUserKeys.get(1).getId());
    assertEquals(fbUser3.getId(), fbUserKeys.get(2).getId());
    assertEquals(fbUser4.getId(), fbUserKeys.get(3).getId());
    final KeysKeysKeys k1 = new KeysKeysKeys(rectKey, fbUserKeys);
    final Key<KeysKeysKeys> k1Key = getDs().save(k1);
    assertEquals(k1.getId(), k1Key.getId());
    final KeysKeysKeys k1Loaded = getDs().get(k1);
    for (final Key<FacebookUser> key : k1Loaded.getUsers()) {
        assertNotNull(key.getId());
    }
    assertNotNull(k1Loaded.getRect().getId());
}
Also used : KeysKeysKeys(org.mongodb.morphia.TestDatastore.KeysKeysKeys) Rectangle(org.mongodb.morphia.testmodel.Rectangle) ArrayList(java.util.ArrayList) Key(org.mongodb.morphia.Key) FacebookUser(org.mongodb.morphia.TestDatastore.FacebookUser) Test(org.junit.Test)

Example 4 with FacebookUser

use of org.mongodb.morphia.TestDatastore.FacebookUser in project morphia by mongodb.

the class TestQuery method testGetByKeysHetero.

@Test
public void testGetByKeysHetero() {
    final Iterable<Key<Object>> keys = getDs().save(asList(new FacebookUser(1, "scott"), new Rectangle(1, 1)));
    final List<Object> entities = getDs().getByKeys(keys);
    assertNotNull(entities);
    assertEquals(2, entities.size());
    int userCount = 0;
    int rectCount = 0;
    for (final Object o : entities) {
        if (o instanceof Rectangle) {
            rectCount++;
        } else if (o instanceof FacebookUser) {
            userCount++;
        }
    }
    assertEquals(1, rectCount);
    assertEquals(1, userCount);
}
Also used : Rectangle(org.mongodb.morphia.testmodel.Rectangle) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) UsesCustomIdObject(org.mongodb.morphia.TestMapper.UsesCustomIdObject) Key(org.mongodb.morphia.Key) FacebookUser(org.mongodb.morphia.TestDatastore.FacebookUser) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 FacebookUser (org.mongodb.morphia.TestDatastore.FacebookUser)4 Key (org.mongodb.morphia.Key)3 ArrayList (java.util.ArrayList)2 KeysKeysKeys (org.mongodb.morphia.TestDatastore.KeysKeysKeys)2 Rectangle (org.mongodb.morphia.testmodel.Rectangle)2 BasicDBObject (com.mongodb.BasicDBObject)1 DBObject (com.mongodb.DBObject)1 MapReduceCommand (com.mongodb.MapReduceCommand)1 Document (org.bson.Document)1 UsesCustomIdObject (org.mongodb.morphia.TestMapper.UsesCustomIdObject)1