Skip to content

Commit 69481b4

Browse files
authored
LLRest: Add equals and hashcode tests for Request (#30584)
Adds tests for the `Request` object's equals and hashcode to remove a `TODO` and because we use the `equals` method in other testing.
1 parent 56d32bc commit 69481b4

File tree

1 file changed

+101
-1
lines changed

1 file changed

+101
-1
lines changed

client/rest/src/test/java/org/elasticsearch/client/RequestTests.java

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@
2626

2727
import org.apache.http.Header;
2828
import org.apache.http.HttpEntity;
29+
import org.apache.http.entity.ByteArrayEntity;
2930
import org.apache.http.entity.ContentType;
3031
import org.apache.http.entity.StringEntity;
3132
import org.apache.http.message.BasicHeader;
3233
import org.apache.http.nio.entity.NStringEntity;
34+
import org.elasticsearch.client.HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory;
3335

3436
import static org.junit.Assert.assertArrayEquals;
3537
import static org.junit.Assert.assertEquals;
38+
import static org.junit.Assert.assertNotEquals;
3639
import static org.junit.Assert.assertNull;
3740
import static org.junit.Assert.fail;
3841

@@ -151,6 +154,103 @@ public void testSetHeaders() {
151154
assertArrayEquals(headers, request.getHeaders());
152155
}
153156

154-
// TODO equals and hashcode
157+
public void testEqualsAndHashCode() {
158+
Request request = randomRequest();
159+
assertEquals(request, request);
155160

161+
Request copy = copy(request);
162+
assertEquals(request, copy);
163+
assertEquals(copy, request);
164+
assertEquals(request.hashCode(), copy.hashCode());
165+
166+
Request mutant = mutate(request);
167+
assertNotEquals(request, mutant);
168+
assertNotEquals(mutant, request);
169+
}
170+
171+
private Request randomRequest() {
172+
Request request = new Request(
173+
randomFrom(new String[] {"GET", "PUT", "DELETE", "POST", "HEAD", "OPTIONS"}),
174+
randomAsciiAlphanumOfLength(5));
175+
176+
int parameterCount = between(0, 5);
177+
for (int i = 0; i < parameterCount; i++) {
178+
request.addParameter(randomAsciiAlphanumOfLength(i), randomAsciiLettersOfLength(3));
179+
}
180+
181+
if (randomBoolean()) {
182+
if (randomBoolean()) {
183+
request.setJsonEntity(randomAsciiAlphanumOfLength(10));
184+
} else {
185+
request.setEntity(randomFrom(new HttpEntity[] {
186+
new StringEntity(randomAsciiAlphanumOfLength(10), ContentType.APPLICATION_JSON),
187+
new NStringEntity(randomAsciiAlphanumOfLength(10), ContentType.APPLICATION_JSON),
188+
new ByteArrayEntity(randomBytesOfLength(40), ContentType.APPLICATION_JSON)
189+
}));
190+
}
191+
}
192+
193+
if (randomBoolean()) {
194+
int headerCount = between(1, 5);
195+
Header[] headers = new Header[headerCount];
196+
for (int i = 0; i < headerCount; i++) {
197+
headers[i] = new BasicHeader(randomAsciiAlphanumOfLength(3), randomAsciiAlphanumOfLength(3));
198+
}
199+
request.setHeaders(headers);
200+
}
201+
202+
if (randomBoolean()) {
203+
request.setHttpAsyncResponseConsumerFactory(new HeapBufferedResponseConsumerFactory(1));
204+
}
205+
206+
return request;
207+
}
208+
209+
private Request copy(Request request) {
210+
Request copy = new Request(request.getMethod(), request.getEndpoint());
211+
copyMutables(request, copy);
212+
return copy;
213+
}
214+
215+
private Request mutate(Request request) {
216+
if (randomBoolean()) {
217+
// Mutate request or method but keep everything else constant
218+
Request mutant = randomBoolean()
219+
? new Request(request.getMethod() + "m", request.getEndpoint())
220+
: new Request(request.getMethod(), request.getEndpoint() + "m");
221+
copyMutables(request, mutant);
222+
return mutant;
223+
}
224+
Request mutant = copy(request);
225+
int mutationType = between(0, 3);
226+
switch (mutationType) {
227+
case 0:
228+
mutant.addParameter(randomAsciiAlphanumOfLength(mutant.getParameters().size() + 4), "extra");
229+
return mutant;
230+
case 1:
231+
mutant.setJsonEntity("mutant"); // randomRequest can't produce this value
232+
return mutant;
233+
case 2:
234+
if (mutant.getHeaders().length > 0) {
235+
mutant.setHeaders(new Header[0]);
236+
} else {
237+
mutant.setHeaders(new BasicHeader("extra", "m"));
238+
}
239+
return mutant;
240+
case 3:
241+
mutant.setHttpAsyncResponseConsumerFactory(new HeapBufferedResponseConsumerFactory(5));
242+
return mutant;
243+
default:
244+
throw new UnsupportedOperationException("Unknown mutation type [" + mutationType + "]");
245+
}
246+
}
247+
248+
private void copyMutables(Request from, Request to) {
249+
for (Map.Entry<String, String> param : from.getParameters().entrySet()) {
250+
to.addParameter(param.getKey(), param.getValue());
251+
}
252+
to.setEntity(from.getEntity());
253+
to.setHeaders(from.getHeaders());
254+
to.setHttpAsyncResponseConsumerFactory(from.getHttpAsyncResponseConsumerFactory());
255+
}
156256
}

0 commit comments

Comments
 (0)