Search in sources :

Example 6 with MongoWriteConcernException

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

the class AbstractRetryableWritesTest method assertExceptionState.

private void assertExceptionState(final RuntimeException e, final BsonValue expectedResult, final String operationName) {
    if (hasErrorLabelsContainField(expectedResult)) {
        if (e instanceof MongoException) {
            MongoException mongoException = (MongoException) e;
            for (String curErrorLabel : getErrorLabelsContainField(expectedResult)) {
                assertTrue(String.format("Expected error label '%s but found labels '%s' for operation %s", curErrorLabel, mongoException.getErrorLabels(), operationName), mongoException.hasErrorLabel(curErrorLabel));
            }
        }
    }
    if (hasErrorLabelsOmitField(expectedResult)) {
        if (e instanceof MongoException) {
            MongoException mongoException = (MongoException) e;
            for (String curErrorLabel : getErrorLabelsOmitField(expectedResult)) {
                assertFalse(String.format("Expected error label '%s omitted but found labels '%s' for operation %s", curErrorLabel, mongoException.getErrorLabels(), operationName), mongoException.hasErrorLabel(curErrorLabel));
            }
        }
    }
    if (hasErrorContainsField(expectedResult)) {
        String expectedError = getErrorContainsField(expectedResult);
        assertTrue(String.format("Expected '%s' but got '%s' for operation %s", expectedError, e.getMessage(), operationName), e.getMessage().toLowerCase().contains(expectedError.toLowerCase()));
    }
    if (hasErrorCodeNameField(expectedResult)) {
        String expectedErrorCodeName = getErrorCodeNameField(expectedResult);
        if (e instanceof MongoCommandException) {
            assertEquals(expectedErrorCodeName, ((MongoCommandException) e).getErrorCodeName());
        } else if (e instanceof MongoWriteConcernException) {
            assertEquals(expectedErrorCodeName, ((MongoWriteConcernException) e).getWriteConcernError().getCodeName());
        }
    }
}
Also used : MongoException(com.mongodb.MongoException) MongoCommandException(com.mongodb.MongoCommandException) MongoWriteConcernException(com.mongodb.MongoWriteConcernException) BsonString(org.bson.BsonString)

Example 7 with MongoWriteConcernException

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

the class CrudProseTest method testWriteConcernErrInfoIsPropagated.

/**
 * 1. WriteConcernError.details exposes writeConcernError.errInfo
 */
@Test
public void testWriteConcernErrInfoIsPropagated() {
    assumeTrue(isDiscoverableReplicaSet() && serverVersionAtLeast(4, 0));
    try {
        setFailPoint();
        collection.insertOne(Document.parse("{ x: 1 }"));
    } catch (MongoWriteConcernException e) {
        assertEquals(e.getWriteConcernError().getCode(), 100);
        assertEquals("UnsatisfiableWriteConcern", e.getWriteConcernError().getCodeName());
        assertEquals(e.getWriteConcernError().getDetails(), new BsonDocument("writeConcern", new BsonDocument("w", new BsonInt32(2)).append("wtimeout", new BsonInt32(0)).append("provenance", new BsonString("clientSupplied"))));
    } catch (Exception ex) {
        fail(format("Incorrect exception thrown in test: %s", ex.getClass()));
    } finally {
        disableFailPoint();
    }
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) MongoWriteConcernException(com.mongodb.MongoWriteConcernException) MongoWriteException(com.mongodb.MongoWriteException) MongoBulkWriteException(com.mongodb.MongoBulkWriteException) MongoWriteConcernException(com.mongodb.MongoWriteConcernException) Test(org.junit.Test)

Example 8 with MongoWriteConcernException

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

the class WriteConcernProseTest method testWriteConcernErrInfoIsPropagated.

// Ensure that the WriteConcernError errInfo object is propagated.
@Test
public void testWriteConcernErrInfoIsPropagated() {
    try {
        setFailPoint();
        insertOneDocument();
    } catch (MongoWriteConcernException e) {
        assertEquals(e.getWriteConcernError().getCode(), 100);
        assertTrue(e.getWriteConcernError().getCodeName().equals("UnsatisfiableWriteConcern"));
        assertEquals(e.getWriteConcernError().getDetails(), new BsonDocument("writeConcern", new BsonDocument("w", new BsonInt32(2)).append("wtimeout", new BsonInt32(0)).append("provenance", new BsonString("clientSupplied"))));
    } catch (Exception ex) {
        fail(format("Incorrect exception thrown in test: %s", ex.getClass()));
    } finally {
        disableFailPoint();
    }
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) MongoWriteConcernException(com.mongodb.MongoWriteConcernException) MongoWriteConcernException(com.mongodb.MongoWriteConcernException) Test(org.junit.Test)

Aggregations

MongoWriteConcernException (com.mongodb.MongoWriteConcernException)8 BsonDocument (org.bson.BsonDocument)6 BsonString (org.bson.BsonString)6 MongoException (com.mongodb.MongoException)4 MongoCommandException (com.mongodb.MongoCommandException)3 WriteConcernResult (com.mongodb.WriteConcernResult)3 BsonInt32 (org.bson.BsonInt32)3 Test (org.junit.Test)3 MongoWriteException (com.mongodb.MongoWriteException)2 ServerAddress (com.mongodb.ServerAddress)2 WriteConcernError (com.mongodb.bulk.WriteConcernError)2 BsonArray (org.bson.BsonArray)2 ClusterFixture.getConnectionString (com.mongodb.ClusterFixture.getConnectionString)1 ClusterFixture.getMultiMongosConnectionString (com.mongodb.ClusterFixture.getMultiMongosConnectionString)1 ConnectionString (com.mongodb.ConnectionString)1 JsonTestServerVersionChecker.skipTest (com.mongodb.JsonTestServerVersionChecker.skipTest)1 MongoBulkWriteException (com.mongodb.MongoBulkWriteException)1 WriteConcern (com.mongodb.WriteConcern)1 WriteError (com.mongodb.WriteError)1 ConnectionDescription (com.mongodb.connection.ConnectionDescription)1