Search in sources :

Example 31 with CreateCollectionOptions

use of com.mongodb.client.model.CreateCollectionOptions in project mongo-java-driver by mongodb.

the class AbstractCrudTest method setUp.

@Before
public void setUp() {
    assumeFalse(skipTest);
    collectionHelper = new CollectionHelper<>(new DocumentCodec(), new MongoNamespace(databaseName, collectionName));
    collectionHelper.create(collectionName, new CreateCollectionOptions(), WriteConcern.ACKNOWLEDGED);
    createMongoClient(commandListener);
    database = getDatabase(databaseName);
    collection = database.getCollection(collectionName, BsonDocument.class);
    helper = new JsonPoweredCrudTestHelper(description, database, collection);
    if (!data.isEmpty()) {
        List<BsonDocument> documents = new ArrayList<>();
        for (BsonValue document : data) {
            documents.add(document.asDocument());
        }
        if (documents.size() > 0) {
            collectionHelper.insertDocuments(documents, WriteConcern.MAJORITY);
        }
    }
    commandListener.reset();
}
Also used : BsonDocument(org.bson.BsonDocument) DocumentCodec(org.bson.codecs.DocumentCodec) ArrayList(java.util.ArrayList) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) MongoNamespace(com.mongodb.MongoNamespace) BsonValue(org.bson.BsonValue) Before(org.junit.Before)

Example 32 with CreateCollectionOptions

use of com.mongodb.client.model.CreateCollectionOptions in project mongo-java-driver by mongodb.

the class AbstractClientSideEncryptionDeadlockTest method setUp.

@BeforeEach
public void setUp() throws IOException, URISyntaxException {
    assumeTrue(serverVersionAtLeast(4, 2));
    assumeTrue(isClientSideEncryptionTest());
    MongoDatabase keyVaultDatabase = getMongoClient().getDatabase("keyvault");
    MongoCollection<BsonDocument> dataKeysCollection = keyVaultDatabase.getCollection("datakeys", BsonDocument.class).withWriteConcern(WriteConcern.MAJORITY);
    dataKeysCollection.drop();
    dataKeysCollection.insertOne(bsonDocumentFromPath("external-key.json"));
    MongoDatabase encryptedDatabase = getMongoClient().getDatabase("db");
    MongoCollection<BsonDocument> encryptedCollection = encryptedDatabase.getCollection("coll", BsonDocument.class).withWriteConcern(WriteConcern.MAJORITY);
    encryptedCollection.drop();
    encryptedDatabase.createCollection("coll", new CreateCollectionOptions().validationOptions(new ValidationOptions().validator(new BsonDocument("$jsonSchema", bsonDocumentFromPath("external-schema.json")))));
    kmsProviders = new HashMap<>();
    Map<String, Object> localProviderMap = new HashMap<>();
    localProviderMap.put("key", Base64.getDecoder().decode("Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZ" + "GJkTXVyZG9uSjFk"));
    kmsProviders.put("local", localProviderMap);
    ClientEncryption clientEncryption = ClientEncryptions.create(ClientEncryptionSettings.builder().keyVaultMongoClientSettings(getKeyVaultClientSettings(new TestCommandListener())).keyVaultNamespace("keyvault.datakeys").kmsProviders(kmsProviders).build());
    cipherText = clientEncryption.encrypt(new BsonString("string0"), new EncryptOptions("AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic").keyAltName("local"));
    clientEncryption.close();
}
Also used : HashMap(java.util.HashMap) TestCommandListener(com.mongodb.internal.connection.TestCommandListener) ClientEncryption(com.mongodb.client.vault.ClientEncryption) BsonString(org.bson.BsonString) ValidationOptions(com.mongodb.client.model.ValidationOptions) BsonDocument(org.bson.BsonDocument) EncryptOptions(com.mongodb.client.model.vault.EncryptOptions) BsonString(org.bson.BsonString) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 33 with CreateCollectionOptions

use of com.mongodb.client.model.CreateCollectionOptions in project mongo-java-driver by mongodb.

the class UnifiedCrudHelper method executeCreateCollection.

public OperationResult executeCreateCollection(final BsonDocument operation) {
    MongoDatabase database = entities.getDatabase(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    String collectionName = arguments.getString("collection").getValue();
    ClientSession session = getSession(arguments);
    CreateCollectionOptions options = new CreateCollectionOptions();
    for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
        switch(cur.getKey()) {
            case "collection":
            case "session":
                break;
            case "expireAfterSeconds":
                options.expireAfter(cur.getValue().asNumber().longValue(), TimeUnit.SECONDS);
                break;
            case "timeseries":
                options.timeSeriesOptions(createTimeSeriesOptions(cur.getValue().asDocument()));
                break;
            default:
                throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
        }
    }
    return resultOf(() -> {
        if (session == null) {
            database.createCollection(collectionName, options);
        } else {
            database.createCollection(session, collectionName, options);
        }
        return null;
    });
}
Also used : BsonDocument(org.bson.BsonDocument) ClientSession(com.mongodb.client.ClientSession) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) BsonString(org.bson.BsonString) Map(java.util.Map) MongoDatabase(com.mongodb.client.MongoDatabase) BsonValue(org.bson.BsonValue)

Example 34 with CreateCollectionOptions

use of com.mongodb.client.model.CreateCollectionOptions in project mongo-java-driver by mongodb.

the class CrudProseTest method testWriteErrorDetailsIsPropagated.

/**
 * 2. WriteError.details exposes writeErrors[].errInfo
 */
@Test
public void testWriteErrorDetailsIsPropagated() {
    assumeTrue(serverVersionAtLeast(3, 2));
    getCollectionHelper().create(getCollectionName(), new CreateCollectionOptions().validationOptions(new ValidationOptions().validator(Filters.type("x", "string"))));
    try {
        collection.insertOne(new Document("x", 1));
        fail("Should throw, as document doesn't match schema");
    } catch (MongoWriteException e) {
        // These assertions doesn't do exactly what's required by the specification, but it's simpler to implement and nearly as
        // effective
        assertTrue(e.getMessage().contains("Write error"));
        assertNotNull(e.getError().getDetails());
        if (serverVersionAtLeast(5, 0)) {
            assertFalse(e.getError().getDetails().isEmpty());
        }
    }
    try {
        collection.insertMany(asList(new Document("x", 1)));
        fail("Should throw, as document doesn't match schema");
    } catch (MongoBulkWriteException e) {
        // These assertions doesn't do exactly what's required by the specification, but it's simpler to implement and nearly as
        // effective
        assertTrue(e.getMessage().contains("Write errors"));
        assertEquals(1, e.getWriteErrors().size());
        if (serverVersionAtLeast(5, 0)) {
            assertFalse(e.getWriteErrors().get(0).getDetails().isEmpty());
        }
    }
}
Also used : MongoWriteException(com.mongodb.MongoWriteException) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) MongoBulkWriteException(com.mongodb.MongoBulkWriteException) ValidationOptions(com.mongodb.client.model.ValidationOptions) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.Test)

Example 35 with CreateCollectionOptions

use of com.mongodb.client.model.CreateCollectionOptions in project spring-data-mongodb by spring-projects.

the class PerformanceTests method setupCollections.

private void setupCollections() {
    MongoDatabase db = this.mongo.getDatabase(DATABASE_NAME);
    for (String collectionName : COLLECTION_NAMES) {
        MongoCollection<Document> collection = db.getCollection(collectionName);
        collection.drop();
        CreateCollectionOptions collectionOptions = new CreateCollectionOptions();
        collectionOptions.capped(false);
        collectionOptions.sizeInBytes(COLLECTION_SIZE);
        db.createCollection(collectionName, collectionOptions);
        collection.createIndex(new Document("firstname", -1));
        collection.createIndex(new Document("lastname", -1));
    }
}
Also used : CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase)

Aggregations

CreateCollectionOptions (com.mongodb.client.model.CreateCollectionOptions)38 Document (org.bson.Document)19 Test (org.junit.Test)19 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)14 ValidationOptions (com.mongodb.client.model.ValidationOptions)11 BasicDBObject (com.mongodb.BasicDBObject)8 MongoDatabase (com.mongodb.client.MongoDatabase)8 BsonDocument (org.bson.BsonDocument)5 MongoNamespace (com.mongodb.MongoNamespace)4 DBObject (com.mongodb.DBObject)3 MongoCommandException (com.mongodb.MongoCommandException)3 BsonString (org.bson.BsonString)3 BsonValue (org.bson.BsonValue)3 Ignore (org.junit.Ignore)3 Test (org.junit.jupiter.api.Test)3 TestCommandListener (com.mongodb.internal.connection.TestCommandListener)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Before (org.junit.Before)2