|
16 | 16 |
|
17 | 17 | package org.modelix.model.sync.bulk
|
18 | 18 |
|
| 19 | +import org.modelix.model.ModelFacade |
19 | 20 | import org.modelix.model.api.IBranch
|
20 | 21 | import org.modelix.model.api.INode
|
21 | 22 | import org.modelix.model.api.IProperty
|
22 | 23 | import org.modelix.model.api.PBranch
|
23 | 24 | import org.modelix.model.api.PNodeAdapter
|
| 25 | +import org.modelix.model.api.addNewChild |
24 | 26 | import org.modelix.model.api.getDescendants
|
25 | 27 | import org.modelix.model.api.getRootNode
|
26 | 28 | import org.modelix.model.client.IdGenerator
|
27 | 29 | import org.modelix.model.data.ModelData
|
28 | 30 | import org.modelix.model.data.NodeData.Companion.ID_PROPERTY_KEY
|
29 | 31 | import org.modelix.model.lazy.CLTree
|
30 | 32 | import org.modelix.model.lazy.ObjectStoreCache
|
| 33 | +import org.modelix.model.operations.AddNewChildOp |
| 34 | +import org.modelix.model.operations.AddNewChildrenOp |
31 | 35 | import org.modelix.model.operations.OTBranch
|
32 | 36 | import org.modelix.model.persistent.MapBasedStore
|
33 | 37 | import org.modelix.model.test.RandomModelChangeGenerator
|
| 38 | +import kotlin.js.JsName |
34 | 39 | import kotlin.random.Random
|
| 40 | +import kotlin.test.Test |
| 41 | +import kotlin.test.assertEquals |
35 | 42 | import kotlin.test.assertTrue
|
36 | 43 |
|
37 |
| -class ModelSynchronizerTest : AbstractModelSyncTest() { |
| 44 | +open class ModelSynchronizerTest : AbstractModelSyncTest() { |
| 45 | + |
| 46 | + @Test |
| 47 | + @JsName("can_handle_added_child_without_original_id_without_existing_sibling") |
| 48 | + fun `can handle added child without original id (without existing sibling)`() { |
| 49 | + val sourceBranch = createLocalBranch().apply { |
| 50 | + runWrite { |
| 51 | + getRootNode().apply { |
| 52 | + setPropertyValue(ID_PROPERTY_KEY, "root") |
| 53 | + addNewChild("test") |
| 54 | + } |
| 55 | + } |
| 56 | + }.toOTBranch() |
| 57 | + |
| 58 | + val targetBranch = createLocalBranch().apply { |
| 59 | + runWrite { |
| 60 | + getRootNode().setPropertyValue(ID_PROPERTY_KEY, "root") |
| 61 | + } |
| 62 | + }.toOTBranch() |
| 63 | + |
| 64 | + runTest(sourceBranch, targetBranch) { |
| 65 | + assertEquals(1, targetBranch.getRootNode().allChildren.count()) |
| 66 | + assertEquals(1, targetBranch.getNumOfUsedOperationsByType()[AddNewChildrenOp::class]) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + @JsName("can_handle_added_child_without_original_id_with_existing_sibling") |
| 72 | + fun `can handle added child without original id (with existing sibling)`() { |
| 73 | + val sourceBranch = createLocalBranch().apply { |
| 74 | + runWrite { |
| 75 | + getRootNode().apply { |
| 76 | + setPropertyValue(ID_PROPERTY_KEY, "root") |
| 77 | + addNewChild("test").setPropertyValue(ID_PROPERTY_KEY, "sibling") |
| 78 | + addNewChild("test") |
| 79 | + } |
| 80 | + } |
| 81 | + }.toOTBranch() |
| 82 | + |
| 83 | + val targetBranch = createLocalBranch().apply { |
| 84 | + runWrite { |
| 85 | + getRootNode().apply { |
| 86 | + setPropertyValue(ID_PROPERTY_KEY, "root") |
| 87 | + addNewChild("test").setPropertyValue(ID_PROPERTY_KEY, "sibling") |
| 88 | + } |
| 89 | + } |
| 90 | + }.toOTBranch() |
| 91 | + |
| 92 | + runTest(sourceBranch, targetBranch) { |
| 93 | + assertEquals(2, targetBranch.getRootNode().allChildren.count()) |
| 94 | + assertEquals(1, targetBranch.getNumOfUsedOperationsByType()[AddNewChildOp::class]) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private fun createLocalBranch() = ModelFacade.toLocalBranch(ModelFacade.newLocalTree()) |
38 | 99 |
|
39 | 100 | override fun runTest(initialData: ModelData, expectedData: ModelData, assertions: OTBranch.() -> Unit) {
|
40 | 101 | val sourceBranch = createOTBranchFromModel(expectedData)
|
41 | 102 | val targetBranch = createOTBranchFromModel(initialData)
|
| 103 | + runTest(sourceBranch, targetBranch, assertions) |
| 104 | + } |
42 | 105 |
|
| 106 | + private fun runTest(sourceBranch: OTBranch, targetBranch: OTBranch, assertions: OTBranch.() -> Unit) { |
43 | 107 | sourceBranch.runRead {
|
44 | 108 | val sourceRoot = sourceBranch.getRootNode()
|
45 | 109 |
|
@@ -138,3 +202,7 @@ class ModelSynchronizerTest : AbstractModelSyncTest() {
|
138 | 202 | }
|
139 | 203 | }
|
140 | 204 | }
|
| 205 | + |
| 206 | +private fun IBranch.toOTBranch(): OTBranch { |
| 207 | + return OTBranch(this, IdGenerator.getInstance(1), ObjectStoreCache(MapBasedStore())) |
| 208 | +} |
0 commit comments