Search in sources :

Example 6 with Mapper

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

the class IndexHelperTest method createIndex.

@Test
public void createIndex() {
    String collectionName = getDs().getCollection(IndexedClass.class).getNamespace().getCollectionName();
    MongoCollection<Document> collection = getDatabase().getCollection(collectionName);
    Mapper mapper = getMapper();
    getIndexHelper().createIndex(collection, mapper.getEntityModel(IndexedClass.class));
    List<Document> indexInfo = getIndexInfo(IndexedClass.class);
    List<String> names = new ArrayList<>(asList("latitude_1", "searchme", "indexName_1"));
    for (Document document : indexInfo) {
        String name = document.get("name").toString();
        if (name.equals("latitude_1")) {
            names.remove(name);
            assertEquals(document.get("key"), parse("{ 'latitude' : 1 }"));
        } else if (name.equals("searchme")) {
            names.remove(name);
            assertEquals(document.get("weights"), parse("{ 'text' : 10 }"));
        } else if (name.equals("indexName_1")) {
            names.remove(name);
            assertEquals(document.get("key"), parse("{'indexName': 1 }"));
        } else {
            if (!"_id_".equals(document.get("name"))) {
                throw new MappingException("Found an index I wasn't expecting:  " + document);
            }
        }
    }
    assertTrue(names.isEmpty(), "Should be empty: " + names);
    collection = getDatabase().getCollection(getDs().getCollection(AbstractParent.class).getNamespace().getCollectionName());
    getIndexHelper().createIndex(collection, mapper.getEntityModel(AbstractParent.class));
    indexInfo = getIndexInfo(AbstractParent.class);
    assertTrue(indexInfo.isEmpty(), "Shouldn't find any indexes: " + indexInfo);
}
Also used : Mapper(dev.morphia.mapping.Mapper) ArrayList(java.util.ArrayList) Document(org.bson.Document) MappingException(dev.morphia.mapping.MappingException) Test(org.testng.annotations.Test)

Example 7 with Mapper

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

the class PathTargetTest method maps.

@Test
public void maps() {
    getMapper().map(Student.class);
    Mapper mapper = getMapper();
    EntityModel entityModel = mapper.getEntityModel(Student.class);
    PathTarget pathTarget = new PathTarget(mapper, entityModel, "grades.$.data.name");
    Assert.assertEquals(pathTarget.translatedPath(), "grades.$.d.name");
    Assert.assertEquals(mapper.getEntityModel(Grade.class).getProperty("data"), pathTarget.getTarget());
    pathTarget = new PathTarget(mapper, entityModel, "grades.$.d.name");
    Assert.assertEquals(pathTarget.translatedPath(), "grades.$.d.name");
    Assert.assertEquals(mapper.getEntityModel(Grade.class).getProperty("d"), pathTarget.getTarget());
}
Also used : Mapper(dev.morphia.mapping.Mapper) PathTarget(dev.morphia.internal.PathTarget) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Test(org.testng.annotations.Test)

Example 8 with Mapper

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

the class TestEnumMapping method testCustomerWithList.

@Test
public void testCustomerWithList() {
    MapperOptions options = MapperOptions.builder(getMapper().getOptions()).storeEmpties(true).storeNulls(true).build();
    final Datastore datastore = Morphia.createDatastore(getMongoClient(), getDatabase().getName(), options);
    Mapper mapper = datastore.getMapper();
    mapper.map(CustomerWithArrayList.class);
    CustomerWithList customer = new CustomerWithList();
    List<WebTemplate> templates1 = new ArrayList<>();
    templates1.add(new WebTemplate("template #1.1"));
    templates1.add(new WebTemplate("template #1.2"));
    customer.add(WebTemplateType.CrewContract, templates1);
    List<WebTemplate> templates2 = new ArrayList<>();
    templates1.add(new WebTemplate("template #2.1"));
    templates1.add(new WebTemplate("template #2.2"));
    customer.add(WebTemplateType.CrewContractHeader, templates2);
    getDs().save(customer);
    final Datastore datastore1 = getDs();
    CustomerWithList loaded = datastore1.find(CustomerWithList.class).filter(eq("_id", customer.id)).first();
    assertEquals(customer.mapWithList, loaded.mapWithList);
}
Also used : MapperOptions(dev.morphia.mapping.MapperOptions) Mapper(dev.morphia.mapping.Mapper) Datastore(dev.morphia.Datastore) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 9 with Mapper

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

the class Projection method knownFields.

private Document knownFields(Mapper mapper, Class<?> clazz) {
    Document projection = new Document();
    mapper.getEntityModel(clazz).getProperties().stream().map(mf -> new PathTarget(mapper, mapper.getEntityModel(clazz), mf.getMappedName()).translatedPath()).forEach(name -> projection.put(name, 1));
    return projection;
}
Also used : Document(org.bson.Document) List(java.util.List) Sofia(dev.morphia.sofia.Sofia) PathTarget(dev.morphia.internal.PathTarget) Mapper(dev.morphia.mapping.Mapper) StringJoiner(java.util.StringJoiner) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Nullable(com.mongodb.lang.Nullable) Entity(dev.morphia.annotations.Entity) ArrayList(java.util.ArrayList) PathTarget(dev.morphia.internal.PathTarget) Document(org.bson.Document)

Example 10 with Mapper

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

the class PathTargetTest method arrays.

@Test
public void arrays() {
    getMapper().map(EntityWithListsAndArrays.class, EmbeddedType.class, Another.class, Child.class);
    Mapper mapper = getMapper();
    EntityModel entityModel = mapper.getEntityModel(EntityWithListsAndArrays.class);
    PathTarget pathTarget = new PathTarget(mapper, entityModel, "listEmbeddedType.1.anotherField");
    Assert.assertEquals(pathTarget.translatedPath(), "listEmbeddedType.1.anotherField");
    Assert.assertEquals(mapper.getEntityModel(Another.class).getProperty("anotherField"), pathTarget.getTarget());
    Assert.assertEquals(new PathTarget(mapper, entityModel, "listEmbeddedType.$").translatedPath(), "listEmbeddedType.$");
    Assert.assertEquals(new PathTarget(mapper, entityModel, "listEmbeddedType.1").translatedPath(), "listEmbeddedType.1");
}
Also used : Mapper(dev.morphia.mapping.Mapper) PathTarget(dev.morphia.internal.PathTarget) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Test(org.testng.annotations.Test)

Aggregations

Mapper (dev.morphia.mapping.Mapper)16 Test (org.testng.annotations.Test)13 PathTarget (dev.morphia.internal.PathTarget)8 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)7 ArrayList (java.util.ArrayList)5 Datastore (dev.morphia.Datastore)3 MapperOptions (dev.morphia.mapping.MapperOptions)2 DocumentReader (dev.morphia.mapping.codec.reader.DocumentReader)2 Document (org.bson.Document)2 Nullable (com.mongodb.lang.Nullable)1 Morphia.createDatastore (dev.morphia.Morphia.createDatastore)1 Entity (dev.morphia.annotations.Entity)1 MappingException (dev.morphia.mapping.MappingException)1 PropertyModel (dev.morphia.mapping.codec.pojo.PropertyModel)1 Sofia (dev.morphia.sofia.Sofia)1 CityPopulation (dev.morphia.test.models.CityPopulation)1 State (dev.morphia.test.models.State)1 EmbeddedType (dev.morphia.test.models.generics.EmbeddedType)1 AbstractVersionedBase (dev.morphia.test.models.versioned.AbstractVersionedBase)1 VersionedChildEntity (dev.morphia.test.models.versioned.VersionedChildEntity)1