Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Test] More robust assertions for sorting and pagination #76654

Merged
merged 2 commits into from
Aug 24, 2021
Merged
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 @@ -230,13 +230,12 @@ public void testPagination() throws IOException, InterruptedException {
final int from = randomIntBetween(0, 3);
final int size = randomIntBetween(2, 5);
final int remaining = total - from;
final List<String> sortFields = List.of(randomFrom("name", "creation"), "_doc");
final String sortFieldsString = sortFields.stream().map(f -> "\"" + f + "\"").collect(Collectors.joining(","));
final String sortField = randomFrom("name", "creation");

final List<Map<String, Object>> apiKeyInfos = new ArrayList<>(remaining);
final Request request1 = new Request("GET", "/_security/_query/api_key");
request1.setOptions(request1.getOptions().toBuilder().addHeader(HttpHeaders.AUTHORIZATION, authHeader));
request1.setJsonEntity("{\"from\":" + from + ",\"size\":" + size + ",\"sort\":[" + sortFieldsString + "]}");
request1.setJsonEntity("{\"from\":" + from + ",\"size\":" + size + ",\"sort\":[\"" + sortField + "\"]}");
int actualSize = collectApiKeys(apiKeyInfos, request1, total, size);
assertThat(actualSize, equalTo(size)); // first batch should be a full page

Expand All @@ -245,13 +244,12 @@ public void testPagination() throws IOException, InterruptedException {
request2.setOptions(request2.getOptions().toBuilder().addHeader(HttpHeaders.AUTHORIZATION, authHeader));
final StringBuilder searchAfter = new StringBuilder();
final List<Object> sortValues = extractSortValues(apiKeyInfos.get(apiKeyInfos.size() - 1));
if (sortFields.get(0).equals("name")) {
if ("name".equals(sortField)) {
searchAfter.append("\"").append(sortValues.get(0)).append("\"");
} else {
searchAfter.append(sortValues.get(0));
}
searchAfter.append(",").append(sortValues.get(1));
request2.setJsonEntity("{\"size\":" + size + ",\"sort\":[" + sortFieldsString + "],\"search_after\":[" + searchAfter + "]}");
request2.setJsonEntity("{\"size\":" + size + ",\"sort\":[\"" + sortField + "\"],\"search_after\":[" + searchAfter + "]}");
actualSize = collectApiKeys(apiKeyInfos, request2, total, size);
if (actualSize == 0 && apiKeyInfos.size() < remaining) {
fail("fail to retrieve all API keys, expect [" + remaining + "] keys, got [" + apiKeyInfos + "]");
Expand All @@ -263,7 +261,7 @@ public void testPagination() throws IOException, InterruptedException {
}

// assert sort values match the field of API key information
if ("name".equals(sortFields.get(0))) {
if ("name".equals(sortField)) {
assertThat(
apiKeyInfos.stream().map(m -> (String) m.get("name")).collect(Collectors.toUnmodifiableList()),
equalTo(apiKeyInfos.stream().map(m -> (String) extractSortValues(m).get(0)).collect(Collectors.toUnmodifiableList())));
Expand All @@ -272,12 +270,12 @@ public void testPagination() throws IOException, InterruptedException {
apiKeyInfos.stream().map(m -> (long) m.get("creation")).collect(Collectors.toUnmodifiableList()),
equalTo(apiKeyInfos.stream().map(m -> (long) extractSortValues(m).get(0)).collect(Collectors.toUnmodifiableList())));
}
assertThat(
apiKeyInfos.stream().map(m -> (String) m.get("name")).collect(Collectors.toUnmodifiableList()),
equalTo(apiKeyNames.subList(from, total)));
assertThat(
apiKeyInfos.stream().map(m -> (String) m.get("id")).collect(Collectors.toUnmodifiableList()),
equalTo(apiKeyIds.subList(from, total)));
assertThat(
apiKeyInfos.stream().map(m -> (String) m.get("name")).collect(Collectors.toUnmodifiableList()),
equalTo(apiKeyNames.subList(from, total)));

// size can be zero, but total should still reflect the number of keys matched
final Request request2 = new Request("GET", "/_security/_query/api_key");
Expand Down Expand Up @@ -335,10 +333,13 @@ public void testSort() throws IOException {

assertQuery(authHeader, "{\"sort\":[\"_doc\"]}", apiKeys -> {
assertThat(apiKeys.size(), equalTo(3));
final List<String> ids = new ArrayList<>(3);
for (int i = 0; i < 3; i++) {
assertThat(apiKeys.get(i).get("id"), equalTo(apiKeyIds.get(i)));
ids.add((String) apiKeys.get(i).get("id"));
assertThat(apiKeys.get(i).get("_sort"), notNullValue());
}
// There is no guarantee that _doc order is the same as creation order
assertThat(ids, containsInAnyOrder(apiKeyIds.toArray()));
});

final String invalidFieldName = randomFrom("doc_type", "api_key_invalidated", "metadata_flattened.letter");
Expand Down