|
| 1 | + |
| 2 | +import com.mongodb.client.model.Filters |
| 3 | +import com.mongodb.kotlin.client.coroutine.MongoClient |
| 4 | +import io.github.cdimascio.dotenv.dotenv |
| 5 | +import kotlinx.coroutines.flow.count |
| 6 | +import kotlinx.coroutines.flow.firstOrNull |
| 7 | +import kotlinx.coroutines.runBlocking |
| 8 | +import org.bson.BsonType |
| 9 | +import org.bson.codecs.pojo.annotations.BsonId |
| 10 | +import org.bson.codecs.pojo.annotations.BsonProperty |
| 11 | +import org.bson.codecs.pojo.annotations.BsonRepresentation |
| 12 | +import org.bson.types.ObjectId |
| 13 | +import org.junit.jupiter.api.AfterAll |
| 14 | +import org.junit.jupiter.api.AfterEach |
| 15 | +import org.junit.jupiter.api.Assertions.* |
| 16 | +import org.junit.jupiter.api.TestInstance |
| 17 | +import java.util.* |
| 18 | +import kotlin.test.* |
| 19 | + |
| 20 | + |
| 21 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 22 | +internal class DataClassTest { |
| 23 | + |
| 24 | + companion object { |
| 25 | + val dotenv = dotenv() |
| 26 | + val client = MongoClient.create(dotenv["MONGODB_CONNECTION_URI"]) |
| 27 | + val database = client.getDatabase("storage_store") |
| 28 | + |
| 29 | + @AfterAll |
| 30 | + @JvmStatic |
| 31 | + fun afterAll() { |
| 32 | + runBlocking { |
| 33 | + client.close() |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + @AfterEach |
| 39 | + fun afterEach() { |
| 40 | + runBlocking { |
| 41 | + database.drop() |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // :snippet-start: data-class |
| 46 | + data class DataStorage(val productName: String, val capacity: Double) |
| 47 | + // :snippet-end: |
| 48 | + |
| 49 | + @Test |
| 50 | + fun insertDataClassTest() = runBlocking { |
| 51 | + // :snippet-start: insert-data-class |
| 52 | + val collection = database.getCollection<DataStorage>("data_storage") |
| 53 | + val record = DataStorage("tape", 5.0) |
| 54 | + collection.insertOne(record) |
| 55 | + // :snippet-end: |
| 56 | + val result = collection.find().firstOrNull() |
| 57 | + assertEquals(record, result) |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + fun retrieveDataClassTest() = runBlocking { |
| 62 | + // :snippet-start: retrieve-data-class |
| 63 | + val collection = database.getCollection<DataStorage>("data_storage_devices") |
| 64 | + // :remove-start: |
| 65 | + val tape = DataStorage("tape", 5.0) |
| 66 | + collection.insertOne(tape) |
| 67 | + // :remove-end: |
| 68 | + |
| 69 | + // Retrieve and print the documents as data classes |
| 70 | + val resultsFlow = collection.find() |
| 71 | + resultsFlow.collect { println(it) } |
| 72 | + // :snippet-end: |
| 73 | + assertEquals(tape, resultsFlow.firstOrNull()) |
| 74 | + } |
| 75 | + |
| 76 | + // :snippet-start: annotated-data-class |
| 77 | + data class NetworkDevice( |
| 78 | + @BsonId |
| 79 | + @BsonRepresentation(BsonType.OBJECT_ID) |
| 80 | + val deviceId: String, |
| 81 | + val name: String, |
| 82 | + @BsonProperty("type") |
| 83 | + val deviceType: String |
| 84 | + ) |
| 85 | + // :snippet-end: |
| 86 | + |
| 87 | + @Test |
| 88 | + fun insertAnnotatedDataClassTest() = runBlocking { |
| 89 | + // :snippet-start: insert-annotated-data-class |
| 90 | + val collection = database.getCollection<NetworkDevice>("network_devices") |
| 91 | + |
| 92 | + // Insert the record |
| 93 | + val deviceId = ObjectId().toHexString() |
| 94 | + val device = NetworkDevice(deviceId, "Enterprise Wi-fi", "router") |
| 95 | + collection.insertOne(device) |
| 96 | + // :snippet-end: |
| 97 | + val result = collection.find().firstOrNull() |
| 98 | + assertEquals(device, result) |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + fun retrieveAnnotatedDataClassTest() = runBlocking { |
| 103 | + // :snippet-start: retrieve-annotated-data-class |
| 104 | + val collection = database.getCollection<NetworkDevice>("network_devices") |
| 105 | + // :remove-start: |
| 106 | + val deviceId = ObjectId().toHexString() |
| 107 | + val device = NetworkDevice(deviceId, "Enterprise Wi-fi", "router") |
| 108 | + collection.insertOne(device) |
| 109 | + // :remove-end: |
| 110 | + |
| 111 | + // Return all documents in the collection as data classes |
| 112 | + val resultsFlow = collection.find() |
| 113 | + resultsFlow.collect { println(it) } |
| 114 | + // :snippet-end: |
| 115 | + assertEquals(device, resultsFlow.firstOrNull()) |
| 116 | + assertEquals(1, resultsFlow.count()) |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + fun recursiveDataClassTypesTest() = runBlocking { |
| 121 | + // :snippet-start: recursive-data-class-types |
| 122 | + data class DataClassTree( |
| 123 | + val content: String, |
| 124 | + val left: DataClassTree?, |
| 125 | + val right: DataClassTree? |
| 126 | + ) |
| 127 | + // :snippet-end: |
| 128 | + // :snippet-start: recursive-data-class-types-retrieve |
| 129 | + val collection = database.getCollection<DataClassTree>("myCollection") |
| 130 | + // :remove-start: |
| 131 | + val exampleTree = DataClassTree( |
| 132 | + "indo-european", |
| 133 | + DataClassTree( |
| 134 | + "germanic", |
| 135 | + DataClassTree("german", null, DataClassTree("high german", null, null)), |
| 136 | + DataClassTree( |
| 137 | + "norse", DataClassTree("swedish", null, null), |
| 138 | + DataClassTree("danish", null, null) |
| 139 | + ) |
| 140 | + ), |
| 141 | + DataClassTree( |
| 142 | + "romance", |
| 143 | + DataClassTree("spanish", null, null), |
| 144 | + DataClassTree("french", null, null) |
| 145 | + ) |
| 146 | + ) |
| 147 | + collection.insertOne(exampleTree) |
| 148 | + // :remove-end: |
| 149 | + |
| 150 | + val filter = Filters.eq("left.left.right.content", "high german") |
| 151 | + val resultsFlow = collection.find(filter) |
| 152 | + resultsFlow.collect { println(it) } |
| 153 | + // :snippet-end: |
| 154 | + assertEquals(exampleTree, resultsFlow.firstOrNull()) |
| 155 | + } |
| 156 | +} |
0 commit comments