|
| 1 | + |
| 2 | +import com.mongodb.client.model.SearchIndexModel |
| 3 | +import com.mongodb.kotlin.client.coroutine.MongoClient |
| 4 | +import config.getConfig |
| 5 | +import kotlinx.coroutines.flow.toList |
| 6 | +import kotlinx.coroutines.runBlocking |
| 7 | +import org.bson.Document |
| 8 | +import org.junit.jupiter.api.AfterAll |
| 9 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 10 | +import org.junit.jupiter.api.Test |
| 11 | +import org.junit.jupiter.api.TestInstance |
| 12 | +import kotlin.test.Ignore |
| 13 | +import kotlin.test.assertFalse |
| 14 | + |
| 15 | +// :replace-start: { |
| 16 | +// "terms": { |
| 17 | +// "CONNECTION_URI_PLACEHOLDER": "\"<connection string>\"" |
| 18 | +// } |
| 19 | +// } |
| 20 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 21 | +class SearchIndexesTest { |
| 22 | + |
| 23 | + companion object { |
| 24 | + private val config = getConfig() |
| 25 | + private val CONNECTION_URI_PLACEHOLDER = config.connectionUri |
| 26 | + |
| 27 | + val mongoClient = MongoClient.create(CONNECTION_URI_PLACEHOLDER) |
| 28 | + val database = mongoClient.getDatabase("sample_mflix") |
| 29 | + val moviesCollection = database.getCollection<Document>("movies") |
| 30 | + |
| 31 | + @AfterAll |
| 32 | + @JvmStatic |
| 33 | + fun afterAll() { |
| 34 | + runBlocking { |
| 35 | + moviesCollection.drop() |
| 36 | + } |
| 37 | + mongoClient.close() |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Ignore |
| 42 | + @Test |
| 43 | + fun singleSearchIndexTest() = runBlocking { |
| 44 | + // :snippet-start: single-search-index-create |
| 45 | + val index = Document( |
| 46 | + "mappings", |
| 47 | + Document("dynamic", true) |
| 48 | + ) |
| 49 | + val resultCreateIndex = moviesCollection.createSearchIndex("myIndex", index) |
| 50 | + // :snippet-end: |
| 51 | + println("Index created: $resultCreateIndex") |
| 52 | + assertEquals("myIndex", resultCreateIndex) |
| 53 | + } |
| 54 | + |
| 55 | + @Ignore |
| 56 | + @Test |
| 57 | + fun multipleSearchIndexTest() = runBlocking { |
| 58 | + // :snippet-start: multi-search-index-create |
| 59 | + val indexOne = SearchIndexModel( |
| 60 | + "myIndex1", |
| 61 | + Document("analyzer", "lucene.standard").append( |
| 62 | + "mappings", Document("dynamic", true) |
| 63 | + ) |
| 64 | + ) |
| 65 | + |
| 66 | + val indexTwo = SearchIndexModel( |
| 67 | + "myIndex2", |
| 68 | + Document("analyzer", "lucene.simple").append( |
| 69 | + "mappings", Document("dynamic", true) |
| 70 | + ) |
| 71 | + ) |
| 72 | + |
| 73 | + val resultCreateIndexes = moviesCollection |
| 74 | + .createSearchIndexes(listOf(indexOne, indexTwo)) |
| 75 | + // :snippet-end: |
| 76 | + assertEquals(listOf("myIndex1", "myIndex2"), resultCreateIndexes.toList()) |
| 77 | + } |
| 78 | + |
| 79 | + @Ignore |
| 80 | + @Test |
| 81 | + fun listSearchIndexTest() = runBlocking { |
| 82 | + // :snippet-start: list-search-indexes |
| 83 | + val searchIndexesList = moviesCollection.listSearchIndexes().toList() |
| 84 | + // :snippet-end: |
| 85 | + |
| 86 | + assertFalse(searchIndexesList.isEmpty()) |
| 87 | + } |
| 88 | + |
| 89 | + @Ignore |
| 90 | + @Test |
| 91 | + fun updateSearchIndexTest() = runBlocking { |
| 92 | + // :snippet-start: update-search-indexes |
| 93 | + moviesCollection.updateSearchIndex( |
| 94 | + "myIndex", |
| 95 | + Document("analyzer", "lucene.simple").append( |
| 96 | + "mappings", |
| 97 | + Document("dynamic", false) |
| 98 | + .append( |
| 99 | + "fields", |
| 100 | + Document( |
| 101 | + "title", |
| 102 | + Document("type", "string") |
| 103 | + ) |
| 104 | + ) |
| 105 | + ) |
| 106 | + ) |
| 107 | + // :snippet-end: |
| 108 | + } |
| 109 | + |
| 110 | + @Ignore |
| 111 | + @Test |
| 112 | + fun dropSearchIndexTest() = runBlocking { |
| 113 | + // :snippet-start: drop-search-index |
| 114 | + moviesCollection.dropSearchIndex("myIndex"); |
| 115 | + // :snippet-end: |
| 116 | + } |
| 117 | + |
| 118 | +} |
| 119 | +// :replace-end: |
0 commit comments