use of dev.morphia.annotations.Validation in project morphia by mongodb.
the class TestDocumentValidation method createValidation.
@Test
public void createValidation() {
getMapper().map(DocumentValidation.class);
getDs().enableDocumentValidation();
assertEquals(parse(DocumentValidation.class.getAnnotation(Validation.class).value()), getValidator());
try {
getDs().save(new DocumentValidation("John", 1, new Date()));
fail("Document should have failed validation");
} catch (MongoWriteException e) {
assertTrue(e.getMessage().contains("Document failed validation"));
}
getDs().save(new DocumentValidation("Harold", 100, new Date()));
}
use of dev.morphia.annotations.Validation in project morphia by mongodb.
the class TestDocumentValidation method overwriteValidation.
@Test
public void overwriteValidation() {
Document validator = parse("{ \"jelly\" : { \"$ne\" : \"rhubarb\" } }");
MongoDatabase database = addValidation(validator);
assertEquals(validator, getValidator());
Document rhubarb = new Document("jelly", "rhubarb").append("number", 20);
database.getCollection("validation").insertOne(new Document("jelly", "grape"));
try {
database.getCollection("validation").insertOne(rhubarb);
fail("Document should have failed validation");
} catch (MongoWriteException e) {
assertTrue(e.getMessage().contains("Document failed validation"));
}
getMapper().map(DocumentValidation.class);
getDs().enableDocumentValidation();
assertEquals(parse(DocumentValidation.class.getAnnotation(Validation.class).value()), getValidator());
try {
database.getCollection("validation").insertOne(rhubarb);
} catch (MongoWriteException e) {
assertFalse(e.getMessage().contains("Document failed validation"));
}
try {
getDs().save(new DocumentValidation("John", 1, new Date()));
fail("Document should have failed validation");
} catch (MongoWriteException e) {
assertTrue(e.getMessage().contains("Document failed validation"));
}
}
Aggregations