From 850d0850822e6914d8476c911bbbc28a8b69d671 Mon Sep 17 00:00:00 2001 From: Sijie Date: Tue, 20 Feb 2024 10:06:35 -0800 Subject: [PATCH] fix SchemaUpdaterTest --- .../workspace/test/WorkspaceMongoIndex.java | 25 +++++++++++++++++++ .../test/database/mongo/MongoStartUpTest.java | 16 +----------- .../database/mongo/SchemaUpdaterTest.java | 12 +++------ 3 files changed, 30 insertions(+), 23 deletions(-) create mode 100644 src/us/kbase/workspace/test/WorkspaceMongoIndex.java diff --git a/src/us/kbase/workspace/test/WorkspaceMongoIndex.java b/src/us/kbase/workspace/test/WorkspaceMongoIndex.java new file mode 100644 index 00000000..28b50321 --- /dev/null +++ b/src/us/kbase/workspace/test/WorkspaceMongoIndex.java @@ -0,0 +1,25 @@ +package us.kbase.workspace.test; + +import com.mongodb.client.MongoDatabase; +import org.bson.Document; + +import java.util.HashSet; +import java.util.Set; + +public class WorkspaceMongoIndex { + + public static Set getAndNormalizeIndexes(final MongoDatabase db, final String collectionName) { + final Set indexes = new HashSet<>(); + for (Document index: db.getCollection(collectionName).listIndexes()) { + // In MongoDB 4.4, the listIndexes and the mongo shell helper method db.collection.getAndNormalizeIndexes() + // 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; + } +} diff --git a/src/us/kbase/workspace/test/database/mongo/MongoStartUpTest.java b/src/us/kbase/workspace/test/database/mongo/MongoStartUpTest.java index 71e647e4..eb038694 100644 --- a/src/us/kbase/workspace/test/database/mongo/MongoStartUpTest.java +++ b/src/us/kbase/workspace/test/database/mongo/MongoStartUpTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.fail; import static us.kbase.common.test.TestCommon.assertExceptionCorrect; import static us.kbase.common.test.TestCommon.set; +import static us.kbase.workspace.test.WorkspaceMongoIndex.getAndNormalizeIndexes; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -297,21 +298,6 @@ private void createIndexes(final String dbname) throws Exception { method.invoke(null, wsdb); } - private Set getAndNormalizeIndexes(final MongoDatabase db, final String collectionName) { - final Set indexes = new HashSet<>(); - for (Document index: db.getCollection(collectionName).listIndexes()) { - // In MongoDB 4.4, the listIndexes and the mongo shell helper method db.collection.getAndNormalizeIndexes() - // 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; - } - @Test public void indexesConfig() throws Exception { final Set expectedIndexes = set( diff --git a/src/us/kbase/workspace/test/database/mongo/SchemaUpdaterTest.java b/src/us/kbase/workspace/test/database/mongo/SchemaUpdaterTest.java index 3d84d984..5fec5968 100644 --- a/src/us/kbase/workspace/test/database/mongo/SchemaUpdaterTest.java +++ b/src/us/kbase/workspace/test/database/mongo/SchemaUpdaterTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.fail; import static org.mockito.Mockito.when; import static us.kbase.workspace.test.WorkspaceTestCommon.basicProv; +import static us.kbase.workspace.test.WorkspaceMongoIndex.getAndNormalizeIndexes; import java.nio.file.Paths; import java.time.Instant; @@ -256,23 +257,18 @@ private void assertIndexesCreated(final MongoDatabase db, final boolean oldTypeI * MongoWorkspaceDB method is already thoroughly tested elsewhere we just check a single * index. */ - final String ns = db.getName() + "." + COL_WS_VER; final Document currentIndex = new Document("v", 2) .append("key", new Document("tyname", 1) .append("tymaj", 1).append("tymin", 1) .append("ws", 1).append("id", 1).append("ver", -1)) - .append("name", "tyname_1_tymaj_1_tymin_1_ws_1_id_1_ver_-1") - .append("ns", ns); - final Set indexes = new HashSet<>(); - db.getCollection("workspaceObjVersions").listIndexes() - .forEach((Consumer) indexes::add); + .append("name", "tyname_1_tymaj_1_tymin_1_ws_1_id_1_ver_-1"); + final Set indexes = getAndNormalizeIndexes(db, "workspaceObjVersions"); assertThat("incorrect current index", indexes.contains(currentIndex), is(true)); if (oldTypeIndex) { final Document typeIndex = new Document("v", 2) .append("key", new Document("type", 1).append("chksum", 1)) - .append("name", "type_1_chksum_1") - .append("ns", ns); + .append("name", "type_1_chksum_1"); assertThat("incorrect old index", indexes.contains(typeIndex), is(true)); } }