use of org.bson.BsonDocument in project mongo-java-driver by mongodb.
the class CommandMonitoringTest method getExpectedEvents.
private List<CommandEvent> getExpectedEvents(final BsonArray expectedEventDocuments) {
List<CommandEvent> expectedEvents = new ArrayList<CommandEvent>(expectedEventDocuments.size());
for (Iterator<BsonValue> iterator = expectedEventDocuments.iterator(); iterator.hasNext(); ) {
BsonDocument curExpectedEventDocument = iterator.next().asDocument();
String eventType = curExpectedEventDocument.keySet().iterator().next();
BsonDocument eventDescriptionDocument = curExpectedEventDocument.getDocument(eventType);
CommandEvent commandEvent;
if (eventType.equals("command_started_event")) {
commandEvent = new CommandStartedEvent(1, null, databaseName, eventDescriptionDocument.getString("command_name").getValue(), eventDescriptionDocument.getDocument("command"));
} else if (eventType.equals("command_succeeded_event")) {
BsonDocument replyDocument = eventDescriptionDocument.get("reply").asDocument();
commandEvent = new CommandSucceededEvent(1, null, eventDescriptionDocument.getString("command_name").getValue(), replyDocument, 1);
} else if (eventType.equals("command_failed_event")) {
commandEvent = new CommandFailedEvent(1, null, eventDescriptionDocument.getString("command_name").getValue(), 1, null);
} else {
throw new UnsupportedOperationException("Unsupported command event type: " + eventType);
}
expectedEvents.add(commandEvent);
}
return expectedEvents;
}
use of org.bson.BsonDocument in project mongo-java-driver by mongodb.
the class CommandMonitoringTest method setUp.
@Before
public void setUp() {
ServerVersion serverVersion = ClusterFixture.getServerVersion();
if (definition.containsKey("ignore_if_server_version_less_than")) {
assumeFalse(serverVersion.compareTo(getServerVersion("ignore_if_server_version_less_than")) < 0);
}
if (definition.containsKey("ignore_if_server_version_greater_than")) {
assumeFalse(serverVersion.compareTo(getServerVersion("ignore_if_server_version_greater_than")) > 0);
}
if (definition.containsKey("ignore_if_topology_type")) {
BsonArray topologyTypes = definition.getArray("ignore_if_topology_type");
for (BsonValue type : topologyTypes) {
String typeString = type.asString().getValue();
if (typeString.equals("sharded")) {
assumeFalse(isSharded());
} else if (typeString.equals("replica_set")) {
assumeFalse(isDiscoverableReplicaSet());
} else if (typeString.equals("standalone")) {
assumeFalse(isSharded());
}
}
}
List<BsonDocument> documents = new ArrayList<BsonDocument>();
for (BsonValue document : data) {
documents.add(document.asDocument());
}
CollectionHelper<Document> collectionHelper = new CollectionHelper<Document>(new DocumentCodec(), new MongoNamespace(databaseName, collectionName));
collectionHelper.drop();
collectionHelper.insertDocuments(documents);
commandListener.reset();
collection = mongoClient.getDatabase(databaseName).getCollection(collectionName, BsonDocument.class);
if (definition.getDocument("operation").containsKey("read_preference")) {
collection = collection.withReadPreference(ReadPreference.valueOf(definition.getDocument("operation").getDocument("read_preference").getString("mode").getValue()));
}
helper = new JsonPoweredCrudTestHelper(description, collection);
}
use of org.bson.BsonDocument in project mongo-java-driver by mongodb.
the class DBObjectCodecTest method shouldEncodeNull.
@Test
public void shouldEncodeNull() {
DBObjectCodec dbObjectCodec = new DBObjectCodec(fromProviders(asList(new ValueCodecProvider(), new DBObjectCodecProvider(), new BsonValueCodecProvider())));
DBObject doc = new BasicDBObject("null", null);
BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
dbObjectCodec.encode(writer, doc, EncoderContext.builder().build());
assertEquals(new BsonDocument("null", BsonNull.VALUE), writer.getDocument());
}
use of org.bson.BsonDocument in project mongo-java-driver by mongodb.
the class DBObjectCodecTest method shouldRespectEncodeIdFirstPropertyInEncoderContext.
@Test
public void shouldRespectEncodeIdFirstPropertyInEncoderContext() {
DBObjectCodec dbObjectCodec = new DBObjectCodec(fromProviders(asList(new ValueCodecProvider(), new DBObjectCodecProvider(), new BsonValueCodecProvider())));
// given
DBObject doc = new BasicDBObject("x", 2).append("_id", 2);
// when
BsonDocument encodedDocument = new BsonDocument();
dbObjectCodec.encode(new BsonDocumentWriter(encodedDocument), doc, EncoderContext.builder().isEncodingCollectibleDocument(true).build());
// then
assertEquals(new ArrayList<String>(encodedDocument.keySet()), asList("_id", "x"));
// when
encodedDocument.clear();
dbObjectCodec.encode(new BsonDocumentWriter(encodedDocument), doc, EncoderContext.builder().isEncodingCollectibleDocument(false).build());
// then
assertEquals(new ArrayList<String>(encodedDocument.keySet()), asList("x", "_id"));
}
use of org.bson.BsonDocument in project mongo-java-driver by mongodb.
the class DBObjectCodecTest method shouldEncodeIterableMapAsMap.
@Test
public void shouldEncodeIterableMapAsMap() {
IterableMap iterableMap = new IterableMap();
iterableMap.put("first", 1);
DBObjectCodec dbObjectCodec = new DBObjectCodec(fromProviders(asList(new ValueCodecProvider(), new DBObjectCodecProvider(), new BsonValueCodecProvider())));
DBObject doc = new BasicDBObject("map", iterableMap);
BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
dbObjectCodec.encode(writer, doc, EncoderContext.builder().build());
assertEquals(new BsonDocument("map", new BsonDocument("first", new BsonInt32(1))), writer.getDocument());
}
Aggregations