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)));
}
}
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"));
}
}
Aggregations