Skip to content

Commit 9d5c2e5

Browse files
Refactoring and tests
1 parent 5c39685 commit 9d5c2e5

File tree

2 files changed

+148
-4
lines changed

2 files changed

+148
-4
lines changed

server/src/main/java/org/elasticsearch/inference/InputType.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,24 +94,26 @@ public static Map<InputType, String> validateInputTypeTranslationValues(
9494
if (value instanceof String == false || Strings.isNullOrEmpty((String) value)) {
9595
validationException.addValidationError(
9696
Strings.format(
97-
"Input type translation value for key [%s] must be a String that is not null and not empty, received: [%s].",
97+
"Input type translation value for key [%s] must be a String that is "
98+
+ "not null and not empty, received: [%s], type: [%s].",
9899
key,
99-
value.getClass().getSimpleName()
100+
value,
101+
value == null ? "null" : value.getClass().getSimpleName()
100102
)
101103
);
102104

103105
throw validationException;
104106
}
105107

106108
try {
107-
var inputTypeKey = InputType.fromRestString(key);
109+
var inputTypeKey = InputType.fromStringValidateSupportedRequestValue(key);
108110
translationMap.put(inputTypeKey, (String) value);
109111
} catch (Exception e) {
110112
validationException.addValidationError(
111113
Strings.format(
112114
"Invalid input type translation for key: [%s], is not a valid value. Must be one of %s",
113115
key,
114-
EnumSet.of(InputType.CLASSIFICATION, InputType.CLUSTERING, InputType.INGEST, InputType.SEARCH)
116+
SUPPORTED_REQUEST_VALUES
115117
)
116118
);
117119

@@ -121,4 +123,15 @@ public static Map<InputType, String> validateInputTypeTranslationValues(
121123

122124
return translationMap;
123125
}
126+
127+
private static InputType fromStringValidateSupportedRequestValue(String name) {
128+
var inputType = fromRestString(name);
129+
if (SUPPORTED_REQUEST_VALUES.contains(inputType) == false) {
130+
throw new IllegalArgumentException(
131+
format("Unrecognized input_type [%s], must be one of %s", inputType, SUPPORTED_REQUEST_VALUES)
132+
);
133+
}
134+
135+
return inputType;
136+
}
124137
}

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/InputTypeTests.java

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77

88
package org.elasticsearch.xpack.inference;
99

10+
import org.elasticsearch.common.ValidationException;
1011
import org.elasticsearch.inference.InputType;
1112
import org.elasticsearch.test.ESTestCase;
1213

14+
import java.util.HashMap;
1315
import java.util.List;
16+
import java.util.Map;
1417

1518
import static org.elasticsearch.core.Strings.format;
1619
import static org.hamcrest.CoreMatchers.is;
@@ -80,4 +83,132 @@ public void testFromRestString_ThrowsErrorForInvalidInputTypes() {
8083

8184
assertThat(thrownException.getMessage(), is("No enum constant org.elasticsearch.inference.InputType.FOO"));
8285
}
86+
87+
public void testValidateInputTypeTranslationValues() {
88+
assertThat(
89+
InputType.validateInputTypeTranslationValues(
90+
Map.of(
91+
InputType.INGEST.toString(),
92+
"ingest_value",
93+
InputType.SEARCH.toString(),
94+
"search_value",
95+
InputType.CLASSIFICATION.toString(),
96+
"classification_value",
97+
InputType.CLUSTERING.toString(),
98+
"clustering_value"
99+
),
100+
new ValidationException()
101+
),
102+
is(
103+
Map.of(
104+
InputType.INGEST,
105+
"ingest_value",
106+
InputType.SEARCH,
107+
"search_value",
108+
InputType.CLASSIFICATION,
109+
"classification_value",
110+
InputType.CLUSTERING,
111+
"clustering_value"
112+
)
113+
)
114+
);
115+
}
116+
117+
public void testValidateInputTypeTranslationValues_ReturnsEmptyMap_WhenTranslationIsNull() {
118+
assertThat(InputType.validateInputTypeTranslationValues(null, new ValidationException()), is(Map.of()));
119+
}
120+
121+
public void testValidateInputTypeTranslationValues_ReturnsEmptyMap_WhenTranslationIsAnEmptyMap() {
122+
assertThat(InputType.validateInputTypeTranslationValues(Map.of(), new ValidationException()), is(Map.of()));
123+
}
124+
125+
public void testValidateInputTypeTranslationValues_ThrowsAnException_WhenInputTypeIsUnspecified() {
126+
var exception = expectThrows(
127+
ValidationException.class,
128+
() -> InputType.validateInputTypeTranslationValues(
129+
Map.of(InputType.INGEST.toString(), "ingest_value", InputType.UNSPECIFIED.toString(), "unspecified_value"),
130+
new ValidationException()
131+
)
132+
);
133+
134+
assertThat(
135+
exception.getMessage(),
136+
is(
137+
"Validation Failed: 1: Invalid input type translation for key: [unspecified], is not a valid value. Must be "
138+
+ "one of [ingest, search, classification, clustering];"
139+
)
140+
);
141+
}
142+
143+
public void testValidateInputTypeTranslationValues_ThrowsAnException_WhenInputTypeIsInternal() {
144+
var exception = expectThrows(
145+
ValidationException.class,
146+
() -> InputType.validateInputTypeTranslationValues(
147+
Map.of(InputType.INGEST.toString(), "ingest_value", InputType.INTERNAL_INGEST.toString(), "internal_ingest_value"),
148+
new ValidationException()
149+
)
150+
);
151+
152+
assertThat(
153+
exception.getMessage(),
154+
is(
155+
"Validation Failed: 1: Invalid input type translation for key: [internal_ingest], is not a valid value. Must be "
156+
+ "one of [ingest, search, classification, clustering];"
157+
)
158+
);
159+
}
160+
161+
public void testValidateInputTypeTranslationValues_ThrowsAnException_WhenValueIsNull() {
162+
var translation = new HashMap<String, Object>();
163+
translation.put(InputType.INGEST.toString(), null);
164+
165+
var exception = expectThrows(
166+
ValidationException.class,
167+
() -> InputType.validateInputTypeTranslationValues(translation, new ValidationException())
168+
);
169+
170+
assertThat(
171+
exception.getMessage(),
172+
is(
173+
"Validation Failed: 1: Input type translation value for key [ingest] must be a String that "
174+
+ "is not null and not empty, received: [null], type: [null].;"
175+
)
176+
);
177+
}
178+
179+
public void testValidateInputTypeTranslationValues_ThrowsAnException_WhenValueIsAnEmptyString() {
180+
var translation = new HashMap<String, Object>();
181+
translation.put(InputType.INGEST.toString(), "");
182+
183+
var exception = expectThrows(
184+
ValidationException.class,
185+
() -> InputType.validateInputTypeTranslationValues(translation, new ValidationException())
186+
);
187+
188+
assertThat(
189+
exception.getMessage(),
190+
is(
191+
"Validation Failed: 1: Input type translation value for key [ingest] must be a String that "
192+
+ "is not null and not empty, received: [], type: [String].;"
193+
)
194+
);
195+
}
196+
197+
public void testValidateInputTypeTranslationValues_ThrowsAnException_WhenValueIsNotAString() {
198+
var translation = new HashMap<String, Object>();
199+
translation.put(InputType.INGEST.toString(), 1);
200+
201+
var exception = expectThrows(
202+
ValidationException.class,
203+
() -> InputType.validateInputTypeTranslationValues(translation, new ValidationException())
204+
);
205+
206+
assertThat(
207+
exception.getMessage(),
208+
is(
209+
"Validation Failed: 1: Input type translation value for key [ingest] must be a String that "
210+
+ "is not null and not empty, received: [1], type: [Integer].;"
211+
)
212+
);
213+
}
83214
}

0 commit comments

Comments
 (0)