Search in sources :

Example 51 with BsonDocument

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

the class GridFSBucketImpl method delete.

@Override
public void delete(final BsonValue id) {
    DeleteResult result = filesCollection.deleteOne(new BsonDocument("_id", id));
    chunksCollection.deleteMany(new BsonDocument("files_id", id));
    if (result.wasAcknowledged() && result.getDeletedCount() == 0) {
        throw new MongoGridFSException(format("No file found with the id: %s", id));
    }
}
Also used : BsonDocument(org.bson.BsonDocument) MongoGridFSException(com.mongodb.MongoGridFSException) DeleteResult(com.mongodb.client.result.DeleteResult)

Example 52 with BsonDocument

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

the class CrudTest method shouldPassAllOutcomes.

@Test
public void shouldPassAllOutcomes() {
    BsonDocument outcome = helper.getOperationResults(definition.getDocument("operation"));
    BsonDocument expectedOutcome = definition.getDocument("outcome");
    assertEquals(description, expectedOutcome.get("result"), outcome.get("result"));
    if (expectedOutcome.containsKey("collection")) {
        assertCollectionEquals(expectedOutcome.getDocument("collection"));
    }
}
Also used : BsonDocument(org.bson.BsonDocument) Test(org.junit.Test)

Example 53 with BsonDocument

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

the class CrudTest method setUp.

@Before
public void setUp() {
    super.setUp();
    List<BsonDocument> documents = new ArrayList<BsonDocument>();
    for (BsonValue document : data) {
        documents.add(document.asDocument());
    }
    getCollectionHelper().insertDocuments(documents);
    collection = database.getCollection(getClass().getName(), BsonDocument.class);
    helper = new JsonPoweredCrudTestHelper(description, collection);
}
Also used : BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) BsonValue(org.bson.BsonValue) Before(org.junit.Before)

Example 54 with BsonDocument

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

the class JsonPoweredCrudTestHelper method getOperationResults.

BsonDocument getOperationResults(final BsonDocument operation) {
    String name = operation.getString("name").getValue();
    BsonDocument arguments = operation.getDocument("arguments");
    String methodName = "get" + name.substring(0, 1).toUpperCase() + name.substring(1) + "Result";
    try {
        Method method = getClass().getDeclaredMethod(methodName, BsonDocument.class);
        return (BsonDocument) method.invoke(this, arguments);
    } catch (NoSuchMethodException e) {
        throw new UnsupportedOperationException("No handler for operation " + methodName);
    } catch (InvocationTargetException e) {
        if (e.getTargetException() instanceof AssumptionViolatedException) {
            throw (AssumptionViolatedException) e.getTargetException();
        }
        if (e.getTargetException() instanceof MongoException) {
            throw (MongoException) e.getTargetException();
        }
        throw (RuntimeException) e.getTargetException();
    } catch (IllegalAccessException e) {
        throw new UnsupportedOperationException("Invalid handler access for operation " + methodName);
    }
}
Also used : MongoException(com.mongodb.MongoException) BsonDocument(org.bson.BsonDocument) AssumptionViolatedException(org.junit.AssumptionViolatedException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 55 with BsonDocument

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

the class DBTest method shouldCreateCollectionWithTheSetCollation.

@Test
public void shouldCreateCollectionWithTheSetCollation() {
    assumeThat(serverVersionAtLeast(3, 4), is(true));
    // Given
    collection.drop();
    Collation collation = Collation.builder().locale("en").caseLevel(true).collationCaseFirst(CollationCaseFirst.OFF).collationStrength(CollationStrength.IDENTICAL).numericOrdering(true).collationAlternate(CollationAlternate.SHIFTED).collationMaxVariable(CollationMaxVariable.SPACE).backwards(true).build();
    DBObject options = BasicDBObject.parse("{ collation: { locale: 'en', caseLevel: true, caseFirst: 'off', strength: 5," + "numericOrdering: true, alternate: 'shifted',  maxVariable: 'space', backwards: true }}");
    // When
    database.createCollection(collectionName, options);
    BsonDocument collectionCollation = getCollectionInfo(collectionName).getDocument("options").getDocument("collation");
    // Then
    BsonDocument collationDocument = collation.asDocument();
    for (String key : collationDocument.keySet()) {
        assertEquals(collationDocument.get(key), collectionCollation.get(key));
    }
    // When - collation set on the database
    database.getCollection(collectionName).drop();
    database.createCollection(collectionName, new BasicDBObject("collation", BasicDBObject.parse(collation.asDocument().toJson())));
    collectionCollation = getCollectionInfo(collectionName).getDocument("options").getDocument("collation");
    // Then
    collationDocument = collation.asDocument();
    for (String key : collationDocument.keySet()) {
        assertEquals(collationDocument.get(key), collectionCollation.get(key));
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Collation(com.mongodb.client.model.Collation) Test(org.junit.Test)

Aggregations

BsonDocument (org.bson.BsonDocument)169 BsonString (org.bson.BsonString)53 BsonValue (org.bson.BsonValue)37 Test (org.junit.Test)36 BsonArray (org.bson.BsonArray)29 BsonInt32 (org.bson.BsonInt32)28 ArrayList (java.util.ArrayList)24 BsonDocumentReader (org.bson.BsonDocumentReader)17 SingleMapReaderImpl (org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl)14 BsonDocumentWriter (org.bson.BsonDocumentWriter)14 BsonInt64 (org.bson.BsonInt64)14 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)10 BsonDouble (org.bson.BsonDouble)8 Document (org.bson.Document)7 MongoNamespace (com.mongodb.MongoNamespace)6 Before (org.junit.Before)6 MongoGridFSException (com.mongodb.MongoGridFSException)5 BsonObjectId (org.bson.BsonObjectId)5 BsonWriter (org.bson.BsonWriter)5 ObjectId (org.bson.types.ObjectId)5