Skip to content

Commit

Permalink
fix MongoStartUpTest.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiangs18 committed Feb 17, 2024
1 parent c26599f commit 58f2b25
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/us/kbase/workspace/test/database/mongo/MongoStartUpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,18 @@ private void createIndexes(final String dbname) throws Exception {
method.invoke(null, wsdb);
}

private Set<Document> getIndexes(final MongoDatabase db, final String collection) {
private Set<Document> getIndexes(final MongoDatabase db, final String collectionName) {
final Set<Document> indexes = new HashSet<>();
db.getCollection(collection).listIndexes().forEach((Consumer<Document>) indexes::add);
for (Document index: db.getCollection(collectionName).listIndexes()) {
// In MongoDB 4.4, the listIndexes and the mongo shell helper method db.collection.getIndexes()
// no longer returns the namespace ns field in the index specification documents.
index.remove("ns");
// some versions of Mongo return ints, some longs. Convert all to longs.
if (index.containsKey("expireAfterSeconds")) {
index.put("expireAfterSeconds", ((Number) index.get("expireAfterSeconds")).longValue());
}
indexes.add(index);
}
return indexes;
}

Expand All @@ -313,18 +322,15 @@ public void indexesConfig() throws Exception {
new Document("v", INDEX_VER)
.append("unique", true)
.append("key", new Document("config", 1))
.append("name", "config_1")
.append("ns", "MongoStartUpTest.config"),
.append("name", "config_1"),
new Document("v", INDEX_VER)
.append("key", new Document("_id", 1))
.append("name", "_id_")
.append("ns", "MongoStartUpTest.config")
);
assertThat("incorrect indexes", getIndexes(db, "config"), is(expectedIndexes));

final MongoDatabase wsdb = mongoClient.getDatabase("indexesConfig");
createIndexes("indexesConfig");
setNamespace(expectedIndexes, "indexesConfig.config");
assertThat("incorrect indexes", getIndexes(wsdb, "config"), is(expectedIndexes));
}

Expand All @@ -334,18 +340,14 @@ public void indexesDynamicConfig() throws Exception {
new Document("v", INDEX_VER)
.append("unique", true)
.append("key", new Document("key", 1))
.append("name", "key_1")
.append("ns", "MongoStartUpTest.dyncfg"),
.append("name", "key_1"),
new Document("v", INDEX_VER)
.append("key", new Document("_id", 1))
.append("name", "_id_")
.append("ns", "MongoStartUpTest.dyncfg")
);
assertThat("incorrect indexes", getIndexes(db, "dyncfg"), is(expectedIndexes));

final MongoDatabase wsdb = mongoClient.getDatabase("indexesDynConfig");
createIndexes("indexesDynConfig");
setNamespace(expectedIndexes, "indexesDynConfig.dyncfg");
assertThat("incorrect indexes", getIndexes(wsdb, "dyncfg"), is(expectedIndexes));
}

Expand Down

0 comments on commit 58f2b25

Please sign in to comment.