diff --git a/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/DeleteLifecycleAction.java b/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/DeleteLifecycleAction.java index 2743af20afa..939248e15d6 100644 --- a/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/DeleteLifecycleAction.java +++ b/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/DeleteLifecycleAction.java @@ -23,6 +23,7 @@ import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -32,10 +33,10 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; import org.elasticsearch.xpack.indexlifecycle.IndexLifecycleMetadata; -import org.elasticsearch.xpack.indexlifecycle.LifecycleAction; import org.elasticsearch.xpack.indexlifecycle.LifecyclePolicy; import java.io.IOException; +import java.util.Objects; import java.util.SortedMap; import java.util.TreeMap; @@ -83,6 +84,38 @@ public class DeleteLifecycleAction return builder; } + @Override + public void readFrom(StreamInput in) throws IOException { + readAcknowledged(in); + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + writeAcknowledged(out); + } + + @Override + public int hashCode() { + return Objects.hash(isAcknowledged()); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (obj.getClass() != getClass()) { + return false; + } + Response other = (Response) obj; + return Objects.equals(isAcknowledged(), other.isAcknowledged()); + } + + @Override + public String toString() { + return Strings.toString(this, true, true); + } + } public static class Request extends AcknowledgedRequest { @@ -119,6 +152,23 @@ public class DeleteLifecycleAction out.writeString(policyName); } + @Override + public int hashCode() { + return Objects.hash(policyName); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (obj.getClass() != getClass()) { + return false; + } + Request other = (Request) obj; + return Objects.equals(policyName, other.policyName); + } + } public static class TransportAction extends TransportMasterNodeAction { diff --git a/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/GetLifecycleAction.java b/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/GetLifecycleAction.java index d481ea0988d..6305cc05aca 100644 --- a/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/GetLifecycleAction.java +++ b/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/GetLifecycleAction.java @@ -21,6 +21,7 @@ import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -33,6 +34,7 @@ import org.elasticsearch.xpack.indexlifecycle.IndexLifecycleMetadata; import org.elasticsearch.xpack.indexlifecycle.LifecyclePolicy; import java.io.IOException; +import java.util.Objects; public class GetLifecycleAction extends Action { @@ -63,10 +65,9 @@ public class GetLifecycleAction public static class Response extends ActionResponse implements ToXContentObject { - private final LifecyclePolicy policy; + private LifecyclePolicy policy; - public Response() { - this(null); + Response() { } public Response(LifecyclePolicy policy) { @@ -79,6 +80,38 @@ public class GetLifecycleAction return builder; } + @Override + public void readFrom(StreamInput in) throws IOException { + policy = new LifecyclePolicy(in); + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + policy.writeTo(out); + } + + @Override + public int hashCode() { + return Objects.hash(policy); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (obj.getClass() != getClass()) { + return false; + } + Response other = (Response) obj; + return Objects.equals(policy, other.policy); + } + + @Override + public String toString() { + return Strings.toString(this, true, true); + } + } public static class Request extends AcknowledgedRequest { @@ -115,6 +148,23 @@ public class GetLifecycleAction out.writeString(policyName); } + @Override + public int hashCode() { + return Objects.hash(policyName); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (obj.getClass() != getClass()) { + return false; + } + Request other = (Request) obj; + return Objects.equals(policyName, other.policyName); + } + } public static class TransportAction extends TransportMasterNodeAction { diff --git a/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/PutLifecycleAction.java b/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/PutLifecycleAction.java index cfcf51ed8ff..412c58c7539 100644 --- a/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/PutLifecycleAction.java +++ b/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/PutLifecycleAction.java @@ -23,6 +23,7 @@ import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.io.stream.StreamInput; @@ -39,6 +40,7 @@ import org.elasticsearch.xpack.indexlifecycle.IndexLifecycleMetadata; import org.elasticsearch.xpack.indexlifecycle.LifecyclePolicy; import java.io.IOException; +import java.util.Objects; import java.util.SortedMap; import java.util.TreeMap; @@ -85,6 +87,38 @@ public class PutLifecycleAction extends Action implements ToXContentObject { @@ -138,6 +172,28 @@ public class PutLifecycleAction extends Action { diff --git a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleMetadataTests.java b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleMetadataTests.java index 950563e8a76..fdc6182a092 100644 --- a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleMetadataTests.java +++ b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleMetadataTests.java @@ -34,7 +34,7 @@ public class IndexLifecycleMetadataTests extends AbstractSerializingTestCase policies = new TreeMap<>(); for (int i = 0; i < numPolicies; i++) { int numberPhases = randomInt(5); diff --git a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/action/DeleteLifecycleRequestTests.java b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/action/DeleteLifecycleRequestTests.java new file mode 100644 index 00000000000..16f3fcf7f49 --- /dev/null +++ b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/action/DeleteLifecycleRequestTests.java @@ -0,0 +1,23 @@ +/* + * 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.indexlifecycle.action; + +import org.elasticsearch.test.AbstractStreamableTestCase; +import org.elasticsearch.xpack.indexlifecycle.action.DeleteLifecycleAction.Request; + +public class DeleteLifecycleRequestTests extends AbstractStreamableTestCase { + + @Override + protected Request createTestInstance() { + return new Request(randomAlphaOfLengthBetween(1, 20)); + } + + @Override + protected Request createBlankInstance() { + return new Request(); + } + +} diff --git a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/action/DeleteLifecycleResponseTests.java b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/action/DeleteLifecycleResponseTests.java new file mode 100644 index 00000000000..80b13800f80 --- /dev/null +++ b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/action/DeleteLifecycleResponseTests.java @@ -0,0 +1,23 @@ +/* + * 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.indexlifecycle.action; + +import org.elasticsearch.test.AbstractStreamableTestCase; +import org.elasticsearch.xpack.indexlifecycle.action.DeleteLifecycleAction.Response; + +public class DeleteLifecycleResponseTests extends AbstractStreamableTestCase { + + @Override + protected Response createTestInstance() { + return new Response(randomBoolean()); + } + + @Override + protected Response createBlankInstance() { + return new Response(); + } + +} diff --git a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/action/GetLifecycleRequestTests.java b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/action/GetLifecycleRequestTests.java new file mode 100644 index 00000000000..43b9ef4e510 --- /dev/null +++ b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/action/GetLifecycleRequestTests.java @@ -0,0 +1,23 @@ +/* + * 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.indexlifecycle.action; + +import org.elasticsearch.test.AbstractStreamableTestCase; +import org.elasticsearch.xpack.indexlifecycle.action.GetLifecycleAction.Request; + +public class GetLifecycleRequestTests extends AbstractStreamableTestCase { + + @Override + protected Request createTestInstance() { + return new Request(randomAlphaOfLengthBetween(1, 20)); + } + + @Override + protected Request createBlankInstance() { + return new Request(); + } + +} diff --git a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/action/GetLifecycleResponseTests.java b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/action/GetLifecycleResponseTests.java new file mode 100644 index 00000000000..aa4393fb805 --- /dev/null +++ b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/action/GetLifecycleResponseTests.java @@ -0,0 +1,57 @@ +/* + * 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.indexlifecycle.action; + +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; +import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.test.AbstractStreamableTestCase; +import org.elasticsearch.xpack.indexlifecycle.DeleteAction; +import org.elasticsearch.xpack.indexlifecycle.LifecycleAction; +import org.elasticsearch.xpack.indexlifecycle.LifecyclePolicy; +import org.elasticsearch.xpack.indexlifecycle.Phase; +import org.elasticsearch.xpack.indexlifecycle.action.GetLifecycleAction.Response; +import org.junit.Before; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class GetLifecycleResponseTests extends AbstractStreamableTestCase { + + private String lifecycleName; + + @Before + public void setup() { + lifecycleName = randomAlphaOfLength(20); // NOCOMMIT we need to randomise the lifecycle name rather + // than use the same name for all instances + } + + @Override + protected Response createTestInstance() { + int numberPhases = randomInt(5); + List phases = new ArrayList<>(numberPhases); + for (int i = 0; i < numberPhases; i++) { + TimeValue after = TimeValue.parseTimeValue(randomTimeValue(0, 1000000000, "s", "m", "h", "d"), "test_after"); + List actions = new ArrayList<>(); + if (randomBoolean()) { + actions.add(new DeleteAction()); + } + phases.add(new Phase(randomAlphaOfLength(10), after, actions)); + } + return new Response(new LifecyclePolicy(lifecycleName, phases)); + } + + @Override + protected Response createBlankInstance() { + return new Response(); + } + + protected NamedWriteableRegistry getNamedWriteableRegistry() { + return new NamedWriteableRegistry( + Arrays.asList(new NamedWriteableRegistry.Entry(LifecycleAction.class, DeleteAction.NAME, DeleteAction::new))); + } + +} diff --git a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/action/PutLifecycleRequestTests.java b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/action/PutLifecycleRequestTests.java new file mode 100644 index 00000000000..2259b26d7ee --- /dev/null +++ b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/action/PutLifecycleRequestTests.java @@ -0,0 +1,73 @@ +/* + * 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.indexlifecycle.action; + +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; +import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.xcontent.NamedXContentRegistry; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.test.AbstractStreamableXContentTestCase; +import org.elasticsearch.xpack.indexlifecycle.DeleteAction; +import org.elasticsearch.xpack.indexlifecycle.LifecycleAction; +import org.elasticsearch.xpack.indexlifecycle.LifecyclePolicy; +import org.elasticsearch.xpack.indexlifecycle.Phase; +import org.elasticsearch.xpack.indexlifecycle.action.PutLifecycleAction.Request; +import org.junit.Before; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class PutLifecycleRequestTests extends AbstractStreamableXContentTestCase { + + private NamedXContentRegistry registry; + private String lifecycleName; + + @Before + public void setup() { + List entries = Arrays + .asList(new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse)); + registry = new NamedXContentRegistry(entries); + lifecycleName = randomAlphaOfLength(20); // NOCOMMIT we need to randomise the lifecycle name rather + // than use the same name for all instances + } + + @Override + protected Request createTestInstance() { + int numberPhases = 1; // NOCOMMIT need to make this more than one when phase order doesn't rely on JSON map order + List phases = new ArrayList<>(numberPhases); + for (int i = 0; i < numberPhases; i++) { + TimeValue after = TimeValue.parseTimeValue(randomTimeValue(0, 1000000000, "s", "m", "h", "d"), "test_after"); + List actions = new ArrayList<>(); + if (randomBoolean()) { + actions.add(new DeleteAction()); + } + phases.add(new Phase(randomAlphaOfLength(10), after, actions)); + } + return new Request(new LifecyclePolicy(lifecycleName, phases)); + } + + @Override + protected Request createBlankInstance() { + return new Request(); + } + + @Override + protected Request doParseInstance(XContentParser parser) { + return PutLifecycleAction.Request.parseRequest(lifecycleName, parser, registry); + } + + protected NamedWriteableRegistry getNamedWriteableRegistry() { + return new NamedWriteableRegistry( + Arrays.asList(new NamedWriteableRegistry.Entry(LifecycleAction.class, DeleteAction.NAME, DeleteAction::new))); + } + + protected boolean supportsUnknownFields() { + return false; + } + +} diff --git a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/action/PutLifecycleResponseTests.java b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/action/PutLifecycleResponseTests.java new file mode 100644 index 00000000000..72c0ef48374 --- /dev/null +++ b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/action/PutLifecycleResponseTests.java @@ -0,0 +1,23 @@ +/* + * 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.indexlifecycle.action; + +import org.elasticsearch.test.AbstractStreamableTestCase; +import org.elasticsearch.xpack.indexlifecycle.action.PutLifecycleAction.Response; + +public class PutLifecycleResponseTests extends AbstractStreamableTestCase { + + @Override + protected Response createTestInstance() { + return new Response(randomBoolean()); + } + + @Override + protected Response createBlankInstance() { + return new Response(); + } + +}