Skip to content

Commit

Permalink
Add extra check for unset initialSearchRequest.
Browse files Browse the repository at this point in the history
Signed-off-by: Yury-Fridlyand <yury.fridlyand@improving.com>
  • Loading branch information
Yury-Fridlyand committed Jun 14, 2023
1 parent 45d8ed6 commit bd27566
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ public OpenSearchResponse search(Function<SearchRequest, SearchResponse> searchA
if (isScroll()) {
openSearchResponse = scrollAction.apply(scrollRequest());
} else {
if (initialSearchRequest == null) {
// Probably a first page search (since there is no scroll set) called on a deserialized
// `OpenSearchScrollRequest`, which has no `initialSearchRequest`.
throw new UnsupportedOperationException("Misuse of OpenSearchScrollRequest");
}
openSearchResponse = searchAction.apply(initialSearchRequest);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

package org.opensearch.sql.opensearch.request;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -142,7 +145,7 @@ void search() {
}

@Test
void search_withoutContext() {
void search_without_context() {
OpenSearchScrollRequest request = new OpenSearchScrollRequest(
new OpenSearchRequest.IndexName("test"),
TimeValue.timeValueMinutes(1),
Expand All @@ -158,6 +161,32 @@ void search_withoutContext() {
assertFalse(response.isEmpty());
}

@Test
@SneakyThrows
void search_without_scroll_and_initial_request_should_throw() {
// Steps: serialize a not used request, deserialize it, then use
OpenSearchScrollRequest request = new OpenSearchScrollRequest(
new OpenSearchRequest.IndexName("test"),
TimeValue.timeValueMinutes(1),
sourceBuilder,
factory
);
var outStream = new BytesStreamOutput();
request.writeTo(outStream);
outStream.flush();
var inStream = new BytesStreamInput(outStream.bytes().toBytesRef().bytes);
var indexMock = mock(OpenSearchIndex.class);
var engine = mock(OpenSearchStorageEngine.class);
when(engine.getTable(any(), any())).thenReturn(indexMock);
var request2 = new OpenSearchScrollRequest(inStream, engine);
assertAll(
() -> assertFalse(request2.isScroll()),
() -> assertNull(request2.getInitialSearchRequest()),
() -> assertThrows(UnsupportedOperationException.class,
() -> request2.search(sr -> fail("search"), sr -> fail("scroll")))
);
}

@Test
void search_withoutIncludes() {
OpenSearchScrollRequest request = new OpenSearchScrollRequest(
Expand Down

0 comments on commit bd27566

Please sign in to comment.