Search in sources :

Example 6 with DocumentValidation

use of dev.morphia.test.models.DocumentValidation in project morphia by mongodb.

the class TestDocumentValidation method update.

@Test
@SuppressWarnings("rawtypes")
public void update() {
    getMapper().map(DocumentValidation.class);
    getDs().enableDocumentValidation();
    getDs().save(new DocumentValidation("Harold", 100, new Date()));
    Query<DocumentValidation> query = getDs().find(DocumentValidation.class);
    UpdateOptions options = new UpdateOptions().bypassDocumentValidation(false);
    Update<DocumentValidation> update = query.update(set("number", 5));
    try {
        update.execute(options);
        fail("Document validation should have complained");
    } catch (MongoWriteException e) {
    // expected
    }
    options.bypassDocumentValidation(true);
    update.execute(options);
    Assert.assertNotNull(query.filter(eq("number", 5)).iterator(new FindOptions().limit(1)).tryNext());
}
Also used : FindOptions(dev.morphia.query.FindOptions) DocumentValidation(dev.morphia.test.models.DocumentValidation) MongoWriteException(com.mongodb.MongoWriteException) Date(java.util.Date) LocalDate(java.time.LocalDate) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Aggregations

DocumentValidation (dev.morphia.test.models.DocumentValidation)6 LocalDate (java.time.LocalDate)6 Date (java.util.Date)6 Test (org.testng.annotations.Test)6 MongoWriteException (com.mongodb.MongoWriteException)5 FindOptions (dev.morphia.query.FindOptions)4 MongoBulkWriteException (com.mongodb.MongoBulkWriteException)2 InsertManyOptions (dev.morphia.InsertManyOptions)2 InsertOneOptions (dev.morphia.InsertOneOptions)2 Validation (dev.morphia.annotations.Validation)2 MongoCommandException (com.mongodb.MongoCommandException)1 MongoDatabase (com.mongodb.client.MongoDatabase)1 ModifyOptions (dev.morphia.ModifyOptions)1 UpdateOptions (dev.morphia.UpdateOptions)1 Document (org.bson.Document)1