Search in sources :

Example 21 with Mapper

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

the class MappedFieldTypeValidatorTest method shouldAllowArraysOfNumbers.

@Test
public void shouldAllowArraysOfNumbers() {
    // given
    MappedClass mappedClass = new MappedClass(EntityWithListsAndArrays.class, new Mapper());
    MappedField mappedField = mappedClass.getMappedField("arrayOfInts");
    // expect
    assertThat(MappedFieldTypeValidator.isArrayOfNumbers(mappedField), is(true));
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) Mapper(org.mongodb.morphia.mapping.Mapper) MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Example 22 with Mapper

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

the class SizeOperationValidatorTest method shouldAllowSizeOperatorForIterableTypesAndIntegerValues.

@Test
public void shouldAllowSizeOperatorForIterableTypesAndIntegerValues() {
    // given
    MappedClass mappedClass = new MappedClass(EntityWithListsAndArrays.class, new Mapper());
    MappedField mappedField = mappedClass.getMappedField("setOfIntegers");
    List<ValidationFailure> validationFailures = new ArrayList<ValidationFailure>();
    // when
    boolean validationApplied = SizeOperationValidator.getInstance().apply(mappedField, SIZE, 3, validationFailures);
    // then
    assertThat(validationApplied, is(true));
    assertThat(validationFailures.size(), is(0));
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) Mapper(org.mongodb.morphia.mapping.Mapper) ArrayList(java.util.ArrayList) MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Example 23 with Mapper

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

the class TestMapper method subTypes.

@Test
public void subTypes() {
    getMorphia().map(NestedImpl.class, AnotherNested.class);
    Mapper mapper = getMorphia().getMapper();
    List<MappedClass> subTypes = mapper.getSubTypes(mapper.getMappedClass(Nested.class));
    Assert.assertTrue(subTypes.contains(mapper.getMappedClass(NestedImpl.class)));
    Assert.assertTrue(subTypes.contains(mapper.getMappedClass(AnotherNested.class)));
}
Also used : Mapper(org.mongodb.morphia.mapping.Mapper) AnotherNested(org.mongodb.morphia.mapping.EmbeddedMappingTest.AnotherNested) Nested(org.mongodb.morphia.mapping.EmbeddedMappingTest.Nested) MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Example 24 with Mapper

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

the class ExternalMapperExtTest method testExternalMapping.

@Test
public void testExternalMapping() throws Exception {
    final Mapper mapper = getMorphia().getMapper();
    final CloneMapper helper = new CloneMapper(mapper);
    helper.map(Skeleton.class, EntityWithNoAnnotations.class);
    final MappedClass mc = mapper.getMappedClass(EntityWithNoAnnotations.class);
    mc.update();
    assertNotNull(mc.getIdField());
    assertNotNull(mc.getEntityAnnotation());
    assertEquals("special", mc.getEntityAnnotation().value());
    EntityWithNoAnnotations ent = new EntityWithNoAnnotations();
    ent.id = "test";
    final Key<EntityWithNoAnnotations> k = getDs().save(ent);
    assertNotNull(k);
    ent = getDs().get(EntityWithNoAnnotations.class, "test");
    assertNotNull(ent);
    assertEquals("test", ent.id);
}
Also used : Mapper(org.mongodb.morphia.mapping.Mapper) MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Example 25 with Mapper

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

the class IndexHelperTest method createIndex.

@Test
public void createIndex() {
    checkMinServerVersion(3.4);
    String collectionName = getDs().getCollection(IndexedClass.class).getName();
    MongoCollection<Document> collection = getDatabase().getCollection(collectionName);
    Mapper mapper = getMorphia().getMapper();
    indexHelper.createIndex(collection, mapper.getMappedClass(IndexedClass.class), false);
    List<DBObject> indexInfo = getDs().getCollection(IndexedClass.class).getIndexInfo();
    assertEquals("Should have 6 indexes", 6, indexInfo.size());
    for (DBObject dbObject : indexInfo) {
        String name = dbObject.get("name").toString();
        if (name.equals("latitude_1")) {
            assertEquals(parse("{ 'latitude' : 1 }"), dbObject.get("key"));
        } else if (name.equals("behind_interface")) {
            assertEquals(parse("{ 'nest.name' : -1} "), dbObject.get("key"));
            assertEquals(parse("{ 'locale' : 'en' , 'caseLevel' : false , 'caseFirst' : 'off' , 'strength' : 2 , 'numericOrdering' :" + " false , 'alternate' : 'non-ignorable' , 'maxVariable' : 'punct' , 'normalization' : false , " + "'backwards' : false , 'version' : '57.1'}"), dbObject.get("collation"));
        } else if (name.equals("nest.name_1")) {
            assertEquals(parse("{ 'nest.name' : 1} "), dbObject.get("key"));
        } else if (name.equals("searchme")) {
            assertEquals(parse("{ 'text' : 10 }"), dbObject.get("weights"));
        } else if (name.equals("indexName_1")) {
            assertEquals(parse("{'indexName': 1 }"), dbObject.get("key"));
        } else {
            if (!"_id_".equals(dbObject.get("name"))) {
                throw new MappingException("Found an index I wasn't expecting:  " + dbObject);
            }
        }
    }
    collection = getDatabase().getCollection(getDs().getCollection(AbstractParent.class).getName());
    indexHelper.createIndex(collection, mapper.getMappedClass(AbstractParent.class), false);
    indexInfo = getDs().getCollection(AbstractParent.class).getIndexInfo();
    assertTrue("Shouldn't find any indexes: " + indexInfo, indexInfo.isEmpty());
}
Also used : Mapper(org.mongodb.morphia.mapping.Mapper) BsonString(org.bson.BsonString) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) MappingException(org.mongodb.morphia.mapping.MappingException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)61 Mapper (org.mongodb.morphia.mapping.Mapper)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 BsonDocument (org.bson.BsonDocument)1 BsonString (org.bson.BsonString)1 Document (org.bson.Document)1 BasicDAO (org.mongodb.morphia.dao.BasicDAO)1 AnotherNested (org.mongodb.morphia.mapping.EmbeddedMappingTest.AnotherNested)1 Nested (org.mongodb.morphia.mapping.EmbeddedMappingTest.Nested)1 MappingException (org.mongodb.morphia.mapping.MappingException)1