nick-boss-tech commented on code in PR #5257:
URL: https://github.com/apache/solr/pull/5257#discussion_r4727664373


##########
ui/shared/src/commonMain/kotlin/org/apache/solr/ui/components/indexquery/data/HttpIndexQueryRepository.kt:
##########
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.solr.ui.components.indexquery.data
+
+import io.ktor.client.HttpClient
+import io.ktor.client.call.body
+import io.ktor.client.request.get
+import io.ktor.http.isSuccess
+import kotlinx.serialization.SerialName
+import kotlinx.serialization.Serializable
+import org.apache.solr.ui.domain.RequestHandlerOption
+
+class HttpIndexQueryRepository(private val httpClient: HttpClient) : 
IndexQueryRepository {
+    override suspend fun listCollections(): Result<List<String>> {
+        val response = httpClient.get("api/collections")
+        return when {
+            response.status.isSuccess() -> 
Result.success(response.body<CollectionsResponse>().collections)
+            else -> Result.failure(Exception("Unknown Error"))
+        }
+    }
+
+    override suspend fun listRequestHandlers(collection: String): 
Result<List<RequestHandlerOption>> {
+        val response = 
httpClient.get("api/collections/$collection/config/requestHandler")
+        return when {
+            response.status.isSuccess() -> {
+                val handlers = 
response.body<RequestHandlersResponse>().config.requestHandler
+                Result.success(
+                    handlers.map { (name, handler) -> 
RequestHandlerOption(name, handler.className) }.sortedBy { it.name },
+                )
+            }
+            else -> Result.failure(Exception("Unknown Error"))
+        }
+    }
+}
+
+@Serializable
+private class CollectionsResponse(val collections: List<String> = emptyList())
+
+@Serializable
+private class RequestHandlersResponse(val config: ConfigResponse)
+
+@Serializable
+private class ConfigResponse(val requestHandler: Map<String, 
RequestHandlerResponse> = emptyMap())
+
+@Serializable
+private class RequestHandlerResponse(@SerialName("class") val className: 
String = "")

Review Comment:
   Done: the DTOs in HttpIndexQueryRepository are data classes now.



##########
ui/shared/src/commonTest/kotlin/org/apache/solr/ui/components/indexquery/data/HttpIndexQueryRepositoryTest.kt:
##########
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.solr.ui.components.indexquery.data
+
+import io.ktor.client.engine.mock.respond
+import io.ktor.http.ContentType
+import io.ktor.http.HttpHeaders
+import io.ktor.http.HttpStatusCode
+import io.ktor.http.fullPath
+import io.ktor.http.headersOf
+import kotlin.test.Test
+import kotlin.test.assertEquals
+import kotlin.test.assertTrue
+import kotlinx.coroutines.test.runTest
+import org.apache.solr.ui.createMockEngine
+import org.apache.solr.ui.domain.RequestHandlerOption
+import org.apache.solr.ui.testHttpClient
+
+class HttpIndexQueryRepositoryTest {
+
+    @Test
+    fun `lists collections from the collections endpoint`() = runTest {
+        val engine = createMockEngine(
+            { request ->
+                assertEquals("/api/collections", request.url.fullPath)
+                respond(
+                    content = """{"collections":["films","books"]}""",
+                    headers = jsonHeaders,
+                )
+            },
+        )
+        val httpClient = testHttpClient(engine)
+
+        try {
+            val result = HttpIndexQueryRepository(httpClient).listCollections()
+
+            assertEquals(listOf("films", "books"), result.getOrThrow())
+            assertEquals(1, engine.requestHistory.size)
+        } finally {
+            httpClient.close()
+        }

Review Comment:
   Done: the tests close the client with `use {}` throughout.



##########
ui/shared/src/commonTest/kotlin/org/apache/solr/ui/components/indexquery/IndexQueryViewModelTest.kt:
##########
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.solr.ui.components.indexquery
+
+import kotlinx.coroutines.test.advanceUntilIdle
+import kotlinx.coroutines.test.runTest
+import org.apache.solr.ui.components.indexquery.data.IndexQueryRepository
+import org.apache.solr.ui.components.indexquery.viewmodel.IndexQueryStateHolder
+import org.apache.solr.ui.domain.RequestHandlerOption
+import kotlin.test.Test
+import kotlin.test.assertEquals
+import kotlin.test.assertFalse
+import kotlin.test.assertNull
+import kotlin.test.assertTrue
+
+private val selectHandler = RequestHandlerOption(name = "/select", className = 
"solr.SearchHandler")
+
+private class FakeIndexQueryRepository(
+    var collections: Result<List<String>> = Result.success(emptyList()),
+    var handlers: Map<String, Result<List<RequestHandlerOption>>> = emptyMap(),
+) : IndexQueryRepository {
+    override suspend fun listCollections(): Result<List<String>> = collections
+
+    override suspend fun listRequestHandlers(collection: String): 
Result<List<RequestHandlerOption>> =
+        handlers[collection] ?: Result.success(emptyList())
+}
+
+class IndexQueryStateHolderTest {
+
+    @Test
+    fun testLoadCollectionsPopulatesList() = runTest {
+        val repository = FakeIndexQueryRepository(collections = 
Result.success(listOf("films", "shop")))
+        val holder = IndexQueryStateHolder(repository, this)
+
+        holder.loadCollections()
+        advanceUntilIdle()
+
+        assertEquals(listOf("films", "shop"), holder.uiState.value.collections)
+        assertFalse(holder.uiState.value.collectionsLoading)
+    }
+
+    @Test
+    fun testSelectingCollectionLoadsItsHandlersAndClearsHandlerChoice() = 
runTest {
+        val repository = FakeIndexQueryRepository(
+            handlers = mapOf("films" to Result.success(listOf(selectHandler))),
+        )
+        val holder = IndexQueryStateHolder(repository, this)
+
+        holder.selectCollection("films")
+        advanceUntilIdle()
+
+        assertEquals(listOf(selectHandler), holder.uiState.value.handlers)
+        assertNull(holder.uiState.value.selectedHandler)
+        assertFalse(holder.uiState.value.handlersLoading)
+    }

Review Comment:
   Reworked: switching collections keeps the chosen handler when the new 
collection offers it too, and the selection is cleared only when loading the 
new collection's handlers fails.



##########
ui/shared/src/jvmMain/kotlin/org/apache/solr/ui/preview/indexquery/PreviewIndexQueryHeader.kt:
##########
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.solr.ui.preview.indexquery
+
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.tooling.preview.Preview
+import org.apache.solr.ui.preview.PreviewContainer
+import org.apache.solr.ui.views.components.HeaderDropdownOption
+import org.apache.solr.ui.views.components.HeaderDropdownState
+import org.apache.solr.ui.views.indexquery.IndexQueryHeader
+
+@Composable
+@Preview
+internal fun PreviewBasicIndexQueryHeader() = PreviewContainer {
+    IndexQueryHeader(
+        collectionDropdown = HeaderDropdownState(
+            options = listOf("dog", "cat").map { HeaderDropdownOption(key = 
it, label = it) },
+            isLoading = false,
+            selectedKey = "cat",
+            onSelect = {},
+        ),
+        requestHandlers = HeaderDropdownState(
+            options = listOf(HeaderDropdownOption(key = "Handle", label = 
"Handle", subtitle = "solr.myRequestHandler")),
+            isLoading = false,
+            selectedKey = null,
+            onSelect = {},
+        ),
+    )
+}
+
+@Composable
+@Preview
+internal fun PreviewLoadingIndexQueryHeader() = PreviewContainer {
+    IndexQueryHeader(
+        collectionDropdown = HeaderDropdownState(
+            options = listOf("dog", "cat").map { HeaderDropdownOption(key = 
it, label = it) },
+            isLoading = true,
+            selectedKey = "cat",
+            onSelect = {},
+        ),
+        requestHandlers = HeaderDropdownState(
+            options = listOf(HeaderDropdownOption(key = "Handle", label = 
"Handle", subtitle = "solr.myRequestHandler")),
+            isLoading = true,
+            selectedKey = "Handle",
+            onSelect = {},
+        ),
+    )
+}
+
+@Composable
+@Preview
+internal fun PreviewNoCollectionsIndexQueryHeader() = PreviewContainer {
+    IndexQueryHeader(
+        collectionDropdown = HeaderDropdownState(
+            options = emptyList(),
+            isLoading = false,
+            selectedKey = null,
+            onSelect = {},
+        ),
+        requestHandlers = HeaderDropdownState(
+            options = emptyList(),
+            isLoading = false,
+            selectedKey = null,
+            onSelect = {},
+        ),
+    )
+}
+
+@Composable
+@Preview
+internal fun PreviewNoRequestHandlersIndexQueryHeader() = PreviewContainer {
+    IndexQueryHeader(
+        collectionDropdown = HeaderDropdownState(
+            options = listOf(HeaderDropdownOption(key = "dog", label = "dog")),

Review Comment:
   Done: the previews now use real-world examples, techproducts and films.



##########
ui/shared/src/commonMain/kotlin/org/apache/solr/ui/components/indexquery/viewmodel/IndexQueryStateHolder.kt:
##########
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.solr.ui.components.indexquery.viewmodel
+
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.flow.StateFlow
+import kotlinx.coroutines.flow.update
+import kotlinx.coroutines.launch
+import org.apache.solr.ui.components.indexquery.data.IndexQueryRepository
+import org.apache.solr.ui.domain.RequestHandlerOption
+
+data class IndexQueryUiState(
+    val collections: List<String> = emptyList(),
+    val collectionsLoading: Boolean = false,
+    val selectedCollection: String? = null,
+    val handlers: List<RequestHandlerOption> = emptyList(),
+    val handlersLoading: Boolean = false,
+    val selectedHandler: String? = null,
+    val error: Boolean = false,
+)
+
+class IndexQueryStateHolder(

Review Comment:
   Done: IndexQueryStateHolder is deleted, and the screen is driven by an 
IndexQueryViewModel integrated into the current UI.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to