/driver-sync/src/test/functional/com/mongodb/client/WriteConcernProseTest.java

http://github.com/mongodb/mongo-java-driver · Java · 88 lines · 62 code · 9 blank · 17 comment · 1 complexity · cbfabccca37309b2163b5271f416afe0 MD5 · raw file

  1. /*
  2. * Copyright 2008-present MongoDB, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.mongodb.client;
  17. import com.mongodb.MongoWriteConcernException;
  18. import org.bson.BsonArray;
  19. import org.bson.BsonDocument;
  20. import org.bson.BsonInt32;
  21. import org.bson.BsonString;
  22. import org.bson.Document;
  23. import org.junit.Before;
  24. import org.junit.Test;
  25. import static com.mongodb.ClusterFixture.isDiscoverableReplicaSet;
  26. import static com.mongodb.ClusterFixture.serverVersionAtLeast;
  27. import static java.lang.String.format;
  28. import static java.util.Arrays.asList;
  29. import static org.junit.Assert.assertEquals;
  30. import static org.junit.Assert.assertTrue;
  31. import static org.junit.Assert.fail;
  32. import static org.junit.Assume.assumeTrue;
  33. // See https://github.com/mongodb/specifications/tree/master/source/change-streams/tests/README.rst#prose-tests
  34. public class WriteConcernProseTest extends DatabaseTestCase {
  35. private BsonDocument failPointDocument;
  36. @Before
  37. @Override
  38. public void setUp() {
  39. assumeTrue(canRunTests());
  40. super.setUp();
  41. }
  42. // Ensure that the WriteConcernError errInfo object is propagated.
  43. @Test
  44. public void testWriteConcernErrInfoIsPropagated() {
  45. try {
  46. setFailPoint();
  47. collection.insertOne(Document.parse("{ x: 1 }"));
  48. } catch (MongoWriteConcernException e) {
  49. assertEquals(e.getWriteConcernError().getCode(), 100);
  50. assertTrue(e.getWriteConcernError().getCodeName().equals("UnsatisfiableWriteConcern"));
  51. assertEquals(e.getWriteConcernError().getDetails(), new BsonDocument("writeConcern",
  52. new BsonDocument("w", new BsonInt32(2))
  53. .append("wtimeout", new BsonInt32(0))
  54. .append("provenance", new BsonString("clientSupplied"))));
  55. } catch (Exception ex) {
  56. fail(format("Incorrect exception thrown in test: %s" + ex.getClass()));
  57. } finally {
  58. disableFailPoint();
  59. }
  60. }
  61. private void setFailPoint() {
  62. failPointDocument = new BsonDocument("configureFailPoint", new BsonString("failCommand"))
  63. .append("mode", new BsonDocument("times", new BsonInt32(1)))
  64. .append("data", new BsonDocument("failCommands", new BsonArray(asList(new BsonString("insert"))))
  65. .append("writeConcernError", new BsonDocument("code", new BsonInt32(100))
  66. .append("codeName", new BsonString("UnsatisfiableWriteConcern"))
  67. .append("errmsg", new BsonString("Not enough data-bearing nodes"))
  68. .append("errInfo", new BsonDocument("writeConcern", new BsonDocument("w", new BsonInt32(2))
  69. .append("wtimeout", new BsonInt32(0))
  70. .append("provenance", new BsonString("clientSupplied"))))));
  71. getCollectionHelper().runAdminCommand(failPointDocument);
  72. }
  73. private void disableFailPoint() {
  74. getCollectionHelper().runAdminCommand(failPointDocument.append("mode", new BsonString("off")));
  75. }
  76. private boolean canRunTests() {
  77. return isDiscoverableReplicaSet() && serverVersionAtLeast(4, 0);
  78. }
  79. }