use of org.mongodb.morphia.testmodel.Rectangle in project morphia by mongodb.
the class TestInterfaces method testDynamicInstantiation.
@Test
public void testDynamicInstantiation() throws Exception {
final DBCollection shapes = getDb().getCollection("shapes");
final DBCollection shapeshifters = getDb().getCollection("shapeshifters");
getMorphia().map(Circle.class).map(Rectangle.class).map(ShapeShifter.class);
final Shape rectangle = new Rectangle(2, 5);
final DBObject rectangleDbObj = getMorphia().toDBObject(rectangle);
shapes.save(rectangleDbObj);
final BasicDBObject rectangleDbObjLoaded = (BasicDBObject) shapes.findOne(new BasicDBObject(Mapper.ID_KEY, rectangleDbObj.get(Mapper.ID_KEY)));
final Shape rectangleLoaded = getMorphia().fromDBObject(getDs(), Shape.class, rectangleDbObjLoaded, new DefaultEntityCache());
assertTrue(rectangle.getArea() == rectangleLoaded.getArea());
assertTrue(rectangleLoaded instanceof Rectangle);
final ShapeShifter shifter = new ShapeShifter();
shifter.setReferencedShape(rectangleLoaded);
shifter.setMainShape(new Circle(2.2));
shifter.getAvailableShapes().add(new Rectangle(3, 3));
shifter.getAvailableShapes().add(new Circle(4.4));
final DBObject shifterDbObj = getMorphia().toDBObject(shifter);
shapeshifters.save(shifterDbObj);
final BasicDBObject shifterDbObjLoaded = (BasicDBObject) shapeshifters.findOne(new BasicDBObject(Mapper.ID_KEY, shifterDbObj.get(Mapper.ID_KEY)));
final ShapeShifter shifterLoaded = getMorphia().fromDBObject(getDs(), ShapeShifter.class, shifterDbObjLoaded, new DefaultEntityCache());
assertNotNull(shifterLoaded);
assertNotNull(shifterLoaded.getReferencedShape());
assertNotNull(shifterLoaded.getReferencedShape().getArea());
assertNotNull(rectangle);
assertNotNull(rectangle.getArea());
assertTrue(rectangle.getArea() == shifterLoaded.getReferencedShape().getArea());
assertTrue(shifterLoaded.getReferencedShape() instanceof Rectangle);
assertTrue(shifter.getMainShape().getArea() == shifterLoaded.getMainShape().getArea());
assertEquals(shifter.getAvailableShapes().size(), shifterLoaded.getAvailableShapes().size());
}
use of org.mongodb.morphia.testmodel.Rectangle in project morphia by mongodb.
the class TestMapreduce method testOldMapReduce.
@Test
@SuppressWarnings("deprecation")
public void testOldMapReduce() throws Exception {
final Random rnd = new Random();
//create 100 circles and rectangles
for (int i = 0; i < 100; i++) {
getAds().insert("shapes", new Circle(rnd.nextDouble()));
getAds().insert("shapes", new Rectangle(rnd.nextDouble(), rnd.nextDouble()));
}
final String map = "function () { if(this['radius']) { emit('circle', {count:1}); return; } emit('rect', {count:1}); }";
final String reduce = "function (key, values) { var total = 0; for ( var i=0; i<values.length; i++ ) {total += values[i].count;} " + "return { count : total }; }";
final MapreduceResults<ResultEntity> mrRes = getDs().mapReduce(MapreduceType.REPLACE, getAds().find(Shape.class), map, reduce, null, null, ResultEntity.class);
Assert.assertEquals(2, mrRes.createQuery().countAll());
Assert.assertEquals(100, mrRes.createQuery().get().getValue().count, 0);
final MapreduceResults<ResultEntity> inline = getDs().mapReduce(MapreduceType.INLINE, getAds().find(Shape.class), map, reduce, null, null, ResultEntity.class);
final Iterator<ResultEntity> iterator = inline.iterator();
Assert.assertEquals(2, count(iterator));
Assert.assertEquals(100, inline.iterator().next().getValue().count, 0);
}
use of org.mongodb.morphia.testmodel.Rectangle in project morphia by mongodb.
the class TestSuperDatastore method testDeleteWillRemoveAnyDocumentWithAMatchingId.
@Test
public void testDeleteWillRemoveAnyDocumentWithAMatchingId() throws Exception {
// given
final String ns = "someCollectionName";
getDb().getCollection(ns).remove(new BasicDBObject());
final Rectangle rect = new Rectangle(10, 10);
ObjectId rectangleId = new ObjectId();
rect.setId(rectangleId);
getAds().save(ns, rect);
final Circle circle = new Circle();
circle.setId(new ObjectId());
getAds().save(ns, circle);
assertEquals(2, getAds().getCount(ns));
// when
getAds().delete(ns, Circle.class, rectangleId);
// then
assertEquals(1, getAds().getCount(ns));
}
use of org.mongodb.morphia.testmodel.Rectangle in project morphia by mongodb.
the class TestSuperDatastore method testGet.
@Test
public void testGet() throws Exception {
final String ns = "hotels";
final Rectangle rect = new Rectangle(10, 10);
getDb().getCollection(ns).remove(new BasicDBObject());
getAds().save(ns, rect);
assertEquals(1, getAds().getCount(ns));
final Rectangle rectLoaded = getAds().get(ns, Rectangle.class, rect.getId());
assertEquals(rect.getId(), rectLoaded.getId());
assertEquals(rect.getArea(), rectLoaded.getArea(), 0);
}
use of org.mongodb.morphia.testmodel.Rectangle in project morphia by mongodb.
the class TestSuperDatastore method testFind.
@Test
public void testFind() throws Exception {
final String ns = "hotels";
Rectangle rect = new Rectangle(10, 10);
ObjectId id = new ObjectId();
rect.setId(id);
getDb().getCollection(ns).remove(new BasicDBObject());
getAds().save(ns, rect);
assertEquals(1, getAds().getCount(ns));
Rectangle rectLoaded = getAds().find(ns, Rectangle.class).get();
assertEquals(rect.getId(), rectLoaded.getId());
assertEquals(rect.getArea(), rectLoaded.getArea(), 0);
rect = new Rectangle(2, 1);
//saved to default collection name (kind)
getAds().save(rect);
assertEquals(1, getAds().getCount(rect));
rect.setId(null);
//saved to default collection name (kind)
getAds().save(rect);
assertEquals(2, getAds().getCount(rect));
rect = new Rectangle(4, 3);
getAds().save(ns, rect);
assertEquals(2, getAds().getCount(ns));
rectLoaded = getAds().find(ns, Rectangle.class).asList().get(1);
assertEquals(rect.getId(), rectLoaded.getId());
assertEquals(rect.getArea(), rectLoaded.getArea(), 0);
getAds().find(ns, Rectangle.class, "_id !=", "-1", 1, 1).get();
}
Aggregations