Search in sources :

Example 1 with Circle

use of org.mongodb.morphia.testmodel.Circle 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 Circle

use of org.mongodb.morphia.testmodel.Circle 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 Circle

use of org.mongodb.morphia.testmodel.Circle 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 Circle

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

the class TestUpdateOps method testMinUsesSuppliedValueWhenThisIsSmallerThanCurrentDocumentValue.

@Test
public void testMinUsesSuppliedValueWhenThisIsSmallerThanCurrentDocumentValue() throws Exception {
    checkMinServerVersion(2.6);
    final ObjectId id = new ObjectId();
    final double newLowerValue = 2D;
    assertInserted(getDs().update(getDs().find(Circle.class).field("id").equal(id), getDs().createUpdateOperations(Circle.class).setOnInsert("radius", 3D), new UpdateOptions().upsert(true)));
    assertUpdated(getDs().update(getDs().find(Circle.class).field("id").equal(id), getDs().createUpdateOperations(Circle.class).min("radius", newLowerValue), new UpdateOptions().upsert(true)), 1);
    final Circle updatedCircle = getDs().get(Circle.class, id);
    assertThat(updatedCircle, is(notNullValue()));
    assertThat(updatedCircle.getRadius(), is(newLowerValue));
}
Also used : Circle(org.mongodb.morphia.testmodel.Circle) ObjectId(org.bson.types.ObjectId) Test(org.junit.Test)

Example 5 with Circle

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

the class TestUpdateOps method testSetOnInsertWhenInserting.

@Test
public void testSetOnInsertWhenInserting() throws Exception {
    checkMinServerVersion(2.4);
    ObjectId id = new ObjectId();
    assertInserted(getDs().update(getDs().find(Circle.class).field("id").equal(id), getDs().createUpdateOperations(Circle.class).setOnInsert("radius", 2D), new UpdateOptions().upsert(true)));
    final Circle updatedCircle = getDs().get(Circle.class, id);
    assertThat(updatedCircle, is(notNullValue()));
    assertThat(updatedCircle.getRadius(), is(2D));
}
Also used : Circle(org.mongodb.morphia.testmodel.Circle) ObjectId(org.bson.types.ObjectId) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)10 Circle (org.mongodb.morphia.testmodel.Circle)10 ObjectId (org.bson.types.ObjectId)5 Rectangle (org.mongodb.morphia.testmodel.Rectangle)4 Shape (org.mongodb.morphia.testmodel.Shape)3 BasicDBObject (com.mongodb.BasicDBObject)2 Random (java.util.Random)2 DBCollection (com.mongodb.DBCollection)1 DBObject (com.mongodb.DBObject)1 HashMap (java.util.HashMap)1 DefaultEntityCache (org.mongodb.morphia.mapping.cache.DefaultEntityCache)1 Article (org.mongodb.morphia.testmodel.Article)1 ShapeShifter (org.mongodb.morphia.testmodel.ShapeShifter)1