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());
}
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));
}
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));
}
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));
}
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());
}
Aggregations