|
26 | 26 |
|
27 | 27 | import org.apache.http.Header;
|
28 | 28 | import org.apache.http.HttpEntity;
|
| 29 | +import org.apache.http.entity.ByteArrayEntity; |
29 | 30 | import org.apache.http.entity.ContentType;
|
30 | 31 | import org.apache.http.entity.StringEntity;
|
31 | 32 | import org.apache.http.message.BasicHeader;
|
32 | 33 | import org.apache.http.nio.entity.NStringEntity;
|
| 34 | +import org.elasticsearch.client.HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory; |
33 | 35 |
|
34 | 36 | import static org.junit.Assert.assertArrayEquals;
|
35 | 37 | import static org.junit.Assert.assertEquals;
|
| 38 | +import static org.junit.Assert.assertNotEquals; |
36 | 39 | import static org.junit.Assert.assertNull;
|
37 | 40 | import static org.junit.Assert.fail;
|
38 | 41 |
|
@@ -151,6 +154,103 @@ public void testSetHeaders() {
|
151 | 154 | assertArrayEquals(headers, request.getHeaders());
|
152 | 155 | }
|
153 | 156 |
|
154 |
| - // TODO equals and hashcode |
| 157 | + public void testEqualsAndHashCode() { |
| 158 | + Request request = randomRequest(); |
| 159 | + assertEquals(request, request); |
155 | 160 |
|
| 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 | + } |
156 | 256 | }
|
0 commit comments