diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/client/WatchSourceBuilder.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/client/WatchSourceBuilder.java index b68ebf8e59f..4e82a0b785e 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/client/WatchSourceBuilder.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/client/WatchSourceBuilder.java @@ -5,18 +5,15 @@ */ package org.elasticsearch.xpack.watcher.client; -import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.action.support.ToXContentToBytes; import org.elasticsearch.common.Nullable; -import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.xpack.watcher.actions.Action; import org.elasticsearch.xpack.watcher.actions.throttler.Throttler; -import org.elasticsearch.xpack.watcher.condition.Condition; import org.elasticsearch.xpack.watcher.condition.AlwaysCondition; +import org.elasticsearch.xpack.watcher.condition.Condition; import org.elasticsearch.xpack.watcher.input.Input; import org.elasticsearch.xpack.watcher.input.none.NoneInput; import org.elasticsearch.xpack.watcher.support.Exceptions; @@ -31,7 +28,7 @@ import java.util.Map; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; -public class WatchSourceBuilder implements ToXContent { +public class WatchSourceBuilder extends ToXContentToBytes implements ToXContent { private Trigger trigger; private Input input = NoneInput.INSTANCE; @@ -165,17 +162,6 @@ public class WatchSourceBuilder implements ToXContent { return builder.endObject(); } - public BytesReference buildAsBytes(XContentType contentType) { - try { - XContentBuilder builder = XContentFactory.contentBuilder(contentType); - toXContent(builder, ToXContent.EMPTY_PARAMS); - return builder.bytes(); - } catch (IOException ioe) { - // todo think of a better std exception for this - throw new ElasticsearchException("failed to render watch source as bytes", ioe); - } - } - static class TransformedAction implements ToXContent { private final String id; diff --git a/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/transport/action/put/PutWatchTests.java b/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/transport/action/put/PutWatchTests.java index 2cb5b78ce46..ddc9c6c2220 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/transport/action/put/PutWatchTests.java +++ b/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/transport/action/put/PutWatchTests.java @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.watcher.transport.action.put; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.xpack.watcher.client.WatchSourceBuilder; import org.elasticsearch.xpack.watcher.condition.AlwaysCondition; import org.elasticsearch.xpack.watcher.test.AbstractWatcherIntegrationTestCase; @@ -45,15 +46,12 @@ public class PutWatchTests extends AbstractWatcherIntegrationTestCase { public void testPutNoTrigger() throws Exception { ensureWatcherStarted(); - try { - watcherClient().preparePutWatch("_name").setSource(watchBuilder() - .input(simpleInput()) - .condition(AlwaysCondition.INSTANCE) - .addAction("_action1", loggingAction("{{ctx.watch_id}}"))) - .get(); - fail("Expected IllegalStateException"); - } catch (IllegalStateException e) { - assertThat(e.getMessage(), is("failed to build watch source. no trigger defined")); - } + ElasticsearchException exception = expectThrows(ElasticsearchException.class, + () -> watcherClient().preparePutWatch("_name").setSource(watchBuilder() + .input(simpleInput()) + .condition(AlwaysCondition.INSTANCE) + .addAction("_action1", loggingAction("{{ctx.watch_id}}"))) + .get()); + assertEquals("Failed to build ToXContent", exception.getMessage()); } }