Skip to content

Commit d8363c1

Browse files
authored
Merge pull request #394 from modelix/issue/MODELIX-678
MODELIX-678 model-server content explorer raises an error when trying to unfold the root node of an empty repository
2 parents 56d2195 + 39d18c1 commit d8363c1

File tree

2 files changed

+90
-25
lines changed

2 files changed

+90
-25
lines changed

model-server/src/main/kotlin/org/modelix/model/server/handlers/ContentExplorer.kt

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import kotlinx.html.tr
3737
import kotlinx.html.ul
3838
import kotlinx.html.unsafe
3939
import org.modelix.api.html.Paths
40-
import org.modelix.model.ModelFacade
4140
import org.modelix.model.api.BuiltinLanguages
4241
import org.modelix.model.api.INodeResolutionScope
4342
import org.modelix.model.api.ITree
@@ -50,19 +49,6 @@ import kotlin.collections.set
5049

5150
class ContentExplorer(private val client: IModelClient, private val repoManager: RepositoriesManager) {
5251

53-
private val rootNodes: List<PNodeAdapter>
54-
get() {
55-
val nodeList = mutableListOf<PNodeAdapter>()
56-
57-
for (repoId in repoManager.getRepositories()) {
58-
val branchRef = repoId.getBranchReference()
59-
val version = ModelFacade.loadCurrentVersion(client, branchRef) ?: continue
60-
val rootNode = PNodeAdapter(ITree.ROOT_ID, TreePointer(version.getTree()))
61-
nodeList.add(rootNode)
62-
}
63-
return nodeList
64-
}
65-
6652
fun init(application: Application) {
6753
application.routing {
6854
get<Paths.getContent> {
@@ -112,19 +98,24 @@ class ContentExplorer(private val client: IModelClient, private val repoManager:
11298
)
11399
}
114100
get<Paths.getNodeIdForVersionHash> {
115-
val id = call.parameters["nodeId"]!!.toLong()
116-
var found: PNodeAdapter? = null
117-
for (node in rootNodes) {
118-
val candidate = PNodeAdapter(id, node.branch).takeIf { it.isValid }
119-
if (candidate != null) {
120-
found = candidate
121-
break
122-
}
101+
val id = call.parameters["nodeId"]?.toLongOrNull()
102+
?: return@get call.respondText("node id not found", status = HttpStatusCode.NotFound)
103+
104+
val versionHash = call.parameters["versionHash"]
105+
?: return@get call.respondText("version hash not found", status = HttpStatusCode.NotFound)
106+
107+
val version = try {
108+
CLVersion.loadFromHash(versionHash, client.storeCache)
109+
} catch (ex: RuntimeException) {
110+
return@get call.respondText("version not found", status = HttpStatusCode.NotFound)
123111
}
124-
if (found == null) {
125-
call.respondText("node id not found", status = HttpStatusCode.NotFound)
112+
113+
val node = PNodeAdapter(id, TreePointer(version.getTree())).takeIf { it.isValid }
114+
115+
if (node != null) {
116+
call.respondHtml { body { nodeInspector(node) } }
126117
} else {
127-
call.respondHtml { body { nodeInspector(found) } }
118+
call.respondText("node id not found", status = HttpStatusCode.NotFound)
128119
}
129120
}
130121
}
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.server.handlers
18+
19+
import io.ktor.client.call.body
20+
import io.ktor.client.request.get
21+
import io.ktor.client.request.post
22+
import io.ktor.serialization.kotlinx.json.json
23+
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
24+
import io.ktor.server.resources.Resources
25+
import io.ktor.server.routing.IgnoreTrailingSlash
26+
import io.ktor.server.testing.ApplicationTestBuilder
27+
import io.ktor.server.testing.testApplication
28+
import io.ktor.server.websocket.WebSockets
29+
import org.modelix.model.client.successful
30+
import org.modelix.model.lazy.CLVersion
31+
import org.modelix.model.server.api.v2.VersionDelta
32+
import org.modelix.model.server.store.InMemoryStoreClient
33+
import org.modelix.model.server.store.LocalModelClient
34+
import kotlin.test.Test
35+
import kotlin.test.assertTrue
36+
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation as ClientContentNegotiation
37+
38+
class ContentExplorerTest {
39+
40+
private val repoId = "test-repo"
41+
private val modelClient = LocalModelClient(InMemoryStoreClient())
42+
private val repoManager = RepositoriesManager(modelClient)
43+
44+
private fun runTest(body: suspend (ApplicationTestBuilder.() -> Unit)) {
45+
testApplication {
46+
install(WebSockets)
47+
install(ContentNegotiation) { json() }
48+
install(Resources)
49+
install(IgnoreTrailingSlash)
50+
application {
51+
ModelReplicationServer(repoManager).init(this)
52+
ContentExplorer(modelClient, repoManager).init(this)
53+
}
54+
55+
body()
56+
}
57+
}
58+
59+
@Test
60+
fun `node inspector finds root node`() = runTest {
61+
val client = createClient {
62+
install(ClientContentNegotiation) { json() }
63+
}
64+
65+
val delta: VersionDelta = client.post("/v2/repositories/$repoId/init").body()
66+
67+
val versionHash = delta.versionHash
68+
val version = CLVersion.loadFromHash(versionHash, modelClient.storeCache)
69+
val nodeId = checkNotNull(version.getTree().root?.id)
70+
71+
val response = client.get("/content/$versionHash/$nodeId/")
72+
assertTrue(response.successful)
73+
}
74+
}

0 commit comments

Comments
 (0)