WatchSourceBuilder to extend ToXContentToBytes

Original commit: elastic/x-pack-elasticsearch@b97ad8f92c
This commit is contained in:
javanna 2017-01-05 23:05:35 +01:00 committed by Luca Cavanna
parent c23de42f29
commit 0b93bc98bf
2 changed files with 11 additions and 27 deletions

View File

@ -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;

View File

@ -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());
}
}