Search in sources :

Example 11 with BsonObjectId

use of org.bson.BsonObjectId in project core-ng-project by neowu.

the class EntityCodecTest method getDocumentId.

@Test
void getDocumentId() {
    TestEntity entity = new TestEntity();
    entity.id = new ObjectId();
    assertEquals(new BsonObjectId(entity.id), entityCodec.getDocumentId(entity));
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonObjectId(org.bson.BsonObjectId) Test(org.junit.jupiter.api.Test)

Example 12 with BsonObjectId

use of org.bson.BsonObjectId in project drill by axbaretto.

the class TestBsonRecordReader method testObjectIdType.

@Test
public void testObjectIdType() throws IOException {
    BsonDocument bsonDoc = new BsonDocument();
    BsonObjectId value = new BsonObjectId(new ObjectId());
    bsonDoc.append("_idKey", value);
    writer.reset();
    bsonReader.write(writer, new BsonDocumentReader(bsonDoc));
    SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader();
    byte[] readByteArray = mapReader.reader("_idKey").readByteArray();
    assertTrue(Arrays.equals(value.getValue().toByteArray(), readByteArray));
}
Also used : BsonDocument(org.bson.BsonDocument) SingleMapReaderImpl(org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl) BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonDocumentReader(org.bson.BsonDocumentReader) BsonObjectId(org.bson.BsonObjectId) Test(org.junit.Test)

Example 13 with BsonObjectId

use of org.bson.BsonObjectId in project drill by apache.

the class TestBsonRecordReader method testObjectIdType.

@Test
public void testObjectIdType() throws IOException {
    BsonDocument bsonDoc = new BsonDocument();
    BsonObjectId value = new BsonObjectId(new ObjectId());
    bsonDoc.append("_idKey", value);
    writer.reset();
    bsonReader.write(writer, new BsonDocumentReader(bsonDoc));
    SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader();
    byte[] readByteArray = mapReader.reader("_idKey").readByteArray();
    assertArrayEquals(value.getValue().toByteArray(), readByteArray);
}
Also used : BsonDocument(org.bson.BsonDocument) SingleMapReaderImpl(org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl) BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonDocumentReader(org.bson.BsonDocumentReader) BsonObjectId(org.bson.BsonObjectId) BaseTest(org.apache.drill.test.BaseTest) Test(org.junit.Test)

Example 14 with BsonObjectId

use of org.bson.BsonObjectId in project pinpoint by naver.

the class WritecontextTest method parseBsonArrayWithValues.

@Test
public void parseBsonArrayWithValues() throws IOException {
    BsonValue a = new BsonString("stest");
    BsonValue b = new BsonDouble(111);
    BsonValue c = new BsonBoolean(true);
    BsonDocument document = new BsonDocument().append("int32", new BsonInt32(12)).append("int64", new BsonInt64(77L)).append("bo\"olean", new BsonBoolean(true)).append("date", new BsonDateTime(new Date().getTime())).append("double", new BsonDouble(12.3)).append("string", new BsonString("pinpoint")).append("objectId", new BsonObjectId(new ObjectId())).append("code", new BsonJavaScript("int i = 10;")).append("codeWithScope", new BsonJavaScriptWithScope("int x = y", new BsonDocument("y", new BsonInt32(1)))).append("regex", new BsonRegularExpression("^test.*regex.*xyz$", "big")).append("symbol", new BsonSymbol("wow")).append("timestamp", new BsonTimestamp(0x12345678, 5)).append("undefined", new BsonUndefined()).append("binary1", new BsonBinary(new byte[] { (byte) 0xe0, 0x4f, (byte) 0xd0, 0x20 })).append("oldBinary", new BsonBinary(BsonBinarySubType.OLD_BINARY, new byte[] { 1, 1, 1, 1, 1 })).append("arrayInt", new BsonArray(Arrays.asList(a, b, c, new BsonInt32(7)))).append("document", new BsonDocument("a", new BsonInt32(77))).append("dbPointer", new BsonDbPointer("db.coll", new ObjectId())).append("null", new BsonNull()).append("decimal128", new BsonDecimal128(new Decimal128(55)));
    BasicDBObject query = new BasicDBObject();
    query.put("ComplexBson", document);
    logger.debug("document:{}", document);
    NormalizedBson stringStringValue = MongoUtil.parseBson(new Object[] { query }, true);
    logger.debug("val:{}", stringStringValue);
    List list = objectMapper.readValue("[" + stringStringValue.getNormalizedBson() + "]", List.class);
    Assert.assertEquals(list.size(), 1);
    Map<String, ?> query1Map = (Map<String, ?>) list.get(0);
    checkValue(query1Map);
}
Also used : BsonString(org.bson.BsonString) BsonBoolean(org.bson.BsonBoolean) BasicDBObject(com.mongodb.BasicDBObject) BsonInt32(org.bson.BsonInt32) BsonDecimal128(org.bson.BsonDecimal128) BsonDouble(org.bson.BsonDouble) ArrayList(java.util.ArrayList) List(java.util.List) BsonJavaScriptWithScope(org.bson.BsonJavaScriptWithScope) BsonNull(org.bson.BsonNull) BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonBinary(org.bson.BsonBinary) BsonDbPointer(org.bson.BsonDbPointer) Decimal128(org.bson.types.Decimal128) BsonDecimal128(org.bson.BsonDecimal128) BsonRegularExpression(org.bson.BsonRegularExpression) BsonObjectId(org.bson.BsonObjectId) BsonJavaScript(org.bson.BsonJavaScript) Date(java.util.Date) BsonTimestamp(org.bson.BsonTimestamp) BsonInt64(org.bson.BsonInt64) BsonSymbol(org.bson.BsonSymbol) BsonDocument(org.bson.BsonDocument) BsonDateTime(org.bson.BsonDateTime) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) Map(java.util.Map) BsonUndefined(org.bson.BsonUndefined) BsonValue(org.bson.BsonValue) Test(org.junit.Test)

Example 15 with BsonObjectId

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

the class GridFSTest method doUpload.

private void doUpload(final BsonDocument rawArguments, final BsonDocument assertion) {
    Throwable error = null;
    ObjectId objectId = null;
    BsonDocument arguments = parseHexDocument(rawArguments, "source");
    try {
        String filename = arguments.getString("filename").getValue();
        InputStream input = new ByteArrayInputStream(arguments.getBinary("source").getData());
        GridFSUploadOptions options = new GridFSUploadOptions();
        BsonDocument rawOptions = arguments.getDocument("options", new BsonDocument());
        if (rawOptions.containsKey("chunkSizeBytes")) {
            options = options.chunkSizeBytes(rawOptions.getInt32("chunkSizeBytes").getValue());
        }
        if (rawOptions.containsKey("metadata")) {
            options = options.metadata(Document.parse(rawOptions.getDocument("metadata").toJson()));
        }
        objectId = gridFSBucket.uploadFromStream(filename, input, options);
    } catch (Throwable e) {
        error = e;
    }
    if (assertion.containsKey("error")) {
        // We don't need to read anything more so don't see the extra chunk
        if (!assertion.getString("error").getValue().equals("ExtraChunk")) {
            assertNotNull("Should have thrown an exception", error);
        }
    } else {
        assertNull("Should not have thrown an exception", error);
        for (BsonValue rawDataItem : assertion.getArray("data", new BsonArray())) {
            BsonDocument dataItem = rawDataItem.asDocument();
            String insert = dataItem.getString("insert", new BsonString("none")).getValue();
            if (insert.equals("expected.files")) {
                List<BsonDocument> documents = processFiles(dataItem.getArray("documents", new BsonArray()), new ArrayList<BsonDocument>());
                assertEquals(filesCollection.count(), documents.size());
                BsonDocument actual = filesCollection.find().first();
                for (BsonDocument expected : documents) {
                    assertEquals(expected.get("length"), actual.get("length"));
                    assertEquals(expected.get("chunkSize"), actual.get("chunkSize"));
                    assertEquals(expected.get("md5"), actual.get("md5"));
                    assertEquals(expected.get("filename"), actual.get("filename"));
                    if (expected.containsKey("metadata")) {
                        assertEquals(expected.get("metadata"), actual.get("metadata"));
                    }
                }
            } else if (insert.equals("expected.chunks")) {
                List<BsonDocument> documents = processChunks(dataItem.getArray("documents", new BsonArray()), new ArrayList<BsonDocument>());
                assertEquals(chunksCollection.count(), documents.size());
                List<BsonDocument> actualDocuments = chunksCollection.find().into(new ArrayList<BsonDocument>());
                for (int i = 0; i < documents.size(); i++) {
                    BsonDocument expected = documents.get(i);
                    BsonDocument actual = actualDocuments.get(i);
                    assertEquals(new BsonObjectId(objectId), actual.getObjectId("files_id"));
                    assertEquals(expected.get("n"), actual.get("n"));
                    assertEquals(expected.get("data"), actual.get("data"));
                }
            }
        }
    }
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) BsonString(org.bson.BsonString) BsonObjectId(org.bson.BsonObjectId) BsonDocument(org.bson.BsonDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) ArrayList(java.util.ArrayList) List(java.util.List) GridFSUploadOptions(com.mongodb.client.gridfs.model.GridFSUploadOptions) BsonValue(org.bson.BsonValue)

Aggregations

BsonObjectId (org.bson.BsonObjectId)27 ObjectId (org.bson.types.ObjectId)23 Test (org.junit.Test)19 GridFSFile (com.mongodb.client.gridfs.model.GridFSFile)12 ArrayList (java.util.ArrayList)11 Document (org.bson.Document)9 GridFSFindIterable (com.mongodb.client.gridfs.GridFSFindIterable)8 BsonDocument (org.bson.BsonDocument)8 List (java.util.List)7 MongoGridFSException (com.mongodb.MongoGridFSException)6 BsonArray (org.bson.BsonArray)6 BsonString (org.bson.BsonString)6 BsonValue (org.bson.BsonValue)6 Date (java.util.Date)5 IOException (java.io.IOException)4 Assertions (org.assertj.core.api.Assertions)4 Before (org.junit.Before)4 RunWith (org.junit.runner.RunWith)4 Autowired (org.springframework.beans.factory.annotation.Autowired)3 ClassPathResource (org.springframework.core.io.ClassPathResource)3