Watcher: Fix equals/hashcode for WatchStatus (elastic/x-pack-elasticsearch#3105)

This was missed in elastic/x-pack-elasticsearch#3103 and fixes to add the headers variable to both
methods to ensure comparisons work as expected.

Original commit: elastic/x-pack-elasticsearch@df5e422698
This commit is contained in:
Alexander Reelsen 2017-11-24 15:32:25 +01:00 committed by GitHub
parent e4e8870b13
commit d89d8abec9
2 changed files with 29 additions and 3 deletions

View File

@ -121,12 +121,13 @@ public class WatchStatus implements ToXContentObject, Streamable {
Objects.equals(lastMetCondition, that.lastMetCondition) &&
Objects.equals(version, that.version) &&
Objects.equals(executionState, that.executionState) &&
Objects.equals(actions, that.actions);
Objects.equals(actions, that.actions) &&
Objects.equals(headers, that.headers);
}
@Override
public int hashCode() {
return Objects.hash(lastChecked, lastMetCondition, actions, version, executionState);
return Objects.hash(lastChecked, lastMetCondition, actions, version, executionState, headers);
}
/**

View File

@ -5,6 +5,8 @@
*/
package org.elasticsearch.xpack.watcher.watch;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
@ -14,11 +16,13 @@ import org.elasticsearch.xpack.watcher.actions.ActionStatus.AckStatus.State;
import org.elasticsearch.xpack.watcher.actions.logging.LoggingAction;
import org.elasticsearch.xpack.watcher.support.xcontent.WatcherParams;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
@ -71,4 +75,25 @@ public class WatchStatusTests extends ESTestCase {
}
}
}
public void testHeadersSerialization() throws IOException {
WatchStatus status = new WatchStatus(now(), Collections.emptyMap());
String key = randomAlphaOfLength(10);
String value = randomAlphaOfLength(10);
Map<String, String> headers = Collections.singletonMap(key, value);
status.setHeaders(headers);
BytesStreamOutput out = new BytesStreamOutput();
status.writeTo(out);
BytesReference bytesReference = out.bytes();
WatchStatus readStatus = WatchStatus.read(bytesReference.streamInput());
assertThat(readStatus, is(status));
assertThat(readStatus.getHeaders(), is(headers));
// test equals
assertThat(readStatus.hashCode(), is(status.hashCode()));
assertThat(readStatus, equalTo(status));
readStatus.getHeaders().clear();
assertThat(readStatus, not(equalTo(status)));
}
}