use of com.mongodb.DBObject in project morphia by mongodb.
the class EmbeddedMappingTest method testNestedInterfaces.
@Test
public void testNestedInterfaces() {
getMorphia().map(WithNested.class, NestedImpl.class);
getDs().ensureIndexes();
final List<DBObject> indexInfo = getDs().getCollection(WithNested.class).getIndexInfo();
boolean indexFound = false;
for (DBObject dbObject : indexInfo) {
indexFound |= "nested.field.fail".equals(((DBObject) dbObject.get("key")).keySet().iterator().next());
}
Assert.assertTrue("Should find the nested field index", indexFound);
WithNested nested = new WithNested();
nested.nested = new NestedImpl("nested value");
getDs().save(nested);
WithNested found;
try {
getDs().find(WithNested.class).field("nested.field").equal("nested value").get();
Assert.fail("Querying against an interface should fail validation");
} catch (ValidationException ignore) {
// all good
}
found = getDs().find(WithNested.class).disableValidation().field("nested.field").equal("nested value").get();
Assert.assertNotNull(found);
Assert.assertEquals(nested, found);
found = getDs().find(WithNested.class).disableValidation().field("nested.field.fails").equal("nested value").get();
Assert.assertNull(found);
}
use of com.mongodb.DBObject in project morphia by mongodb.
the class EmbeddedMappingTest method validateNestedInterfaces.
@Test
public void validateNestedInterfaces() {
getMorphia().map(WithNestedValidated.class, Nested.class, NestedImpl.class, AnotherNested.class);
try {
getDs().ensureIndexes();
} catch (MappingException e) {
Assert.assertEquals("Could not resolve path 'nested.field.fail' against 'org.mongodb.morphia.mapping" + ".EmbeddedMappingTest$WithNestedValidated'.", e.getMessage());
}
final List<DBObject> indexInfo = getDs().getCollection(WithNestedValidated.class).getIndexInfo();
boolean indexFound = false;
for (DBObject dbObject : indexInfo) {
indexFound |= "nested.field.fail".equals(((DBObject) dbObject.get("key")).keySet().iterator().next());
}
Assert.assertFalse("Should not find the nested field index", indexFound);
}
use of com.mongodb.DBObject in project morphia by mongodb.
the class QueryFactoryTest method createQuery.
@Test
public void createQuery() {
final AtomicInteger counter = new AtomicInteger();
final QueryFactory queryFactory = new DefaultQueryFactory() {
@Override
public <T> Query<T> createQuery(final Datastore datastore, final DBCollection collection, final Class<T> type, final DBObject query) {
counter.incrementAndGet();
return super.createQuery(datastore, collection, type, query);
}
};
getDs().setQueryFactory(queryFactory);
final Query<String> query = getDs().find(String.class);
final Query<String> other = getDs().find(String.class);
Assert.assertNotSame(other, query);
Assert.assertEquals(2, counter.get());
}
use of com.mongodb.DBObject in project morphia by mongodb.
the class MapperOptionsTest method shouldFindField.
private void shouldFindField(final HasCollectionValuedMap hm, final Map<String, Collection<String>> expected) {
final DBObject dbObj;
getDs().save(hm);
dbObj = getDs().getCollection(HasCollectionValuedMap.class).findOne();
Assert.assertTrue("Should find the field", dbObj.containsField("properties"));
Assert.assertEquals(expected, getDs().find(HasCollectionValuedMap.class).get().properties);
}
use of com.mongodb.DBObject in project morphia by mongodb.
the class MapperOptionsTest method shouldNotFindField.
private void shouldNotFindField(final HasList hl) {
getDs().save(hl);
DBObject dbObj = getDs().getCollection(HasList.class).findOne();
Assert.assertFalse("field should not exist, value = " + dbObj.get("names"), dbObj.containsField("names"));
Assert.assertNull(getDs().find(HasList.class).get().names);
}
Aggregations