16
16
17
17
package com .mongodb .client ;
18
18
19
+ import com .mongodb .MongoBulkWriteException ;
19
20
import com .mongodb .MongoWriteConcernException ;
21
+ import com .mongodb .MongoWriteException ;
22
+ import com .mongodb .client .model .CreateCollectionOptions ;
23
+ import com .mongodb .client .model .Filters ;
24
+ import com .mongodb .client .model .ValidationOptions ;
20
25
import org .bson .BsonArray ;
21
26
import org .bson .BsonDocument ;
22
27
import org .bson .BsonInt32 ;
30
35
import static java .lang .String .format ;
31
36
import static java .util .Arrays .asList ;
32
37
import static org .junit .Assert .assertEquals ;
38
+ import static org .junit .Assert .assertFalse ;
39
+ import static org .junit .Assert .assertNotNull ;
33
40
import static org .junit .Assert .assertTrue ;
34
41
import static org .junit .Assert .fail ;
35
42
import static org .junit .Assume .assumeTrue ;
36
43
37
- // See https://github.com/mongodb/specifications/tree/master/source/change-streams/tests/README.rst#prose-tests
38
- public class WriteConcernProseTest extends DatabaseTestCase {
44
+ /**
45
+ * See https://github.com/mongodb/specifications/blob/master/source/crud/tests/README.rst#prose-tests
46
+ */
47
+ public class CrudProseTest extends DatabaseTestCase {
39
48
private BsonDocument failPointDocument ;
40
49
41
50
@ Before
42
51
@ Override
43
52
public void setUp () {
44
- assumeTrue (canRunTests ());
45
53
super .setUp ();
46
54
}
47
55
48
- // Ensure that the WriteConcernError errInfo object is propagated.
56
+ /**
57
+ * 1. WriteConcernError.details exposes writeConcernError.errInfo
58
+ */
49
59
@ Test
50
60
public void testWriteConcernErrInfoIsPropagated () {
61
+ assumeTrue (isDiscoverableReplicaSet () && serverVersionAtLeast (4 , 0 ));
62
+
51
63
try {
52
64
setFailPoint ();
53
65
collection .insertOne (Document .parse ("{ x: 1 }" ));
54
66
} catch (MongoWriteConcernException e ) {
55
67
assertEquals (e .getWriteConcernError ().getCode (), 100 );
56
- assertTrue ( e .getWriteConcernError ().getCodeName (). equals ( "UnsatisfiableWriteConcern" ));
68
+ assertEquals ( "UnsatisfiableWriteConcern" , e .getWriteConcernError ().getCodeName ());
57
69
assertEquals (e .getWriteConcernError ().getDetails (), new BsonDocument ("writeConcern" ,
58
70
new BsonDocument ("w" , new BsonInt32 (2 ))
59
71
.append ("wtimeout" , new BsonInt32 (0 ))
60
72
.append ("provenance" , new BsonString ("clientSupplied" ))));
61
73
} catch (Exception ex ) {
62
- fail (format ("Incorrect exception thrown in test: %s" + ex .getClass ()));
74
+ fail (format ("Incorrect exception thrown in test: %s" , ex .getClass ()));
63
75
} finally {
64
76
disableFailPoint ();
65
77
}
66
78
}
67
79
80
+ /**
81
+ * 2. WriteError.details exposes writeErrors[].errInfo
82
+ */
83
+ @ Test
84
+ public void testWriteErrorDetailsIsPropagated () {
85
+ assumeTrue (serverVersionAtLeast (3 , 2 ));
86
+
87
+ getCollectionHelper ().create (getCollectionName (),
88
+ new CreateCollectionOptions ()
89
+ .validationOptions (new ValidationOptions ()
90
+ .validator (Filters .type ("x" , "string" ))));
91
+
92
+ try {
93
+ collection .insertOne (new Document ("x" , 1 ));
94
+ fail ("Should throw, as document doesn't match schema" );
95
+ } catch (MongoWriteException e ) {
96
+ // These assertions doesn't do exactly what's required by the specification, but it's simpler to implement and nearly as
97
+ // effective
98
+ assertTrue (e .getMessage ().contains ("Write error" ));
99
+ assertNotNull (e .getError ().getDetails ());
100
+ if (serverVersionAtLeast (5 , 0 )) {
101
+ assertFalse (e .getError ().getDetails ().isEmpty ());
102
+ }
103
+ }
104
+
105
+ try {
106
+ collection .insertMany (asList (new Document ("x" , 1 )));
107
+ fail ("Should throw, as document doesn't match schema" );
108
+ } catch (MongoBulkWriteException e ) {
109
+ // These assertions doesn't do exactly what's required by the specification, but it's simpler to implement and nearly as
110
+ // effective
111
+ assertTrue (e .getMessage ().contains ("Write errors" ));
112
+ assertEquals (1 , e .getWriteErrors ().size ());
113
+ if (serverVersionAtLeast (5 , 0 )) {
114
+ assertFalse (e .getWriteErrors ().get (0 ).getDetails ().isEmpty ());
115
+ }
116
+ }
117
+ }
118
+
68
119
private void setFailPoint () {
69
120
failPointDocument = new BsonDocument ("configureFailPoint" , new BsonString ("failCommand" ))
70
121
.append ("mode" , new BsonDocument ("times" , new BsonInt32 (1 )))
@@ -81,8 +132,4 @@ private void setFailPoint() {
81
132
private void disableFailPoint () {
82
133
getCollectionHelper ().runAdminCommand (failPointDocument .append ("mode" , new BsonString ("off" )));
83
134
}
84
-
85
- private boolean canRunTests () {
86
- return isDiscoverableReplicaSet () && serverVersionAtLeast (4 , 0 );
87
- }
88
135
}
0 commit comments