Search in sources :

Example 1 with MongoWriteException

use of com.mongodb.MongoWriteException in project mongo-java-driver by mongodb.

the class ReplaceAcceptanceTest method shouldThrowExceptionIfTryingToChangeTheIdOfADocument.

@Test
public void shouldThrowExceptionIfTryingToChangeTheIdOfADocument() {
    // Given
    Document firstDocument = new Document("_id", 1).append("a", 1);
    collection.insertOne(firstDocument);
    // When
    Document filter = new Document("a", 1);
    Document newDocumentWithDifferentId = new Document("_id", 2).append("a", 3);
    try {
        collection.replaceOne(filter, newDocumentWithDifferentId);
        fail("Should have thrown an exception");
    } catch (MongoWriteException e) {
        // Then
        assertThat("Error code should match one of these error codes", e.getCode(), anyOf(is(13596), is(16837)));
    }
}
Also used : MongoWriteException(com.mongodb.MongoWriteException) Document(org.bson.Document) Test(org.junit.Test)

Example 2 with MongoWriteException

use of com.mongodb.MongoWriteException 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)

Aggregations

MongoWriteException (com.mongodb.MongoWriteException)2 Document (org.bson.Document)2 Test (org.junit.Test)2 WriteConcernException (com.mongodb.WriteConcernException)1 MongoDatabase (com.mongodb.client.MongoDatabase)1 Date (java.util.Date)1 Validation (org.mongodb.morphia.annotations.Validation)1 DocumentValidation (org.mongodb.morphia.entities.DocumentValidation)1