Search in sources :

Example 1 with Mapper

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

the class TestMapping method subTypes.

@Test
public void subTypes() {
    getMapper().map(EmbeddedType.class, Another.class, Child.class);
    Mapper mapper = getMapper();
    List<EntityModel> subTypes = mapper.getEntityModel(EmbeddedType.class).getSubtypes();
    Assert.assertTrue(subTypes.contains(mapper.getEntityModel(Another.class)));
    Assert.assertTrue(subTypes.contains(mapper.getEntityModel(Child.class)));
}
Also used : Mapper(dev.morphia.mapping.Mapper) EmbeddedType(dev.morphia.test.models.generics.EmbeddedType) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Test(org.testng.annotations.Test)

Example 2 with Mapper

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

the class TestMapping method testBasicMapping.

@Test
public void testBasicMapping() {
    Mapper mapper = getDs().getMapper();
    mapper.map(List.of(State.class, CityPopulation.class));
    final State state = new State();
    state.state = "NY";
    state.biggest = new CityPopulation("NYC", 8336817L);
    state.smallest = new CityPopulation("Red House", 38L);
    getDs().save(state);
    Query<State> query = getDs().find(State.class).filter(eq("_id", state.id));
    State loaded = query.first();
    assertEquals(loaded, state);
    assertEquals(mapper.getEntityModel(State.class).getProperties().stream().map(PropertyModel::getMappedName).collect(toList()), List.of("_id", "state", "biggestCity", "smallestCity"));
}
Also used : Mapper(dev.morphia.mapping.Mapper) State(dev.morphia.test.models.State) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) CityPopulation(dev.morphia.test.models.CityPopulation) Test(org.testng.annotations.Test)

Example 3 with Mapper

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

the class TestVersioning method testCanMapAnEntityWithAnAbstractVersionedParent.

@Test
public void testCanMapAnEntityWithAnAbstractVersionedParent() {
    Datastore datastore = Morphia.createDatastore(getMongoClient(), TEST_DB_NAME);
    Mapper mapper = datastore.getMapper();
    mapper.map(VersionedChildEntity.class);
    List<EntityModel> mappedEntities = mapper.getMappedEntities();
    assertEquals(mappedEntities.size(), 2, mappedEntities.toString());
    List<Class<?>> list = new ArrayList<>();
    for (EntityModel entityModel : mappedEntities) {
        list.add(entityModel.getType());
    }
    assertTrue(list.contains(VersionedChildEntity.class));
    assertTrue(list.contains(AbstractVersionedBase.class));
}
Also used : Mapper(dev.morphia.mapping.Mapper) VersionedChildEntity(dev.morphia.test.models.versioned.VersionedChildEntity) Datastore(dev.morphia.Datastore) Morphia.createDatastore(dev.morphia.Morphia.createDatastore) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) ArrayList(java.util.ArrayList) AbstractVersionedBase(dev.morphia.test.models.versioned.AbstractVersionedBase) Test(org.testng.annotations.Test)

Example 4 with Mapper

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

the class MorphiaKeyCursor method fromDocument.

private <I> I fromDocument(Class<I> type, Document document) {
    Class<I> aClass = type;
    Mapper mapper = datastore.getMapper();
    if (document.containsKey(mapper.getOptions().getDiscriminatorKey())) {
        aClass = mapper.getClass(document);
    }
    DocumentReader reader = new DocumentReader(document);
    return datastore.getCodecRegistry().get(aClass).decode(reader, DecoderContext.builder().build());
}
Also used : Mapper(dev.morphia.mapping.Mapper) DocumentReader(dev.morphia.mapping.codec.reader.DocumentReader)

Example 5 with Mapper

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

the class TestEnumMapping method testCustomerWithArrayList.

@Test
public void testCustomerWithArrayList() {
    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);
    CustomerWithArrayList customer = new CustomerWithArrayList();
    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();
    CustomerWithArrayList loaded = datastore1.find(CustomerWithArrayList.class).filter(eq("_id", customer.id)).first();
    assertEquals(customer.mapWithArrayList, loaded.mapWithArrayList);
}
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)

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