|
@@ -14,15 +14,18 @@ import org.elasticsearch.search.sort.SortMode;
|
|
import org.elasticsearch.search.sort.SortOrder;
|
|
import org.elasticsearch.search.sort.SortOrder;
|
|
import org.elasticsearch.test.ESTestCase;
|
|
import org.elasticsearch.test.ESTestCase;
|
|
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
import java.util.Set;
|
|
import java.util.stream.IntStream;
|
|
import java.util.stream.IntStream;
|
|
|
|
|
|
|
|
+import static org.hamcrest.Matchers.containsInAnyOrder;
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
|
|
|
public class TransportQueryApiKeyActionTests extends ESTestCase {
|
|
public class TransportQueryApiKeyActionTests extends ESTestCase {
|
|
|
|
|
|
public void testTranslateFieldSortBuilders() {
|
|
public void testTranslateFieldSortBuilders() {
|
|
|
|
+ final String metadataField = randomAlphaOfLengthBetween(3, 8);
|
|
final List<String> fieldNames = List.of(
|
|
final List<String> fieldNames = List.of(
|
|
"_doc",
|
|
"_doc",
|
|
"username",
|
|
"username",
|
|
@@ -30,14 +33,16 @@ public class TransportQueryApiKeyActionTests extends ESTestCase {
|
|
"name",
|
|
"name",
|
|
"creation",
|
|
"creation",
|
|
"expiration",
|
|
"expiration",
|
|
|
|
+ "type",
|
|
"invalidated",
|
|
"invalidated",
|
|
- "metadata." + randomAlphaOfLengthBetween(3, 8)
|
|
|
|
|
|
+ "metadata." + metadataField
|
|
);
|
|
);
|
|
|
|
|
|
final List<FieldSortBuilder> originals = fieldNames.stream().map(this::randomFieldSortBuilderWithName).toList();
|
|
final List<FieldSortBuilder> originals = fieldNames.stream().map(this::randomFieldSortBuilderWithName).toList();
|
|
|
|
|
|
|
|
+ List<String> sortFields = new ArrayList<>();
|
|
final SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.searchSource();
|
|
final SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.searchSource();
|
|
- TransportQueryApiKeyAction.translateFieldSortBuilders(originals, searchSourceBuilder);
|
|
|
|
|
|
+ TransportQueryApiKeyAction.translateFieldSortBuilders(originals, searchSourceBuilder, sortFields::add);
|
|
|
|
|
|
IntStream.range(0, originals.size()).forEach(i -> {
|
|
IntStream.range(0, originals.size()).forEach(i -> {
|
|
final FieldSortBuilder original = originals.get(i);
|
|
final FieldSortBuilder original = originals.get(i);
|
|
@@ -57,6 +62,8 @@ public class TransportQueryApiKeyActionTests extends ESTestCase {
|
|
assertThat(translated.getFieldName(), equalTo("api_key_invalidated"));
|
|
assertThat(translated.getFieldName(), equalTo("api_key_invalidated"));
|
|
} else if (original.getFieldName().startsWith("metadata.")) {
|
|
} else if (original.getFieldName().startsWith("metadata.")) {
|
|
assertThat(translated.getFieldName(), equalTo("metadata_flattened." + original.getFieldName().substring(9)));
|
|
assertThat(translated.getFieldName(), equalTo("metadata_flattened." + original.getFieldName().substring(9)));
|
|
|
|
+ } else if ("type".equals(original.getFieldName())) {
|
|
|
|
+ assertThat(translated.getFieldName(), equalTo("runtime_key_type"));
|
|
} else {
|
|
} else {
|
|
fail("unrecognized field name: [" + original.getFieldName() + "]");
|
|
fail("unrecognized field name: [" + original.getFieldName() + "]");
|
|
}
|
|
}
|
|
@@ -68,6 +75,19 @@ public class TransportQueryApiKeyActionTests extends ESTestCase {
|
|
assertThat(translated.sortMode(), equalTo(original.sortMode()));
|
|
assertThat(translated.sortMode(), equalTo(original.sortMode()));
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
+ assertThat(
|
|
|
|
+ sortFields,
|
|
|
|
+ containsInAnyOrder(
|
|
|
|
+ "creator.principal",
|
|
|
|
+ "creator.realm",
|
|
|
|
+ "name",
|
|
|
|
+ "creation_time",
|
|
|
|
+ "expiration_time",
|
|
|
|
+ "runtime_key_type",
|
|
|
|
+ "api_key_invalidated",
|
|
|
|
+ "metadata_flattened." + metadataField
|
|
|
|
+ )
|
|
|
|
+ );
|
|
}
|
|
}
|
|
|
|
|
|
public void testNestedSortingIsNotAllowed() {
|
|
public void testNestedSortingIsNotAllowed() {
|
|
@@ -75,7 +95,11 @@ public class TransportQueryApiKeyActionTests extends ESTestCase {
|
|
fieldSortBuilder.setNestedSort(new NestedSortBuilder("name"));
|
|
fieldSortBuilder.setNestedSort(new NestedSortBuilder("name"));
|
|
final IllegalArgumentException e = expectThrows(
|
|
final IllegalArgumentException e = expectThrows(
|
|
IllegalArgumentException.class,
|
|
IllegalArgumentException.class,
|
|
- () -> TransportQueryApiKeyAction.translateFieldSortBuilders(List.of(fieldSortBuilder), SearchSourceBuilder.searchSource())
|
|
|
|
|
|
+ () -> TransportQueryApiKeyAction.translateFieldSortBuilders(
|
|
|
|
+ List.of(fieldSortBuilder),
|
|
|
|
+ SearchSourceBuilder.searchSource(),
|
|
|
|
+ ignored -> {}
|
|
|
|
+ )
|
|
);
|
|
);
|
|
assertThat(e.getMessage(), equalTo("nested sorting is not supported for API Key query"));
|
|
assertThat(e.getMessage(), equalTo("nested sorting is not supported for API Key query"));
|
|
}
|
|
}
|