Search in sources :

Example 1 with Validation

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()));
}
Also used : Validation(dev.morphia.annotations.Validation) DocumentValidation(dev.morphia.test.models.DocumentValidation) DocumentValidation(dev.morphia.test.models.DocumentValidation) MongoWriteException(com.mongodb.MongoWriteException) Date(java.util.Date) LocalDate(java.time.LocalDate) Test(org.testng.annotations.Test)

Example 2 with Validation

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"));
    }
}
Also used : Validation(dev.morphia.annotations.Validation) DocumentValidation(dev.morphia.test.models.DocumentValidation) DocumentValidation(dev.morphia.test.models.DocumentValidation) MongoWriteException(com.mongodb.MongoWriteException) Document(org.bson.Document) Date(java.util.Date) LocalDate(java.time.LocalDate) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.testng.annotations.Test)

Aggregations

MongoWriteException (com.mongodb.MongoWriteException)2 Validation (dev.morphia.annotations.Validation)2 DocumentValidation (dev.morphia.test.models.DocumentValidation)2 LocalDate (java.time.LocalDate)2 Date (java.util.Date)2 Test (org.testng.annotations.Test)2 MongoDatabase (com.mongodb.client.MongoDatabase)1 Document (org.bson.Document)1