|
8 | 8 | import static org.junit.Assert.assertEquals; |
9 | 9 | import static org.junit.Assert.assertNotNull; |
10 | 10 | import static org.junit.Assert.assertNull; |
| 11 | +import static org.junit.Assert.assertThrows; |
11 | 12 | import static org.junit.Assert.assertTrue; |
12 | 13 |
|
13 | 14 | import java.util.List; |
@@ -198,4 +199,87 @@ public void testGetHighlightConfigPerFieldOptions() { |
198 | 199 | assertEquals(3, config.fields().get("title").get("number_of_fragments")); |
199 | 200 | assertEquals("plain", config.fields().get("body").get("type")); |
200 | 201 | } |
| 202 | + |
| 203 | + @Test |
| 204 | + public void testGetHighlightConfigEmptyFieldNameThrows() { |
| 205 | + JSONObject json = new JSONObject("{\"query\": \"source=t\", \"highlight\": [\"\"]}"); |
| 206 | + PPLQueryRequest request = new PPLQueryRequest("source=t", json, "/_plugins/_ppl"); |
| 207 | + IllegalArgumentException e = |
| 208 | + assertThrows(IllegalArgumentException.class, request::getHighlightConfig); |
| 209 | + assertTrue(e.getMessage().contains("non-empty")); |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + public void testGetHighlightConfigNegativeFragmentSizeThrows() { |
| 214 | + JSONObject json = |
| 215 | + new JSONObject( |
| 216 | + "{\"query\": \"source=t\", \"highlight\": {" |
| 217 | + + "\"fields\": {\"*\": {}}, \"fragment_size\": -1}}"); |
| 218 | + PPLQueryRequest request = new PPLQueryRequest("source=t", json, "/_plugins/_ppl"); |
| 219 | + IllegalArgumentException e = |
| 220 | + assertThrows(IllegalArgumentException.class, request::getHighlightConfig); |
| 221 | + assertTrue(e.getMessage().contains("fragment_size must be a positive integer")); |
| 222 | + } |
| 223 | + |
| 224 | + @Test |
| 225 | + public void testGetHighlightConfigZeroFragmentSizeThrows() { |
| 226 | + JSONObject json = |
| 227 | + new JSONObject( |
| 228 | + "{\"query\": \"source=t\", \"highlight\": {" |
| 229 | + + "\"fields\": {\"*\": {}}, \"fragment_size\": 0}}"); |
| 230 | + PPLQueryRequest request = new PPLQueryRequest("source=t", json, "/_plugins/_ppl"); |
| 231 | + IllegalArgumentException e = |
| 232 | + assertThrows(IllegalArgumentException.class, request::getHighlightConfig); |
| 233 | + assertTrue(e.getMessage().contains("fragment_size must be a positive integer")); |
| 234 | + } |
| 235 | + |
| 236 | + @Test |
| 237 | + public void testGetHighlightConfigExceedMaxFieldsArrayThrows() { |
| 238 | + // Build an array with 101 fields |
| 239 | + StringBuilder sb = new StringBuilder("{\"query\": \"source=t\", \"highlight\": ["); |
| 240 | + for (int i = 0; i < 101; i++) { |
| 241 | + if (i > 0) sb.append(","); |
| 242 | + sb.append("\"field").append(i).append("\""); |
| 243 | + } |
| 244 | + sb.append("]}"); |
| 245 | + JSONObject json = new JSONObject(sb.toString()); |
| 246 | + PPLQueryRequest request = new PPLQueryRequest("source=t", json, "/_plugins/_ppl"); |
| 247 | + IllegalArgumentException e = |
| 248 | + assertThrows(IllegalArgumentException.class, request::getHighlightConfig); |
| 249 | + assertTrue(e.getMessage().contains("fields count exceeds maximum")); |
| 250 | + } |
| 251 | + |
| 252 | + @Test |
| 253 | + public void testGetHighlightConfigExceedMaxFieldsObjectThrows() { |
| 254 | + // Build an object with 101 fields |
| 255 | + StringBuilder sb = new StringBuilder("{\"query\": \"source=t\", \"highlight\": {\"fields\": {"); |
| 256 | + for (int i = 0; i < 101; i++) { |
| 257 | + if (i > 0) sb.append(","); |
| 258 | + sb.append("\"field").append(i).append("\": {}"); |
| 259 | + } |
| 260 | + sb.append("}}}"); |
| 261 | + JSONObject json = new JSONObject(sb.toString()); |
| 262 | + PPLQueryRequest request = new PPLQueryRequest("source=t", json, "/_plugins/_ppl"); |
| 263 | + IllegalArgumentException e = |
| 264 | + assertThrows(IllegalArgumentException.class, request::getHighlightConfig); |
| 265 | + assertTrue(e.getMessage().contains("fields count exceeds maximum")); |
| 266 | + } |
| 267 | + |
| 268 | + @Test |
| 269 | + public void testGetHighlightConfigExceedMaxTagsThrows() { |
| 270 | + // Build pre_tags array with 11 entries |
| 271 | + StringBuilder sb = |
| 272 | + new StringBuilder( |
| 273 | + "{\"query\": \"source=t\", \"highlight\": {\"fields\": {\"*\": {}}, \"pre_tags\": ["); |
| 274 | + for (int i = 0; i < 11; i++) { |
| 275 | + if (i > 0) sb.append(","); |
| 276 | + sb.append("\"tag").append(i).append("\""); |
| 277 | + } |
| 278 | + sb.append("]}}"); |
| 279 | + JSONObject json = new JSONObject(sb.toString()); |
| 280 | + PPLQueryRequest request = new PPLQueryRequest("source=t", json, "/_plugins/_ppl"); |
| 281 | + IllegalArgumentException e = |
| 282 | + assertThrows(IllegalArgumentException.class, request::getHighlightConfig); |
| 283 | + assertTrue(e.getMessage().contains("pre_tags count exceeds maximum")); |
| 284 | + } |
201 | 285 | } |
0 commit comments