Search in sources :

Example 81 with BsonInt32

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

the class DBObjectCodecTest method shouldEncodedNestedMapsListsAndDocuments.

@Test
public void shouldEncodedNestedMapsListsAndDocuments() {
    // {"0" : 0, "1", 1}
    byte[] zeroOneDocumentBytes = new byte[] { 19, 0, 0, 0, 16, 48, 0, 0, 0, 0, 0, 16, 49, 0, 1, 0, 0, 0, 0 };
    Map<String, Object> zeroOneMap = new HashMap<String, Object>();
    zeroOneMap.put("0", 0);
    zeroOneMap.put("1", 1);
    DBObject zeroOneDBObject = new BasicDBObject();
    zeroOneDBObject.putAll(zeroOneMap);
    DBObject zeroOneDBList = new BasicDBList();
    zeroOneDBList.putAll(zeroOneMap);
    List<Integer> zeroOneList = asList(0, 1);
    DBObjectCodec dbObjectCodec = new DBObjectCodec(fromProviders(asList(new ValueCodecProvider(), new DBObjectCodecProvider(), new BsonValueCodecProvider())));
    DBObject doc = new BasicDBObject().append("map", zeroOneMap).append("dbDocument", zeroOneDBObject).append("dbList", zeroOneDBList).append("list", zeroOneList).append("array", new int[] { 0, 1 }).append("lazyDoc", new LazyDBObject(zeroOneDocumentBytes, new LazyBSONCallback())).append("lazyArray", new LazyDBList(zeroOneDocumentBytes, new LazyBSONCallback()));
    BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
    dbObjectCodec.encode(writer, doc, EncoderContext.builder().build());
    BsonDocument zeroOneBsonDocument = new BsonDocument().append("0", new BsonInt32(0)).append("1", new BsonInt32(1));
    BsonArray zeroOneBsonArray = new BsonArray(asList(new BsonInt32(0), new BsonInt32(1)));
    assertEquals(new BsonDocument("map", zeroOneBsonDocument).append("dbDocument", zeroOneBsonDocument).append("dbList", zeroOneBsonArray).append("list", zeroOneBsonArray).append("array", zeroOneBsonArray).append("lazyDoc", zeroOneBsonDocument).append("lazyArray", zeroOneBsonArray), writer.getDocument());
}
Also used : HashMap(java.util.HashMap) ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) BsonInt32(org.bson.BsonInt32) BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument) LazyBSONCallback(org.bson.LazyBSONCallback) BsonArray(org.bson.BsonArray) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Example 82 with BsonInt32

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

the class DBObjectCodecTest method shouldNotGenerateIdIfPresent.

@Test
public void shouldNotGenerateIdIfPresent() {
    DBObjectCodec dbObjectCodec = new DBObjectCodec(fromProviders(asList(new ValueCodecProvider(), new DBObjectCodecProvider(), new BsonValueCodecProvider())));
    DBObject document = new BasicDBObject("_id", 1);
    assertTrue(dbObjectCodec.documentHasId(document));
    document = dbObjectCodec.generateIdIfAbsentFromDocument(document);
    assertTrue(dbObjectCodec.documentHasId(document));
    assertEquals(new BsonInt32(1), dbObjectCodec.getDocumentId(document));
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) BsonInt32(org.bson.BsonInt32) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Example 83 with BsonInt32

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

the class AbstractExplainTest method testExplainOfFind.

@Test
public void testExplainOfFind() {
    assumeTrue(serverVersionAtLeast(3, 0));
    MongoCollection<BsonDocument> collection = client.getDatabase(getDefaultDatabaseName()).getCollection("explainTest", BsonDocument.class);
    collection.drop();
    collection.insertOne(new BsonDocument("_id", new BsonInt32(1)));
    FindIterable<BsonDocument> iterable = collection.find().filter(Filters.eq("_id", 1));
    Document explainDocument = iterable.explain();
    assertNotNull(explainDocument);
    assertTrue(explainDocument.containsKey("queryPlanner"));
    assertTrue(explainDocument.containsKey("executionStats"));
    explainDocument = iterable.explain(ExplainVerbosity.QUERY_PLANNER);
    assertNotNull(explainDocument);
    assertTrue(explainDocument.containsKey("queryPlanner"));
    assertFalse(explainDocument.containsKey("executionStats"));
    BsonDocument explainBsonDocument = iterable.explain(BsonDocument.class);
    assertNotNull(explainBsonDocument);
    assertTrue(explainBsonDocument.containsKey("queryPlanner"));
    assertTrue(explainBsonDocument.containsKey("executionStats"));
    explainBsonDocument = iterable.explain(BsonDocument.class, ExplainVerbosity.QUERY_PLANNER);
    assertNotNull(explainBsonDocument);
    assertTrue(explainBsonDocument.containsKey("queryPlanner"));
    assertFalse(explainBsonDocument.containsKey("executionStats"));
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.Test)

Example 84 with BsonInt32

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

the class AbstractServerSelectionProseTest method operationCountBasedSelectionWithinLatencyWindow.

/**
 * <a href="https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection-tests.rst#prose-test">
 * {@code operationCount}-based Selection Within Latency Window</a>.
 */
@Test
@SuppressWarnings("try")
void operationCountBasedSelectionWithinLatencyWindow() throws InterruptedException, ExecutionException {
    assumeTrue(isSharded());
    ConnectionString multiMongosConnectionString = getMultiMongosConnectionString();
    assumeTrue(multiMongosConnectionString != null);
    assumeTrue(multiMongosConnectionString.getSslEnabled() == null || !multiMongosConnectionString.getSslEnabled());
    assertEquals(2, multiMongosConnectionString.getHosts().size());
    String appName = "loadBalancingTest";
    int timeoutSeconds = 60;
    int tasks = 10;
    int opsPerTask = 100;
    TestCommandListener commandListener = new TestCommandListener(singletonList("commandStartedEvent"), singletonList("drop"));
    MongoClientSettings clientSettings = getMongoClientSettingsBuilder().applicationName(appName).applyConnectionString(multiMongosConnectionString).applyToConnectionPoolSettings(builder -> builder.minSize(tasks)).addCommandListener(commandListener).build();
    BsonDocument configureFailPoint = new BsonDocument().append("configureFailPoint", new BsonString("failCommand")).append("mode", new BsonDocument().append("times", new BsonInt32(10_000))).append("data", new BsonDocument().append("failCommands", new BsonArray(singletonList(new BsonString("find")))).append("blockConnection", BsonBoolean.valueOf(true)).append("blockTimeMS", new BsonInt32(500)).append("appName", new BsonString(appName)));
    ServerAddress serverWithFailPoint = clientSettings.getClusterSettings().getHosts().get(0);
    ExecutorService executor = Executors.newFixedThreadPool(tasks);
    try (MongoClient client = createClient(clientSettings)) {
        MongoCollection<Document> collection = client.getDatabase(getDefaultDatabaseName()).getCollection("operationCountBasedSelectionWithinLatencyWindow");
        collection.drop();
        try (FailPoint ignored = FailPoint.enable(configureFailPoint, serverWithFailPoint)) {
            Map<ServerAddress, Double> selectionRates = doSelections(collection, commandListener, executor, tasks, opsPerTask, timeoutSeconds);
            double expectedServerWithFpSelectionRateUpperBound = 0.25;
            assertTrue(selectionRates.containsKey(serverWithFailPoint));
            assertTrue(selectionRates.get(serverWithFailPoint) < expectedServerWithFpSelectionRateUpperBound, selectionRates::toString);
            assertEquals(1, selectionRates.values().stream().mapToDouble(Double::doubleValue).sum(), 0.01, selectionRates::toString);
        }
        commandListener.reset();
        Map<ServerAddress, Double> selectionRates = doSelections(collection, commandListener, executor, tasks, opsPerTask, timeoutSeconds);
        selectionRates.values().forEach(rate -> assertEquals(0.5, rate, 0.1, selectionRates::toString));
    } finally {
        executor.shutdownNow();
        assertTrue(executor.awaitTermination(timeoutSeconds, SECONDS));
    }
}
Also used : TestCommandListener(com.mongodb.internal.connection.TestCommandListener) ServerAddress(com.mongodb.ServerAddress) BsonString(org.bson.BsonString) ClusterFixture.getMultiMongosConnectionString(com.mongodb.ClusterFixture.getMultiMongosConnectionString) ConnectionString(com.mongodb.ConnectionString) MongoClientSettings(com.mongodb.MongoClientSettings) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) ExecutorService(java.util.concurrent.ExecutorService) ClusterFixture.getMultiMongosConnectionString(com.mongodb.ClusterFixture.getMultiMongosConnectionString) ConnectionString(com.mongodb.ConnectionString) Test(org.junit.jupiter.api.Test)

Example 85 with BsonInt32

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

the class NativeAuthenticationHelper method getAuthCommand.

public static BsonDocument getAuthCommand(final String userName, final String authHash, final String nonce) {
    String key = nonce + userName + authHash;
    BsonDocument cmd = new BsonDocument();
    cmd.put("authenticate", new BsonInt32(1));
    cmd.put("user", new BsonString(userName));
    cmd.put("nonce", new BsonString(nonce));
    cmd.put("key", new BsonString(hexMD5(key.getBytes(StandardCharsets.UTF_8))));
    return cmd;
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonString(org.bson.BsonString)

Aggregations

BsonInt32 (org.bson.BsonInt32)106 BsonDocument (org.bson.BsonDocument)91 BsonString (org.bson.BsonString)58 BsonArray (org.bson.BsonArray)26 Test (org.junit.Test)26 Document (org.bson.Document)23 BsonInt64 (org.bson.BsonInt64)20 Test (org.junit.jupiter.api.Test)16 BsonValue (org.bson.BsonValue)15 ArrayList (java.util.ArrayList)13 List (java.util.List)9 BsonDouble (org.bson.BsonDouble)9 DisplayName (org.junit.jupiter.api.DisplayName)8 BsonNull (org.bson.BsonNull)7 MongoClientSettings (com.mongodb.MongoClientSettings)6 TestCommandListener (com.mongodb.internal.connection.TestCommandListener)5 Map (java.util.Map)5 BsonBoolean (org.bson.BsonBoolean)5 ClusterFixture.serverVersionAtLeast (com.mongodb.ClusterFixture.serverVersionAtLeast)4 ServerAddress (com.mongodb.ServerAddress)4