Search in sources :

Example 1 with DocumentValidation

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

the class TestDocumentValidation method findAndModify.

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

Example 2 with DocumentValidation

use of dev.morphia.test.models.DocumentValidation 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 3 with DocumentValidation

use of dev.morphia.test.models.DocumentValidation 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)

Example 4 with DocumentValidation

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

the class TestDocumentValidation method insert.

@Test
public void insert() {
    getMapper().map(DocumentValidation.class);
    getDs().enableDocumentValidation();
    try {
        getDs().insert(new DocumentValidation("Harold", 8, new Date()));
        fail("Document validation should have complained");
    } catch (MongoWriteException e) {
    // expected
    }
    getDs().insert(new DocumentValidation("Harold", 8, new Date()), new InsertOneOptions().bypassDocumentValidation(true));
    Query<DocumentValidation> query = getDs().find(DocumentValidation.class).filter(eq("number", 8));
    Assert.assertNotNull(query.iterator(new FindOptions().limit(1)).tryNext());
    List<DocumentValidation> list = asList(new DocumentValidation("Harold", 8, new Date()), new DocumentValidation("John", 8, new Date()), new DocumentValidation("Sarah", 8, new Date()), new DocumentValidation("Amy", 8, new Date()), new DocumentValidation("James", 8, new Date()));
    try {
        getDs().insert(list);
        fail("Document validation should have complained");
    } catch (MongoBulkWriteException e) {
    // expected
    }
    getDs().insert(list, new InsertManyOptions().bypassDocumentValidation(true));
    assertTrue(query.filter(eq("number", 8)).iterator().hasNext());
}
Also used : FindOptions(dev.morphia.query.FindOptions) DocumentValidation(dev.morphia.test.models.DocumentValidation) MongoWriteException(com.mongodb.MongoWriteException) InsertOneOptions(dev.morphia.InsertOneOptions) MongoBulkWriteException(com.mongodb.MongoBulkWriteException) Date(java.util.Date) LocalDate(java.time.LocalDate) InsertManyOptions(dev.morphia.InsertManyOptions) Test(org.testng.annotations.Test)

Example 5 with DocumentValidation

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

the class TestDocumentValidation method save.

@Test
public void save() {
    getMapper().map(DocumentValidation.class);
    getDs().enableDocumentValidation();
    try {
        getDs().save(new DocumentValidation("Harold", 8, new Date()));
        fail("Document validation should have complained");
    } catch (MongoWriteException e) {
    // expected
    }
    getDs().save(new DocumentValidation("Harold", 8, new Date()), new InsertOneOptions().bypassDocumentValidation(true));
    Query<DocumentValidation> query = getDs().find(DocumentValidation.class).filter(eq("number", 8));
    Assert.assertNotNull(query.iterator(new FindOptions().limit(1)).tryNext());
    List<DocumentValidation> list = asList(new DocumentValidation("Harold", 8, new Date()), new DocumentValidation("Harold", 8, new Date()), new DocumentValidation("Harold", 8, new Date()), new DocumentValidation("Harold", 8, new Date()), new DocumentValidation("Harold", 8, new Date()));
    try {
        getDs().save(list);
        fail("Document validation should have complained");
    } catch (MongoBulkWriteException e) {
    // expected
    }
    getDs().save(list, new InsertManyOptions().bypassDocumentValidation(true));
    assertTrue(query.filter(eq("number", 8)).iterator().hasNext());
}
Also used : FindOptions(dev.morphia.query.FindOptions) DocumentValidation(dev.morphia.test.models.DocumentValidation) MongoWriteException(com.mongodb.MongoWriteException) InsertOneOptions(dev.morphia.InsertOneOptions) MongoBulkWriteException(com.mongodb.MongoBulkWriteException) Date(java.util.Date) LocalDate(java.time.LocalDate) InsertManyOptions(dev.morphia.InsertManyOptions) 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