Search in sources :

Example 36 with ObjectId

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

the class CollectionAcceptanceTest method shouldAcceptDocumentsWithAllValidValueTypes.

@Test
public void shouldAcceptDocumentsWithAllValidValueTypes() {
    Document doc = new Document();
    doc.append("_id", new ObjectId());
    doc.append("bool", true);
    doc.append("int", 3);
    doc.append("long", 5L);
    doc.append("str", "Hello MongoDB");
    doc.append("double", 1.1);
    doc.append("date", new Date());
    doc.append("ts", new BsonTimestamp(5, 1));
    doc.append("pattern", new BsonRegularExpression("abc"));
    doc.append("minKey", new MinKey());
    doc.append("maxKey", new MaxKey());
    doc.append("js", new Code("code"));
    doc.append("jsWithScope", new CodeWithScope("code", new Document()));
    doc.append("null", null);
    doc.append("binary", new Binary((byte) 42, new byte[] { 10, 11, 12 }));
    doc.append("list", Arrays.asList(7, 8, 9));
    doc.append("doc list", Arrays.asList(new Document("x", 1), new Document("x", 2)));
    collection.insertOne(doc);
    Document found = collection.find().first();
    assertNotNull(found);
    assertEquals(ObjectId.class, found.get("_id").getClass());
    assertEquals(Boolean.class, found.get("bool").getClass());
    assertEquals(Integer.class, found.get("int").getClass());
    assertEquals(Long.class, found.get("long").getClass());
    assertEquals(String.class, found.get("str").getClass());
    assertEquals(Double.class, found.get("double").getClass());
    assertEquals(Date.class, found.get("date").getClass());
    assertEquals(BsonTimestamp.class, found.get("ts").getClass());
    assertEquals(BsonRegularExpression.class, found.get("pattern").getClass());
    assertEquals(MinKey.class, found.get("minKey").getClass());
    assertEquals(MaxKey.class, found.get("maxKey").getClass());
    assertEquals(Code.class, found.get("js").getClass());
    assertEquals(CodeWithScope.class, found.get("jsWithScope").getClass());
    assertNull(found.get("null"));
    assertEquals(Binary.class, found.get("binary").getClass());
    assertTrue(found.get("list") instanceof List);
    assertTrue(found.get("doc list") instanceof List);
}
Also used : MinKey(org.bson.types.MinKey) ObjectId(org.bson.types.ObjectId) MaxKey(org.bson.types.MaxKey) CodeWithScope(org.bson.types.CodeWithScope) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Binary(org.bson.types.Binary) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) BsonRegularExpression(org.bson.BsonRegularExpression) Code(org.bson.types.Code) Date(java.util.Date) BsonTimestamp(org.bson.BsonTimestamp) Test(org.junit.Test)

Example 37 with ObjectId

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

the class FindAndReplaceAcceptanceTest method shouldReplaceAndReturnOriginalItemWithDocumentRequiringACustomEncoder.

@Test
public void shouldReplaceAndReturnOriginalItemWithDocumentRequiringACustomEncoder() {
    Worker pat = new Worker(new ObjectId(), "Pat", "Sales", new Date(), 0);
    CodecRegistry codecRegistry = fromProviders(asList(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(), new WorkerCodecProvider()));
    MongoCollection<Worker> collection = database.getCollection(getCollectionName(), Worker.class).withCodecRegistry(codecRegistry);
    collection.insertOne(pat);
    assertThat(collection.count(), is(1L));
    Worker jordan = new Worker(pat.getId(), "Jordan", "Engineer", new Date(), 1);
    Worker returnedDocument = collection.findOneAndReplace(new Document("name", "Pat"), jordan);
    assertThat("Document, retrieved from getOneAndReplace, should match the document inserted before", returnedDocument, equalTo(pat));
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) ObjectId(org.bson.types.ObjectId) DocumentCodecProvider(org.bson.codecs.DocumentCodecProvider) WorkerCodecProvider(com.mongodb.client.test.WorkerCodecProvider) Worker(com.mongodb.client.test.Worker) Document(org.bson.Document) ReturnDocument(com.mongodb.client.model.ReturnDocument) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) Date(java.util.Date) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Example 38 with ObjectId

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

the class FindAndReplaceAcceptanceTest method shouldReplaceAndReturnNewItemWithDocumentRequiringACustomEncoder.

@Test
public void shouldReplaceAndReturnNewItemWithDocumentRequiringACustomEncoder() {
    Worker pat = new Worker(new ObjectId(), "Pat", "Sales", new Date(), 3);
    CodecRegistry codecRegistry = fromProviders(asList(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(), new WorkerCodecProvider()));
    MongoCollection<Worker> collection = database.getCollection(getCollectionName(), Worker.class).withCodecRegistry(codecRegistry);
    collection.insertOne(pat);
    assertThat(collection.count(), is(1L));
    Worker jordan = new Worker(pat.getId(), "Jordan", "Engineer", new Date(), 7);
    Worker returnedDocument = collection.findOneAndReplace(new Document("name", "Pat"), jordan, new FindOneAndReplaceOptions().returnDocument(ReturnDocument.AFTER));
    assertThat("Worker retrieved from replaceOneAndGet should match the updated Worker", returnedDocument, equalTo(jordan));
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) ObjectId(org.bson.types.ObjectId) DocumentCodecProvider(org.bson.codecs.DocumentCodecProvider) WorkerCodecProvider(com.mongodb.client.test.WorkerCodecProvider) Worker(com.mongodb.client.test.Worker) Document(org.bson.Document) ReturnDocument(com.mongodb.client.model.ReturnDocument) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) Date(java.util.Date) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Example 39 with ObjectId

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

the class FindAndReplaceAcceptanceTest method shouldInsertDocumentWhenFilterDoesNotMatchAnyDocumentsAndUpsertFlagIsSet.

@Test
public void shouldInsertDocumentWhenFilterDoesNotMatchAnyDocumentsAndUpsertFlagIsSet() {
    Document originalDocument = new Document(KEY, VALUE_TO_CARE_ABOUT);
    collection.insertOne(originalDocument);
    assertThat(collection.count(), is(1L));
    Document replacementDocument = new Document("_id", new ObjectId()).append("foo", "bar");
    Document document = collection.findOneAndReplace(new Document(KEY, "valueThatDoesNotMatch"), replacementDocument, new FindOneAndReplaceOptions().upsert(true).returnDocument(ReturnDocument.AFTER));
    assertThat(collection.count(), is(2L));
    assertThat("Document retrieved from replaceOneAndGet with filter that doesn't match should match the replacement document", document, equalTo(replacementDocument));
}
Also used : FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) ObjectId(org.bson.types.ObjectId) Document(org.bson.Document) ReturnDocument(com.mongodb.client.model.ReturnDocument) Test(org.junit.Test)

Example 40 with ObjectId

use of org.bson.types.ObjectId 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)

Aggregations

ObjectId (org.bson.types.ObjectId)249 Test (org.junit.Test)157 BasicDBObject (com.mongodb.BasicDBObject)54 Document (org.bson.Document)38 DBObject (com.mongodb.DBObject)35 ArrayList (java.util.ArrayList)28 BsonObjectId (org.bson.BsonObjectId)27 Date (java.util.Date)24 DBRef (com.mongodb.DBRef)15 List (java.util.List)15 StreamRuleMock (org.graylog2.streams.matchers.StreamRuleMock)14 Message (org.graylog2.plugin.Message)13 BasicBSONObject (org.bson.BasicBSONObject)12 Query (org.springframework.data.mongodb.core.query.Query)11 GridFSFile (com.mongodb.client.gridfs.model.GridFSFile)10 Map (java.util.Map)10 Binary (org.bson.types.Binary)10 GridFSFindIterable (com.mongodb.client.gridfs.GridFSFindIterable)8 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)7 Code (org.bson.types.Code)7