|
7 | 7 |
|
8 | 8 | package org.elasticsearch.xpack.inference; |
9 | 9 |
|
| 10 | +import org.elasticsearch.common.ValidationException; |
10 | 11 | import org.elasticsearch.inference.InputType; |
11 | 12 | import org.elasticsearch.test.ESTestCase; |
12 | 13 |
|
| 14 | +import java.util.HashMap; |
13 | 15 | import java.util.List; |
| 16 | +import java.util.Map; |
14 | 17 |
|
15 | 18 | import static org.elasticsearch.core.Strings.format; |
16 | 19 | import static org.hamcrest.CoreMatchers.is; |
@@ -80,4 +83,132 @@ public void testFromRestString_ThrowsErrorForInvalidInputTypes() { |
80 | 83 |
|
81 | 84 | assertThat(thrownException.getMessage(), is("No enum constant org.elasticsearch.inference.InputType.FOO")); |
82 | 85 | } |
| 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 | + } |
83 | 214 | } |
0 commit comments