Search in sources :

Example 96 with DBObject

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);
}
Also used : ValidationException(org.mongodb.morphia.query.ValidationException) DBObject(com.mongodb.DBObject) Test(org.junit.Test)

Example 97 with DBObject

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);
}
Also used : DBObject(com.mongodb.DBObject) Test(org.junit.Test)

Example 98 with DBObject

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());
}
Also used : DBCollection(com.mongodb.DBCollection) Datastore(org.mongodb.morphia.Datastore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DBObject(com.mongodb.DBObject) Test(org.junit.Test)

Example 99 with DBObject

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);
}
Also used : DBObject(com.mongodb.DBObject)

Example 100 with DBObject

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);
}
Also used : DBObject(com.mongodb.DBObject)

Aggregations

DBObject (com.mongodb.DBObject)545 BasicDBObject (com.mongodb.BasicDBObject)386 Test (org.junit.Test)214 DBCollection (com.mongodb.DBCollection)83 YearFilterPagingRequest (org.devgateway.ocds.web.rest.controller.request.YearFilterPagingRequest)54 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)52 ApiOperation (io.swagger.annotations.ApiOperation)47 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)46 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)41 DBCursor (com.mongodb.DBCursor)40 ArrayList (java.util.ArrayList)38 HashMap (java.util.HashMap)38 List (java.util.List)31 CustomProjectionOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation)31 Map (java.util.Map)26 ObjectId (org.bson.types.ObjectId)26 BasicDBList (com.mongodb.BasicDBList)24 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)20 BSONObject (org.bson.BSONObject)19 MongoException (com.mongodb.MongoException)18