Search in sources :

Example 21 with ObjectId

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

the class DBCollectionTest method testWriteConcernExceptionOnUpdate.

@Test
public void testWriteConcernExceptionOnUpdate() throws UnknownHostException {
    assumeThat(isDiscoverableReplicaSet(), is(true));
    ObjectId id = new ObjectId();
    collection.insert(new BasicDBObject("_id", id));
    try {
        WriteResult res = collection.update(new BasicDBObject("_id", id), new BasicDBObject("$set", new BasicDBObject("x", 1)), false, false, new WriteConcern(5).withWTimeout(1, MILLISECONDS));
        fail("Write should have failed but succeeded with result " + res);
    } catch (WriteConcernException e) {
        assertEquals(1, e.getWriteConcernResult().getCount());
        assertTrue(e.getWriteConcernResult().isUpdateOfExisting());
        assertEquals(null, e.getWriteConcernResult().getUpsertedId());
    }
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) Test(org.junit.Test)

Example 22 with ObjectId

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

the class DBCollectionTest method testWriteConcernExceptionOnUpsert.

@Test
public void testWriteConcernExceptionOnUpsert() throws UnknownHostException {
    assumeThat(isDiscoverableReplicaSet(), is(true));
    ObjectId id = new ObjectId();
    try {
        WriteResult res = collection.update(new BasicDBObject("_id", id), new BasicDBObject("$set", new BasicDBObject("x", 1)), true, false, new WriteConcern(5).withWTimeout(1, MILLISECONDS));
        fail("Write should have failed but succeeded with result " + res);
    } catch (WriteConcernException e) {
        assertEquals(1, e.getWriteConcernResult().getCount());
        assertFalse(e.getWriteConcernResult().isUpdateOfExisting());
        assertEquals(new BsonObjectId(id), e.getWriteConcernResult().getUpsertedId());
    }
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonObjectId(org.bson.BsonObjectId) Test(org.junit.Test)

Example 23 with ObjectId

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

the class DBCollectionTest method testBulkWriteOperation.

@Test
public void testBulkWriteOperation() {
    // given
    collection.insert(Arrays.<DBObject>asList(new BasicDBObject("_id", 3), new BasicDBObject("_id", 4), new BasicDBObject("_id", 5), new BasicDBObject("_id", 6).append("z", 1), new BasicDBObject("_id", 7).append("z", 1), new BasicDBObject("_id", 8).append("z", 2), new BasicDBObject("_id", 9).append("z", 2)));
    // when
    BulkWriteOperation bulkWriteOperation = collection.initializeOrderedBulkOperation();
    bulkWriteOperation.insert(new BasicDBObject("_id", 0));
    ObjectId upsertOneId = new ObjectId();
    ObjectId upsertTwoId = new ObjectId();
    bulkWriteOperation.find(new BasicDBObject("_id", upsertOneId)).upsert().updateOne(new BasicDBObject("$set", new BasicDBObject("x", 2)));
    bulkWriteOperation.find(new BasicDBObject("_id", upsertTwoId)).upsert().replaceOne(new BasicDBObject("_id", upsertTwoId).append("y", 2));
    bulkWriteOperation.find(new BasicDBObject("_id", 3)).removeOne();
    bulkWriteOperation.find(new BasicDBObject("_id", 4)).updateOne(new BasicDBObject("$set", new BasicDBObject("x", 1)));
    bulkWriteOperation.find(new BasicDBObject("_id", 5)).replaceOne(new BasicDBObject("_id", 5).append("y", 1));
    bulkWriteOperation.find(new BasicDBObject("z", 1)).remove();
    bulkWriteOperation.find(new BasicDBObject("z", 2)).update(new BasicDBObject("$set", new BasicDBObject("z", 3)));
    BulkWriteResult result = bulkWriteOperation.execute();
    // then
    assertTrue(bulkWriteOperation.isOrdered());
    assertTrue(result.isAcknowledged());
    assertEquals(1, result.getInsertedCount());
    assertEquals(4, result.getMatchedCount());
    assertEquals(3, result.getRemovedCount());
    if (result.isModifiedCountAvailable()) {
        assertEquals(4, result.getModifiedCount());
    }
    assertEquals(Arrays.asList(new BulkWriteUpsert(1, upsertOneId), new BulkWriteUpsert(2, upsertTwoId)), result.getUpserts());
    assertEquals(Arrays.<DBObject>asList(new BasicDBObject("_id", 0), new BasicDBObject("_id", 4).append("x", 1), new BasicDBObject("_id", 5).append("y", 1), new BasicDBObject("_id", 8).append("z", 3), new BasicDBObject("_id", 9).append("z", 3), new BasicDBObject("_id", upsertOneId).append("x", 2), new BasicDBObject("_id", upsertTwoId).append("y", 2)), collection.find().sort(new BasicDBObject("_id", 1)).toArray());
    // when
    try {
        bulkWriteOperation.insert(new BasicDBObject());
        fail();
    } catch (IllegalStateException e) {
    // then should throw
    }
    // when
    try {
        bulkWriteOperation.find(new BasicDBObject());
        fail();
    } catch (IllegalStateException e) {
    // then should throw
    }
    // when
    try {
        bulkWriteOperation.execute();
        fail();
    } catch (IllegalStateException e) {
    // then should throw
    }
    // when
    try {
        bulkWriteOperation.execute(WriteConcern.ACKNOWLEDGED);
        fail();
    } catch (IllegalStateException e) {
    // then should throw
    }
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) Test(org.junit.Test)

Example 24 with ObjectId

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

the class DBCollectionTest method testWriteConcernExceptionOnFindAndModify.

@Test
public void testWriteConcernExceptionOnFindAndModify() throws UnknownHostException {
    assumeThat(serverVersionAtLeast(3, 2), is(true));
    assumeThat(isDiscoverableReplicaSet(), is(true));
    ObjectId id = new ObjectId();
    WriteConcern writeConcern = new WriteConcern(5, 1);
    // FindAndUpdateOperation path
    try {
        collection.findAndModify(new BasicDBObject("_id", id), null, null, false, new BasicDBObject("$set", new BasicDBObject("x", 1)), true, true, writeConcern);
        fail("Expected findAndModify to error");
    } catch (WriteConcernException e) {
        assertNotNull(e.getServerAddress());
        assertNotNull(e.getErrorMessage());
        assertTrue(e.getCode() > 0);
        assertTrue(e.getWriteConcernResult().wasAcknowledged());
        assertEquals(1, e.getWriteConcernResult().getCount());
        assertFalse(e.getWriteConcernResult().isUpdateOfExisting());
        assertEquals(new BsonObjectId(id), e.getWriteConcernResult().getUpsertedId());
    }
    // FindAndReplaceOperation path
    try {
        collection.findAndModify(new BasicDBObject("_id", id), null, null, false, new BasicDBObject("x", 1), true, true, writeConcern);
        fail("Expected findAndModify to error");
    } catch (WriteConcernException e) {
        assertNotNull(e.getServerAddress());
        assertNotNull(e.getErrorMessage());
        assertTrue(e.getCode() > 0);
        assertTrue(e.getWriteConcernResult().wasAcknowledged());
        assertEquals(1, e.getWriteConcernResult().getCount());
        assertTrue(e.getWriteConcernResult().isUpdateOfExisting());
        assertNull(e.getWriteConcernResult().getUpsertedId());
    }
    // FindAndDeleteOperation path
    try {
        collection.findAndModify(new BasicDBObject("_id", id), null, null, true, null, false, false, writeConcern);
        fail("Expected findAndModify to error");
    } catch (WriteConcernException e) {
        assertNotNull(e.getServerAddress());
        assertNotNull(e.getErrorMessage());
        assertTrue(e.getCode() > 0);
        assertTrue(e.getWriteConcernResult().wasAcknowledged());
        assertEquals(1, e.getWriteConcernResult().getCount());
        assertFalse(e.getWriteConcernResult().isUpdateOfExisting());
        assertNull(e.getWriteConcernResult().getUpsertedId());
    }
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonObjectId(org.bson.BsonObjectId) Test(org.junit.Test)

Example 25 with ObjectId

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

the class ConcreteCodec method decode.

@Override
public Concrete decode(final BsonReader reader, final DecoderContext decoderContext) {
    reader.readStartDocument();
    ObjectId id = reader.readObjectId("_id");
    String str = reader.readString("str");
    int i = reader.readInt32("i");
    long l = reader.readInt64("l");
    double d = reader.readDouble("d");
    long date = reader.readDateTime("date");
    reader.readEndDocument();
    return new Concrete(id, str, i, l, d, date);
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId)

Aggregations

ObjectId (org.bson.types.ObjectId)194 Test (org.junit.Test)113 BasicDBObject (com.mongodb.BasicDBObject)46 DBObject (com.mongodb.DBObject)28 Date (java.util.Date)23 Document (org.bson.Document)21 ArrayList (java.util.ArrayList)20 BsonObjectId (org.bson.BsonObjectId)16 StreamRuleMock (org.graylog2.streams.matchers.StreamRuleMock)14 Message (org.graylog2.plugin.Message)13 BasicBSONObject (org.bson.BasicBSONObject)12 List (java.util.List)11 Binary (org.bson.types.Binary)10 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)9 Map (java.util.Map)8 DBRef (com.mongodb.DBRef)7 Code (org.bson.types.Code)7 Stream (org.graylog2.plugin.streams.Stream)7 ImmutableList (com.google.common.collect.ImmutableList)6 BsonDocument (org.bson.BsonDocument)6