|
| 1 | +package org.testcontainers.mongodb; |
| 2 | + |
| 3 | +import com.mongodb.ConnectionString; |
| 4 | +import com.mongodb.MongoClientSettings; |
| 5 | +import com.mongodb.client.ListSearchIndexesIterable; |
| 6 | +import com.mongodb.client.MongoClient; |
| 7 | +import com.mongodb.client.MongoClients; |
| 8 | +import com.mongodb.client.MongoCollection; |
| 9 | +import com.mongodb.client.MongoDatabase; |
| 10 | +import com.mongodb.client.model.Aggregates; |
| 11 | +import com.mongodb.client.model.search.SearchOperator; |
| 12 | +import com.mongodb.client.model.search.SearchOptions; |
| 13 | +import com.mongodb.client.model.search.SearchPath; |
| 14 | +import org.bson.BsonDocument; |
| 15 | +import org.bson.Document; |
| 16 | +import org.bson.codecs.configuration.CodecRegistries; |
| 17 | +import org.bson.codecs.configuration.CodecRegistry; |
| 18 | +import org.bson.codecs.pojo.PojoCodecProvider; |
| 19 | +import org.bson.conversions.Bson; |
| 20 | +import org.bson.json.JsonWriterSettings; |
| 21 | +import org.slf4j.Logger; |
| 22 | +import org.slf4j.LoggerFactory; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.net.URISyntaxException; |
| 26 | +import java.nio.charset.StandardCharsets; |
| 27 | +import java.nio.file.Files; |
| 28 | +import java.nio.file.Paths; |
| 29 | +import java.time.Instant; |
| 30 | +import java.time.temporal.ChronoUnit; |
| 31 | +import java.util.Collections; |
| 32 | +import java.util.concurrent.TimeUnit; |
| 33 | + |
| 34 | +import static org.awaitility.Awaitility.await; |
| 35 | + |
| 36 | +public class AtlasLocalDataAccess implements AutoCloseable { |
| 37 | + |
| 38 | + private static final Logger log = LoggerFactory.getLogger(AtlasLocalDataAccess.class); |
| 39 | + |
| 40 | + private final MongoClient mongoClient; |
| 41 | + |
| 42 | + private final MongoDatabase testDB; |
| 43 | + |
| 44 | + private final MongoCollection<TestData> testCollection; |
| 45 | + |
| 46 | + private final String collectionName; |
| 47 | + |
| 48 | + public AtlasLocalDataAccess(String connectionString, String databaseName, String collectionName) { |
| 49 | + this.collectionName = collectionName; |
| 50 | + log.info("DataAccess connecting to {}", connectionString); |
| 51 | + |
| 52 | + CodecRegistry pojoCodecRegistry = CodecRegistries.fromProviders( |
| 53 | + PojoCodecProvider.builder().automatic(true).build() |
| 54 | + ); |
| 55 | + CodecRegistry codecRegistry = CodecRegistries.fromRegistries( |
| 56 | + MongoClientSettings.getDefaultCodecRegistry(), |
| 57 | + pojoCodecRegistry |
| 58 | + ); |
| 59 | + MongoClientSettings clientSettings = MongoClientSettings |
| 60 | + .builder() |
| 61 | + .applyConnectionString(new ConnectionString(connectionString)) |
| 62 | + .codecRegistry(codecRegistry) |
| 63 | + .build(); |
| 64 | + mongoClient = MongoClients.create(clientSettings); |
| 65 | + testDB = mongoClient.getDatabase(databaseName); |
| 66 | + testCollection = testDB.getCollection(collectionName, TestData.class); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void close() { |
| 71 | + mongoClient.close(); |
| 72 | + } |
| 73 | + |
| 74 | + public void initAtlasSearchIndex() throws URISyntaxException, IOException, InterruptedException { |
| 75 | + //Create the collection (if it doesn't exist). Required because unlike other database operations, createSearchIndex will fail if the collection doesn't exist yet |
| 76 | + testDB.createCollection(collectionName); |
| 77 | + |
| 78 | + //Read the atlas search index JSON from a resource file |
| 79 | + String atlasSearchIndexJson = new String( |
| 80 | + Files.readAllBytes(Paths.get(getClass().getResource("/atlas-local-index.json").toURI())), |
| 81 | + StandardCharsets.UTF_8 |
| 82 | + ); |
| 83 | + log.info( |
| 84 | + "Creating Atlas Search index AtlasSearchIndex on collection {}:\n{}", |
| 85 | + collectionName, |
| 86 | + atlasSearchIndexJson |
| 87 | + ); |
| 88 | + testCollection.createSearchIndex("AtlasSearchIndex", BsonDocument.parse(atlasSearchIndexJson)); |
| 89 | + |
| 90 | + //wait for the atlas search index to be ready |
| 91 | + Instant start = Instant.now(); |
| 92 | + await() |
| 93 | + .atMost(5, TimeUnit.SECONDS) |
| 94 | + .pollInterval(10, TimeUnit.MILLISECONDS) |
| 95 | + .pollInSameThread() |
| 96 | + .until(this::getIndexStatus, "READY"::equalsIgnoreCase); |
| 97 | + |
| 98 | + log.info( |
| 99 | + "Atlas Search index AtlasSearchIndex on collection {} is ready (took {} milliseconds) to create.", |
| 100 | + collectionName, |
| 101 | + start.until(Instant.now(), ChronoUnit.MILLIS) |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + private String getIndexStatus() { |
| 106 | + ListSearchIndexesIterable<Document> searchIndexes = testCollection.listSearchIndexes(); |
| 107 | + for (Document searchIndex : searchIndexes) { |
| 108 | + if (searchIndex.get("name").equals("AtlasSearchIndex")) { |
| 109 | + return searchIndex.getString("status"); |
| 110 | + } |
| 111 | + } |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + public void insertData(TestData data) { |
| 116 | + log.info("Inserting document {}", data); |
| 117 | + testCollection.insertOne(data); |
| 118 | + } |
| 119 | + |
| 120 | + public TestData findAtlasSearch(String test) { |
| 121 | + Bson searchClause = Aggregates.search( |
| 122 | + SearchOperator.of(SearchOperator.text(SearchPath.fieldPath("test"), test).fuzzy()), |
| 123 | + SearchOptions.searchOptions().index("AtlasSearchIndex") |
| 124 | + ); |
| 125 | + log.trace( |
| 126 | + "Searching for document using Atlas Search:\n{}", |
| 127 | + searchClause.toBsonDocument().toJson(JsonWriterSettings.builder().indent(true).build()) |
| 128 | + ); |
| 129 | + return testCollection.aggregate(Collections.singletonList(searchClause)).first(); |
| 130 | + } |
| 131 | + |
| 132 | + public static class TestData { |
| 133 | + |
| 134 | + String test; |
| 135 | + |
| 136 | + int test2; |
| 137 | + |
| 138 | + boolean test3; |
| 139 | + |
| 140 | + public TestData() {} |
| 141 | + |
| 142 | + public TestData(String test, int test2, boolean test3) { |
| 143 | + this.test = test; |
| 144 | + this.test2 = test2; |
| 145 | + this.test3 = test3; |
| 146 | + } |
| 147 | + |
| 148 | + public String getTest() { |
| 149 | + return test; |
| 150 | + } |
| 151 | + |
| 152 | + public void setTest(String test) { |
| 153 | + this.test = test; |
| 154 | + } |
| 155 | + |
| 156 | + public int getTest2() { |
| 157 | + return test2; |
| 158 | + } |
| 159 | + |
| 160 | + public void setTest2(int test2) { |
| 161 | + this.test2 = test2; |
| 162 | + } |
| 163 | + |
| 164 | + public boolean isTest3() { |
| 165 | + return test3; |
| 166 | + } |
| 167 | + |
| 168 | + public void setTest3(boolean test3) { |
| 169 | + this.test3 = test3; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public String toString() { |
| 174 | + return "TestData{" + "test='" + test + '\'' + ", test2=" + test2 + ", test3=" + test3 + '}'; |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments