Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Spring Data 3.2.4 and Mongo 4.2.3 #615

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
java_version: ['11', '16']
steps:
# Check out the project
- uses: actions/checkout@v2.3.5
- uses: actions/checkout@v2.4.0
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis

Expand Down
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@
<jsch.version>0.1.55</jsch.version>
<metrics-jdbi3.version>4.1.25</metrics-jdbi3.version>
<metrics-jetty9.version>4.1.25</metrics-jetty9.version>
<!-- Note: the mongo version should match the one that Spring Data Mongo depends on -->
<mongodb.version>4.2.3</mongodb.version>
<postgresql.version>42.2.23</postgresql.version>
<retrying-again.version>1.0.0</retrying-again.version>
<spring.version>5.3.9</spring.version>
<spring-data-mongodb.version>2.2.13.RELEASE</spring-data-mongodb.version>
<spring-data-mongodb.version>3.2.4</spring-data-mongodb.version>
Copy link
Member Author

@sleberknight sleberknight Jan 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of date; we just got a new PR for v3.3.1 for kiwi-bom (see kiwiproject/kiwi-bom#71 )

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and now one for v3.3.2 ; see again kiwiproject/kiwi-bom#71 (3.3.1) and kiwiproject/kiwi-bom#91 (3.3.2)

<stax2-api.version>4.2.1</stax2-api.version>
<stax-ex.version>1.8.3</stax-ex.version>
<!--
Expand Down Expand Up @@ -561,6 +563,14 @@
</exclusions>
</dependency>

<!-- The mongo driver to use is optional in Spring Data's POM. You have to choose either the mongodb-driver-sync
or the mongodb-driver-reactivestreams as the driver. -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>${mongodb.version}</version>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should probably be provided scope

</dependency>

<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>stax2-api</artifactId>
Expand Down
33 changes: 11 additions & 22 deletions src/main/java/org/kiwiproject/mongo/MongoClientWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
import static org.kiwiproject.base.KiwiPreconditions.checkArgumentNotBlank;

import com.google.common.annotations.Beta;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import lombok.Getter;

import java.io.Closeable;

/**
* A wrapper around a {@link MongoClient} that also provides {@link MongoDatabase} and {@link DB} instances
* A wrapper around a {@link MongoClient} that also provides {@link MongoDatabase} instance
* for a specific database, which is specified in the constructor.
*
* @deprecated with no replacement since we no longer support the older Mongo 3.x versions and our code must
* therefore use only the new Mongo APIs (i.e. {@code com.mongodb.DB} doesn't exist in the Mongo 4.x drivers).
*/
@Beta
@Deprecated(forRemoval = true, since = "0.26.0")
public class MongoClientWrapper implements Closeable {

/**
Expand All @@ -30,33 +33,19 @@ public class MongoClientWrapper implements Closeable {
@Getter
private final MongoDatabase mongoDatabase;

private final DB db;

/**
* Create a new instance with the given Mongo connection URI. Uses {@code databaseName} to get both
* {@link MongoDatabase} and {@link DB} instances, which are available from this instance via their
* getter methods.
* Create a new instance with the given Mongo connection URI. Uses {@code databaseName} to get
* a {@link MongoDatabase} instance, which is available from this instance via the getter method.
*
* @param mongoUri the Mongo connection URI
* @param databaseName the database associated with this instance
*/
@SuppressWarnings("deprecation")
public MongoClientWrapper(String mongoUri, String databaseName) {
checkArgumentNotBlank(mongoUri, "mongoUri cannot be blank");
checkArgumentNotBlank(databaseName, "databaseName cannot be blank");

mongoClient = new MongoClient(new MongoClientURI(mongoUri));
mongoDatabase = mongoClient.getDatabase(databaseName);
db = mongoClient.getDB(databaseName);
}

/**
* Return the (deprecated) {@link DB} instance for clients that have not yet migrated to {@link MongoDatabase}.
*
* @return the DB instance associated with this instance (as specified in the constructor)
*/
public DB getDB() {
return db;
this.mongoClient = MongoClients.create(mongoUri);
mongoDatabase = this.mongoClient.getDatabase(databaseName);
}

/**
Expand Down
29 changes: 20 additions & 9 deletions src/main/java/org/kiwiproject/net/KiwiUrls.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@ public static URL createHttpsUrlObject(String hostname, int port, String path) {
}

/**
* Extract all of the relevant sections from the given {@code uri}.
* Extract all the relevant sections from the given {@code uri}.
* <p>
* As an example, if given https://news.bbc.co.uk:8080/a-news-article" this would return the following:
* As an example, if given "https://news.bbc.co.uk:8080/a-news-article" this would return the following:
* <ul>
* <li>scheme = "https"</li>
* <li>subDomainName = "news"</li>
Expand Down Expand Up @@ -496,7 +496,7 @@ public static Optional<String> extractSubDomainNameFrom(String url) {

/**
* Searches the {@code commaDelimitedUrls} for its domains, and if found, replaces all entries with
* {@code replacmentDomain}. The {@code commaDelimitedUrls} can be a standalone URL.
* {@code replacementDomain}. The {@code commaDelimitedUrls} can be a standalone URL.
*
* @param commaDelimitedUrls the comma delimited URLs to search
* @param replacementDomain the domain to replace if found
Expand Down Expand Up @@ -534,7 +534,7 @@ private static String domainOrNull(String url) {

/**
* Replace {@code expectedDomain} in {@code url} with {@code replacementDomain} if and only if {@code url}'s domain
* is equal to {@code expectedDomain}. If the given {@code url} has no domain, or is not the expected domain, b
* is equal to {@code expectedDomain}. If the given {@code url} has no domain, or is not the expected domain, be
* lenient and just return the original URL.
*/
private static String replaceDomain(URL url, String expectedDomain, String replacementDomain) {
Expand Down Expand Up @@ -574,8 +574,16 @@ private static int defaultPortForScheme(String scheme) {
}

/**
* Converts a query string (comprised of key/value pairs separated by '&amp;' characters) into a Map of String of
* Strings
* Converts a query string (comprised of key/value pairs separated by '&amp;' characters) into a Map whose keys
* are the parameter names and values are the parameter values.
* <p>
* Note specifically that this method does <strong>not</strong> decode the query string parameters. It splits on
* literal &amp; characters to obtain the key/value pairs and then on the literal {@code =} to get the name and
* value of each pair.
* <p>
* Also note that if a parameter has multiple values, only the <em>first</em> one is returned, e.g. the value of
* "topping" in the query string {@code topping=pepperoni&topping=banana+pepper&topping=sausage} will always be
* "pepperoni".
*
* @param queryString the query string to create the map from
* @return a map of the query params
Expand All @@ -588,12 +596,15 @@ public static Map<String, String> queryStringToMap(String queryString) {

return Arrays.stream(queryString.split("&"))
.map(keyValue -> keyValue.split("="))
.collect(toMap(splat -> splat[0], splat -> splat[1]));
.collect(toMap(splat -> splat[0], splat -> splat[1], (value1, value2) -> value1));
}

/**
* Converts a Map of String of Strings into one (potentially long) string of key/value parameters (each key/value
* parameter is separated by an '=' character), with each parameter pair separated by an '&amp;' character.
* Converts a Map containing String keys and values into one (potentially long) string of key/value parameters
* (each key/value parameter is separated by an '=' character), with each parameter pair separated by an '&amp;'
* character.
* <p>
* Note specifically that this method does <strong>not</strong> URL encode the parameters.
*
* @param parameters the map of the parameters to create the query string from
* @return a concatenated query string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.springframework.context.ApplicationListener;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDbFactory;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.support.MongoRepositoryFactory;

Expand Down Expand Up @@ -123,7 +123,7 @@ public void attachListeners(ApplicationListener<?>... listeners) {
* @return a new MongoTemplate instance
*/
public static MongoTemplate initializeMongoTemplate(String mongoUri) {
var mongoDbFactory = new SimpleMongoClientDbFactory(mongoUri);
var mongoDbFactory = new SimpleMongoClientDatabaseFactory(mongoUri);

var mongoTemplate = new MongoTemplate(mongoDbFactory);
mongoTemplate.setWriteConcern(WriteConcern.ACKNOWLEDGED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.kiwiproject.junit.jupiter.MongoServerExtension;
import org.kiwiproject.spring.util.MongoTestHelpers;

@SuppressWarnings("removal")
@DisplayName("MongoClientWrapper")
class MongoClientWrapperTest {

Expand Down Expand Up @@ -49,13 +50,6 @@ void shouldHaveMongoDatabase() {
assertThat(mongoDatabase.getName()).isEqualTo(databaseName);
}

@Test
void shouldHaveDB() {
var db = clientWrapper.getDB();
assertThat(db).isNotNull();
assertThat(db.getName()).isEqualTo(databaseName);
}

@Test
void shouldCloseMongoClient() {
var mongoClient = clientWrapper.getMongoClient();
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/org/kiwiproject/net/KiwiUrlsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
Expand Down Expand Up @@ -414,6 +415,13 @@ void testQueryStringToMap_WhenMultipleUrlParameter() {
);
}

@RepeatedTest(10)
void testQueryStringToMap_OnlyReturnsOneValue_ForParametersWithMultipleValues() {
var parameters = KiwiUrls.queryStringToMap("topping=pepperoni&topping=banana+pepper&topping=sausage");

assertThat(parameters).containsExactly(entry("topping", "pepperoni"));
}

@Test
void testToQueryString_WhenNullProperties() {
assertThat(KiwiUrls.toQueryString(null)).isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.kiwiproject.time.KiwiInstants;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDbFactory;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
import org.springframework.data.mongodb.core.query.Criteria;

import java.time.Instant;
Expand All @@ -56,7 +56,7 @@ class KiwiSpringMongoQueriesTest {
@BeforeAll
static void beforeAll() {
var connectionString = MONGO_SERVER_EXTENSION.getConnectionString();
var mongoClientDbFactory = new SimpleMongoClientDbFactory(connectionString);
var mongoClientDbFactory = new SimpleMongoClientDatabaseFactory(connectionString);
mongoTemplate = new MongoTemplate(mongoClientDbFactory);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
@Slf4j
class PagingQueryEmbeddedMongoTest {

// Currently only testing against Mongo 4.0
// Currently, only testing against Mongo 4.0
private static final Version.Main MONGODB_VERSION = Version.Main.V4_0;

private static MongodExecutable mongodExecutable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.kiwiproject.search.KiwiSearching;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDbFactory;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.query.Criteria;
Expand Down Expand Up @@ -86,7 +86,7 @@ static void beforeAll() {
mongoUri, databaseName);
LOG.info("Mongo connection string: {}", connectionString);

var dbFactory = new SimpleMongoClientDbFactory(connectionString);
var dbFactory = new SimpleMongoClientDatabaseFactory(connectionString);
mongoTemplate = new MongoTemplate(dbFactory);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import de.bwaldvogel.mongo.backend.memory.MemoryBackend;
import lombok.experimental.UtilityClass;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDbFactory;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;

@UtilityClass
public class MongoTestHelpers {
Expand All @@ -24,7 +24,7 @@ public static String mongoConnectionString(MongoServer mongoServer) {

public static MongoTemplate newMongoTemplate(MongoServer mongoServer) {
var connectionString = MongoTestHelpers.mongoConnectionString(mongoServer);
var factory = new SimpleMongoClientDbFactory(connectionString);
var factory = new SimpleMongoClientDatabaseFactory(connectionString);
return new MongoTemplate(factory);
}
}