Search in sources :

Example 6 with BSONException

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

the class AbstractExplicitUuidCodecUuidRepresentationTest method shouldDecodeDBObjectWithUuidRepresentation.

@Test
public void shouldDecodeDBObjectWithUuidRepresentation() {
    bsonDocumentCollection.insertOne(new BsonDocument("standard", new BsonBinary(uuid, UuidRepresentation.STANDARD)).append("legacy", new BsonBinary(uuid, uuidRepresentationForExplicitEncoding)));
    DBObject document;
    try {
        document = dbObjectCollection.find().first();
        assertNotNull(document);
    } catch (BSONException e) {
        if (uuidCodec.getUuidRepresentation() != STANDARD) {
            throw e;
        }
        return;
    }
    if (uuidRepresentationForClient == UuidRepresentation.STANDARD) {
        assertEquals(UUID.class, document.get("standard").getClass());
        assertEquals(uuid, document.get("standard"));
        assertEquals(Binary.class, document.get("legacy").getClass());
        assertEquals(new Binary(BsonBinarySubType.UUID_LEGACY, encodedValue), document.get("legacy"));
    } else {
        if (uuidRepresentationForClient == UuidRepresentation.JAVA_LEGACY) {
            assertEquals(UUID.class, document.get("standard").getClass());
            assertEquals(uuid, document.get("standard"));
        } else {
            assertEquals(Binary.class, document.get("standard").getClass());
            assertEquals(new Binary(BsonBinarySubType.UUID_STANDARD, standardEncodedValue), document.get("standard"));
        }
        assertEquals(UUID.class, document.get("legacy").getClass());
        assertEquals(uuid, document.get("legacy"));
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonBinary(org.bson.BsonBinary) BSONException(org.bson.BSONException) Binary(org.bson.types.Binary) BsonBinary(org.bson.BsonBinary) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 7 with BSONException

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

the class AbstractExplicitUuidCodecUuidRepresentationTest method shouldDecodePojoWithLegacyUuidRepresentation.

@Test
public void shouldDecodePojoWithLegacyUuidRepresentation() {
    bsonDocumentCollection.insertOne(new BsonDocument("_id", new BsonBinary(uuid, uuidRepresentationForExplicitEncoding)));
    try {
        UuidIdPojo document = uuidIdPojoCollection.find().first();
        assertNotNull(document);
        assertEquals(uuid, document.getId());
    } catch (BSONException e) {
        if (uuidCodec.getUuidRepresentation() == uuidRepresentationForExplicitEncoding) {
            throw e;
        }
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonBinary(org.bson.BsonBinary) BSONException(org.bson.BSONException) Test(org.junit.Test)

Example 8 with BSONException

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

the class AbstractUuidRepresentationTest method shouldDecodeDocumentWithUuidRepresentation.

@Test
public void shouldDecodeDocumentWithUuidRepresentation() {
    bsonDocumentCollection.insertOne(new BsonDocument("standard", new BsonBinary(uuid, UuidRepresentation.STANDARD)).append("legacy", new BsonBinary(uuid, uuidRepresentation == UuidRepresentation.UNSPECIFIED || uuidRepresentation == UuidRepresentation.STANDARD ? UuidRepresentation.PYTHON_LEGACY : uuidRepresentation)));
    Document document;
    try {
        document = documentCollection.find().first();
        assertNotNull(document);
    } catch (BSONException e) {
        if (uuidRepresentation != STANDARD && uuidRepresentation != JAVA_LEGACY) {
            throw e;
        }
        return;
    }
    if (uuidRepresentation == UuidRepresentation.UNSPECIFIED) {
        assertEquals(Binary.class, document.get("standard").getClass());
        assertEquals(new Binary(BsonBinarySubType.UUID_STANDARD, standardEncodedValue), document.get("standard"));
        assertEquals(Binary.class, document.get("legacy").getClass());
        assertEquals(new Binary(BsonBinarySubType.UUID_LEGACY, standardEncodedValue), document.get("legacy"));
    } else if (uuidRepresentation == UuidRepresentation.STANDARD) {
        assertEquals(UUID.class, document.get("standard").getClass());
        assertEquals(uuid, document.get("standard"));
        assertEquals(Binary.class, document.get("legacy").getClass());
        assertEquals(new Binary(BsonBinarySubType.UUID_LEGACY, standardEncodedValue), document.get("legacy"));
    } else {
        assertEquals(Binary.class, document.get("standard").getClass());
        assertEquals(new Binary(BsonBinarySubType.UUID_STANDARD, standardEncodedValue), document.get("standard"));
        assertEquals(UUID.class, document.get("legacy").getClass());
        assertEquals(uuid, document.get("legacy"));
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonBinary(org.bson.BsonBinary) BSONException(org.bson.BSONException) Binary(org.bson.types.Binary) BsonBinary(org.bson.BsonBinary) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) UUID(java.util.UUID) Test(org.junit.Test)

Example 9 with BSONException

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

the class AbstractUuidRepresentationTest method shouldDecodeDbObjectWithUuidRepresentation.

@Test
public void shouldDecodeDbObjectWithUuidRepresentation() {
    bsonDocumentCollection.insertOne(new BsonDocument("standard", new BsonBinary(uuid, UuidRepresentation.STANDARD)).append("legacy", new BsonBinary(uuid, uuidRepresentation == UuidRepresentation.UNSPECIFIED || uuidRepresentation == UuidRepresentation.STANDARD ? UuidRepresentation.PYTHON_LEGACY : uuidRepresentation)));
    DBObject document;
    try {
        document = dbObjectCollection.find().first();
        assertNotNull(document);
    } catch (BSONException e) {
        if (uuidRepresentation != STANDARD && uuidRepresentation != JAVA_LEGACY) {
            throw e;
        }
        return;
    }
    if (uuidRepresentation == UuidRepresentation.UNSPECIFIED) {
        assertEquals(Binary.class, document.get("standard").getClass());
        assertEquals(new Binary(BsonBinarySubType.UUID_STANDARD, standardEncodedValue), document.get("standard"));
        assertEquals(Binary.class, document.get("legacy").getClass());
        assertEquals(new Binary(BsonBinarySubType.UUID_LEGACY, standardEncodedValue), document.get("legacy"));
    } else if (uuidRepresentation == UuidRepresentation.STANDARD) {
        assertEquals(UUID.class, document.get("standard").getClass());
        assertEquals(uuid, document.get("standard"));
        assertEquals(Binary.class, document.get("legacy").getClass());
        assertEquals(new Binary(BsonBinarySubType.UUID_LEGACY, standardEncodedValue), document.get("legacy"));
    } else {
        assertEquals(Binary.class, document.get("standard").getClass());
        assertEquals(new Binary(BsonBinarySubType.UUID_STANDARD, standardEncodedValue), document.get("standard"));
        assertEquals(UUID.class, document.get("legacy").getClass());
        assertEquals(uuid, document.get("legacy"));
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonBinary(org.bson.BsonBinary) BSONException(org.bson.BSONException) Binary(org.bson.types.Binary) BsonBinary(org.bson.BsonBinary) UUID(java.util.UUID) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 10 with BSONException

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

the class UuidCodec method encode.

@Override
public void encode(final BsonWriter writer, final UUID value, final EncoderContext encoderContext) {
    byte[] binaryData = new byte[16];
    writeLongToArrayBigEndian(binaryData, 0, value.getMostSignificantBits());
    writeLongToArrayBigEndian(binaryData, 8, value.getLeastSignificantBits());
    switch(encoderUuidRepresentation) {
        case C_SHARP_LEGACY:
            UuidCodecHelper.reverseByteArray(binaryData, 0, 4);
            UuidCodecHelper.reverseByteArray(binaryData, 4, 2);
            UuidCodecHelper.reverseByteArray(binaryData, 6, 2);
            break;
        case JAVA_LEGACY:
            UuidCodecHelper.reverseByteArray(binaryData, 0, 8);
            UuidCodecHelper.reverseByteArray(binaryData, 8, 8);
            break;
        case PYTHON_LEGACY:
        case STANDARD:
            break;
        default:
            throw new BSONException("Unexpected UUID representation");
    }
    // changed the default subtype to STANDARD since 3.0
    if (encoderUuidRepresentation == UuidRepresentation.STANDARD) {
        writer.writeBinaryData(new BsonBinary(BsonBinarySubType.UUID_STANDARD, binaryData));
    } else {
        writer.writeBinaryData(new BsonBinary(BsonBinarySubType.UUID_LEGACY, binaryData));
    }
}
Also used : BsonBinary(org.bson.BsonBinary) BSONException(org.bson.BSONException)

Aggregations

BSONException (org.bson.BSONException)12 BsonBinary (org.bson.BsonBinary)9 BsonDocument (org.bson.BsonDocument)8 Test (org.junit.Test)8 Document (org.bson.Document)4 Binary (org.bson.types.Binary)4 JsonParseException (org.bson.json.JsonParseException)3 BasicDBObject (com.mongodb.BasicDBObject)2 DBObject (com.mongodb.DBObject)2 MongoSKDocument (fr.romitou.mongosk.elements.MongoSKDocument)2 UUID (java.util.UUID)2 SubscriberHelpers (fr.romitou.mongosk.SubscriberHelpers)1 MongoSKDatabase (fr.romitou.mongosk.elements.MongoSKDatabase)1 ArrayList (java.util.ArrayList)1 Record (org.apache.pulsar.functions.api.Record)1