Search in sources :

Example 16 with BsonValue

use of org.bson.BsonValue in project mongo-java-driver by mongodb.

the class CommandMonitoringTest method setUp.

@Before
public void setUp() {
    ServerVersion serverVersion = ClusterFixture.getServerVersion();
    if (definition.containsKey("ignore_if_server_version_less_than")) {
        assumeFalse(serverVersion.compareTo(getServerVersion("ignore_if_server_version_less_than")) < 0);
    }
    if (definition.containsKey("ignore_if_server_version_greater_than")) {
        assumeFalse(serverVersion.compareTo(getServerVersion("ignore_if_server_version_greater_than")) > 0);
    }
    if (definition.containsKey("ignore_if_topology_type")) {
        BsonArray topologyTypes = definition.getArray("ignore_if_topology_type");
        for (BsonValue type : topologyTypes) {
            String typeString = type.asString().getValue();
            if (typeString.equals("sharded")) {
                assumeFalse(isSharded());
            } else if (typeString.equals("replica_set")) {
                assumeFalse(isDiscoverableReplicaSet());
            } else if (typeString.equals("standalone")) {
                assumeFalse(isSharded());
            }
        }
    }
    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();
    collectionHelper.insertDocuments(documents);
    commandListener.reset();
    collection = mongoClient.getDatabase(databaseName).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, collection);
}
Also used : ServerVersion(com.mongodb.connection.ServerVersion) ArrayList(java.util.ArrayList) DocumentCodec(org.bson.codecs.DocumentCodec) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BsonString(org.bson.BsonString) MongoNamespace(com.mongodb.MongoNamespace) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) CollectionHelper(com.mongodb.client.test.CollectionHelper) BsonValue(org.bson.BsonValue) Before(org.junit.Before)

Example 17 with BsonValue

use of org.bson.BsonValue in project mongo-java-driver by mongodb.

the class GridFSTest method doDelete.

private void doDelete(final BsonDocument arguments, final BsonDocument assertion) {
    Throwable error = null;
    try {
        gridFSBucket.delete(arguments.getObjectId("id").getValue());
    } catch (MongoGridFSException e) {
        error = e;
    }
    if (assertion.containsKey("error")) {
        assertNotNull("Should have thrown an exception", error);
    } else {
        assertNull("Should not have thrown an exception", error);
        for (BsonValue rawDataItem : assertion.getArray("data")) {
            BsonDocument dataItem = rawDataItem.asDocument();
            for (BsonValue deletedItem : dataItem.getArray("deletes", new BsonArray())) {
                String delete = dataItem.getString("delete", new BsonString("none")).getValue();
                BsonObjectId id = new BsonObjectId(new ObjectId());
                if (delete.equals("expected.files")) {
                    id = deletedItem.asDocument().getDocument("q").getObjectId("_id");
                } else if (delete.equals("expected.chunks")) {
                    id = deletedItem.asDocument().getDocument("q").getObjectId("files_id");
                }
                assertEquals(filesCollection.count(new BsonDocument("_id", id)), 0);
                assertEquals(chunksCollection.count(new BsonDocument("files_id", id)), 0);
            }
        }
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonString(org.bson.BsonString) MongoGridFSException(com.mongodb.MongoGridFSException) BsonArray(org.bson.BsonArray) BsonString(org.bson.BsonString) BsonObjectId(org.bson.BsonObjectId) BsonValue(org.bson.BsonValue)

Example 18 with BsonValue

use of org.bson.BsonValue in project camel by apache.

the class MongoDbBasicConverters method fromStringToList.

@Converter
public static List<Bson> fromStringToList(String value) {
    final CodecRegistry codecRegistry = CodecRegistries.fromProviders(Arrays.asList(new ValueCodecProvider(), new BsonValueCodecProvider(), new DocumentCodecProvider()));
    JsonReader reader = new JsonReader(value);
    BsonArrayCodec arrayReader = new BsonArrayCodec(codecRegistry);
    BsonArray docArray = arrayReader.decode(reader, DecoderContext.builder().build());
    List<Bson> answer = new ArrayList<>(docArray.size());
    for (BsonValue doc : docArray) {
        answer.add(doc.asDocument());
    }
    return answer;
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) DocumentCodecProvider(org.bson.codecs.DocumentCodecProvider) BsonArray(org.bson.BsonArray) BsonArrayCodec(org.bson.codecs.BsonArrayCodec) ArrayList(java.util.ArrayList) JsonReader(org.bson.json.JsonReader) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Bson(org.bson.conversions.Bson) BsonValue(org.bson.BsonValue) IOConverter(org.apache.camel.converter.IOConverter) Converter(org.apache.camel.Converter)

Example 19 with BsonValue

use of org.bson.BsonValue in project mongo-java-driver by mongodb.

the class GridFSTest method doDelete.

private void doDelete(final BsonDocument arguments, final BsonDocument assertion) {
    Throwable error = null;
    try {
        new MongoOperation<Void>() {

            @Override
            public void execute() {
                gridFSBucket.delete(arguments.getObjectId("id").getValue(), getCallback());
            }
        }.get();
    } catch (MongoGridFSException e) {
        error = e;
    }
    if (assertion.containsKey("error")) {
        assertNotNull("Should have thrown an exception", error);
    } else {
        assertNull("Should not have thrown an exception", error);
        for (BsonValue rawDataItem : assertion.getArray("data")) {
            BsonDocument dataItem = rawDataItem.asDocument();
            for (BsonValue deletedItem : dataItem.getArray("deletes", new BsonArray())) {
                String delete = dataItem.getString("delete", new BsonString("none")).getValue();
                BsonObjectId id = new BsonObjectId(new ObjectId());
                if (delete.equals("expected.files")) {
                    id = deletedItem.asDocument().getDocument("q").getObjectId("_id");
                } else if (delete.equals("expected.chunks")) {
                    id = deletedItem.asDocument().getDocument("q").getObjectId("files_id");
                }
                long filesCount = getFilesCount(new BsonDocument("_id", id));
                long chunksCount = getChunksCount(new BsonDocument("files_id", id));
                assertEquals(filesCount, 0);
                assertEquals(chunksCount, 0);
            }
        }
    }
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) MongoGridFSException(com.mongodb.MongoGridFSException) BsonString(org.bson.BsonString) BsonObjectId(org.bson.BsonObjectId) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonValue(org.bson.BsonValue)

Example 20 with BsonValue

use of org.bson.BsonValue in project mongo-java-driver by mongodb.

the class GridFSTest method doUpload.

private void doUpload(final BsonDocument rawArguments, final BsonDocument assertion) {
    Throwable error = null;
    ObjectId objectId = null;
    BsonDocument arguments = parseHexDocument(rawArguments, "source");
    try {
        final String filename = arguments.getString("filename").getValue();
        final InputStream inputStream = new ByteArrayInputStream(arguments.getBinary("source").getData());
        final GridFSUploadOptions options = new GridFSUploadOptions();
        BsonDocument rawOptions = arguments.getDocument("options", new BsonDocument());
        if (rawOptions.containsKey("chunkSizeBytes")) {
            options.chunkSizeBytes(rawOptions.getInt32("chunkSizeBytes").getValue());
        }
        if (rawOptions.containsKey("metadata")) {
            options.metadata(Document.parse(rawOptions.getDocument("metadata").toJson()));
        }
        objectId = new MongoOperation<ObjectId>() {

            @Override
            public void execute() {
                gridFSBucket.uploadFromStream(filename, toAsyncInputStream(inputStream), options, getCallback());
            }
        }.get();
    } catch (Throwable e) {
        error = e;
    }
    if (assertion.containsKey("error")) {
        // We don't need to read anything more so don't see the extra chunk
        if (!assertion.getString("error").getValue().equals("ExtraChunk")) {
            assertNotNull("Should have thrown an exception", error);
        }
    } else {
        assertNull("Should not have thrown an exception", error);
        for (BsonValue rawDataItem : assertion.getArray("data", new BsonArray())) {
            BsonDocument dataItem = rawDataItem.asDocument();
            String insert = dataItem.getString("insert", new BsonString("none")).getValue();
            if (insert.equals("expected.files")) {
                List<BsonDocument> documents = processFiles(dataItem.getArray("documents", new BsonArray()), new ArrayList<BsonDocument>());
                assertEquals(getFilesCount(new BsonDocument()), documents.size());
                BsonDocument actual = new MongoOperation<BsonDocument>() {

                    @Override
                    public void execute() {
                        filesCollection.find().first(getCallback());
                    }
                }.get();
                for (BsonDocument expected : documents) {
                    assertEquals(expected.get("length"), actual.get("length"));
                    assertEquals(expected.get("chunkSize"), actual.get("chunkSize"));
                    assertEquals(expected.get("md5"), actual.get("md5"));
                    assertEquals(expected.get("filename"), actual.get("filename"));
                    if (expected.containsKey("metadata")) {
                        assertEquals(expected.get("metadata"), actual.get("metadata"));
                    }
                }
            } else if (insert.equals("expected.chunks")) {
                List<BsonDocument> documents = processChunks(dataItem.getArray("documents", new BsonArray()), new ArrayList<BsonDocument>());
                assertEquals(getChunksCount(new BsonDocument()), documents.size());
                List<BsonDocument> actualDocuments = new MongoOperation<List<BsonDocument>>() {

                    @Override
                    public void execute() {
                        chunksCollection.find().into(new ArrayList<BsonDocument>(), getCallback());
                    }
                }.get();
                for (int i = 0; i < documents.size(); i++) {
                    BsonDocument expected = documents.get(i);
                    BsonDocument actual;
                    actual = actualDocuments.get(i);
                    assertEquals(new BsonObjectId(objectId), actual.getObjectId("files_id"));
                    assertEquals(expected.get("n"), actual.get("n"));
                    assertEquals(expected.get("data"), actual.get("data"));
                }
            }
        }
    }
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) ByteArrayInputStream(java.io.ByteArrayInputStream) AsyncStreamHelper.toAsyncInputStream(com.mongodb.async.client.gridfs.helpers.AsyncStreamHelper.toAsyncInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) BsonString(org.bson.BsonString) BsonObjectId(org.bson.BsonObjectId) BsonDocument(org.bson.BsonDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) ArrayList(java.util.ArrayList) List(java.util.List) GridFSUploadOptions(com.mongodb.client.gridfs.model.GridFSUploadOptions) BsonValue(org.bson.BsonValue)

Aggregations

BsonValue (org.bson.BsonValue)45 BsonDocument (org.bson.BsonDocument)37 ArrayList (java.util.ArrayList)17 BsonArray (org.bson.BsonArray)15 BsonString (org.bson.BsonString)14 BsonInt32 (org.bson.BsonInt32)6 BsonObjectId (org.bson.BsonObjectId)4 Test (org.junit.Test)4 MongoNamespace (com.mongodb.MongoNamespace)3 AggregateToCollectionOperation (com.mongodb.operation.AggregateToCollectionOperation)3 List (java.util.List)3 BsonInt64 (org.bson.BsonInt64)3 BsonNumber (org.bson.BsonNumber)3 ObjectId (org.bson.types.ObjectId)3 Before (org.junit.Before)3 ConnectionString (com.mongodb.ConnectionString)2 MongoGridFSException (com.mongodb.MongoGridFSException)2 GridFSUploadOptions (com.mongodb.client.gridfs.model.GridFSUploadOptions)2 FindOptions (com.mongodb.client.model.FindOptions)2 CommandSucceededEvent (com.mongodb.event.CommandSucceededEvent)2