Search in sources :

Example 1 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 2 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 3 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 4 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 5 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)499 Test (org.junit.Test)265 BasicDBObject (com.mongodb.BasicDBObject)86 Document (org.bson.Document)82 Date (java.util.Date)77 DBObject (com.mongodb.DBObject)52 Decimal128 (org.bson.types.Decimal128)46 ArrayList (java.util.ArrayList)41 BsonObjectId (org.bson.BsonObjectId)37 List (java.util.List)35 Test (org.testng.annotations.Test)27 UUID (java.util.UUID)19 DBRef (com.mongodb.DBRef)18 AllTypes (io.realm.entities.AllTypes)18 Map (java.util.Map)18 Before (org.junit.Before)18 BigDecimal (java.math.BigDecimal)16 Stream (org.graylog2.plugin.streams.Stream)16 StreamRuleMock (org.graylog2.streams.matchers.StreamRuleMock)16 Set (java.util.Set)15