Search in sources :

Example 1 with BSONException

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

the class AbstractExplicitUuidCodecUuidRepresentationTest method shouldDecodeDocumentWithUuidRepresentation.

@Test
public void shouldDecodeDocumentWithUuidRepresentation() {
    bsonDocumentCollection.insertOne(new BsonDocument("standard", new BsonBinary(uuid, UuidRepresentation.STANDARD)).append("legacy", new BsonBinary(uuid, uuidRepresentationForExplicitEncoding)));
    Document document;
    try {
        document = documentCollection.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) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.Test)

Example 2 with BSONException

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

the class AbstractExplicitUuidCodecUuidRepresentationTest method shouldDecodePojoWithStandardUuidRepresentation.

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

Example 3 with BSONException

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

the class AbstractUuidRepresentationTest method shouldDecodePojoWithLegacyUuidRepresentation.

@Test
public void shouldDecodePojoWithLegacyUuidRepresentation() {
    bsonDocumentCollection.insertOne(new BsonDocument("_id", new BsonBinary(uuid, uuidRepresentation == UuidRepresentation.UNSPECIFIED || uuidRepresentation == UuidRepresentation.STANDARD ? UuidRepresentation.PYTHON_LEGACY : uuidRepresentation)));
    try {
        UuidIdPojo document = uuidIdPojoCollection.find().first();
        assertNotNull(document);
        assertEquals(uuid, document.getId());
    } catch (BSONException e) {
        assertNotEquals(UuidRepresentation.C_SHARP_LEGACY, uuidRepresentation);
        assertNotEquals(UuidRepresentation.PYTHON_LEGACY, uuidRepresentation);
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonBinary(org.bson.BsonBinary) BSONException(org.bson.BSONException) Test(org.junit.Test)

Example 4 with BSONException

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

the class AbstractUuidRepresentationTest method shouldDecodePojoWithStandardUuidRepresentation.

@Test
public void shouldDecodePojoWithStandardUuidRepresentation() {
    bsonDocumentCollection.insertOne(new BsonDocument("_id", new BsonBinary(uuid, UuidRepresentation.STANDARD)));
    try {
        UuidIdPojo document = uuidIdPojoCollection.find().first();
        assertNotNull(document);
        assertEquals(uuid, document.getId());
    } catch (BSONException e) {
        assertNotEquals(UuidRepresentation.STANDARD, uuidRepresentation);
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonBinary(org.bson.BsonBinary) BSONException(org.bson.BSONException) Test(org.junit.Test)

Example 5 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)9 BsonBinary (org.bson.BsonBinary)9 BsonDocument (org.bson.BsonDocument)8 Test (org.junit.Test)8 Binary (org.bson.types.Binary)4 BasicDBObject (com.mongodb.BasicDBObject)2 DBObject (com.mongodb.DBObject)2 UUID (java.util.UUID)2 Document (org.bson.Document)2