Copilot commented on code in PR #17937:
URL: https://github.com/apache/druid/pull/17937#discussion_r2070065526


##########
integration-tests/src/test/java/org/apache/druid/tests/query/ITSqlQueryTest.java:
##########
@@ -0,0 +1,366 @@
+/*
+ * 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.druid.tests.query;
+
+import com.google.inject.Inject;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.testing.IntegrationTestingConfig;
+import org.apache.druid.testing.guice.DruidTestModuleFactory;
+import org.apache.druid.tests.TestNGGroup;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpStatus;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.util.EntityUtils;
+import org.testng.annotations.Guice;
+import org.testng.annotations.Test;
+
+import javax.ws.rs.core.MediaType;
+import java.io.IOException;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import java.util.function.Function;
+
+/**
+ * Test the SQL endpoint with different Content-Type
+ */
+@Test(groups = {TestNGGroup.QUERY, TestNGGroup.CENTRALIZED_DATASOURCE_SCHEMA})
+@Guice(moduleFactory = DruidTestModuleFactory.class)
+public class ITSqlQueryTest
+{
+  private static final Logger LOG = new Logger(ITSqlQueryTest.class);
+
+  @Inject
+  IntegrationTestingConfig config;
+
+  interface IExecutable
+  {
+    void execute(String endpoint) throws IOException;
+  }
+
+  interface OnRequest
+  {
+    void on(HttpPost request) throws IOException;
+  }
+
+  interface OnResponse
+  {
+    void on(int statusCode, HttpEntity response) throws IOException;
+  }
+
+  private void executeWithRetry(String endpoint, String contentType, 
IExecutable executable)
+  {
+    Throwable lastException = null;
+    for (int i = 1; i <= 5; i++) {
+      LOG.info("Query to %s with Content-Type = %s, tries = %s", endpoint, 
contentType, i);
+      try {
+        executable.execute(endpoint);
+        return;
+      }
+      catch (IOException e) {
+        // Only catch IOException
+        lastException = e;
+      }
+      try {
+        Thread.sleep(200);
+      }
+      catch (InterruptedException ignored) {
+        break;
+      }
+    }
+    throw new ISE(contentType + " failed after 5 tries, last exception: " + 
lastException);
+  }
+
+  private void executeQuery(
+      String contentType,
+      OnRequest onRequest,
+      OnResponse onResponse
+  )
+  {
+    IExecutable executable = (endpoint) -> {
+      try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
+        HttpPost request = new HttpPost(endpoint);
+        if (contentType != null) {
+          request.addHeader("Content-Type", contentType);
+        }
+        onRequest.on(request);
+
+        try (CloseableHttpResponse response = client.execute(request)) {
+          HttpEntity responseEntity = response.getEntity();
+          assertNotNull(responseEntity);
+
+          onResponse.on(
+              response.getStatusLine().getStatusCode(),
+              responseEntity
+          );
+        }
+      }
+    };
+
+    // Send query to broker to exeucte
+    executeWithRetry(StringUtils.format("%s/druid/v2/sql/", 
config.getBrokerUrl()), contentType, executable);
+
+    // Send query to router to execute
+    executeWithRetry(StringUtils.format("%s/druid/v2/sql/", 
config.getRouterUrl()), contentType, executable);
+  }
+
+  private void assertEquals(String expected, String actual)
+  {
+    if (!expected.equals(actual)) {
+      throw new ISE("Expected [%s] but got [%s]", expected, actual);
+    }
+  }
+
+  private void assertEquals(int expected, int actual)
+  {
+    if (expected != actual) {
+      throw new ISE("Expected [%d] but got [%d]", expected, actual);
+    }
+  }
+
+  private void assertNotNull(Object object)
+  {
+    if (object == null) {
+      throw new ISE("Expected not null");
+    }
+  }
+
+  private void assertStringCompare(String expected, String actual, 
Function<String, Boolean> predicate)
+  {
+    if (!predicate.apply(expected)) {
+      throw new ISE("Expected: [%s] but got [%s]", expected, actual);
+    }
+  }
+
+  @Test
+  public void testNullContentType()
+  {
+    executeQuery(
+        null,
+        (request) -> {
+          request.setEntity(new StringEntity("select 1"));
+        },
+        (statusCode, responseEntity) -> {
+          assertEquals(HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE, statusCode);
+
+          String responseBody = EntityUtils.toString(responseEntity).trim();
+          assertStringCompare("Unsupported Content-Type:", responseBody, 
responseBody::contains);
+        }
+    );
+  }
+
+  @Test
+  public void testUnsupportedCotentType()

Review Comment:
   The method name 'testUnsupportedCotentType' appears to contain a spelling 
error; consider renaming it to 'testUnsupportedContentType' for clarity.
   ```suggestion
     public void testUnsupportedContentType()
   ```



-- 
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