Search in sources :

Example 1 with RenameCollectionOptions

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));
}
Also used : RenameCollectionOptions(com.mongodb.client.model.RenameCollectionOptions) BsonString(org.bson.BsonString) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) MongoNamespace(com.mongodb.MongoNamespace) Test(org.junit.Test)

Example 2 with RenameCollectionOptions

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");
    });
}
Also used : RenameCollectionOptions(com.mongodb.client.model.RenameCollectionOptions) MongoNamespace(com.mongodb.MongoNamespace) Test(org.junit.jupiter.api.Test)

Aggregations

MongoNamespace (com.mongodb.MongoNamespace)2 RenameCollectionOptions (com.mongodb.client.model.RenameCollectionOptions)2 BsonDocument (org.bson.BsonDocument)1 BsonString (org.bson.BsonString)1 Document (org.bson.Document)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1