@Parameters(method = "validReadConcernValues") @Test public void should_parse_read_concern_from_connection_string(String readConcernString, ReadConcern expectedReadConcern) throws Exception { // given final JsonObject configWithConnectionString = new JsonObject().put( "connection_string", String.format("mongodb://localhost:27017/mydb?replicaSet=myRs&readconcernlevel=%s", readConcernString) ); // when final ReadConcern parsedReadConcern = new MongoClientOptionsParser(configWithConnectionString) .settings() .getReadConcern(); // then assertEquals(expectedReadConcern, parsedReadConcern); }
@Parameters(method = "validReadConcernValues") @Test public void should_fallback_to_config_property_if_read_concern_not_present_in_connection_string(String readConcernString, ReadConcern expectedReadConcern) throws Exception { // given final JsonObject configWithReadConcernAsSeparateProperty = new JsonObject() .put("connection_string", "mongodb://localhost:27017/mydb?replicaSet=myRs") .put("readConcernLevel", readConcernString); // when final ReadConcern parsedReadConcern = new MongoClientOptionsParser(configWithReadConcernAsSeparateProperty) .settings() .getReadConcern(); // then assertEquals(expectedReadConcern, parsedReadConcern); }
@Test public void should_return_default_read_concern_in_case_of_missing_read_concern_in_connection_string_or_config_object() throws Exception { // given final JsonObject configWithConnectionString = new JsonObject().put( "connection_string", "mongodb://localhost:27017/mydb?replicaSet=myRs" ); // when final ReadConcern parsedReadConcern = new MongoClientOptionsParser(configWithConnectionString) .settings() .getReadConcern(); // then assertEquals(ReadConcern.DEFAULT, parsedReadConcern); }
@Test public void passThrough() { Collation collation = Collation.builder() .locale("en") .caseLevel(true) .build(); DBCollectionCountOptions options = new CountOptions() .collation(collation) .hint("i'm a hint") .limit(18) .maxTime(15, TimeUnit.MINUTES) .readPreference(ReadPreference.secondaryPreferred()) .readConcern(ReadConcern.LOCAL) .skip(12) .getOptions(); assertEquals(collation, options.getCollation()); assertEquals("i'm a hint", options.getHintString()); assertEquals(18, options.getLimit()); assertEquals(15, options.getMaxTime(TimeUnit.MINUTES)); assertEquals(ReadPreference.secondaryPreferred(), options.getReadPreference()); assertEquals(ReadConcern.LOCAL, options.getReadConcern()); assertEquals(12, options.getSkip()); }
private ReadConcern getReadConcern(String readConcern) { if ("LOCAL".equalsIgnoreCase(readConcern)) { return ReadConcern.LOCAL; } if ("MAJORITY".equalsIgnoreCase(readConcern)) { return ReadConcern.MAJORITY; } return ReadConcern.DEFAULT; }
@Test public void should_prefer_read_concern_passed_via_connection_string_over_property_value() throws Exception { // given final JsonObject configWithReadConcernPassedTwice = new JsonObject() .put("connection_string", "mongodb://localhost:27017/mydb?replicaSet=myRs&readconcernlevel=majority") .put("readConcernLevel", "linearizable"); // when final ReadConcern parsedReadConcern = new MongoClientOptionsParser(configWithReadConcernPassedTwice) .settings() .getReadConcern(); // then assertEquals(ReadConcern.MAJORITY, parsedReadConcern); }
private Object[] validReadConcernValues() { return new Object[]{ new Object[]{"local", ReadConcern.LOCAL}, new Object[]{"majority", ReadConcern.MAJORITY}, new Object[]{"linearizable", ReadConcern.LINEARIZABLE} }; }
private static ReadConcern readConcern(final String value) { return when(value.toUpperCase()) .is("DEFAULT", ReadConcern.DEFAULT) .is("LOCAL", ReadConcern.LOCAL) .is("MAJORITY", ReadConcern.MAJORITY) .orElseThrow(() -> new IllegalArgumentException("readConcern: " + value)); }
@Override public ReadConcern getReadConcern() { return collection.getReadConcern(); }
@Override public MongoCollection<TDocument> withReadConcern(ReadConcern arg0) { return new ProfiledMongoCollection<TDocument>(collection.withReadConcern(arg0)); }
@Override public ReadConcern getReadConcern() { return database.getReadConcern(); }
@Override public MongoDatabase withReadConcern(ReadConcern readConcern) { return new ProfiledMongoDatabase(database.withReadConcern(readConcern)); }
@Override public ReadConcern getReadConcern() { return wrapped.getReadConcern(); }
@Override public MongoCollection<TDocument> withReadConcern(final ReadConcern readConcern) { return new MongoCollectionImpl<TDocument>(wrapped.withReadConcern(readConcern), observableAdapter); }
@Override public MongoDatabase withReadConcern(final ReadConcern readConcern) { return new MongoDatabaseImpl(wrapped.withReadConcern(readConcern), observableAdapter); }
@Override public GridFSBucket withReadConcern(final ReadConcern readConcern) { return new GridFSBucketImpl(wrapped.withReadConcern(readConcern), observableAdapter); }
@Override public MongoDatabase withReadConcern(ReadConcern readConcern) { return null; }
@Override public MongoCollection<TDocument> withReadConcern(final ReadConcern readConcern) { return new MongoCollectionImpl<TDocument>(wrapped.withReadConcern(readConcern)); }
@Override public MongoDatabase withReadConcern(final ReadConcern readConcern) { return new MongoDatabaseImpl(wrapped.withReadConcern(readConcern)); }
@Override public GridFSBucket withReadConcern(final ReadConcern readConcern) { return new GridFSBucketImpl(wrapped.withReadConcern(readConcern)); }
Optional<ReadConcern> readConcern() { return tryToParseFromConnectionString().map(this::lift).orElseGet(this::tryToParseFromConfig); }
private Optional<ReadConcern> lift(ReadConcern readConcern) { return Optional.ofNullable(readConcern); }
private Optional<ReadConcern> tryToParseFromConnectionString() { return Optional.ofNullable(connectionString) .flatMap(cs -> Optional.ofNullable(cs.getReadConcern())); }
private Optional<ReadConcern> tryToParseFromConfig() { return Optional.ofNullable(config) .flatMap(cfg -> Optional.ofNullable(cfg.getString("readConcernLevel"))) .map(ReadConcernLevel::fromString) .map(ReadConcern::new); }
@Test public void passThrough() { Collation collation = Collation.builder() .locale("en") .caseLevel(true) .build(); DBCollectionFindOptions options = new FindOptions() .batchSize(42) .limit(18) .modifier("i'm a", "modifier") .modifier("i am", 2) .projection(new BasicDBObject("field", "value")) .maxTime(15, TimeUnit.MINUTES) .maxAwaitTime(45, TimeUnit.SECONDS) .skip(12) .sort(new BasicDBObject("field", -1)) .cursorType(CursorType.TailableAwait) .noCursorTimeout(true) .oplogReplay(true) .partial(true) .readPreference(ReadPreference.secondaryPreferred(2, TimeUnit.MINUTES)) .readConcern(ReadConcern.LOCAL) .collation(collation).getOptions(); assertEquals(42, options.getBatchSize()); assertEquals(18, options.getLimit()); assertEquals(new BasicDBObject("i'm a", "modifier") .append("i am", 2), options.getModifiers()); assertEquals(new BasicDBObject("field", "value"), options.getProjection()); assertEquals(15, options.getMaxTime(TimeUnit.MINUTES)); assertEquals(45, options.getMaxAwaitTime(TimeUnit.SECONDS)); assertEquals(12, options.getSkip()); assertEquals(new BasicDBObject("field", -1), options.getSort()); assertEquals(CursorType.TailableAwait, options.getCursorType()); assertTrue(options.isNoCursorTimeout()); assertTrue(options.isOplogReplay()); assertTrue(options.isPartial()); assertEquals(ReadPreference.secondaryPreferred(2, TimeUnit.MINUTES), options.getReadPreference()); assertEquals(ReadConcern.LOCAL, options.getReadConcern()); assertEquals(collation, options.getCollation()); }
/** * Get the read concern for the MongoDatabase. * * @return the {@link com.mongodb.ReadConcern} * @since 1.2 * @mongodb.server.release 3.2 */ ReadConcern getReadConcern();
/** * Create a new MongoDatabase instance with a different read concern. * * @param readConcern the new {@link ReadConcern} for the database * @return a new MongoDatabase instance with the different ReadConcern * @since 1.2 * @mongodb.server.release 3.2 */ MongoDatabase withReadConcern(ReadConcern readConcern);
/** * Get the read concern for the GridFSBucket. * * @return the {@link com.mongodb.ReadConcern} * @mongodb.server.release 3.2 * @mongodb.driver.manual reference/readConcern/ Read Concern */ ReadConcern getReadConcern();
/** * Create a new MongoDatabase instance with a different read concern. * * @param readConcern the new {@link ReadConcern} for the database * @return a new GridFSBucket instance with the different ReadConcern * @mongodb.server.release 3.2 * @mongodb.driver.manual reference/readConcern/ Read Concern */ GridFSBucket withReadConcern(ReadConcern readConcern);
/** * Get the read concern for the MongoCollection. * * @return the {@link com.mongodb.ReadConcern} * @since 1.2 * @mongodb.server.release 3.2 */ ReadConcern getReadConcern();
/** * Create a new MongoCollection instance with a different read concern. * * @param readConcern the new {@link ReadConcern} for the collection * @return a new MongoCollection instance with the different ReadConcern * @since 1.2 * @mongodb.server.release 3.2 */ MongoCollection<TDocument> withReadConcern(ReadConcern readConcern);
/** * Create a new MongoDatabase instance with a different read concern. * * @param readConcern the new {@link ReadConcern} for the collection * @return a new MongoDatabase instance with the different ReadConcern * @since 1.2 * @mongodb.server.release 3.2 */ MongoDatabase withReadConcern(ReadConcern readConcern);