Skip to content

Commit df298ce

Browse files
Working integration tests
1 parent bd43116 commit df298ce

File tree

13 files changed

+382
-253
lines changed

13 files changed

+382
-253
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/inference/action/PutCCMConfigurationAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public SecureString getApiKey() {
7676
return apiKey;
7777
}
7878

79-
public Boolean getEnabled() {
79+
public Boolean getEnabledField() {
8080
return enabled;
8181
}
8282

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/inference/action/CCMEnabledActionResponseTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515

1616
public class CCMEnabledActionResponseTests extends AbstractBWCWireSerializationTestCase<CCMEnabledActionResponse> {
1717

18+
public void testIsEnabled() {
19+
var responseEnabled = new CCMEnabledActionResponse(true);
20+
assertTrue(responseEnabled.isEnabled());
21+
22+
var responseDisabled = new CCMEnabledActionResponse(false);
23+
assertFalse(responseDisabled.isEnabled());
24+
}
25+
1826
@Override
1927
protected CCMEnabledActionResponse mutateInstanceForVersion(CCMEnabledActionResponse instance, TransportVersion version) {
2028
return instance;

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/inference/action/PutCCMConfigurationActionRequestTests.java

Lines changed: 119 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@
88
package org.elasticsearch.xpack.core.inference.action;
99

1010
import org.elasticsearch.TransportVersion;
11+
import org.elasticsearch.common.Strings;
1112
import org.elasticsearch.common.io.stream.Writeable;
1213
import org.elasticsearch.common.settings.SecureString;
14+
import org.elasticsearch.core.TimeValue;
15+
import org.elasticsearch.xcontent.XContentFactory;
16+
import org.elasticsearch.xcontent.XContentParserConfiguration;
17+
import org.elasticsearch.xcontent.XContentType;
1318
import org.elasticsearch.xpack.core.ml.AbstractBWCWireSerializationTestCase;
1419

1520
import java.io.IOException;
21+
import java.nio.charset.StandardCharsets;
1622

1723
import static org.hamcrest.Matchers.containsString;
24+
import static org.hamcrest.Matchers.is;
1825

1926
public class PutCCMConfigurationActionRequestTests extends AbstractBWCWireSerializationTestCase<PutCCMConfigurationAction.Request> {
2027

@@ -27,7 +34,11 @@ public void testValidate_BothFieldsNull_ReturnsException() {
2734

2835
public void testValidate_BothFieldsSet_ReturnsException() {
2936
var req = new PutCCMConfigurationAction.Request(
30-
new SecureString("key".toCharArray()), Boolean.TRUE, randomTimeValue(), randomTimeValue());
37+
new SecureString("key".toCharArray()),
38+
Boolean.TRUE,
39+
randomTimeValue(),
40+
randomTimeValue()
41+
);
3142
var ex = req.validate();
3243
assertNotNull(ex);
3344
assertThat(ex.getMessage(), containsString("Only one of [api_key] or [enabled] can be provided but not both"));
@@ -41,16 +52,19 @@ public void testValidate_EnabledTrue_ReturnsException() {
4152
}
4253

4354
public void testValidate_ApiKeyEmpty() {
44-
var req = new PutCCMConfigurationAction.Request(
45-
new SecureString("".toCharArray()), null, randomTimeValue(), randomTimeValue());
55+
var req = new PutCCMConfigurationAction.Request(new SecureString("".toCharArray()), null, randomTimeValue(), randomTimeValue());
4656
var ex = req.validate();
4757
assertNotNull(ex);
4858
assertThat(ex.getMessage(), containsString("The [api_key] field cannot be an empty string"));
4959
}
5060

5161
public void testValidate_ApiKeyValid_DoesNotReturnAnException() {
5262
var req = new PutCCMConfigurationAction.Request(
53-
new SecureString("validkey".toCharArray()), null, randomTimeValue(), randomTimeValue());
63+
new SecureString("validkey".toCharArray()),
64+
null,
65+
randomTimeValue(),
66+
randomTimeValue()
67+
);
5468
var ex = req.validate();
5569
assertNull(ex);
5670
}
@@ -61,6 +75,106 @@ public void testValidate_EnabledFalse_DoesNotReturnAnException() {
6175
assertNull(ex);
6276
}
6377

78+
public void testParse_SuccessfullyParsesApiKeyRequest() throws IOException {
79+
var testKey = "test_key";
80+
var requestJson = Strings.format("""
81+
{
82+
"api_key": "%s"
83+
}
84+
""", testKey);
85+
86+
try (
87+
var parser = XContentFactory.xContent(XContentType.JSON)
88+
.createParser(XContentParserConfiguration.EMPTY, requestJson.getBytes(StandardCharsets.UTF_8))
89+
) {
90+
var request = PutCCMConfigurationAction.Request.parseRequest(TimeValue.THIRTY_SECONDS, TimeValue.ZERO, parser);
91+
assertThat(request.getApiKey(), is(testKey));
92+
assertNull(request.getEnabledField());
93+
}
94+
}
95+
96+
public void testParse_SuccessfullyParsesEnabledRequest() throws IOException {
97+
var requestJson = """
98+
{
99+
"enabled": false
100+
}
101+
""";
102+
try (
103+
var parser = XContentFactory.xContent(XContentType.JSON)
104+
.createParser(XContentParserConfiguration.EMPTY, requestJson.getBytes(StandardCharsets.UTF_8))
105+
) {
106+
var request = PutCCMConfigurationAction.Request.parseRequest(TimeValue.THIRTY_SECONDS, TimeValue.ZERO, parser);
107+
assertNull(request.getApiKey());
108+
assertFalse(request.getEnabledField());
109+
}
110+
}
111+
112+
public void testParse_BothFieldsSet_ReturnsException() throws IOException {
113+
var requestJson = """
114+
{
115+
"api_key": "test_key",
116+
"enabled": true
117+
}
118+
""";
119+
try (
120+
var parser = XContentFactory.xContent(XContentType.JSON)
121+
.createParser(XContentParserConfiguration.EMPTY, requestJson.getBytes(StandardCharsets.UTF_8))
122+
) {
123+
var request = PutCCMConfigurationAction.Request.parseRequest(TimeValue.THIRTY_SECONDS, TimeValue.ZERO, parser);
124+
var ex = request.validate();
125+
assertNotNull(ex);
126+
assertThat(ex.getMessage(), containsString("Only one of [api_key] or [enabled] can be provided but not both"));
127+
}
128+
}
129+
130+
public void testParse_BothFieldsSetWithEnabledFalse_ReturnsException() throws IOException {
131+
var requestJson = """
132+
{
133+
"api_key": "test_key",
134+
"enabled": false
135+
}
136+
""";
137+
try (
138+
var parser = XContentFactory.xContent(XContentType.JSON)
139+
.createParser(XContentParserConfiguration.EMPTY, requestJson.getBytes(StandardCharsets.UTF_8))
140+
) {
141+
var request = PutCCMConfigurationAction.Request.parseRequest(TimeValue.THIRTY_SECONDS, TimeValue.ZERO, parser);
142+
var ex = request.validate();
143+
assertNotNull(ex);
144+
assertThat(ex.getMessage(), containsString("Only one of [api_key] or [enabled] can be provided but not both"));
145+
}
146+
}
147+
148+
public void testParse_EmptyBody_ReturnsException() throws IOException {
149+
var requestJson = "{}";
150+
try (
151+
var parser = XContentFactory.xContent(XContentType.JSON)
152+
.createParser(XContentParserConfiguration.EMPTY, requestJson.getBytes(StandardCharsets.UTF_8))
153+
) {
154+
var request = PutCCMConfigurationAction.Request.parseRequest(TimeValue.THIRTY_SECONDS, TimeValue.ZERO, parser);
155+
var ex = request.validate();
156+
assertNotNull(ex);
157+
assertThat(ex.getMessage(), containsString("At least one of [api_key] or [enabled] must be provided"));
158+
}
159+
}
160+
161+
public void testParse_ApiKeyEmpty_ReturnsException() throws IOException {
162+
var requestJson = """
163+
{
164+
"api_key": ""
165+
}
166+
""";
167+
try (
168+
var parser = XContentFactory.xContent(XContentType.JSON)
169+
.createParser(XContentParserConfiguration.EMPTY, requestJson.getBytes(StandardCharsets.UTF_8))
170+
) {
171+
var request = PutCCMConfigurationAction.Request.parseRequest(TimeValue.THIRTY_SECONDS, TimeValue.ZERO, parser);
172+
var ex = request.validate();
173+
assertNotNull(ex);
174+
assertThat(ex.getMessage(), containsString("The [api_key] field cannot be an empty string"));
175+
}
176+
}
177+
64178
@Override
65179
protected PutCCMConfigurationAction.Request mutateInstanceForVersion(
66180
PutCCMConfigurationAction.Request instance,
@@ -87,7 +201,7 @@ protected PutCCMConfigurationAction.Request createTestInstance() {
87201
@Override
88202
protected PutCCMConfigurationAction.Request mutateInstance(PutCCMConfigurationAction.Request instance) throws IOException {
89203
var apiKey = instance.getApiKey();
90-
var enabled = instance.getEnabled();
204+
var enabled = instance.getEnabledField();
91205
var masterNodeTimeout = instance.masterNodeTimeout();
92206
var ackTimeout = instance.ackTimeout();
93207

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.inference;
9+
10+
import org.elasticsearch.client.Request;
11+
import org.elasticsearch.client.Response;
12+
import org.elasticsearch.common.Strings;
13+
import org.elasticsearch.test.rest.ESRestTestCase;
14+
import org.elasticsearch.xcontent.XContentFactory;
15+
import org.elasticsearch.xcontent.XContentType;
16+
import org.elasticsearch.xpack.core.inference.action.CCMEnabledActionResponse;
17+
import org.elasticsearch.xpack.core.inference.action.PutCCMConfigurationAction;
18+
19+
import java.io.IOException;
20+
21+
import static org.elasticsearch.xpack.core.inference.action.CCMEnabledActionResponse.ENABLED_FIELD_NAME;
22+
import static org.elasticsearch.xpack.inference.InferenceBaseRestTest.assertStatusOkOrCreated;
23+
import static org.elasticsearch.xpack.inference.rest.Paths.INFERENCE_CCM_PATH;
24+
import static org.hamcrest.Matchers.instanceOf;
25+
26+
public class CCMBaseIT extends ESRestTestCase {
27+
28+
static final String PUT_METHOD = "PUT";
29+
static final String GET_METHOD = "GET";
30+
31+
static CCMEnabledActionResponse putCCMConfiguration(PutCCMConfigurationAction.Request request) throws IOException {
32+
var response = putRawRequest(INFERENCE_CCM_PATH, request);
33+
assertStatusOkOrCreated(response);
34+
var responseAsMap = entityAsMap(response);
35+
36+
var enabled = responseAsMap.get(ENABLED_FIELD_NAME);
37+
38+
assertThat(enabled, instanceOf(Boolean.class));
39+
return new CCMEnabledActionResponse((Boolean) enabled);
40+
}
41+
42+
static Response putRawRequest(String endpoint, PutCCMConfigurationAction.Request actionRequest) throws IOException {
43+
var builder = XContentFactory.contentBuilder(XContentType.JSON);
44+
actionRequest.toXContent(builder, null);
45+
46+
return putRawRequest(endpoint, Strings.toString(builder));
47+
}
48+
49+
static Response putRawRequest(String endpoint, String requestBody) throws IOException {
50+
var request = new Request(PUT_METHOD, endpoint);
51+
request.setJsonEntity(requestBody);
52+
return client().performRequest(request);
53+
}
54+
55+
static CCMEnabledActionResponse getCCMConfiguration() throws IOException {
56+
var getRequest = new Request(GET_METHOD, INFERENCE_CCM_PATH);
57+
var getResponse = client().performRequest(getRequest);
58+
assertStatusOkOrCreated(getResponse);
59+
var responseAsMap = entityAsMap(getResponse);
60+
61+
var enabled = responseAsMap.get(ENABLED_FIELD_NAME);
62+
assertThat(enabled, instanceOf(Boolean.class));
63+
return new CCMEnabledActionResponse((Boolean) enabled);
64+
}
65+
}

0 commit comments

Comments
 (0)