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)));
}
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"));
}
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));
}
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());
}
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);
}
Aggregations