Skip to content

Commit ce666cf

Browse files
committed
test(bulk-model-sync-gradle): refactor tests
Extract side effect from test case into ChangeApplier and avoid companion objects in the tests.
1 parent c134cff commit ce666cf

File tree

4 files changed

+85
-61
lines changed

4 files changed

+85
-61
lines changed

bulk-model-sync-gradle-test/ci.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ curl -X GET --retry 30 --retry-connrefused --retry-delay 1 http://localhost:2830
3131

3232
./gradlew runSyncTestPush --console=plain --stacktrace
3333
./gradlew test --tests 'PushTest'
34+
./gradlew test --tests 'ChangeApplier'
3435
./gradlew runSyncTestPull --console=plain --stacktrace
3536
./gradlew test --tests 'PullTest'
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2024.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.modelix.model.sync.bulk.gradle.test
18+
19+
import GraphLang.L_GraphLang
20+
import GraphLang.N_Edge
21+
import GraphLang.N_Node
22+
import GraphLang._C_UntypedImpl_Edge
23+
import GraphLang._C_UntypedImpl_Node
24+
import jetbrains.mps.lang.core.L_jetbrains_mps_lang_core
25+
import kotlinx.coroutines.runBlocking
26+
import org.modelix.metamodel.TypedLanguagesRegistry
27+
import org.modelix.metamodel.typed
28+
import org.modelix.model.ModelFacade
29+
import org.modelix.model.api.ConceptReference
30+
import org.modelix.model.api.getDescendants
31+
import org.modelix.model.client2.ModelClientV2PlatformSpecificBuilder
32+
import org.modelix.model.client2.runWrite
33+
import org.modelix.model.lazy.RepositoryId
34+
import kotlin.test.Test
35+
36+
/**
37+
* Not an actual test. Just a preparation for [PullTest].
38+
* Marking it as a test makes it easily callable from the ci script.
39+
*/
40+
class ChangeApplier {
41+
42+
@Test
43+
fun applyChangesForPullTest() {
44+
val url = "http://0.0.0.0:28309/v2"
45+
val branchRef = ModelFacade.createBranchReference(RepositoryId("ci-test"), "master")
46+
val client = ModelClientV2PlatformSpecificBuilder().url(url).build().apply { runBlocking { init() } }
47+
48+
TypedLanguagesRegistry.register(L_GraphLang)
49+
TypedLanguagesRegistry.register(L_jetbrains_mps_lang_core)
50+
51+
runBlocking {
52+
client.runWrite(branchRef) { rootNode ->
53+
val graphNodes = rootNode
54+
.getDescendants(false)
55+
.filter { it.getConceptReference() == ConceptReference(_C_UntypedImpl_Node.getUID()) }
56+
.map { it.typed<N_Node>() }
57+
.toList()
58+
59+
graphNodes[0].name = "X"
60+
graphNodes[1].name = "Y"
61+
graphNodes[2].name = "Z"
62+
63+
val edges = rootNode
64+
.getDescendants(false)
65+
.filter { it.getConceptReference() == ConceptReference(_C_UntypedImpl_Edge.getUID()) }
66+
.map { it.typed<N_Edge>() }
67+
.toList()
68+
69+
edges[0].source = graphNodes[1]
70+
edges[0].target = graphNodes[3]
71+
}
72+
}
73+
}
74+
}

bulk-model-sync-gradle-test/src/test/kotlin/org/modelix/model/sync/bulk/gradle/test/PullTest.kt

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,18 @@
1616

1717
package org.modelix.model.sync.bulk.gradle.test
1818

19-
import org.junit.jupiter.api.BeforeAll
2019
import org.junit.jupiter.api.Test
20+
import org.junit.jupiter.api.TestInstance
2121
import org.xmlunit.builder.Input
2222
import org.xmlunit.xpath.JAXPXPathEngine
2323
import java.io.File
24-
import javax.xml.transform.Source
2524
import kotlin.test.assertContentEquals
2625

26+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
2727
class PullTest {
28-
companion object {
29-
private lateinit var source: Source
30-
31-
@JvmStatic
32-
@BeforeAll
33-
fun initSource() {
34-
val localModel = File("build/test-repo/solutions/GraphSolution/models/GraphSolution.example.mps").readText()
35-
source = Input.fromString(localModel).build()
36-
}
37-
}
28+
29+
private val localModel = File("build/test-repo/solutions/GraphSolution/models/GraphSolution.example.mps").readText()
30+
private val source = Input.fromString(localModel).build()
3831

3932
@Test
4033
fun `properties were synced to local`() {
Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,24 @@
11
package org.modelix.model.sync.bulk.gradle.test
22

3-
import GraphLang.L_GraphLang
4-
import GraphLang.N_Edge
5-
import GraphLang.N_Node
6-
import GraphLang._C_UntypedImpl_Edge
7-
import GraphLang._C_UntypedImpl_Node
8-
import jetbrains.mps.lang.core.L_jetbrains_mps_lang_core
93
import kotlinx.coroutines.runBlocking
104
import org.junit.jupiter.api.Test
11-
import org.modelix.metamodel.TypedLanguagesRegistry
12-
import org.modelix.metamodel.typed
5+
import org.junit.jupiter.api.TestInstance
136
import org.modelix.model.ModelFacade
14-
import org.modelix.model.api.ConceptReference
15-
import org.modelix.model.api.getDescendants
167
import org.modelix.model.api.getRootNode
17-
import org.modelix.model.client2.IModelClientV2
188
import org.modelix.model.client2.ModelClientV2PlatformSpecificBuilder
199
import org.modelix.model.client2.getReplicatedModel
20-
import org.modelix.model.client2.runWrite
2110
import org.modelix.model.data.ModelData
2211
import org.modelix.model.data.NodeData
23-
import org.modelix.model.lazy.BranchReference
2412
import org.modelix.model.lazy.RepositoryId
2513
import org.modelix.model.sync.bulk.asExported
2614
import java.io.File
2715
import kotlin.test.assertContentEquals
2816

17+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
2918
class PushTest {
19+
private val url = "http://0.0.0.0:28309/v2"
20+
private val branchRef = ModelFacade.createBranchReference(RepositoryId("ci-test"), "master")
21+
private val client = ModelClientV2PlatformSpecificBuilder().url(url).build().apply { runBlocking { init() } }
3022

3123
@Test
3224
fun `nodes were synced to server`() {
@@ -36,48 +28,12 @@ class PushTest {
3628
val modules = files.map { ModelData.fromJson(it.readText()) }
3729
val inputModel = ModelData(root = NodeData(children = modules.map { it.root }))
3830

39-
TypedLanguagesRegistry.register(L_GraphLang)
40-
TypedLanguagesRegistry.register(L_jetbrains_mps_lang_core)
41-
42-
val repoId = RepositoryId("ci-test")
43-
val branchName = "master"
44-
val url = "http://0.0.0.0:28309/v2"
45-
46-
val branchRef = ModelFacade.createBranchReference(repoId, branchName)
47-
val client = ModelClientV2PlatformSpecificBuilder().url(url).build().apply { runBlocking { init() } }
4831
val replicatedModel = client.getReplicatedModel(branchRef)
4932
val branch = runBlocking { replicatedModel.start() }
5033

5134
branch.runRead {
5235
assertContentEquals(inputModel.root.children, branch.getRootNode().allChildren.map { it.asExported() })
5336
}
5437
replicatedModel.dispose()
55-
56-
applyChangesForPullTest(client, branchRef)
57-
}
58-
59-
private fun applyChangesForPullTest(client: IModelClientV2, branchRef: BranchReference) {
60-
runBlocking {
61-
client.runWrite(branchRef) { rootNode ->
62-
val graphNodes = rootNode
63-
.getDescendants(false)
64-
.filter { it.getConceptReference() == ConceptReference(_C_UntypedImpl_Node.getUID()) }
65-
.map { it.typed<N_Node>() }
66-
.toList()
67-
68-
graphNodes[0].name = "X"
69-
graphNodes[1].name = "Y"
70-
graphNodes[2].name = "Z"
71-
72-
val edges = rootNode
73-
.getDescendants(false)
74-
.filter { it.getConceptReference() == ConceptReference(_C_UntypedImpl_Edge.getUID()) }
75-
.map { it.typed<N_Edge>() }
76-
.toList()
77-
78-
edges[0].source = graphNodes[1]
79-
edges[0].target = graphNodes[3]
80-
}
81-
}
8238
}
8339
}

0 commit comments

Comments
 (0)