Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,31 @@ public void testExceptionOnCursorExplain() throws IOException {
assertThat(resp.query("/error/type"), equalTo("IllegalArgumentException"));
}

@Test
public void emptyCursorReturnsBadRequest() throws IOException {
assertEmptyCursorRejected(getSqlRequest("{\"cursor\": \"\"}", false));
}

@Test
public void emptyCursorCloseReturnsBadRequest() throws IOException {
assertEmptyCursorRejected(getSqlCursorCloseRequest("{\"cursor\": \"\"}"));
}

private void assertEmptyCursorRejected(Request request) throws IOException {
Response response = null;
try {
executeRequest(request);
} catch (ResponseException ex) {
response = ex.getResponse();
}

JSONObject resp = new JSONObject(TestUtils.getResponseBody(response));
assertThat(resp.getInt("status"), equalTo(400));
assertThat(resp.query("/error/reason"), equalTo("Invalid SQL query"));
assertThat(resp.query("/error/details"), equalTo("Cursor is empty or invalid"));
assertThat(resp.query("/error/type"), equalTo("IllegalArgumentException"));
}

/**
* For fetch_size = 0, default to non-pagination behaviour for simple queries This can be verified
* by checking that cursor is not present, and old default limit applies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,11 @@ private static int getRawErrorCode(Exception ex) {
* @param sqlRequest client request
* @return true if this cursor was generated by the legacy engine, false otherwise.
*/
private static boolean isLegacyCursor(SqlRequest sqlRequest) {
static boolean isLegacyCursor(SqlRequest sqlRequest) {
String cursor = sqlRequest.cursor();
return cursor != null && CursorType.getById(cursor.substring(0, 1)) != CursorType.NULL;
return cursor != null
&& !cursor.isEmpty()
&& CursorType.getById(cursor.substring(0, 1)) != CursorType.NULL;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ private static SqlRequest parseSqlRequestFromPayload(RestRequest restRequest) {
try {
jsonContent = new JSONObject(content);
if (jsonContent.has(SQL_CURSOR_FIELD_NAME)) {
return new SqlRequest(jsonContent.getString(SQL_CURSOR_FIELD_NAME));
String cursor = jsonContent.getString(SQL_CURSOR_FIELD_NAME);
if (cursor == null || cursor.isBlank()) {
throw new IllegalArgumentException("Cursor is empty or invalid");
}
return new SqlRequest(cursor);
}
String sql = jsonContent.getString(SQL_FIELD_NAME);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.opensearch.sql.legacy.plugin;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
Expand All @@ -30,6 +31,7 @@
import org.opensearch.sql.legacy.metrics.MetricName;
import org.opensearch.sql.legacy.metrics.Metrics;
import org.opensearch.sql.legacy.metrics.NumericMetric;
import org.opensearch.sql.legacy.request.SqlRequest;
import org.opensearch.sql.legacy.util.LogCapture;

public class RestSqlActionTest {
Expand Down Expand Up @@ -194,4 +196,26 @@ public void handleExceptionLogsServerErrorAtErrorWithStackTrace() throws Excepti
assertEquals(Level.ERROR, events.get(0).getLevel());
assertSame(failure, events.get(0).getThrown());
}

@Test
public void emptyCursorIsClientError() {
assertEquals(
RestStatus.BAD_REQUEST,
RestSqlAction.getRestStatus(new IllegalArgumentException("Cursor is empty or invalid")));
}

@Test
public void isLegacyCursorRejectsEmptyAndNullCursorsWithoutThrowing() {
assertFalse(RestSqlAction.isLegacyCursor(new SqlRequest("")));
assertFalse(RestSqlAction.isLegacyCursor(new SqlRequest((String) null)));
assertFalse(RestSqlAction.isLegacyCursor(new SqlRequest("select 1", null)));
}

@Test
public void isLegacyCursorDetectsLegacyAndV2Cursors() {
assertTrue(RestSqlAction.isLegacyCursor(new SqlRequest("d:eyJhIjp7fSwicyI6IkRYRjFaWEo1")));
assertTrue(RestSqlAction.isLegacyCursor(new SqlRequest("a:aggregation-cursor")));
assertTrue(RestSqlAction.isLegacyCursor(new SqlRequest("j:join-cursor")));
assertFalse(RestSqlAction.isLegacyCursor(new SqlRequest("n:v2-cursor")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,40 @@ public void testGenerateSqlRequest_unsupportedType() {

SqlRequest sqlRequest = SqlRequestFactory.getSqlRequest(this.restRequest);
}

@Test
public void testGenerateSqlRequest_cursorFromPayload() {
String payload = "{ \"cursor\": \"d:eyJhIjp7fSwicyI6IkRYRjFaWEo1\" }";
Mockito.when(this.restRequest.content()).thenReturn(new BytesArray(payload));
Mockito.when(this.restRequest.method()).thenReturn(RestRequest.Method.POST);

SqlRequest sqlRequest = SqlRequestFactory.getSqlRequest(this.restRequest);
Assert.assertEquals("d:eyJhIjp7fSwicyI6IkRYRjFaWEo1", sqlRequest.cursor());
}

@Test
public void testGenerateSqlRequest_emptyCursor() {
String payload = "{ \"cursor\": \"\" }";
Mockito.when(this.restRequest.content()).thenReturn(new BytesArray(payload));
Mockito.when(this.restRequest.method()).thenReturn(RestRequest.Method.POST);

IllegalArgumentException exception =
Assert.assertThrows(
IllegalArgumentException.class,
() -> SqlRequestFactory.getSqlRequest(this.restRequest));
Assert.assertEquals("Cursor is empty or invalid", exception.getMessage());
}

@Test
public void testGenerateSqlRequest_blankCursor() {
String payload = "{ \"cursor\": \" \" }";
Mockito.when(this.restRequest.content()).thenReturn(new BytesArray(payload));
Mockito.when(this.restRequest.method()).thenReturn(RestRequest.Method.POST);

IllegalArgumentException exception =
Assert.assertThrows(
IllegalArgumentException.class,
() -> SqlRequestFactory.getSqlRequest(this.restRequest));
Assert.assertEquals("Cursor is empty or invalid", exception.getMessage());
}
}
Loading