Search in sources :

Example 1 with Rectangle

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());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) Circle(org.mongodb.morphia.testmodel.Circle) Shape(org.mongodb.morphia.testmodel.Shape) Rectangle(org.mongodb.morphia.testmodel.Rectangle) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) ShapeShifter(org.mongodb.morphia.testmodel.ShapeShifter) Test(org.junit.Test)

Example 2 with Rectangle

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);
}
Also used : Circle(org.mongodb.morphia.testmodel.Circle) Shape(org.mongodb.morphia.testmodel.Shape) Random(java.util.Random) Rectangle(org.mongodb.morphia.testmodel.Rectangle) Test(org.junit.Test)

Example 3 with Rectangle

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));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Circle(org.mongodb.morphia.testmodel.Circle) ObjectId(org.bson.types.ObjectId) Rectangle(org.mongodb.morphia.testmodel.Rectangle) Test(org.junit.Test)

Example 4 with Rectangle

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);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Rectangle(org.mongodb.morphia.testmodel.Rectangle) Test(org.junit.Test)

Example 5 with Rectangle

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();
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) ObjectId(org.bson.types.ObjectId) Rectangle(org.mongodb.morphia.testmodel.Rectangle) 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