Search in sources :

Example 1 with WriteConcernException

use of com.mongodb.WriteConcernException in project morphia by mongodb.

the class TestDocumentValidation method update.

@Test
public void update() {
    getMorphia().map(DocumentValidation.class);
    getDs().enableDocumentValidation();
    getDs().save(new DocumentValidation("Harold", 100, new Date()));
    Query<DocumentValidation> query = getDs().find(DocumentValidation.class);
    UpdateOperations<DocumentValidation> updates = getDs().createUpdateOperations(DocumentValidation.class).set("number", 5);
    UpdateOptions options = new UpdateOptions().bypassDocumentValidation(false);
    try {
        getDs().update(query, updates, options);
        fail("Document validation should have complained");
    } catch (WriteConcernException e) {
    // expected
    }
    options.bypassDocumentValidation(true);
    getDs().update(query, updates, options);
    Assert.assertNotNull(query.field("number").equal(5).get());
}
Also used : DocumentValidation(org.mongodb.morphia.entities.DocumentValidation) WriteConcernException(com.mongodb.WriteConcernException) Date(java.util.Date) Test(org.junit.Test)

Example 2 with WriteConcernException

use of com.mongodb.WriteConcernException in project morphia by mongodb.

the class TestDocumentValidation method overwriteValidation.

@Test
public void overwriteValidation() {
    Document validator = Document.parse("{ jelly : { $ne : 'rhubarb' } }");
    MongoDatabase database = addValidation(validator, "validation");
    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"));
    }
    getMorphia().map(DocumentValidation.class);
    getDs().enableDocumentValidation();
    assertEquals(Document.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 (WriteConcernException e) {
        assertTrue(e.getMessage().contains("Document failed validation"));
    }
}
Also used : DocumentValidation(org.mongodb.morphia.entities.DocumentValidation) Validation(org.mongodb.morphia.annotations.Validation) DocumentValidation(org.mongodb.morphia.entities.DocumentValidation) MongoWriteException(com.mongodb.MongoWriteException) WriteConcernException(com.mongodb.WriteConcernException) Document(org.bson.Document) Date(java.util.Date) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.Test)

Example 3 with WriteConcernException

use of com.mongodb.WriteConcernException in project morphia by mongodb.

the class TestDocumentValidation method insert.

@Test
public void insert() {
    getMorphia().map(DocumentValidation.class);
    getDs().enableDocumentValidation();
    try {
        getAds().insert(new DocumentValidation("Harold", 8, new Date()));
        fail("Document validation should have complained");
    } catch (WriteConcernException e) {
    // expected
    }
    getAds().insert(new DocumentValidation("Harold", 8, new Date()), new InsertOptions().bypassDocumentValidation(true));
    Query<DocumentValidation> query = getDs().find(DocumentValidation.class).field("number").equal(8);
    Assert.assertNotNull(query.get());
    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 {
        getAds().insert(list);
        fail("Document validation should have complained");
    } catch (WriteConcernException e) {
    // expected
    }
    getAds().insert(list, new InsertOptions().bypassDocumentValidation(true));
    Assert.assertFalse(query.field("number").equal(8).asList().isEmpty());
}
Also used : DocumentValidation(org.mongodb.morphia.entities.DocumentValidation) WriteConcernException(com.mongodb.WriteConcernException) Date(java.util.Date) Test(org.junit.Test)

Example 4 with WriteConcernException

use of com.mongodb.WriteConcernException in project morphia by mongodb.

the class TestDocumentValidation method save.

@Test
public void save() {
    getMorphia().map(DocumentValidation.class);
    getDs().enableDocumentValidation();
    try {
        getDs().save(new DocumentValidation("Harold", 8, new Date()));
        fail("Document validation should have complained");
    } catch (WriteConcernException e) {
    // expected
    }
    getDs().save(new DocumentValidation("Harold", 8, new Date()), new InsertOptions().bypassDocumentValidation(true));
    Query<DocumentValidation> query = getDs().find(DocumentValidation.class).field("number").equal(8);
    Assert.assertNotNull(query.get());
    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 (WriteConcernException e) {
    // expected
    }
    getDs().save(list, new InsertOptions().bypassDocumentValidation(true));
    Assert.assertFalse(query.field("number").equal(8).asList().isEmpty());
}
Also used : DocumentValidation(org.mongodb.morphia.entities.DocumentValidation) WriteConcernException(com.mongodb.WriteConcernException) Date(java.util.Date) Test(org.junit.Test)

Example 5 with WriteConcernException

use of com.mongodb.WriteConcernException 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());
}
Also used : DocumentValidation(org.mongodb.morphia.entities.DocumentValidation) WriteConcernException(com.mongodb.WriteConcernException) Document(org.bson.Document) Date(java.util.Date) Test(org.junit.Test)

Aggregations

WriteConcernException (com.mongodb.WriteConcernException)8 Date (java.util.Date)6 Test (org.junit.Test)6 DocumentValidation (org.mongodb.morphia.entities.DocumentValidation)6 Document (org.bson.Document)2 Validation (org.mongodb.morphia.annotations.Validation)2 DuplicateKeyException (com.mongodb.DuplicateKeyException)1 MongoException (com.mongodb.MongoException)1 MongoNamespace (com.mongodb.MongoNamespace)1 MongoWriteException (com.mongodb.MongoWriteException)1 WriteConcernResult (com.mongodb.WriteConcernResult)1 MongoDatabase (com.mongodb.client.MongoDatabase)1 BsonDocument (org.bson.BsonDocument)1 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)1