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