Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watcher: Fix chain input toXcontent serialization #31721

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
"Test get watch api with chained input and basic auth":
- do:
cluster.health:
wait_for_status: yellow

- do:
xpack.watcher.put_watch:
id: "my_watch"
body: >
{
"trigger": {
"schedule": {
"cron": "0 0 0 1 * ? 2099"
}
},
"input": {
"chain": {
"inputs": [
{
"http": {
"http": {
"request": {
"url" : "http://localhost/",
"auth": {
"basic": {
"username": "Username123",
"password": "Password123"
}
}
}
}
}
}
]
}
},
"actions": {
"logging": {
"logging": {
"text": "logging statement here"
}
}
}
}

- do:
xpack.watcher.get_watch:
id: "my_watch"
- match: { found : true}
- match: { _id: "my_watch" }
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.startArray(INPUTS.getPreferredName());
for (Tuple<String, Input> tuple : inputs) {
builder.startObject().startObject(tuple.v1());
builder.field(tuple.v2().type(), tuple.v2());
builder.field(tuple.v2().type(), tuple.v2(), params);
builder.endObject().endObject();
}
builder.endArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand All @@ -18,6 +19,7 @@
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.SecuritySettingsSourceField;
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
import org.elasticsearch.xpack.core.watcher.input.Input;
import org.elasticsearch.xpack.core.watcher.watch.Payload;
import org.elasticsearch.xpack.watcher.common.http.HttpRequestTemplate;
import org.elasticsearch.xpack.watcher.common.http.auth.basic.BasicAuth;
Expand All @@ -29,6 +31,7 @@
import org.elasticsearch.xpack.watcher.input.simple.SimpleInputFactory;
import org.elasticsearch.xpack.watcher.test.WatcherTestUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -46,6 +49,7 @@
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.sameInstance;

public class ChainInputTests extends ESTestCase {

Expand Down Expand Up @@ -220,4 +224,24 @@ public void testParsingShouldBeStrictWhenStartingInputs() throws Exception {
expectThrows(ElasticsearchParseException.class, () -> chainInputFactory.parseInput("test", parser));
assertThat(e.getMessage(), containsString("Expected starting JSON object after [first] in watch [test]"));
}

public void testThatXContentParametersArePassedToInputs() throws Exception {
ToXContent.Params randomParams = new ToXContent.MapParams(Collections.singletonMap(randomAlphaOfLength(5), randomAlphaOfLength(5)));
ChainInput chainInput = new ChainInput(Collections.singletonList(Tuple.tuple("whatever", new Input() {
@Override
public String type() {
return "test";
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) {
assertThat(params, sameInstance(randomParams));
return builder;
}
})));

try (XContentBuilder builder = jsonBuilder()) {
chainInput.toXContent(builder, randomParams);
}
}
}