|
5 | 5 | */
|
6 | 6 | package org.elasticsearch.integration;
|
7 | 7 |
|
8 |
| -import org.apache.http.Header; |
9 |
| -import org.apache.http.entity.ContentType; |
10 |
| -import org.apache.http.entity.StringEntity; |
11 |
| -import org.apache.http.message.BasicHeader; |
12 | 8 | import org.apache.http.util.EntityUtils;
|
13 | 9 | import org.elasticsearch.action.DocWriteResponse;
|
14 | 10 | import org.elasticsearch.action.bulk.BulkResponse;
|
15 | 11 | import org.elasticsearch.action.get.GetResponse;
|
16 |
| -import org.elasticsearch.client.Response; |
| 12 | +import org.elasticsearch.client.Request; |
| 13 | +import org.elasticsearch.client.RequestOptions; |
17 | 14 | import org.elasticsearch.common.settings.SecureString;
|
18 | 15 | import org.elasticsearch.common.settings.Settings;
|
19 | 16 | import org.elasticsearch.common.xcontent.XContentType;
|
|
24 | 21 | import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken;
|
25 | 22 |
|
26 | 23 | import java.io.IOException;
|
27 |
| -import java.util.Collections; |
28 | 24 |
|
29 | 25 | import static org.hamcrest.Matchers.containsString;
|
30 |
| -import static org.hamcrest.Matchers.equalTo; |
31 | 26 |
|
32 | 27 | public class BulkUpdateTests extends SecurityIntegTestCase {
|
33 | 28 |
|
@@ -77,46 +72,48 @@ public void testThatBulkUpdateDoesNotLoseFields() {
|
77 | 72 |
|
78 | 73 | public void testThatBulkUpdateDoesNotLoseFieldsHttp() throws IOException {
|
79 | 74 | final String path = "/index1/type/1";
|
80 |
| - final Header basicAuthHeader = new BasicHeader("Authorization", |
81 |
| - UsernamePasswordToken.basicAuthHeaderValue(SecuritySettingsSource.TEST_USER_NAME, |
82 |
| - new SecureString(SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()))); |
| 75 | + final RequestOptions.Builder optionsBuilder = RequestOptions.DEFAULT.toBuilder(); |
| 76 | + optionsBuilder.addHeader("Authorization", UsernamePasswordToken.basicAuthHeaderValue(SecuritySettingsSource.TEST_USER_NAME, |
| 77 | + new SecureString(SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()))); |
| 78 | + final RequestOptions options = optionsBuilder.build(); |
83 | 79 |
|
84 |
| - StringEntity body = new StringEntity("{\"test\":\"test\"}", ContentType.APPLICATION_JSON); |
85 |
| - Response response = getRestClient().performRequest("PUT", path, Collections.emptyMap(), body, basicAuthHeader); |
86 |
| - assertThat(response.getStatusLine().getStatusCode(), equalTo(201)); |
| 80 | + Request createRequest = new Request("PUT", path); |
| 81 | + createRequest.setOptions(options); |
| 82 | + createRequest.setJsonEntity("{\"test\":\"test\"}"); |
| 83 | + getRestClient().performRequest(createRequest); |
87 | 84 |
|
88 |
| - response = getRestClient().performRequest("GET", path, basicAuthHeader); |
89 |
| - assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
90 |
| - assertThat(EntityUtils.toString(response.getEntity()), containsString("\"test\":\"test\"")); |
| 85 | + Request getRequest = new Request("GET", path); |
| 86 | + getRequest.setOptions(options); |
| 87 | + assertThat(EntityUtils.toString(getRestClient().performRequest(getRequest).getEntity()), containsString("\"test\":\"test\"")); |
91 | 88 |
|
92 | 89 | if (randomBoolean()) {
|
93 | 90 | flushAndRefresh();
|
94 | 91 | }
|
95 | 92 |
|
96 | 93 | //update with new field
|
97 |
| - body = new StringEntity("{\"doc\": {\"not test\": \"not test\"}}", ContentType.APPLICATION_JSON); |
98 |
| - response = getRestClient().performRequest("POST", path + "/_update", Collections.emptyMap(), body, basicAuthHeader); |
99 |
| - assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
| 94 | + Request updateRequest = new Request("POST", path + "/_update"); |
| 95 | + updateRequest.setOptions(options); |
| 96 | + updateRequest.setJsonEntity("{\"doc\": {\"not test\": \"not test\"}}"); |
| 97 | + getRestClient().performRequest(updateRequest); |
100 | 98 |
|
101 |
| - response = getRestClient().performRequest("GET", path, basicAuthHeader); |
102 |
| - assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
103 |
| - String responseBody = EntityUtils.toString(response.getEntity()); |
104 |
| - assertThat(responseBody, containsString("\"test\":\"test\"")); |
105 |
| - assertThat(responseBody, containsString("\"not test\":\"not test\"")); |
| 99 | + String afterUpdate = EntityUtils.toString(getRestClient().performRequest(getRequest).getEntity()); |
| 100 | + assertThat(afterUpdate, containsString("\"test\":\"test\"")); |
| 101 | + assertThat(afterUpdate, containsString("\"not test\":\"not test\"")); |
106 | 102 |
|
107 | 103 | // this part is important. Without this, the document may be read from the translog which would bypass the bug where
|
108 | 104 | // FLS kicks in because the request can't be found and only returns meta fields
|
109 | 105 | flushAndRefresh();
|
110 | 106 |
|
111 |
| - body = new StringEntity("{\"update\": {\"_index\": \"index1\", \"_type\": \"type\", \"_id\": \"1\"}}\n" + |
112 |
| - "{\"doc\": {\"bulk updated\":\"bulk updated\"}}\n", ContentType.APPLICATION_JSON); |
113 |
| - response = getRestClient().performRequest("POST", "/_bulk", Collections.emptyMap(), body, basicAuthHeader); |
114 |
| - assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
115 |
| - |
116 |
| - response = getRestClient().performRequest("GET", path, basicAuthHeader); |
117 |
| - responseBody = EntityUtils.toString(response.getEntity()); |
118 |
| - assertThat(responseBody, containsString("\"test\":\"test\"")); |
119 |
| - assertThat(responseBody, containsString("\"not test\":\"not test\"")); |
120 |
| - assertThat(responseBody, containsString("\"bulk updated\":\"bulk updated\"")); |
| 107 | + Request bulkRequest = new Request("POST", "/_bulk"); |
| 108 | + bulkRequest.setOptions(options); |
| 109 | + bulkRequest.setJsonEntity( |
| 110 | + "{\"update\": {\"_index\": \"index1\", \"_type\": \"type\", \"_id\": \"1\"}}\n" + |
| 111 | + "{\"doc\": {\"bulk updated\":\"bulk updated\"}}\n"); |
| 112 | + getRestClient().performRequest(bulkRequest); |
| 113 | + |
| 114 | + String afterBulk = EntityUtils.toString(getRestClient().performRequest(getRequest).getEntity()); |
| 115 | + assertThat(afterBulk, containsString("\"test\":\"test\"")); |
| 116 | + assertThat(afterBulk, containsString("\"not test\":\"not test\"")); |
| 117 | + assertThat(afterBulk, containsString("\"bulk updated\":\"bulk updated\"")); |
121 | 118 | }
|
122 | 119 | }
|
0 commit comments