|
42 | 42 | import static io.gravitee.rest.api.service.impl.search.lucene.transformer.ApiDocumentTransformer.FIELD_TAGS;
|
43 | 43 | import static io.gravitee.rest.api.service.impl.search.lucene.transformer.ApiDocumentTransformer.FIELD_TAGS_SPLIT;
|
44 | 44 | import static io.gravitee.rest.api.service.impl.search.lucene.transformer.ApiDocumentTransformer.FIELD_TYPE;
|
| 45 | +import static io.gravitee.rest.api.service.impl.search.lucene.transformer.ApiDocumentTransformer.SPECIAL_CHARS; |
45 | 46 | import static org.assertj.core.api.Assertions.assertThat;
|
46 | 47 | import static org.assertj.core.api.Assertions.catchThrowable;
|
47 | 48 |
|
|
51 | 52 | import io.gravitee.apim.core.membership.model.PrimaryOwnerEntity;
|
52 | 53 | import io.gravitee.apim.core.search.model.IndexableApi;
|
53 | 54 | import io.gravitee.definition.model.DefinitionVersion;
|
| 55 | +import java.lang.reflect.Field; |
| 56 | +import java.lang.reflect.Method; |
| 57 | +import java.text.Collator; |
| 58 | +import java.util.ArrayList; |
| 59 | +import java.util.Comparator; |
| 60 | +import java.util.HashMap; |
54 | 61 | import java.util.List;
|
| 62 | +import java.util.Locale; |
55 | 63 | import java.util.Map;
|
56 | 64 | import java.util.Set;
|
| 65 | +import java.util.stream.Collectors; |
| 66 | +import org.apache.logging.log4j.util.PropertySource; |
57 | 67 | import org.apache.lucene.index.IndexableField;
|
| 68 | +import org.apache.lucene.util.BytesRef; |
58 | 69 | import org.assertj.core.api.SoftAssertions;
|
59 | 70 | import org.junit.jupiter.api.Test;
|
60 | 71 |
|
@@ -295,4 +306,57 @@ void should_transform_a_native_api() {
|
295 | 306 | softly.assertThat(result.getFields(FIELD_HOSTS)).extracting(IndexableField::stringValue).contains("native.kafka");
|
296 | 307 | });
|
297 | 308 | }
|
| 309 | + |
| 310 | + @Test |
| 311 | + void should_sort_names_by_bytesref() throws Exception { |
| 312 | + List<String> names = List.of("Nano", "zorro", "äther", "Vem", "épée", "épona", "Öko", "bns"); |
| 313 | + List<String> expectedSorted = List.of("äther", "bns", "épée", "épona", "Nano", "Öko", "Vem", "zorro"); |
| 314 | + |
| 315 | + Method toSortedValueMethod = IndexableApiDocumentTransformer.class.getDeclaredMethod("toSortedValue", String.class); |
| 316 | + toSortedValueMethod.setAccessible(true); |
| 317 | + Map<String, BytesRef> bytesRefMap = new HashMap<>(); |
| 318 | + for (String name : names) { |
| 319 | + BytesRef key = (BytesRef) toSortedValueMethod.invoke(cut, name); |
| 320 | + bytesRefMap.put(name, key); |
| 321 | + } |
| 322 | + List<String> sortedByBytesRef = new ArrayList<>(names); |
| 323 | + sortedByBytesRef.sort(Comparator.comparing(bytesRefMap::get, BytesRef::compareTo)); |
| 324 | + |
| 325 | + // Also sort with collator directly for comparison |
| 326 | + List<String> sortedByCollator = new ArrayList<>(names); |
| 327 | + Field collatorField = IndexableApiDocumentTransformer.class.getDeclaredField("collator"); |
| 328 | + collatorField.setAccessible(true); |
| 329 | + Collator collator = (Collator) collatorField.get(cut); |
| 330 | + sortedByCollator.sort(collator); |
| 331 | + |
| 332 | + // Assertions |
| 333 | + assertThat(sortedByBytesRef).isEqualTo(expectedSorted); |
| 334 | + assertThat(sortedByCollator).isEqualTo(expectedSorted); |
| 335 | + } |
| 336 | + |
| 337 | + @Test |
| 338 | + void should_sort_names_with_special_characters_correctly() throws Exception { |
| 339 | + List<String> names = List.of("épée-bar", "épée", "zorro/name", "äther", "nano"); |
| 340 | + List<String> expectedSorted = List.of("äther", "épée", "épée-bar", "nano", "zorro/name"); |
| 341 | + Method toSortedValueMethod = IndexableApiDocumentTransformer.class.getDeclaredMethod("toSortedValue", String.class); |
| 342 | + toSortedValueMethod.setAccessible(true); |
| 343 | + Map<String, BytesRef> bytesRefMap = new HashMap<>(); |
| 344 | + for (String name : names) { |
| 345 | + BytesRef key = (BytesRef) toSortedValueMethod.invoke(cut, name); |
| 346 | + bytesRefMap.put(name, key); |
| 347 | + } |
| 348 | + List<String> sortedByBytesRef = new ArrayList<>(names); |
| 349 | + sortedByBytesRef.sort(Comparator.comparing(bytesRefMap::get, BytesRef::compareTo)); |
| 350 | + |
| 351 | + // Also sort with collator directly for comparison |
| 352 | + List<String> sortedByCollator = new ArrayList<>(names); |
| 353 | + Field collatorField = IndexableApiDocumentTransformer.class.getDeclaredField("collator"); |
| 354 | + collatorField.setAccessible(true); |
| 355 | + Collator collator = (Collator) collatorField.get(cut); |
| 356 | + sortedByCollator.sort(collator); |
| 357 | + |
| 358 | + // Assertions |
| 359 | + assertThat(sortedByBytesRef).isEqualTo(expectedSorted); |
| 360 | + assertThat(sortedByCollator).isEqualTo(expectedSorted); |
| 361 | + } |
298 | 362 | }
|
0 commit comments