Add tests for SqlGetIndicesAction.Request serialization (elastic/x-pack-elasticsearch#2452)
This request is already serializeable so we may as well add tests for that so we don't break it. Original commit: elastic/x-pack-elasticsearch@16cca77ccf
This commit is contained in:
parent
94de0d8041
commit
8b6a0daa05
|
@ -33,8 +33,10 @@ import org.elasticsearch.xpack.sql.analysis.catalog.EsIndex;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static java.util.Comparator.comparing;
|
||||
|
@ -114,6 +116,24 @@ public class SqlGetIndicesAction
|
|||
this.indicesOptions = indicesOptions;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == null || obj.getClass() != getClass()) {
|
||||
return false;
|
||||
}
|
||||
Request other = (Request) obj;
|
||||
return Arrays.equals(indices, other.indices)
|
||||
&& indicesOptions.equals(other.indicesOptions)
|
||||
&& local == other.local
|
||||
&& masterNodeTimeout.equals(other.masterNodeTimeout)
|
||||
&& getParentTask().equals(other.getParentTask());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(Arrays.hashCode(indices), indicesOptions, local, masterNodeTimeout, getParentTask());
|
||||
}
|
||||
}
|
||||
|
||||
public static class RequestBuilder extends MasterNodeReadOperationRequestBuilder<Request, Response, RequestBuilder> {
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.xpack.sql.plugin;
|
||||
|
||||
import org.elasticsearch.action.support.IndicesOptions;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.tasks.TaskId;
|
||||
import org.elasticsearch.test.AbstractStreamableTestCase;
|
||||
import org.elasticsearch.test.EqualsHashCodeTestUtils.MutateFunction;
|
||||
import org.elasticsearch.xpack.sql.plugin.SqlGetIndicesAction.Request;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class SqlGetIndicesRequestTests extends AbstractStreamableTestCase<SqlGetIndicesAction.Request> {
|
||||
@Override
|
||||
protected Request createTestInstance() {
|
||||
Request request = new Request(randomIndicesOptions(), randomIndices());
|
||||
request.local(randomBoolean());
|
||||
request.masterNodeTimeout(randomTimeValue());
|
||||
request.setParentTask(randomTaskId());
|
||||
return request;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Request createBlankInstance() {
|
||||
return new Request();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MutateFunction<Request> getMutateFunction() {
|
||||
return SqlGetIndicesRequestTests::mutate;
|
||||
}
|
||||
|
||||
private static Request mutate(Request request) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Supplier<Request> supplier = randomFrom(
|
||||
() -> {
|
||||
Request mutant = new Request(
|
||||
randomValueOtherThan(request.indicesOptions(), SqlGetIndicesRequestTests::randomIndicesOptions),
|
||||
request.indices());
|
||||
mutant.local(request.local());
|
||||
mutant.masterNodeTimeout(request.masterNodeTimeout());
|
||||
mutant.setParentTask(request.getParentTask());
|
||||
return mutant;
|
||||
},
|
||||
() -> {
|
||||
Request mutant = new Request(
|
||||
request.indicesOptions(),
|
||||
randomValueOtherThanMany(i -> Arrays.equals(request.indices(), i), SqlGetIndicesRequestTests::randomIndices));
|
||||
mutant.local(request.local());
|
||||
mutant.masterNodeTimeout(request.masterNodeTimeout());
|
||||
mutant.setParentTask(request.getParentTask());
|
||||
return mutant;
|
||||
}, () -> {
|
||||
Request mutant = new Request(request.indicesOptions(), request.indices());
|
||||
mutant.local(false == request.local());
|
||||
mutant.masterNodeTimeout(request.masterNodeTimeout());
|
||||
mutant.setParentTask(request.getParentTask());
|
||||
return mutant;
|
||||
}, () -> {
|
||||
Request mutant = new Request(request.indicesOptions(), request.indices());
|
||||
mutant.local(request.local());
|
||||
mutant.masterNodeTimeout(randomValueOtherThan(request.masterNodeTimeout(),
|
||||
() -> TimeValue.parseTimeValue(randomTimeValue(), "test")));
|
||||
mutant.setParentTask(request.getParentTask());
|
||||
return mutant;
|
||||
}, () -> {
|
||||
Request mutant = new Request(request.indicesOptions(), request.indices());
|
||||
mutant.local(false == request.local());
|
||||
mutant.masterNodeTimeout(request.masterNodeTimeout());
|
||||
mutant.setParentTask(randomValueOtherThan(request.getParentTask(), SqlGetIndicesRequestTests::randomTaskId));
|
||||
return mutant;
|
||||
});
|
||||
return supplier.get();
|
||||
}
|
||||
|
||||
private static IndicesOptions randomIndicesOptions() {
|
||||
return IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(),
|
||||
randomBoolean(), randomBoolean());
|
||||
}
|
||||
|
||||
private static String[] randomIndices() {
|
||||
String[] indices = new String[between(1, 10)];
|
||||
for (int i = 0; i < indices.length; i++) {
|
||||
indices[i] = randomAlphaOfLength(5);
|
||||
}
|
||||
return indices;
|
||||
}
|
||||
|
||||
private static TaskId randomTaskId() {
|
||||
return randomBoolean() ? TaskId.EMPTY_TASK_ID : new TaskId(randomAlphaOfLength(5), randomLong());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue