use of org.mongodb.morphia.entities.DocumentValidation in project morphia by mongodb.
the class TestDocumentValidation method saveToNewCollection.
@Test
public void saveToNewCollection() {
getMorphia().map(DocumentValidation.class);
final Document validator = Document.parse("{ number : { $gt : 10 } }");
String collection = "newdocs";
addValidation(validator, collection);
try {
getAds().save(collection, new DocumentValidation("Harold", 8, new Date()));
fail("Document validation should have complained");
} catch (WriteConcernException e) {
// expected
}
getAds().save(collection, new DocumentValidation("Harold", 8, new Date()), new InsertOptions().bypassDocumentValidation(true));
Query<DocumentValidation> query = getAds().createQuery(collection, DocumentValidation.class).field("number").equal(8);
Assert.assertNotNull(query.get());
}
use of org.mongodb.morphia.entities.DocumentValidation in project morphia by mongodb.
the class TestDocumentValidation method createValidation.
@Test
public void createValidation() {
getMorphia().map(DocumentValidation.class);
getDs().enableDocumentValidation();
assertEquals(Document.parse(DocumentValidation.class.getAnnotation(Validation.class).value()), getValidator());
try {
getDs().save(new DocumentValidation("John", 1, new Date()));
fail("Document should have failed validation");
} catch (WriteConcernException e) {
assertTrue(e.getMessage().contains("Document failed validation"));
}
getDs().save(new DocumentValidation("Harold", 100, new Date()));
}
Aggregations