Skip to content

Commit e8749d9

Browse files
authored
Simplify EsqlRestValidationTestCase (elastic#139193)
1 parent e561262 commit e8749d9

File tree

1 file changed

+39
-67
lines changed

1 file changed

+39
-67
lines changed

x-pack/plugin/esql/qa/server/src/main/java/org/elasticsearch/xpack/esql/qa/rest/EsqlRestValidationTestCase.java

Lines changed: 39 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
package org.elasticsearch.xpack.esql.qa.rest;
99

1010
import org.elasticsearch.client.Request;
11-
import org.elasticsearch.client.RequestOptions;
12-
import org.elasticsearch.client.Response;
1311
import org.elasticsearch.client.ResponseException;
1412
import org.elasticsearch.client.RestClient;
1513
import org.elasticsearch.client.WarningsHandler;
@@ -20,6 +18,7 @@
2018
import org.junit.Before;
2119

2220
import java.io.IOException;
21+
import java.util.List;
2322

2423
import static org.hamcrest.Matchers.containsString;
2524
import static org.hamcrest.Matchers.equalTo;
@@ -28,24 +27,6 @@ public abstract class EsqlRestValidationTestCase extends ESRestTestCase {
2827

2928
private static final String indexName = "test_esql";
3029
private static final String aliasName = "alias-test_esql";
31-
protected static final String[] existentIndexWithWildcard = new String[] {
32-
indexName + ",inexistent*",
33-
indexName + "*,inexistent*",
34-
"inexistent*," + indexName };
35-
private static final String[] existentIndexWithoutWildcard = new String[] { indexName + ",inexistent", "inexistent," + indexName };
36-
protected static final String[] existentAliasWithWildcard = new String[] {
37-
aliasName + ",inexistent*",
38-
aliasName + "*,inexistent*",
39-
"inexistent*," + aliasName };
40-
private static final String[] existentAliasWithoutWildcard = new String[] { aliasName + ",inexistent", "inexistent," + aliasName };
41-
private static final String[] inexistentIndexNameWithWildcard = new String[] { "inexistent*", "inexistent1*,inexistent2*" };
42-
private static final String[] inexistentIndexNameWithoutWildcard = new String[] { "inexistent", "inexistent1,inexistent2" };
43-
private static final String createAlias = "{\"actions\":[{\"add\":{\"index\":\"" + indexName + "\",\"alias\":\"" + aliasName + "\"}}]}";
44-
private static final String removeAlias = "{\"actions\":[{\"remove\":{\"index\":\""
45-
+ indexName
46-
+ "\",\"alias\":\""
47-
+ aliasName
48-
+ "\"}}]}";
4930

5031
@Before
5132
@After
@@ -73,79 +54,76 @@ public void wipeTestData() throws IOException {
7354
}
7455
}
7556

76-
private String getInexistentIndexErrorMessage() {
77-
return "\"reason\" : \"Found 1 problem\\nline 1:1: Unknown index ";
78-
}
79-
80-
public void testInexistentIndexNameWithWildcard() throws IOException {
81-
assertErrorMessages(inexistentIndexNameWithWildcard, getInexistentIndexErrorMessage(), 400);
57+
public void testInexistentIndexNameWithWildcard() {
58+
for (String pattern : List.of("inexistent*", "inexistent1*,inexistent2*")) {
59+
assertError(pattern, 400, "Found 1 problem\\nline 1:1: Unknown index [" + clusterSpecificIndexName(pattern) + "]");
60+
}
8261
}
8362

84-
public void testInexistentIndexNameWithoutWildcard() throws IOException {
85-
assertErrorMessages(inexistentIndexNameWithoutWildcard, getInexistentIndexErrorMessage(), 400);
63+
public void testInexistentIndexNameWithoutWildcard() {
64+
for (String pattern : List.of("inexistent", "inexistent1,inexistent2")) {
65+
assertError(pattern, "Found 1 problem\\nline 1:1: Unknown index [" + clusterSpecificIndexName(pattern) + "]", 400);
66+
}
8667
}
8768

8869
public void testExistentIndexWithoutWildcard() throws IOException {
89-
for (String indexName : existentIndexWithoutWildcard) {
90-
assertErrorMessage(indexName, "\"reason\" : \"no such index [inexistent]\"", 404);
70+
for (String pattern : List.of(indexName + ",inexistent", "inexistent," + indexName)) {
71+
assertError(pattern, 404, "no such index [inexistent]");
9172
}
9273
}
9374

9475
public void testExistentIndexWithWildcard() throws IOException {
95-
assertValidRequestOnIndices(existentIndexWithWildcard);
76+
for (String pattern : List.of(indexName + ",inexistent*", indexName + "*,inexistent*", "inexistent*," + indexName)) {
77+
assertOK(client().performRequest(createRequest(pattern)));
78+
}
9679
}
9780

9881
public void testAlias() throws IOException {
99-
createAlias();
82+
updateAliases("""
83+
{"actions":[{"add":{"index":"%s","alias":"%s"}}]}
84+
""".formatted(indexName, aliasName));
10085

101-
for (String indexName : existentAliasWithoutWildcard) {
102-
assertErrorMessage(indexName, "\"reason\" : \"no such index [inexistent]\"", 404);
86+
for (String indexName : List.of(aliasName + ",inexistent", "inexistent," + aliasName)) {
87+
assertError(indexName, "no such index [inexistent]", 404);
10388
}
104-
assertValidRequestOnIndices(existentAliasWithWildcard);
105-
106-
deleteAlias();
107-
}
108-
109-
private void assertErrorMessages(String[] indices, String errorMessage, int statusCode) throws IOException {
110-
for (String indexName : indices) {
111-
assertErrorMessage(indexName, errorMessage + "[" + clusterSpecificIndexName(indexName) + "]", statusCode);
89+
for (String indexName : List.of(aliasName + ",inexistent*", aliasName + "*,inexistent*", "inexistent*," + aliasName)) {
90+
assertOK(client().performRequest(createRequest(indexName)));
11291
}
92+
93+
updateAliases("""
94+
{"actions":[{"remove":{"index":"%s","alias":"%s"}}]}
95+
""".formatted(indexName, aliasName));
11396
}
11497

11598
protected String clusterSpecificIndexName(String indexName) {
11699
return indexName;
117100
}
118101

119-
private void assertErrorMessage(String indexName, String errorMessage, int statusCode) throws IOException {
120-
var specificName = clusterSpecificIndexName(indexName);
121-
final var request = createRequest(specificName);
122-
ResponseException exc = expectThrows(ResponseException.class, () -> client().performRequest(request));
102+
private void assertError(String indexName, String errorMessage, int statusCode) {
103+
ResponseException exc = expectThrows(ResponseException.class, () -> client().performRequest(createRequest(indexName)));
104+
assertThat(exc.getResponse().getStatusLine().getStatusCode(), equalTo(statusCode));
105+
assertThat(exc.getMessage(), containsString("\"reason\" : \"" + errorMessage + "\""));
106+
}
123107

108+
private void assertError(String indexName, int statusCode, String errorMessage) {
109+
ResponseException exc = expectThrows(ResponseException.class, () -> client().performRequest(createRequest(indexName)));
124110
assertThat(exc.getResponse().getStatusLine().getStatusCode(), equalTo(statusCode));
125-
assertThat(exc.getMessage(), containsString(errorMessage));
111+
assertThat(exc.getMessage(), containsString("\"reason\" : \"" + errorMessage + "\""));
126112
}
127113

128114
private Request createRequest(String indexName) throws IOException {
129115
final var request = new Request("POST", "/_query");
130116
request.addParameter("error_trace", "true");
131117
request.addParameter("pretty", "true");
132118
request.setJsonEntity(
133-
Strings.toString(JsonXContent.contentBuilder().startObject().field("query", "from " + indexName).endObject())
119+
Strings.toString(
120+
JsonXContent.contentBuilder().startObject().field("query", "from " + clusterSpecificIndexName(indexName)).endObject()
121+
)
134122
);
135-
RequestOptions.Builder options = request.getOptions().toBuilder();
136-
options.setWarningsHandler(WarningsHandler.PERMISSIVE);
137-
request.setOptions(options);
123+
request.setOptions(request.getOptions().toBuilder().setWarningsHandler(WarningsHandler.PERMISSIVE));
138124
return request;
139125
}
140126

141-
private void assertValidRequestOnIndices(String[] indices) throws IOException {
142-
for (String indexName : indices) {
143-
final var request = createRequest(clusterSpecificIndexName(indexName));
144-
Response response = client().performRequest(request);
145-
assertOK(response);
146-
}
147-
}
148-
149127
// Returned client is used to load the test data, either in the local cluster or a remote one (for
150128
// multi-clusters). The client()/adminClient() will always connect to the local cluster
151129
protected RestClient provisioningClient() throws IOException {
@@ -156,15 +134,9 @@ protected RestClient provisioningAdminClient() throws IOException {
156134
return adminClient();
157135
}
158136

159-
private void createAlias() throws IOException {
137+
private void updateAliases(String update) throws IOException {
160138
var r = new Request("POST", "_aliases");
161-
r.setJsonEntity(createAlias);
139+
r.setJsonEntity(update);
162140
assertOK(provisioningClient().performRequest(r));
163141
}
164-
165-
private void deleteAlias() throws IOException {
166-
var r = new Request("POST", "/_aliases/");
167-
r.setJsonEntity(removeAlias);
168-
assertOK(provisioningAdminClient().performRequest(r));
169-
}
170142
}

0 commit comments

Comments
 (0)