Search in sources :

Example 11 with ObjectId

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

the class BsonBinaryWriterTest method testWriteDBPointer.

@Test
public void testWriteDBPointer() {
    writer.writeStartDocument();
    BsonDbPointer dbPointer = new BsonDbPointer("my.test", new ObjectId("50d3332018c6a1d8d1662b61"));
    writer.writeDBPointer("pt", dbPointer);
    writer.writeEndDocument();
    byte[] expectedValues = { 33, 0, 0, 0, 12, 112, 116, 0, 8, 0, 0, 0, 109, 121, 46, 116, 101, 115, 116, 0, 80, -45, 51, 32, 24, -58, -95, -40, -47, 102, 43, 97, 0 };
    assertArrayEquals(expectedValues, buffer.toByteArray());
    BsonReader reader = createReaderForBytes(expectedValues);
    reader.readStartDocument();
    assertThat(reader.readBsonType(), is(BsonType.DB_POINTER));
    assertEquals("pt", reader.readName());
    assertEquals(dbPointer, reader.readDBPointer());
    reader.readEndDocument();
}
Also used : ObjectId(org.bson.types.ObjectId) Test(org.junit.Test)

Example 12 with ObjectId

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

the class DocumentCodecTest method testPrimitiveBSONTypeCodecs.

@Test
public void testPrimitiveBSONTypeCodecs() throws IOException {
    DocumentCodec documentCodec = new DocumentCodec();
    Document doc = new Document();
    doc.put("oid", new ObjectId());
    doc.put("integer", 1);
    doc.put("long", 2L);
    doc.put("string", "hello");
    doc.put("double", 3.2);
    doc.put("decimal", Decimal128.parse("0.100"));
    doc.put("binary", new Binary(BsonBinarySubType.USER_DEFINED, new byte[] { 0, 1, 2, 3 }));
    doc.put("date", new Date(1000));
    doc.put("boolean", true);
    doc.put("code", new Code("var i = 0"));
    doc.put("minkey", new MinKey());
    doc.put("maxkey", new MaxKey());
    //        doc.put("pattern", Pattern.compile("^hello"));  // TODO: Pattern doesn't override equals method!
    doc.put("null", null);
    documentCodec.encode(writer, doc, EncoderContext.builder().build());
    BsonInput bsonInput = createInputBuffer();
    Document decodedDocument = documentCodec.decode(new BsonBinaryReader(bsonInput), DecoderContext.builder().build());
    assertEquals(doc, decodedDocument);
}
Also used : MinKey(org.bson.types.MinKey) BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonInput(org.bson.io.BsonInput) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput) MaxKey(org.bson.types.MaxKey) BsonBinaryReader(org.bson.BsonBinaryReader) Binary(org.bson.types.Binary) Document(org.bson.Document) Code(org.bson.types.Code) Date(java.util.Date) Test(org.junit.Test)

Example 13 with ObjectId

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

the class JsonReader method visitDBPointerConstructor.

private BsonDbPointer visitDBPointerConstructor() {
    verifyToken(JsonTokenType.LEFT_PAREN);
    JsonToken namespaceToken = popToken();
    if (namespaceToken.getType() != JsonTokenType.STRING) {
        throw new JsonParseException("JSON reader expected a string but found '%s'.", namespaceToken.getValue());
    }
    verifyToken(JsonTokenType.COMMA);
    JsonToken idToken = popToken();
    if (namespaceToken.getType() != JsonTokenType.STRING) {
        throw new JsonParseException("JSON reader expected a string but found '%s'.", idToken.getValue());
    }
    verifyToken(JsonTokenType.RIGHT_PAREN);
    return new BsonDbPointer(namespaceToken.getValue(String.class), new ObjectId(idToken.getValue(String.class)));
}
Also used : ObjectId(org.bson.types.ObjectId) BsonDbPointer(org.bson.BsonDbPointer)

Example 14 with ObjectId

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

the class JsonWriterTest method testObjectIdShell.

@Test
public void testObjectIdShell() {
    writer = new JsonWriter(stringWriter, JsonWriterSettings.builder().outputMode(JsonMode.SHELL).build());
    ObjectId objectId = new ObjectId("4d0ce088e447ad08b4721a37");
    writer.writeStartDocument();
    writer.writeObjectId("_id", objectId);
    writer.writeEndDocument();
    String expected = "{ \"_id\" : ObjectId(\"4d0ce088e447ad08b4721a37\") }";
    assertEquals(expected, stringWriter.toString());
}
Also used : ObjectId(org.bson.types.ObjectId) Test(org.junit.Test)

Example 15 with ObjectId

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

the class JsonReaderTest method testObjectIdTenGen.

@Test
public void testObjectIdTenGen() {
    String json = "ObjectId(\"4d0ce088e447ad08b4721a37\")";
    bsonReader = new JsonReader(json);
    assertEquals(BsonType.OBJECT_ID, bsonReader.readBsonType());
    ObjectId objectId = bsonReader.readObjectId();
    assertEquals("4d0ce088e447ad08b4721a37", objectId.toString());
    assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
}
Also used : ObjectId(org.bson.types.ObjectId) Test(org.junit.Test)

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