88package org .elasticsearch .xpack .core .inference .action ;
99
1010import org .elasticsearch .TransportVersion ;
11+ import org .elasticsearch .common .Strings ;
1112import org .elasticsearch .common .io .stream .Writeable ;
1213import 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 ;
1318import org .elasticsearch .xpack .core .ml .AbstractBWCWireSerializationTestCase ;
1419
1520import java .io .IOException ;
21+ import java .nio .charset .StandardCharsets ;
1622
1723import static org .hamcrest .Matchers .containsString ;
24+ import static org .hamcrest .Matchers .is ;
1825
1926public 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
0 commit comments