Search in sources :

Example 51 with Mapper

use of org.mongodb.morphia.mapping.Mapper in project morphia by mongodb.

the class PathTargetTest method maps.

@Test
public void maps() {
    getMorphia().map(Student.class, Article.class);
    Mapper mapper = getMorphia().getMapper();
    MappedClass mappedClass = mapper.getMappedClass(Student.class);
    PathTarget pathTarget = new PathTarget(mapper, mappedClass, "grades.$.data.name");
    Assert.assertEquals("grades.$.d.name", pathTarget.translatedPath());
    Assert.assertEquals(mapper.getMappedClass(Grade.class).getMappedFieldByJavaField("data"), pathTarget.getTarget());
    pathTarget = new PathTarget(mapper, mappedClass, "grades.$.d.name");
    Assert.assertEquals("grades.$.d.name", pathTarget.translatedPath());
    Assert.assertEquals(mapper.getMappedClass(Grade.class).getMappedField("d"), pathTarget.getTarget());
    pathTarget = new PathTarget(mapper, mapper.getMappedClass(Article.class), "translations");
    Assert.assertEquals("translations", pathTarget.translatedPath());
}
Also used : Mapper(org.mongodb.morphia.mapping.Mapper) MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Example 52 with Mapper

use of org.mongodb.morphia.mapping.Mapper in project morphia by mongodb.

the class PathTargetTest method subClasses.

@Test
public void subClasses() {
    getMorphia().map(ParentType.class, EmbeddedType.class, EmbeddedSubtype.class);
    Mapper mapper = getMorphia().getMapper();
    PathTarget pathTarget = new PathTarget(mapper, mapper.getMappedClass(ParentType.class), "embedded.flag");
    Assert.assertEquals("embedded.flag", pathTarget.translatedPath());
    Assert.assertEquals(mapper.getMappedClass(EmbeddedSubtype.class).getMappedFieldByJavaField("flag"), pathTarget.getTarget());
}
Also used : Mapper(org.mongodb.morphia.mapping.Mapper) ParentType(org.mongodb.morphia.entities.ParentType) Test(org.junit.Test)

Example 53 with Mapper

use of org.mongodb.morphia.mapping.Mapper in project morphia by mongodb.

the class TestMapping method testToMongoObjectCorrectlyMapsSerializableFieldForIssue591.

@Test
public void testToMongoObjectCorrectlyMapsSerializableFieldForIssue591() {
    // given
    Mapper mapper = new Mapper();
    User user = new User();
    user.id = 1;
    user.userObject = new SerializableObject();
    MappedClass mc = new MappedClass(User.class, mapper);
    MappedField mf = mc.getMappedField("userObject");
    // when
    Object dbValue = mapper.toMongoObject(mf, null, user.userObject);
    Class<byte[]> byteArrayClass = byte[].class;
    // then
    assertThat(dbValue, is(instanceOf(byteArrayClass)));
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) Mapper(org.mongodb.morphia.mapping.Mapper) DBObject(com.mongodb.DBObject) MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Example 54 with Mapper

use of org.mongodb.morphia.mapping.Mapper in project morphia by mongodb.

the class TestMapping method testToMongoObjectCorrectlyMapsSerializableListOfObjectsForIssue591.

@Test
public void testToMongoObjectCorrectlyMapsSerializableListOfObjectsForIssue591() {
    // given
    Mapper mapper = new Mapper();
    ListEntity user = new ListEntity();
    user.id = 1;
    List<Object> list = new ArrayList<Object>();
    list.add("value");
    user.list = list;
    MappedClass mc = new MappedClass(ListEntity.class, mapper);
    MappedField mf = mc.getMappedField("list");
    // when
    Object dbValue = mapper.toMongoObject(mf, null, user.list);
    Class<byte[]> byteArrayClass = byte[].class;
    // then
    assertThat(dbValue, is(instanceOf(byteArrayClass)));
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) Mapper(org.mongodb.morphia.mapping.Mapper) ArrayList(java.util.ArrayList) DBObject(com.mongodb.DBObject) MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Example 55 with Mapper

use of org.mongodb.morphia.mapping.Mapper in project morphia by mongodb.

the class QueryValidatorTest method shouldNotAllowGeoWithinWhenValueDoesNotContainKeyword.

@Test
public void shouldNotAllowGeoWithinWhenValueDoesNotContainKeyword() {
    // expect
    MappedClass mappedClass = new MappedClass(GeoEntity.class, new Mapper());
    MappedField mappedField = mappedClass.getMappedField("array");
    assertThat(QueryValidator.isCompatibleForOperator(mappedClass, mappedField, List.class, GEO_WITHIN, new BasicDBObject("notValidKey", 1), new ArrayList<ValidationFailure>()), is(false));
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) Mapper(org.mongodb.morphia.mapping.Mapper) BasicDBObject(com.mongodb.BasicDBObject) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Aggregations

Mapper (org.mongodb.morphia.mapping.Mapper)62 Test (org.junit.Test)61 MappedClass (org.mongodb.morphia.mapping.MappedClass)53 MappedField (org.mongodb.morphia.mapping.MappedField)46 ArrayList (java.util.ArrayList)41 BasicDBObject (com.mongodb.BasicDBObject)9 DBObject (com.mongodb.DBObject)5 List (java.util.List)5 ObjectId (org.bson.types.ObjectId)5 SimpleEntity (org.mongodb.morphia.entities.SimpleEntity)5 Key (org.mongodb.morphia.Key)4 ParentType (org.mongodb.morphia.entities.ParentType)2 DefaultEntityCache (org.mongodb.morphia.mapping.cache.DefaultEntityCache)2 CollectionIdConverter (edu.stanford.bmir.protege.web.server.collection.CollectionIdConverter)1 CollectionItemConverter (edu.stanford.bmir.protege.web.server.collection.CollectionItemConverter)1 FormDataConverter (edu.stanford.bmir.protege.web.server.form.FormDataConverter)1 FormDataValueConverter (edu.stanford.bmir.protege.web.server.form.FormDataValueConverter)1 BsonDocument (org.bson.BsonDocument)1 BsonString (org.bson.BsonString)1 Document (org.bson.Document)1