Skip to content

Commit c84ecc5

Browse files
committed
Update change stream specification test runner
Tests more change stream operation types JAVA-3133
1 parent 9127ec5 commit c84ecc5

File tree

5 files changed

+92
-20
lines changed

5 files changed

+92
-20
lines changed

driver-async/src/test/functional/com/mongodb/async/client/ChangeStreamsTest.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.mongodb.ClusterFixture;
2020
import com.mongodb.MongoException;
2121
import com.mongodb.MongoNamespace;
22+
import com.mongodb.WriteConcern;
2223
import com.mongodb.async.AsyncBatchCursor;
2324
import com.mongodb.async.FutureResultCallback;
2425
import com.mongodb.client.CommandMonitoringTestHelper;
@@ -32,7 +33,6 @@
3233
import org.bson.BsonArray;
3334
import org.bson.BsonDocument;
3435
import org.bson.BsonInt32;
35-
import org.bson.BsonString;
3636
import org.bson.BsonValue;
3737
import org.bson.codecs.BsonDocumentCodec;
3838
import org.junit.After;
@@ -118,10 +118,12 @@ public void setUp() {
118118
}
119119
}
120120

121+
CollectionHelper.dropDatabase(namespace.getDatabaseName(), WriteConcern.MAJORITY);
121122
CollectionHelper<BsonDocument> collectionHelper = new CollectionHelper<BsonDocument>(new BsonDocumentCodec(), namespace);
122123
collectionHelper.drop();
123124
collectionHelper.create();
124125

126+
CollectionHelper.dropDatabase(namespace2.getDatabaseName(), WriteConcern.MAJORITY);
125127
CollectionHelper<BsonDocument> collectionHelper2 = new CollectionHelper<BsonDocument>(new BsonDocumentCodec(), namespace2);
126128
collectionHelper2.drop();
127129
collectionHelper2.create();
@@ -170,13 +172,18 @@ private void checkStreamValues(final BsonDocument result, final AsyncBatchCursor
170172
ChangeStreamDocument<BsonDocument> actual = results.poll();
171173
assumeNotNull(actual);
172174

173-
BsonDocument ns = expected.getDocument("ns", new BsonDocument());
174-
MongoNamespace expectedNamespace = new MongoNamespace(
175-
ns.getString("db", new BsonString("db")).getValue(),
176-
ns.getString("coll", new BsonString("coll")).getValue());
175+
MongoNamespace expectedNamespace = null;
176+
if (expected.containsKey("ns")) {
177+
BsonDocument nsDocument = expected.getDocument("ns");
178+
expectedNamespace = nsDocument != null
179+
? new MongoNamespace(nsDocument.getString("db").getValue(), nsDocument.getString("coll").getValue())
180+
: null;
181+
}
177182
assertEquals(expectedNamespace, actual.getNamespace());
178183
assertEquals(OperationType.fromString(expected.getString("operationType").getValue()), actual.getOperationType());
179-
actual.getFullDocument().remove("_id");
184+
if (actual.getFullDocument() != null) {
185+
actual.getFullDocument().remove("_id");
186+
}
180187

181188
assertEquals(expected.get("fullDocument"), actual.getFullDocument());
182189
}
@@ -264,8 +271,8 @@ public static Collection<Object[]> data() throws URISyntaxException, IOException
264271
BsonDocument testDocument = JsonPoweredTestHelper.getTestDocument(file);
265272
MongoNamespace namespace = new MongoNamespace(testDocument.getString("database_name").getValue(),
266273
testDocument.getString("collection_name").getValue());
267-
MongoNamespace namespace2 = new MongoNamespace(testDocument.getString("database_name").getValue(),
268-
testDocument.getString("collection_name").getValue());
274+
MongoNamespace namespace2 = new MongoNamespace(testDocument.getString("database2_name").getValue(),
275+
testDocument.getString("collection2_name").getValue());
269276
for (BsonValue test : testDocument.getArray("tests")) {
270277
data.add(new Object[]{file.getName(), test.asDocument().getString("description").getValue(),
271278
namespace, namespace2, test.asDocument()});

driver-async/src/test/functional/com/mongodb/async/client/JsonPoweredCrudTestHelper.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.mongodb.MongoBulkWriteException;
2121
import com.mongodb.MongoException;
22+
import com.mongodb.MongoNamespace;
2223
import com.mongodb.ReadConcern;
2324
import com.mongodb.ReadConcernLevel;
2425
import com.mongodb.ReadPreference;
@@ -89,7 +90,7 @@ BsonDocument getOperationResults(final BsonDocument operation) {
8990

9091
BsonDocument getOperationResults(final BsonDocument operation, @Nullable final ClientSession clientSession) {
9192
BsonDocument collectionOptions = operation.getDocument("collectionOptions", new BsonDocument());
92-
BsonDocument arguments = operation.getDocument("arguments");
93+
BsonDocument arguments = operation.getDocument("arguments", new BsonDocument());
9394

9495
String methodName = createMethodName(operation.getString("name").getValue(),
9596
operation.getString("object", new BsonString("")).getValue());
@@ -724,6 +725,37 @@ BsonDocument getBulkWriteResult(final BsonDocument collectionOptions, final Bson
724725
}
725726
}
726727

728+
729+
BsonDocument getRenameResult(final BsonDocument collectionOptions, final BsonDocument arguments,
730+
@Nullable final ClientSession clientSession) {
731+
732+
FutureResultCallback<Void> futureResultCallback = new FutureResultCallback<Void>();
733+
734+
MongoNamespace toNamespace = new MongoNamespace(database.getName(), arguments.getString("to").getValue());
735+
if (clientSession == null) {
736+
getCollection(collectionOptions).renameCollection(toNamespace, futureResultCallback);
737+
} else {
738+
getCollection(collectionOptions).renameCollection(clientSession, toNamespace, futureResultCallback);
739+
}
740+
futureResult(futureResultCallback);
741+
return new BsonDocument("ok", new BsonInt32(1));
742+
}
743+
744+
BsonDocument getDropResult(final BsonDocument collectionOptions, final BsonDocument arguments,
745+
@Nullable final ClientSession clientSession) {
746+
747+
FutureResultCallback<Void> futureResultCallback = new FutureResultCallback<Void>();
748+
749+
if (clientSession == null) {
750+
getCollection(collectionOptions).drop(futureResultCallback);
751+
} else {
752+
getCollection(collectionOptions).drop(clientSession, futureResultCallback);
753+
}
754+
futureResult(futureResultCallback);
755+
return new BsonDocument("ok", new BsonInt32(1));
756+
}
757+
758+
727759
Collation getCollation(final BsonDocument bsonCollation) {
728760
Collation.Builder builder = Collation.builder();
729761
if (bsonCollation.containsKey("locale")) {

driver-core/src/test/functional/com/mongodb/client/test/CollectionHelper.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,15 @@ public static void drop(final MongoNamespace namespace, final WriteConcern write
9999
}
100100

101101
public static void dropDatabase(final String name) {
102+
dropDatabase(name, WriteConcern.ACKNOWLEDGED);
103+
}
104+
105+
public static void dropDatabase(final String name, final WriteConcern writeConcern) {
102106
if (name == null) {
103107
return;
104108
}
105109
try {
106-
new DropDatabaseOperation(name, WriteConcern.ACKNOWLEDGED).execute(getBinding());
110+
new DropDatabaseOperation(name, writeConcern).execute(getBinding());
107111
} catch (MongoCommandException e) {
108112
if (!e.getErrorMessage().contains("ns not found")) {
109113
throw e;

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.mongodb.ClusterFixture;
2020
import com.mongodb.MongoException;
2121
import com.mongodb.MongoNamespace;
22+
import com.mongodb.WriteConcern;
2223
import com.mongodb.client.model.changestream.ChangeStreamDocument;
2324
import com.mongodb.client.model.changestream.OperationType;
2425
import com.mongodb.client.test.CollectionHelper;
@@ -29,7 +30,6 @@
2930
import org.bson.BsonArray;
3031
import org.bson.BsonDocument;
3132
import org.bson.BsonInt32;
32-
import org.bson.BsonString;
3333
import org.bson.BsonValue;
3434
import org.bson.codecs.BsonDocumentCodec;
3535
import org.junit.After;
@@ -110,10 +110,12 @@ public void setUp() {
110110
}
111111
}
112112

113+
CollectionHelper.dropDatabase(namespace.getDatabaseName(), WriteConcern.MAJORITY);
113114
CollectionHelper<BsonDocument> collectionHelper = new CollectionHelper<BsonDocument>(new BsonDocumentCodec(), namespace);
114115
collectionHelper.drop();
115116
collectionHelper.create();
116117

118+
CollectionHelper.dropDatabase(namespace2.getDatabaseName(), WriteConcern.MAJORITY);
117119
CollectionHelper<BsonDocument> collectionHelper2 = new CollectionHelper<BsonDocument>(new BsonDocumentCodec(), namespace2);
118120
collectionHelper2.drop();
119121
collectionHelper2.create();
@@ -155,14 +157,18 @@ private void checkStreamValues(final BsonDocument result, final MongoCursor<Chan
155157
BsonDocument expected = success.asDocument();
156158
ChangeStreamDocument<BsonDocument> actual = cursor.next();
157159

158-
BsonDocument ns = expected.getDocument("ns", new BsonDocument());
159-
MongoNamespace expectedNamespace = new MongoNamespace(
160-
ns.getString("db", new BsonString("db")).getValue(),
161-
ns.getString("coll", new BsonString("coll")).getValue());
160+
MongoNamespace expectedNamespace = null;
161+
if (expected.containsKey("ns")) {
162+
BsonDocument nsDocument = expected.getDocument("ns");
163+
expectedNamespace = nsDocument != null
164+
? new MongoNamespace(nsDocument.getString("db").getValue(), nsDocument.getString("coll").getValue())
165+
: null;
166+
}
162167
assertEquals(expectedNamespace, actual.getNamespace());
163168
assertEquals(OperationType.fromString(expected.getString("operationType").getValue()), actual.getOperationType());
164-
actual.getFullDocument().remove("_id");
165-
169+
if (actual.getFullDocument() != null) {
170+
actual.getFullDocument().remove("_id");
171+
}
166172

167173
assertEquals(expected.get("fullDocument"), actual.getFullDocument());
168174
}
@@ -235,8 +241,9 @@ public static Collection<Object[]> data() throws URISyntaxException, IOException
235241
BsonDocument testDocument = JsonPoweredTestHelper.getTestDocument(file);
236242
MongoNamespace namespace = new MongoNamespace(testDocument.getString("database_name").getValue(),
237243
testDocument.getString("collection_name").getValue());
238-
MongoNamespace namespace2 = new MongoNamespace(testDocument.getString("database_name").getValue(),
239-
testDocument.getString("collection_name").getValue());
244+
MongoNamespace namespace2 = new MongoNamespace(testDocument.getString("database2_name").getValue(),
245+
testDocument.getString("collection2_name").getValue());
246+
240247
for (BsonValue test : testDocument.getArray("tests")) {
241248
data.add(new Object[]{file.getName(), test.asDocument().getString("description").getValue(),
242249
namespace, namespace2, test.asDocument()});

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.mongodb.MongoBulkWriteException;
2020
import com.mongodb.MongoException;
21+
import com.mongodb.MongoNamespace;
2122
import com.mongodb.ReadConcern;
2223
import com.mongodb.ReadConcernLevel;
2324
import com.mongodb.ReadPreference;
@@ -86,7 +87,7 @@ public BsonDocument getOperationResults(final BsonDocument operation) {
8687

8788
BsonDocument getOperationResults(final BsonDocument operation, @Nullable final ClientSession clientSession) {
8889
BsonDocument collectionOptions = operation.getDocument("collectionOptions", new BsonDocument());
89-
BsonDocument arguments = operation.getDocument("arguments");
90+
BsonDocument arguments = operation.getDocument("arguments", new BsonDocument());
9091

9192
String methodName = createMethodName(operation.getString("name").getValue(),
9293
operation.getString("object", new BsonString("")).getValue());
@@ -691,6 +692,27 @@ BsonDocument getBulkWriteResult(final BsonDocument collectionOptions, final Bson
691692
}
692693
}
693694

695+
BsonDocument getRenameResult(final BsonDocument collectionOptions, final BsonDocument arguments,
696+
@Nullable final ClientSession clientSession) {
697+
MongoNamespace toNamespace = new MongoNamespace(database.getName(), arguments.getString("to").getValue());
698+
if (clientSession == null) {
699+
getCollection(collectionOptions).renameCollection(toNamespace);
700+
} else {
701+
getCollection(collectionOptions).renameCollection(clientSession, toNamespace);
702+
}
703+
return new BsonDocument("ok", new BsonInt32(1));
704+
}
705+
706+
BsonDocument getDropResult(final BsonDocument collectionOptions, final BsonDocument arguments,
707+
@Nullable final ClientSession clientSession) {
708+
if (clientSession == null) {
709+
getCollection(collectionOptions).drop();
710+
} else {
711+
getCollection(collectionOptions).drop(clientSession);
712+
}
713+
return new BsonDocument("ok", new BsonInt32(1));
714+
}
715+
694716
Collation getCollation(final BsonDocument bsonCollation) {
695717
Collation.Builder builder = Collation.builder();
696718
if (bsonCollation.containsKey("locale")) {

0 commit comments

Comments
 (0)