From 8b6a0daa05716a053c60aadff4c3bf581b9e6085 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Mon, 11 Sep 2017 14:37:57 -0400 Subject: [PATCH] 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@16cca77ccf6a825ac2f61a360f801abb7a5a4c32 --- .../xpack/sql/plugin/SqlGetIndicesAction.java | 20 ++++ .../sql/plugin/SqlGetIndicesRequestTests.java | 97 +++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 sql/server/src/test/java/org/elasticsearch/xpack/sql/plugin/SqlGetIndicesRequestTests.java diff --git a/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlGetIndicesAction.java b/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlGetIndicesAction.java index 351fbb09fab..661d3cd27f0 100644 --- a/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlGetIndicesAction.java +++ b/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlGetIndicesAction.java @@ -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 { diff --git a/sql/server/src/test/java/org/elasticsearch/xpack/sql/plugin/SqlGetIndicesRequestTests.java b/sql/server/src/test/java/org/elasticsearch/xpack/sql/plugin/SqlGetIndicesRequestTests.java new file mode 100644 index 00000000000..1dcecaa5efd --- /dev/null +++ b/sql/server/src/test/java/org/elasticsearch/xpack/sql/plugin/SqlGetIndicesRequestTests.java @@ -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 { + @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 getMutateFunction() { + return SqlGetIndicesRequestTests::mutate; + } + + private static Request mutate(Request request) { + @SuppressWarnings("unchecked") + Supplier 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()); + } +}