Search in sources :

Example 1 with Article

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

the class TestMapping method testMaps.

@Test
public void testMaps() throws Exception {
    final DBCollection articles = getDb().getCollection("articles");
    getMorphia().map(Article.class).map(Translation.class).map(Circle.class);
    final Article related = new Article();
    final BasicDBObject relatedDbObj = (BasicDBObject) getMorphia().toDBObject(related);
    articles.save(relatedDbObj);
    final Article relatedLoaded = getMorphia().fromDBObject(getDs(), Article.class, articles.findOne(new BasicDBObject(Mapper.ID_KEY, relatedDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
    final Article article = new Article();
    article.setTranslation("en", new Translation("Hello World", "Just a test"));
    article.setTranslation("is", new Translation("Halló heimur", "Bara að prófa"));
    article.setAttribute("myDate", new Date());
    article.setAttribute("myString", "Test");
    article.setAttribute("myInt", 123);
    article.putRelated("test", relatedLoaded);
    final BasicDBObject articleDbObj = (BasicDBObject) getMorphia().toDBObject(article);
    articles.save(articleDbObj);
    final Article articleLoaded = getMorphia().fromDBObject(getDs(), Article.class, articles.findOne(new BasicDBObject(Mapper.ID_KEY, articleDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
    assertEquals(article.getTranslations().size(), articleLoaded.getTranslations().size());
    assertEquals(article.getTranslation("en").getTitle(), articleLoaded.getTranslation("en").getTitle());
    assertEquals(article.getTranslation("is").getBody(), articleLoaded.getTranslation("is").getBody());
    assertEquals(article.getAttributes().size(), articleLoaded.getAttributes().size());
    assertEquals(article.getAttribute("myDate"), articleLoaded.getAttribute("myDate"));
    assertEquals(article.getAttribute("myString"), articleLoaded.getAttribute("myString"));
    assertEquals(article.getAttribute("myInt"), articleLoaded.getAttribute("myInt"));
    assertEquals(article.getRelated().size(), articleLoaded.getRelated().size());
    assertEquals(article.getRelated("test").getId(), articleLoaded.getRelated("test").getId());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) Translation(org.mongodb.morphia.testmodel.Translation) Article(org.mongodb.morphia.testmodel.Article) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) Date(java.util.Date) Test(org.junit.Test)

Example 2 with Article

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

the class TestUpdateOps method testSetUnset.

@Test
public void testSetUnset() throws Exception {
    Datastore ds = getDs();
    final Key<Circle> key = ds.save(new Circle(1));
    assertUpdated(ds.update(ds.find(Circle.class).filter("radius", 1D), ds.createUpdateOperations(Circle.class).set("radius", 2D), new UpdateOptions().multi(false)), 1);
    assertThat(ds.getByKey(Circle.class, key).getRadius(), is(2D));
    assertUpdated(ds.update(ds.find(Circle.class).filter("radius", 2D), ds.createUpdateOperations(Circle.class).unset("radius"), new UpdateOptions().multi(false)), 1);
    assertThat(ds.getByKey(Circle.class, key).getRadius(), is(0D));
    Article article = new Article();
    ds.save(article);
    ds.update(ds.find(Article.class), ds.createUpdateOperations(Article.class).set("translations", new HashMap<String, Translation>()));
    ds.update(ds.find(Article.class), ds.createUpdateOperations(Article.class).unset("translations"));
}
Also used : Circle(org.mongodb.morphia.testmodel.Circle) HashMap(java.util.HashMap) Article(org.mongodb.morphia.testmodel.Article) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 Article (org.mongodb.morphia.testmodel.Article)2 BasicDBObject (com.mongodb.BasicDBObject)1 DBCollection (com.mongodb.DBCollection)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 DefaultEntityCache (org.mongodb.morphia.mapping.cache.DefaultEntityCache)1 Circle (org.mongodb.morphia.testmodel.Circle)1 Translation (org.mongodb.morphia.testmodel.Translation)1