Search in sources :

Example 1 with MongoNamespace

use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.

the class CrudTest method setUp.

@Before
@Override
public void setUp() {
    super.setUp();
    collection = Fixture.initializeCollection(new MongoNamespace(getDefaultDatabaseName(), getClass().getName())).withDocumentClass(BsonDocument.class);
    new MongoOperation<Void>() {

        @Override
        public void execute() {
            List<BsonDocument> documents = new ArrayList<BsonDocument>();
            for (BsonValue document : data) {
                documents.add(document.asDocument());
            }
            collection.insertMany(documents, getCallback());
        }
    }.get();
}
Also used : BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) List(java.util.List) MongoNamespace(com.mongodb.MongoNamespace) BsonValue(org.bson.BsonValue) Before(org.junit.Before)

Example 2 with MongoNamespace

use of com.mongodb.MongoNamespace 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 3 with MongoNamespace

use of com.mongodb.MongoNamespace in project incubator-skywalking by apache.

the class MongoDBMethodInterceptorTest method setUp.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Before
public void setUp() throws Exception {
    interceptor = new MongoDBMethodInterceptor();
    Config.Plugin.MongoDB.TRACE_PARAM = true;
    when(enhancedInstance.getSkyWalkingDynamicField()).thenReturn("127.0.0.1:27017");
    BsonDocument document = new BsonDocument();
    document.append("name", new BsonString("by"));
    MongoNamespace mongoNamespace = new MongoNamespace("test.user");
    Decoder decoder = PowerMockito.mock(Decoder.class);
    FindOperation findOperation = new FindOperation(mongoNamespace, decoder);
    findOperation.filter(document);
    arguments = new Object[] { findOperation };
    argumentTypes = new Class[] { findOperation.getClass() };
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) MongoNamespace(com.mongodb.MongoNamespace) Decoder(org.bson.codecs.Decoder) FindOperation(com.mongodb.operation.FindOperation) Before(org.junit.Before)

Example 4 with MongoNamespace

use of com.mongodb.MongoNamespace in project engine by Lumeer.

the class MongoDbStorage method renameCollection.

@Override
public void renameCollection(final String oldCollectionName, final String newCollectionName) {
    if (getCollectionCache() != null) {
        collectionCacheRef.get().lock(COLLECTION_CACHE);
        try {
            final List<String> collections = getCollectionCache();
            if (collections != null) {
                collections.remove(oldCollectionName);
                collections.add(newCollectionName);
            }
            if (hasCollection(oldCollectionName)) {
                database.getCollection(oldCollectionName).renameCollection(new MongoNamespace(database.getName(), newCollectionName));
            }
        } finally {
            collectionCacheRef.get().unlock(COLLECTION_CACHE);
        }
    } else {
        if (hasCollection(oldCollectionName)) {
            database.getCollection(oldCollectionName).renameCollection(new MongoNamespace(database.getName(), newCollectionName));
        }
    }
}
Also used : MongoNamespace(com.mongodb.MongoNamespace)

Example 5 with MongoNamespace

use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.

the class CommandMonitoringTest method setUp.

@Before
public void setUp() {
    assumeFalse(skipTest);
    List<BsonDocument> documents = new ArrayList<BsonDocument>();
    for (BsonValue document : data) {
        documents.add(document.asDocument());
    }
    CollectionHelper<Document> collectionHelper = new CollectionHelper<Document>(new DocumentCodec(), new MongoNamespace(databaseName, collectionName));
    collectionHelper.drop();
    if (!documents.isEmpty()) {
        collectionHelper.insertDocuments(documents);
    }
    commandListener.reset();
    MongoDatabase database = mongoClient.getDatabase(databaseName);
    collection = database.getCollection(collectionName, BsonDocument.class);
    if (definition.getDocument("operation").containsKey("read_preference")) {
        collection = collection.withReadPreference(ReadPreference.valueOf(definition.getDocument("operation").getDocument("read_preference").getString("mode").getValue()));
    }
    helper = new JsonPoweredCrudTestHelper(description, database, collection);
}
Also used : BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) DocumentCodec(org.bson.codecs.DocumentCodec) CollectionHelper(com.mongodb.client.test.CollectionHelper) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) MongoNamespace(com.mongodb.MongoNamespace) BsonValue(org.bson.BsonValue) Before(org.junit.Before)

Aggregations

MongoNamespace (com.mongodb.MongoNamespace)55 BsonDocument (org.bson.BsonDocument)34 BsonString (org.bson.BsonString)21 Document (org.bson.Document)20 Before (org.junit.Before)17 MongoClientSettings (com.mongodb.MongoClientSettings)15 BsonValue (org.bson.BsonValue)13 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)10 Map (java.util.Map)10 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)10 DocumentCodec (org.bson.codecs.DocumentCodec)8 CollectionHelper (com.mongodb.client.test.CollectionHelper)7 Test (org.junit.jupiter.api.Test)7 ClientEncryptionSettings (com.mongodb.ClientEncryptionSettings)6 ConnectionString (com.mongodb.ConnectionString)6 Test (org.junit.Test)6 IndexOptions (com.mongodb.client.model.IndexOptions)5 AggregateToCollectionOperation (com.mongodb.internal.operation.AggregateToCollectionOperation)5 DisplayName (org.junit.jupiter.api.DisplayName)5