Skip to content

Commit 6eda69b

Browse files
committed
Fix tests
1 parent 250b5f2 commit 6eda69b

File tree

4 files changed

+22
-25
lines changed

4 files changed

+22
-25
lines changed

x-pack/plugin/fleet/src/main/java/org/elasticsearch/xpack/fleet/action/GetSecretResponse.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,8 @@ public class GetSecretResponse extends ActionResponse implements ToXContentObjec
2424

2525
public GetSecretResponse(StreamInput in) throws IOException {
2626
super(in);
27-
id = in.readString();
28-
if (in.readByte() == 0) {
29-
value = in.readString();
30-
} else {
31-
value = in.readStringArray();
32-
}
27+
this.id = in.readString();
28+
this.value = in.readString();
3329
}
3430

3531
public GetSecretResponse(String id, Object value) {

x-pack/plugin/fleet/src/main/java/org/elasticsearch/xpack/fleet/action/PostSecretRequest.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.elasticsearch.xcontent.ParseField;
1717
import org.elasticsearch.xcontent.XContentBuilder;
1818
import org.elasticsearch.xcontent.XContentParser;
19+
import org.elasticsearch.xcontent.XContentParser.Token;
1920

2021
import java.io.IOException;
2122
import java.util.Objects;
@@ -33,12 +34,13 @@ public class PostSecretRequest extends ActionRequest {
3334

3435
static {
3536
PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> {
36-
if (p.currentToken() == XContentParser.Token.VALUE_STRING) {
37+
Token token = p.currentToken();
38+
if (token == XContentParser.Token.VALUE_STRING) {
3739
return p.text();
38-
} else if (p.currentToken() == XContentParser.Token.START_ARRAY) {
40+
} else if (token == XContentParser.Token.START_ARRAY) {
3941
return p.list().stream().map(s -> (String) s).toArray(String[]::new);
4042
} else {
41-
throw new IllegalArgumentException("Unexpected token: " + p.currentToken());
43+
throw new IllegalArgumentException("Unexpected token: " + token);
4244
}
4345
}, VALUE_FIELD, ObjectParser.ValueType.STRING_ARRAY);
4446
}
@@ -50,19 +52,12 @@ public static PostSecretRequest fromXContent(XContentParser parser) throws IOExc
5052
private final Object value;
5153

5254
public PostSecretRequest(Object value) {
53-
if ((value instanceof String == false) && (value instanceof String[] == false)) {
54-
throw new IllegalArgumentException("value must be a string or an array of strings");
55-
}
5655
this.value = value;
5756
}
5857

5958
public PostSecretRequest(StreamInput in) throws IOException {
6059
super(in);
61-
if (in.readByte() == 0) {
62-
this.value = in.readString();
63-
} else {
64-
this.value = in.readStringArray();
65-
}
60+
this.value = in.readString();
6661
}
6762

6863
public Object value() {
@@ -84,10 +79,8 @@ public XContentBuilder toXContent(XContentBuilder builder) throws IOException {
8479
public void writeTo(StreamOutput out) throws IOException {
8580
super.writeTo(out);
8681
if (value instanceof String) {
87-
out.writeBoolean(true);
8882
out.writeString((String) value);
8983
} else if (value instanceof String[]) {
90-
out.writeBoolean(false);
9184
out.writeStringArray((String[]) value);
9285
}
9386
}
@@ -100,6 +93,12 @@ public ActionRequestValidationException validate() {
10093
return exception;
10194
}
10295

96+
if ((this.value instanceof String == false) && (this.value instanceof String[] == false)) {
97+
ActionRequestValidationException exception = new ActionRequestValidationException();
98+
exception.addValidationError("value must be a string or an array of strings");
99+
return exception;
100+
}
101+
103102
return null;
104103
}
105104

x-pack/plugin/fleet/src/test/java/org/elasticsearch/xpack/fleet/action/GetSecretResponseTests.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,4 @@ protected GetSecretResponse createTestInstance() {
2626
protected GetSecretResponse mutateInstance(GetSecretResponse instance) {
2727
return new GetSecretResponse(instance.id(), randomAlphaOfLength(10));
2828
}
29-
30-
public void testValidateResponseWithMultiValue() {
31-
String[] secrets = { "secret1", "secret2" };
32-
GetSecretResponse res = new GetSecretResponse(randomAlphaOfLength(10), secrets);
33-
assertEquals(res.equals(secrets), true);
34-
}
3529
}

x-pack/plugin/fleet/src/test/java/org/elasticsearch/xpack/fleet/action/PostSecretRequestTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,12 @@ public void testValidateRequestWithoutValue() {
5151
assertThat(e.validationErrors().size(), equalTo(1));
5252
assertThat(e.validationErrors().get(0), containsString("value is missing"));
5353
}
54+
55+
public void testValidateRequestWithInvalidValue() {
56+
PostSecretRequest req = new PostSecretRequest(123);
57+
ActionRequestValidationException e = req.validate();
58+
assertNotNull(e);
59+
assertThat(e.validationErrors().size(), equalTo(1));
60+
assertThat(e.validationErrors().get(0), containsString("value must be a string or an array of strings"));
61+
}
5462
}

0 commit comments

Comments
 (0)