use of com.mongodb.client.model.RenameCollectionOptions in project mongo-java-driver by mongodb.
the class CollectionAcceptanceTest method shouldBeAbleToRenameCollectionToAnExistingCollectionNameAndReplaceItWhenDropIsTrue.
@Test
public void shouldBeAbleToRenameCollectionToAnExistingCollectionNameAndReplaceItWhenDropIsTrue() {
//given
String existingCollectionName = "anExistingCollection";
String keyInOriginalCollection = "someKey";
String valueInOriginalCollection = "someValue";
collection.insertOne(new Document(keyInOriginalCollection, valueInOriginalCollection));
MongoCollection<Document> existingCollection = database.getCollection(existingCollectionName);
String keyInExistingCollection = "aDifferentDocument";
String valueInExistingCollection = "withADifferentValue";
existingCollection.insertOne(new Document(keyInExistingCollection, valueInExistingCollection));
assertThat(database.listCollectionNames().into(new ArrayList<String>()).contains(getCollectionName()), is(true));
assertThat(database.listCollectionNames().into(new ArrayList<String>()).contains(existingCollectionName), is(true));
//when
collection.renameCollection(new MongoNamespace(getDatabaseName(), existingCollectionName), new RenameCollectionOptions().dropTarget(true));
//then
assertThat(database.listCollectionNames().into(new ArrayList<String>()).contains(getCollectionName()), is(false));
assertThat(database.listCollectionNames().into(new ArrayList<String>()).contains(existingCollectionName), is(true));
MongoCollection<Document> replacedCollection = database.getCollection(existingCollectionName);
assertThat(replacedCollection.find().first().get(keyInExistingCollection), is(nullValue()));
assertThat(replacedCollection.find().first().get(keyInOriginalCollection).toString(), is(valueInOriginalCollection));
}
use of com.mongodb.client.model.RenameCollectionOptions in project mongo-java-driver by mongodb.
the class MongoCollectionImplTest method testRenameCollection.
@Test
public void testRenameCollection() {
MongoNamespace mongoNamespace = new MongoNamespace("db2.coll2");
RenameCollectionOptions options = new RenameCollectionOptions().dropTarget(true);
assertAll("renameCollection", () -> assertAll("check validation", () -> assertThrows(IllegalArgumentException.class, () -> collection.renameCollection(null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.renameCollection(mongoNamespace, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.renameCollection(clientSession, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.renameCollection(clientSession, mongoNamespace, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.renameCollection(null, mongoNamespace)), () -> assertThrows(IllegalArgumentException.class, () -> collection.renameCollection(null, mongoNamespace, options))), () -> {
Publisher<Void> expected = mongoOperationPublisher.renameCollection(clientSession, mongoNamespace, new RenameCollectionOptions());
assertPublisherIsTheSameAs(expected, collection.renameCollection(mongoNamespace), "Default");
}, () -> {
Publisher<Void> expected = mongoOperationPublisher.renameCollection(null, mongoNamespace, options);
assertPublisherIsTheSameAs(expected, collection.renameCollection(mongoNamespace, options), "With options");
}, () -> {
Publisher<Void> expected = mongoOperationPublisher.renameCollection(clientSession, mongoNamespace, new RenameCollectionOptions());
assertPublisherIsTheSameAs(expected, collection.renameCollection(clientSession, mongoNamespace), "With client session");
}, () -> {
Publisher<Void> expected = mongoOperationPublisher.renameCollection(clientSession, mongoNamespace, options);
assertPublisherIsTheSameAs(expected, collection.renameCollection(clientSession, mongoNamespace, options), "With client session & options");
});
}
Aggregations