Search in sources :

Example 6 with ValidationOptions

use of com.mongodb.client.model.ValidationOptions in project morphia by mongodb.

the class TestMapreduce method testBypassDocumentValidation.

@Test
public void testBypassDocumentValidation() {
    checkMinServerVersion(3.4);
    getDs().save(asList(new Book("The Banquet", "Dante", 2), new Book("Divine Comedy", "Dante", 1), new Book("Eclogues", "Dante", 2), new Book("The Odyssey", "Homer", 10), new Book("Iliad", "Homer", 10)));
    Document validator = Document.parse("{ count : { $gt : '10' } }");
    ValidationOptions validationOptions = new ValidationOptions().validator(validator).validationLevel(ValidationLevel.STRICT).validationAction(ValidationAction.ERROR);
    MongoDatabase database = getMongoClient().getDatabase(TEST_DB_NAME);
    database.getCollection("counts").drop();
    database.createCollection("counts", new CreateCollectionOptions().validationOptions(validationOptions));
    final String map = "function () { emit(this.author, 1); return; }";
    final String reduce = "function (key, values) { return values.length }";
    MapReduceOptions<CountResult> options = new MapReduceOptions<CountResult>().query(getDs().find(Book.class)).resultType(CountResult.class).outputType(OutputType.REPLACE).map(map).reduce(reduce);
    try {
        getDs().mapReduce(options);
        fail("Document validation should have complained.");
    } catch (MongoCommandException e) {
    // expected
    }
    getDs().mapReduce(options.bypassDocumentValidation(true));
    Assert.assertEquals(2, count(getDs().find(CountResult.class).iterator()));
}
Also used : MongoCommandException(com.mongodb.MongoCommandException) Book(org.mongodb.morphia.aggregation.AggregationTest.Book) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) Document(org.bson.Document) ValidationOptions(com.mongodb.client.model.ValidationOptions) CountResult(org.mongodb.morphia.aggregation.AggregationTest.CountResult) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.Test)

Example 7 with ValidationOptions

use of com.mongodb.client.model.ValidationOptions in project morphia by mongodb.

the class AggregationTest method testBypassDocumentValidation.

@Test
public void testBypassDocumentValidation() {
    checkMinServerVersion(3.2);
    getDs().save(asList(new User("john doe", new Date()), new User("John Doe", new Date())));
    MongoDatabase database = getMongoClient().getDatabase(TEST_DB_NAME);
    database.getCollection("out_users").drop();
    database.createCollection("out_users", new CreateCollectionOptions().validationOptions(new ValidationOptions().validator(Document.parse("{ age : { gte : 13 } }"))));
    try {
        getDs().createAggregation(User.class).match(getDs().find(User.class).field("name").equal("john doe")).out("out_users", User.class);
        fail("Document validation should have complained.");
    } catch (MongoCommandException e) {
    // expected
    }
    getDs().createAggregation(User.class).match(getDs().find(User.class).field("name").equal("john doe")).out("out_users", User.class, builder().bypassDocumentValidation(true).build());
    Assert.assertEquals(1, getAds().find("out_users", User.class).count());
}
Also used : MongoCommandException(com.mongodb.MongoCommandException) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) ValidationOptions(com.mongodb.client.model.ValidationOptions) Date(java.util.Date) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.Test)

Aggregations

ValidationOptions (com.mongodb.client.model.ValidationOptions)7 CreateCollectionOptions (com.mongodb.client.model.CreateCollectionOptions)4 MongoDatabase (com.mongodb.client.MongoDatabase)3 IndexOptionDefaults (com.mongodb.client.model.IndexOptionDefaults)3 CreateCollectionOperation (com.mongodb.operation.CreateCollectionOperation)3 BsonDocument (org.bson.BsonDocument)3 MongoCommandException (com.mongodb.MongoCommandException)2 Test (org.junit.Test)2 BasicDBObject (com.mongodb.BasicDBObject)1 CommandResult (com.mongodb.CommandResult)1 Date (java.util.Date)1 Document (org.bson.Document)1 Book (org.mongodb.morphia.aggregation.AggregationTest.Book)1 CountResult (org.mongodb.morphia.aggregation.AggregationTest.CountResult)1