Search in sources :

Example 11 with Rectangle

use of org.mongodb.morphia.testmodel.Rectangle in project morphia by mongodb.

the class TestDatastore method testIdUpdatedOnSave.

@Test
public void testIdUpdatedOnSave() throws Exception {
    final Rectangle rect = new Rectangle(10, 10);
    getDs().save(rect);
    assertNotNull(rect.getId());
}
Also used : Rectangle(org.mongodb.morphia.testmodel.Rectangle) Test(org.junit.Test)

Example 12 with Rectangle

use of org.mongodb.morphia.testmodel.Rectangle in project morphia by mongodb.

the class TestDatastore method testSaveAndDelete.

@Test
public void testSaveAndDelete() {
    getDs().getCollection(Rectangle.class).drop();
    final Rectangle rect = new Rectangle(10, 10);
    ObjectId id = new ObjectId();
    rect.setId(id);
    //test delete(entity)
    getDs().save(rect);
    assertEquals(1, getDs().getCount(rect));
    getDs().delete(rect);
    assertEquals(0, getDs().getCount(rect));
    //test delete(entity, id)
    getDs().save(rect);
    assertEquals(1, getDs().getCount(rect));
    getDs().delete(rect.getClass(), 1);
    assertEquals(1, getDs().getCount(rect));
    getDs().delete(rect.getClass(), id);
    assertEquals(0, getDs().getCount(rect));
    //test delete(entity, {id})
    getDs().save(rect);
    assertEquals(1, getDs().getCount(rect));
    getDs().delete(rect.getClass(), singletonList(rect.getId()));
    assertEquals(0, getDs().getCount(rect));
    //test delete(entity, {id,id})
    ObjectId id1 = (ObjectId) getDs().save(new Rectangle(10, 10)).getId();
    ObjectId id2 = (ObjectId) getDs().save(new Rectangle(10, 10)).getId();
    assertEquals(2, getDs().getCount(rect));
    getDs().delete(rect.getClass(), asList(id1, id2));
    assertEquals(0, getDs().getCount(rect));
    //test delete(Class, {id,id})
    id1 = (ObjectId) getDs().save(new Rectangle(20, 20)).getId();
    id2 = (ObjectId) getDs().save(new Rectangle(20, 20)).getId();
    assertEquals("datastore should have saved two entities with autogenerated ids", 2, getDs().getCount(rect));
    getDs().delete(rect.getClass(), asList(id1, id2));
    assertEquals("datastore should have deleted two entities with autogenerated ids", 0, getDs().getCount(rect));
    //test delete(entity, {id}) with one left
    id1 = (ObjectId) getDs().save(new Rectangle(20, 20)).getId();
    getDs().save(new Rectangle(20, 20));
    assertEquals(2, getDs().getCount(rect));
    getDs().delete(rect.getClass(), singletonList(id1));
    assertEquals(1, getDs().getCount(rect));
    getDs().getCollection(Rectangle.class).drop();
    //test delete(Class, {id}) with one left
    id1 = (ObjectId) getDs().save(new Rectangle(20, 20)).getId();
    getDs().save(new Rectangle(20, 20));
    assertEquals(2, getDs().getCount(rect));
    getDs().delete(Rectangle.class, singletonList(id1));
    assertEquals(1, getDs().getCount(rect));
}
Also used : ObjectId(org.bson.types.ObjectId) Rectangle(org.mongodb.morphia.testmodel.Rectangle) Test(org.junit.Test)

Example 13 with Rectangle

use of org.mongodb.morphia.testmodel.Rectangle in project morphia by mongodb.

the class TestSuperDatastore method testDeleteDoesNotDeleteAnythingWhenGivenAnIncorrectId.

@Test
public void testDeleteDoesNotDeleteAnythingWhenGivenAnIncorrectId() throws Exception {
    // given
    final String ns = "someCollectionName";
    getDb().getCollection(ns).remove(new BasicDBObject());
    final Rectangle rect = new Rectangle(10, 10);
    ObjectId id = new ObjectId();
    rect.setId(id);
    getAds().save(ns, rect);
    assertEquals(1, getAds().getCount(ns));
    // when giving an ID that is not the entity ID.  Note that at the time of writing this will also log a validation warning
    getAds().delete(ns, Rectangle.class, 1);
    // then
    assertEquals(1, getAds().getCount(ns));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) ObjectId(org.bson.types.ObjectId) Rectangle(org.mongodb.morphia.testmodel.Rectangle) Test(org.junit.Test)

Example 14 with Rectangle

use of org.mongodb.morphia.testmodel.Rectangle in project morphia by mongodb.

the class TestSuperDatastore method testDeleteWithAnEntityTypeAndId.

@Test
public void testDeleteWithAnEntityTypeAndId() throws Exception {
    // given
    final String ns = "someCollectionName";
    getDb().getCollection(ns).remove(new BasicDBObject());
    final Rectangle rect = new Rectangle(10, 10);
    ObjectId id = new ObjectId();
    rect.setId(id);
    getAds().save(ns, rect);
    assertEquals(1, getAds().getCount(ns));
    // when
    getAds().delete(ns, Rectangle.class, id);
    // then
    assertEquals(0, getAds().getCount(ns));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) ObjectId(org.bson.types.ObjectId) Rectangle(org.mongodb.morphia.testmodel.Rectangle) Test(org.junit.Test)

Example 15 with Rectangle

use of org.mongodb.morphia.testmodel.Rectangle in project morphia by mongodb.

the class TestMapping method testDbRefMapping.

@Test
public void testDbRefMapping() throws Exception {
    getMorphia().map(ContainsRef.class).map(Rectangle.class);
    final DBCollection stuff = getDb().getCollection("stuff");
    final DBCollection rectangles = getDb().getCollection("rectangles");
    assertTrue("'ne' field should not be persisted!", !getMorphia().getMapper().getMCMap().get(ContainsRef.class.getName()).containsJavaFieldName("ne"));
    final Rectangle r = new Rectangle(1, 1);
    final DBObject rDbObject = getMorphia().toDBObject(r);
    rDbObject.put("_ns", rectangles.getName());
    rectangles.save(rDbObject);
    final ContainsRef cRef = new ContainsRef();
    cRef.rect = new DBRef((String) rDbObject.get("_ns"), rDbObject.get("_id"));
    final DBObject cRefDbObject = getMorphia().toDBObject(cRef);
    stuff.save(cRefDbObject);
    final BasicDBObject cRefDbObjectLoaded = (BasicDBObject) stuff.findOne(BasicDBObjectBuilder.start("_id", cRefDbObject.get("_id")).get());
    final ContainsRef cRefLoaded = getMorphia().fromDBObject(getDs(), ContainsRef.class, cRefDbObjectLoaded, new DefaultEntityCache());
    assertNotNull(cRefLoaded);
    assertNotNull(cRefLoaded.rect);
    assertNotNull(cRefLoaded.rect.getId());
    assertNotNull(cRefLoaded.rect.getCollectionName());
    assertEquals(cRefLoaded.rect.getId(), cRef.rect.getId());
    assertEquals(cRefLoaded.rect.getCollectionName(), cRef.rect.getCollectionName());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) Rectangle(org.mongodb.morphia.testmodel.Rectangle) DBRef(com.mongodb.DBRef) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)23 Rectangle (org.mongodb.morphia.testmodel.Rectangle)23 BasicDBObject (com.mongodb.BasicDBObject)9 ObjectId (org.bson.types.ObjectId)5 Circle (org.mongodb.morphia.testmodel.Circle)4 DBObject (com.mongodb.DBObject)3 Shape (org.mongodb.morphia.testmodel.Shape)3 DBCollection (com.mongodb.DBCollection)2 Random (java.util.Random)2 Key (org.mongodb.morphia.Key)2 FacebookUser (org.mongodb.morphia.TestDatastore.FacebookUser)2 DefaultEntityCache (org.mongodb.morphia.mapping.cache.DefaultEntityCache)2 DBRef (com.mongodb.DBRef)1 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 KeysKeysKeys (org.mongodb.morphia.TestDatastore.KeysKeysKeys)1 UsesCustomIdObject (org.mongodb.morphia.TestMapper.UsesCustomIdObject)1 UpdateResults (org.mongodb.morphia.query.UpdateResults)1 ShapeShifter (org.mongodb.morphia.testmodel.ShapeShifter)1