frankgh commented on code in PR #196: URL: https://github.com/apache/cassandra-sidecar/pull/196#discussion_r1962435611
########## integration-tests/src/integrationTest/org/apache/cassandra/sidecar/routes/ConnectedClientStatsHandlerIntegrationTest.java: ########## @@ -0,0 +1,175 @@ +/* + * 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.cassandra.sidecar.routes; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.junit.jupiter.api.Test; + +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Session; +import io.vertx.core.buffer.Buffer; +import io.vertx.ext.web.client.HttpResponse; +import io.vertx.ext.web.client.predicate.ResponsePredicate; +import org.apache.cassandra.sidecar.common.response.ConnectedClientStatsResponse; +import org.apache.cassandra.sidecar.common.response.data.ClientConnectionEntry; +import org.apache.cassandra.sidecar.testing.SharedClusterSidecarIntegrationTestBase; +import org.apache.cassandra.sidecar.utils.SimpleCassandraVersion; + +import static org.apache.cassandra.testing.TestUtils.DC1_RF1; +import static org.apache.cassandra.testing.TestUtils.TEST_KEYSPACE; +import static org.apache.cassandra.testing.utils.AssertionUtils.getBlocking; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Test the client-stats endpoint with cassandra container. + */ +class ConnectedClientStatsHandlerIntegrationTest extends SharedClusterSidecarIntegrationTestBase +{ + private static final int DEFAULT_CONNECTION_COUNT = 2; + + @Override + protected void initializeSchemaForTest() + { + createTestKeyspace(TEST_KEYSPACE, DC1_RF1); + } + + @Test + void retrieveClientStatsDefault() + { + Map<String, Boolean> expectedParams = Map.of("summary", true); + String testRoute = "/api/v1/cassandra/stats/connected-clients"; + HttpResponse<Buffer> response = getBlocking(trustedClient().get(server.actualPort(), "localhost", testRoute) + .expect(ResponsePredicate.SC_OK) + .send()); + assertClientStatsResponse(response, expectedParams); + } + + @Test + void retrieveClientStatsListConnections() + { + Map<String, Boolean> expectedParams = Map.of("summary", false); + String testRoute = "/api/v1/cassandra/stats/connected-clients?summary=false"; + HttpResponse<Buffer> response = getBlocking(trustedClient().get(server.actualPort(), "localhost", testRoute) + .expect(ResponsePredicate.SC_OK) + .send()); + assertClientStatsResponse(response, expectedParams); + } + + @Test + void retrieveClientStatsListConnectionsWithKeyspace() + { + try (Cluster driverCluster = createDriverCluster(cluster.delegate()); Session session = driverCluster.connect()) + { + session.execute("USE " + TEST_KEYSPACE); + + Map<String, Boolean> expectedParams = Map.of("summary", false); + String testRoute = "/api/v1/cassandra/stats/connected-clients?summary=false"; + HttpResponse<Buffer> response = getBlocking(trustedClient().get(server.actualPort(), "localhost", testRoute) + .expect(ResponsePredicate.SC_OK) + .send()); + assertClientStatsResponse(response, expectedParams, 4, true); + } + } + + @Test + void retrieveClientStatsMultipleConnections() + { + // Creates an additional connection pair + try (Cluster driverCluster = createDriverCluster(cluster.delegate()); Session ignored = driverCluster.connect()) + { + Map<String, Boolean> expectedParams = Map.of("summary", false); + String testRoute = "/api/v1/cassandra/stats/connected-clients?summary=false"; + HttpResponse<Buffer> response = getBlocking(trustedClient().get(server.actualPort(), "localhost", testRoute) + .expect(ResponsePredicate.SC_OK) + .send()); + assertClientStatsResponse(response, expectedParams, 4); + } + } + + /** + * Expects unrecognized params to be ignored and invalid value for the expected parameter to be defaulted to true + * to prevent heavyweight query in the bad request case. + */ + @Test + void retrieveClientStatsInvalidParameterValue() + { + Map<String, Boolean> expectedParams = Map.of("summary", true); + String testRoute = "/api/v1/cassandra/stats/connected-clients?summary=123&bad-arg=xyz"; + HttpResponse<Buffer> response = getBlocking(trustedClient().get(server.actualPort(), "localhost", testRoute) + .expect(ResponsePredicate.SC_OK) Review Comment: This should still be a 200. We don't validate unexpected query parameters, and looking at the `org.apache.cassandra.sidecar.utils.RequestUtils#parseBooleanQueryParam` logic, we only allow `true` or `false` otherwise we use the default value. -- 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]

