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