Skip to content

Commit

Permalink
Merge pull request #394 from modelix/issue/MODELIX-678
Browse files Browse the repository at this point in the history
MODELIX-678 model-server content explorer raises an error when trying to unfold the root node of an empty repository
  • Loading branch information
mhuster23 authored Jan 15, 2024
2 parents 56d2195 + 39d18c1 commit d8363c1
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import kotlinx.html.tr
import kotlinx.html.ul
import kotlinx.html.unsafe
import org.modelix.api.html.Paths
import org.modelix.model.ModelFacade
import org.modelix.model.api.BuiltinLanguages
import org.modelix.model.api.INodeResolutionScope
import org.modelix.model.api.ITree
Expand All @@ -50,19 +49,6 @@ import kotlin.collections.set

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

private val rootNodes: List<PNodeAdapter>
get() {
val nodeList = mutableListOf<PNodeAdapter>()

for (repoId in repoManager.getRepositories()) {
val branchRef = repoId.getBranchReference()
val version = ModelFacade.loadCurrentVersion(client, branchRef) ?: continue
val rootNode = PNodeAdapter(ITree.ROOT_ID, TreePointer(version.getTree()))
nodeList.add(rootNode)
}
return nodeList
}

fun init(application: Application) {
application.routing {
get<Paths.getContent> {
Expand Down Expand Up @@ -112,19 +98,24 @@ class ContentExplorer(private val client: IModelClient, private val repoManager:
)
}
get<Paths.getNodeIdForVersionHash> {
val id = call.parameters["nodeId"]!!.toLong()
var found: PNodeAdapter? = null
for (node in rootNodes) {
val candidate = PNodeAdapter(id, node.branch).takeIf { it.isValid }
if (candidate != null) {
found = candidate
break
}
val id = call.parameters["nodeId"]?.toLongOrNull()
?: return@get call.respondText("node id not found", status = HttpStatusCode.NotFound)

val versionHash = call.parameters["versionHash"]
?: return@get call.respondText("version hash not found", status = HttpStatusCode.NotFound)

val version = try {
CLVersion.loadFromHash(versionHash, client.storeCache)
} catch (ex: RuntimeException) {
return@get call.respondText("version not found", status = HttpStatusCode.NotFound)
}
if (found == null) {
call.respondText("node id not found", status = HttpStatusCode.NotFound)

val node = PNodeAdapter(id, TreePointer(version.getTree())).takeIf { it.isValid }

if (node != null) {
call.respondHtml { body { nodeInspector(node) } }
} else {
call.respondHtml { body { nodeInspector(found) } }
call.respondText("node id not found", status = HttpStatusCode.NotFound)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2024.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.modelix.model.server.handlers

import io.ktor.client.call.body
import io.ktor.client.request.get
import io.ktor.client.request.post
import io.ktor.serialization.kotlinx.json.json
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
import io.ktor.server.resources.Resources
import io.ktor.server.routing.IgnoreTrailingSlash
import io.ktor.server.testing.ApplicationTestBuilder
import io.ktor.server.testing.testApplication
import io.ktor.server.websocket.WebSockets
import org.modelix.model.client.successful
import org.modelix.model.lazy.CLVersion
import org.modelix.model.server.api.v2.VersionDelta
import org.modelix.model.server.store.InMemoryStoreClient
import org.modelix.model.server.store.LocalModelClient
import kotlin.test.Test
import kotlin.test.assertTrue
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation as ClientContentNegotiation

class ContentExplorerTest {

private val repoId = "test-repo"
private val modelClient = LocalModelClient(InMemoryStoreClient())
private val repoManager = RepositoriesManager(modelClient)

private fun runTest(body: suspend (ApplicationTestBuilder.() -> Unit)) {
testApplication {
install(WebSockets)
install(ContentNegotiation) { json() }
install(Resources)
install(IgnoreTrailingSlash)
application {
ModelReplicationServer(repoManager).init(this)
ContentExplorer(modelClient, repoManager).init(this)
}

body()
}
}

@Test
fun `node inspector finds root node`() = runTest {
val client = createClient {
install(ClientContentNegotiation) { json() }
}

val delta: VersionDelta = client.post("/v2/repositories/$repoId/init").body()

val versionHash = delta.versionHash
val version = CLVersion.loadFromHash(versionHash, modelClient.storeCache)
val nodeId = checkNotNull(version.getTree().root?.id)

val response = client.get("/content/$versionHash/$nodeId/")
assertTrue(response.successful)
}
}

0 comments on commit d8363c1

Please sign in to comment.