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