Skip to content

Commit

Permalink
Add round trip tests for request and response
Browse files Browse the repository at this point in the history
  • Loading branch information
nik9000 committed Aug 16, 2016
1 parent 47271a8 commit c93f9dc
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static org.elasticsearch.action.ValidateActions.addValidationError;
Expand Down Expand Up @@ -525,4 +526,38 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeBoolean(updateAllTypes);
waitForActiveShards.writeTo(out);
}

@Override
public boolean equals(Object obj) {
if (getClass() != obj.getClass()) return false;
CreateIndexRequest other = (CreateIndexRequest) obj;
return Objects.equals(cause, other.cause)
&& Objects.equals(index, other.index)
&& Objects.equals(settings, other.settings)
&& Objects.equals(timeout, other.timeout)
&& Objects.equals(mappings, other.mappings)
&& Objects.equals(customs, other.customs)
&& Objects.equals(aliases, other.aliases)
&& Objects.equals(updateAllTypes, other.updateAllTypes)
&& Objects.equals(waitForActiveShards, other.waitForActiveShards);
}

@Override
public int hashCode() {
return Objects.hash(cause, index, settings, timeout, mappings, customs, aliases, updateAllTypes, waitForActiveShards);
}

@Override
public String toString() {
return "CreateIndex["
+ "cause=" + cause
+ ",index=" + index
+ ",settings=" + settings
+ ",timeout=" + timeout
+ ",mappings=" + mappings
+ ",customs=" + customs
+ ",aliases=" + aliases
+ ",updateAllTypes=" + updateAllTypes
+ ",waitForActiveShards=" + waitForActiveShards + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
*/
public class MigrateIndexRequest extends AcknowledgedRequest<MigrateIndexRequest> implements IndicesRequest {
private String sourceIndex;
private Script script;
private CreateIndexRequest createIndexRequest;
private Script script;

/**
* Build an empty request.
Expand Down Expand Up @@ -75,18 +75,20 @@ public ActionRequestValidationException validate() {
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
readTimeout(in);
sourceIndex = in.readString();
script = in.readOptionalWriteable(Script::new);
createIndexRequest = new CreateIndexRequest();
createIndexRequest.readFrom(in);
script = in.readOptionalWriteable(Script::new);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
writeTimeout(out);
out.writeString(sourceIndex);
out.writeOptionalWriteable(script);
createIndexRequest.writeTo(out);
out.writeOptionalWriteable(script);
}

@Override
Expand Down Expand Up @@ -145,4 +147,32 @@ public String[] indices() {
public IndicesOptions indicesOptions() {
return IndicesOptions.strictSingleIndexNoExpandForbidClosed();
}

@Override
public String toString() {
return "MigrateIndex["
+ "source=" + sourceIndex
+ ",create=" + createIndexRequest
+ ",script=" + script
+ ",timeout=" + timeout
+ ",masterNodeTimeout=" + masterNodeTimeout
+ ",parentTask=" + getParentTask() + "]";
}

@Override
public boolean equals(Object obj) {
if (getClass() != obj.getClass()) return false;
MigrateIndexRequest other = (MigrateIndexRequest) obj;
return Objects.equals(sourceIndex, other.sourceIndex)
&& Objects.equals(createIndexRequest, other.createIndexRequest)
&& Objects.equals(script, other.script)
&& Objects.equals(timeout, other.timeout)
&& Objects.equals(masterNodeTimeout, other.masterNodeTimeout)
&& Objects.equals(getParentTask(), other.getParentTask());
}

@Override
public int hashCode() {
return Objects.hash(sourceIndex, script, createIndexRequest, timeout, masterNodeTimeout, getParentTask());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
package org.elasticsearch.action.admin.indices.migrate;

import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.Objects;

public class MigrateIndexResponse extends AcknowledgedResponse implements ToXContent {
private boolean noop; // NOCOMMIT we can do better. Probably like reindex, make the task status useful and capture that in the response.
Expand Down Expand Up @@ -65,4 +67,23 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.field("noop", isNoop());
return builder;
}

@Override
public String toString() {
return Strings.toString(this, true);
}

@Override
public boolean equals(Object obj) {
if (getClass() != obj.getClass()) return false;
MigrateIndexResponse other = (MigrateIndexResponse) obj;
return Objects.equals(isAcknowledged(), other.isAcknowledged())
&& Objects.equals(noop, other.noop);
}

@Override
public int hashCode() {
return Objects.hash(isAcknowledged(), noop);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import org.elasticsearch.tasks.Task;
import org.elasticsearch.tasks.TaskId;

/**
* Task subclass that supports getting information about the currently running migration.
*/
public class MigrateIndexTask extends Task {
private volatile Operation operation;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.action.admin.indices.migrate;

import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService.ScriptType;
import org.elasticsearch.test.ESTestCase;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.Matchers.containsString;

public class MigrateIndexRequestTests extends ESTestCase {
public void testRoundTripThroughTransport() throws IOException {
MigrateIndexRequest original = randomRequest();
try (BytesStreamOutput out = new BytesStreamOutput()) {
original.writeTo(out);
try (StreamInput in = out.bytes().streamInput()) {
MigrateIndexRequest read = new MigrateIndexRequest();
read.readFrom(in);
assertEquals(original, read);
}
}
}

public void testToStringIsSane() {
String string = randomRequest().toString();
assertThat(string, containsString("MigrateIndex["));
assertThat(string, containsString("source="));
assertThat(string, containsString("create="));
assertThat(string, containsString("script="));
assertThat(string, containsString("timeout="));
assertThat(string, containsString("masterNodeTimeout="));
assertThat(string, containsString("parentTask="));
}

private MigrateIndexRequest randomRequest() {
MigrateIndexRequest request = new MigrateIndexRequest(randomAsciiOfLength(5), randomAsciiOfLength(5));
int settingsCount = between(0, 5);
if (settingsCount > 0) {
Settings.Builder settings = Settings.builder();
for (int i = 0; i < settingsCount; i++) {
settings.put(randomAsciiOfLength(5), randomAsciiOfLength(5));
}
request.getCreateIndexRequest().settings(settings);
}
int typesCount = between(0, 5);
if (typesCount > 0) {
for (int i = 0; i < typesCount; i++) {
Map<String, Object> mapping = new HashMap<>();
int mappingSize = between(0, 5);
for (int p = 0; p < mappingSize; p++) {
mapping.put(randomAsciiOfLength(5), randomAsciiOfLength(5));
}
request.getCreateIndexRequest().mapping(randomAsciiOfLength(5), mapping);
}
}
int aliasCount = between(0, 5);
for (int i = 0; i < aliasCount; i++) {
request.getCreateIndexRequest().alias(randomAlias());
}
if (randomBoolean()) {
request.setScript(randomScript());
}
if (randomBoolean()) {
request.timeout(randomPositiveTimeValue());
}
if (randomBoolean()) {
request.masterNodeTimeout(randomPositiveTimeValue());
}
if (randomBoolean()) {
request.setParentTask(randomAsciiOfLength(5), randomLong());
}
return request;
}

private Alias randomAlias() {
Alias alias = new Alias(randomAsciiOfLength(5));
if (randomBoolean()) {
// We don't need a valid query for this test which is nice because random queries are hard
alias.filter(randomAsciiOfLength(10));
}
if (randomBoolean()) {
alias.indexRouting(randomAsciiOfLength(5));
}
if (randomBoolean()) {
alias.searchRouting(randomAsciiOfLength(5));
}
return alias;
}

private Script randomScript() {
int paramsLength = between(0, 10);
Map<String, Object> params = new HashMap<>(paramsLength);
for (int i = 0; i < paramsLength; i++) {
params.put(randomAsciiOfLength(5), randomAsciiOfLength(5));
}
return new Script(randomAsciiOfLength(5), randomFrom(ScriptType.values()), randomAsciiOfLength(5), params);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.action.admin.indices.migrate;

import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.test.ESTestCase;

import java.io.IOException;

import static org.hamcrest.Matchers.containsString;

public class MigrateIndexResponseTests extends ESTestCase {
public void testRoundTripThroughTransport() throws IOException {
MigrateIndexResponse original = randomResponse();
try (BytesStreamOutput out = new BytesStreamOutput()) {
original.writeTo(out);
try (StreamInput in = out.bytes().streamInput()) {
MigrateIndexResponse read = new MigrateIndexResponse();
read.readFrom(in);
assertEquals(original, read);
}
}
}

public void testToStringIsSane() {
String string = randomResponse().toString();
assertThat(string, containsString("\"acknowledged\":"));
assertThat(string, containsString("\"noop\":"));
}

private MigrateIndexResponse randomResponse() {
return new MigrateIndexResponse(randomBoolean(), randomBoolean());
}
}

0 comments on commit c93f9dc

Please sign in to comment.