|
24 | 24 | import com.maxmind.geoip2.record.MaxMind; |
25 | 25 |
|
26 | 26 | import org.elasticsearch.common.util.set.Sets; |
| 27 | +import org.elasticsearch.core.SuppressForbidden; |
27 | 28 | import org.elasticsearch.test.ESTestCase; |
28 | 29 |
|
29 | 30 | import java.lang.reflect.Method; |
| 31 | +import java.lang.reflect.Modifier; |
30 | 32 | import java.lang.reflect.ParameterizedType; |
31 | 33 | import java.lang.reflect.Type; |
32 | 34 | import java.net.InetAddress; |
@@ -479,6 +481,36 @@ public void testUnknownMaxMindResponseClassess() { |
479 | 481 | ); |
480 | 482 | } |
481 | 483 |
|
| 484 | + /* |
| 485 | + * This tests that this test has a mapping in TYPE_TO_MAX_MIND_CLASS for all MaxMind classes exposed through GeoIpDatabase. |
| 486 | + */ |
| 487 | + public void testUsedMaxMindResponseClassesAreAccountedFor() { |
| 488 | + Set<Class<? extends AbstractResponse>> usedMaxMindResponseClasses = getUsedMaxMindResponseClasses(); |
| 489 | + Set<Class<? extends AbstractResponse>> supportedMaxMindClasses = new HashSet<>(TYPE_TO_MAX_MIND_CLASS.values()); |
| 490 | + Set<Class<? extends AbstractResponse>> usedButNotSupportedMaxMindResponseClasses = Sets.difference( |
| 491 | + usedMaxMindResponseClasses, |
| 492 | + supportedMaxMindClasses |
| 493 | + ); |
| 494 | + assertThat( |
| 495 | + "MaxmindIpDataLookups exposes MaxMind response classes that this test does not know what to do with. Add mappings to " |
| 496 | + + "TYPE_TO_MAX_MIND_CLASS for the following: " |
| 497 | + + usedButNotSupportedMaxMindResponseClasses, |
| 498 | + usedButNotSupportedMaxMindResponseClasses, |
| 499 | + empty() |
| 500 | + ); |
| 501 | + Set<Class<? extends AbstractResponse>> supportedButNotUsedMaxMindClasses = Sets.difference( |
| 502 | + supportedMaxMindClasses, |
| 503 | + usedMaxMindResponseClasses |
| 504 | + ); |
| 505 | + assertThat( |
| 506 | + "This test claims to support MaxMind response classes that are not exposed in GeoIpDatabase. Remove the following from " |
| 507 | + + "TYPE_TO_MAX_MIND_CLASS: " |
| 508 | + + supportedButNotUsedMaxMindClasses, |
| 509 | + supportedButNotUsedMaxMindClasses, |
| 510 | + empty() |
| 511 | + ); |
| 512 | + } |
| 513 | + |
482 | 514 | /* |
483 | 515 | * This is the list of field types that causes us to stop recursing. That is, fields of these types are the lowest-level fields that |
484 | 516 | * we care about. |
@@ -597,4 +629,34 @@ private static String getFormattedList(Set<String> fields) { |
597 | 629 | } |
598 | 630 | return result.toString(); |
599 | 631 | } |
| 632 | + |
| 633 | + /* |
| 634 | + * This returns all AbstractResponse classes that are declared in transform methods in classes defined in MaxmindIpDataLookups. |
| 635 | + */ |
| 636 | + @SuppressWarnings("unchecked") |
| 637 | + @SuppressForbidden(reason = "Need declared classes and methods") |
| 638 | + private static Set<Class<? extends AbstractResponse>> getUsedMaxMindResponseClasses() { |
| 639 | + Set<Class<? extends AbstractResponse>> result = new HashSet<>(); |
| 640 | + Class<?>[] declaredClasses = MaxmindIpDataLookups.class.getDeclaredClasses(); |
| 641 | + for (Class<?> declaredClass : declaredClasses) { |
| 642 | + if (Modifier.isAbstract(declaredClass.getModifiers())) { |
| 643 | + continue; |
| 644 | + } |
| 645 | + Method[] declaredMethods = declaredClass.getDeclaredMethods(); |
| 646 | + Optional<Method> nonAbstractTransformMethod = Arrays.stream(declaredMethods) |
| 647 | + .filter( |
| 648 | + method -> method.getName().equals("transform") |
| 649 | + && method.getParameterTypes().length == 1 |
| 650 | + && Modifier.isAbstract(method.getParameterTypes()[0].getModifiers()) == false |
| 651 | + ) |
| 652 | + .findAny(); |
| 653 | + if (nonAbstractTransformMethod.isPresent()) { |
| 654 | + Class<?> responseClass = nonAbstractTransformMethod.get().getParameterTypes()[0]; |
| 655 | + if (AbstractResponse.class.isAssignableFrom(responseClass)) { |
| 656 | + result.add((Class<? extends AbstractResponse>) responseClass); |
| 657 | + } |
| 658 | + } |
| 659 | + } |
| 660 | + return result; |
| 661 | + } |
600 | 662 | } |
0 commit comments