Skip to content

Commit

Permalink
Fix test cleanup to prevent conflicts between tests. (#1608)
Browse files Browse the repository at this point in the history
  • Loading branch information
vbabanin authored Jan 21, 2025
1 parent 75cb0e6 commit 51c736e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import com.mongodb.ClientEncryptionSettings;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoCredential;
import com.mongodb.MongoNamespace;
import com.mongodb.MongoSecurityException;
import com.mongodb.WriteConcern;
import com.mongodb.client.model.vault.EncryptOptions;
import com.mongodb.client.test.CollectionHelper;
import com.mongodb.client.vault.ClientEncryption;
import com.mongodb.client.vault.ClientEncryptions;
import org.bson.BsonBinary;
Expand Down Expand Up @@ -57,6 +59,7 @@ public class ClientSideEncryptionExternalKeyVaultTest {
private MongoClient client, clientEncrypted;
private ClientEncryption clientEncryption;
private final boolean withExternalKeyVault;
private static final MongoNamespace NAMESPACE = new MongoNamespace("db", ClientSideEncryptionExternalKeyVaultTest.class.getName());

public ClientSideEncryptionExternalKeyVaultTest(final boolean withExternalKeyVault) {
this.withExternalKeyVault = withExternalKeyVault;
Expand Down Expand Up @@ -84,7 +87,7 @@ public void setUp() throws IOException, URISyntaxException {
+ "UN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk");
localMasterkey.put("key", localMasterkeyBytes);
kmsProviders.put("local", localMasterkey);
schemaMap.put("db.coll", bsonDocumentFromPath("external-schema.json"));
schemaMap.put(NAMESPACE.getFullName(), bsonDocumentFromPath("external-schema.json"));

AutoEncryptionSettings.Builder autoEncryptionSettingsBuilder = AutoEncryptionSettings.builder()
.keyVaultNamespace("keyvault.datakeys")
Expand Down Expand Up @@ -123,8 +126,8 @@ public void setUp() throws IOException, URISyntaxException {
public void testExternal() {
boolean authExceptionThrown = false;
MongoCollection<BsonDocument> coll = clientEncrypted
.getDatabase("db")
.getCollection("coll", BsonDocument.class);
.getDatabase(NAMESPACE.getDatabaseName())
.getCollection(NAMESPACE.getCollectionName(), BsonDocument.class);
try {
coll.insertOne(new BsonDocument().append("encrypted", new BsonString("test")));
} catch (MongoSecurityException mse) {
Expand Down Expand Up @@ -169,5 +172,7 @@ public void after() {
// ignore
}
}

CollectionHelper.drop(NAMESPACE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.mongodb.client.model.bulk.ClientBulkWriteOptions;
import com.mongodb.client.model.bulk.ClientBulkWriteResult;
import com.mongodb.client.model.bulk.ClientNamespacedWriteModel;
import com.mongodb.client.test.CollectionHelper;
import com.mongodb.event.CommandStartedEvent;
import com.mongodb.internal.connection.TestCommandListener;
import org.bson.BsonArray;
Expand All @@ -47,6 +48,7 @@
import org.bson.RawBsonDocument;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.codecs.pojo.PojoCodecProvider;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -96,7 +98,7 @@
* <a href="https://github.com/mongodb/specifications/blob/master/source/crud/tests/README.md#prose-tests">CRUD Prose Tests</a>.
*/
public class CrudProseTest {
private static final MongoNamespace NAMESPACE = new MongoNamespace("db", "coll");
private static final MongoNamespace NAMESPACE = new MongoNamespace("db", CrudProseTest.class.getName());

@DisplayName("1. WriteConcernError.details exposes writeConcernError.errInfo")
@Test
Expand Down Expand Up @@ -367,7 +369,8 @@ private void testBulkWriteSplitsWhenExceedingMaxMessageSizeBytesDueToNsInfo(
Document helloResponse = droppedDatabase(client).runCommand(new Document("hello", 1));
int maxBsonObjectSize = helloResponse.getInteger("maxBsonObjectSize");
int maxMessageSizeBytes = helloResponse.getInteger("maxMessageSizeBytes");
int opsBytes = maxMessageSizeBytes - 1122;
// By the spec test, we have to subtract only 1122, however, we have different collection name.
int opsBytes = maxMessageSizeBytes - 1118 - NAMESPACE.getCollectionName().length();
int numModels = opsBytes / maxBsonObjectSize;
int remainderBytes = opsBytes % maxBsonObjectSize;
List<ClientNamespacedWriteModel> models = new ArrayList<>(nCopies(
Expand Down Expand Up @@ -613,4 +616,9 @@ private static ClientBulkWriteResult runInTransaction(final ClientSession sessio
throw throwable;
}
}

@AfterAll
public static void cleanUp() {
CollectionHelper.drop(NAMESPACE);
}
}

0 comments on commit 51c736e

Please sign in to comment.