|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.plugin.transport; |
| 7 | + |
| 8 | +import static org.junit.Assert.assertTrue; |
| 9 | + |
| 10 | +import java.lang.reflect.Constructor; |
| 11 | +import java.lang.reflect.Field; |
| 12 | +import java.lang.reflect.Method; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +/** |
| 18 | + * Alerting and other plugins call {@code PPLQueryAction} with this module's classes plus core, ppl, |
| 19 | + * and protocol, but without the {@code opensearch} module (#5923). The classes they load must not |
| 20 | + * reference that module's types. |
| 21 | + */ |
| 22 | +public class ThinJarClassesTest { |
| 23 | + |
| 24 | + private static final String EXCLUDED_PACKAGE = "org.opensearch.sql.opensearch."; |
| 25 | + |
| 26 | + private static final List<Class<?>> LOADED_BY_OTHER_PLUGINS = |
| 27 | + List.of( |
| 28 | + PPLQueryAction.class, |
| 29 | + TransportPPLQueryRequest.class, |
| 30 | + TransportPPLQueryResponse.class, |
| 31 | + PPLQueryTask.class); |
| 32 | + |
| 33 | + @Test |
| 34 | + public void classesLoadedByOtherPluginsDoNotReferenceTheOpenSearchModule() { |
| 35 | + List<String> violations = new ArrayList<>(); |
| 36 | + for (Class<?> type : LOADED_BY_OTHER_PLUGINS) { |
| 37 | + for (Class<?> referenced : referencedTypes(type)) { |
| 38 | + if (referenced.getName().startsWith(EXCLUDED_PACKAGE)) { |
| 39 | + violations.add(type.getSimpleName() + " -> " + referenced.getName()); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + assertTrue("Types outside the thin jar: " + violations, violations.isEmpty()); |
| 44 | + } |
| 45 | + |
| 46 | + private static List<Class<?>> referencedTypes(Class<?> type) { |
| 47 | + List<Class<?>> types = new ArrayList<>(); |
| 48 | + for (Class<?> c = type; c != null && c != Object.class; c = c.getSuperclass()) { |
| 49 | + types.add(c); |
| 50 | + types.addAll(List.of(c.getInterfaces())); |
| 51 | + for (Field field : c.getDeclaredFields()) { |
| 52 | + types.add(field.getType()); |
| 53 | + } |
| 54 | + for (Method method : c.getDeclaredMethods()) { |
| 55 | + types.add(method.getReturnType()); |
| 56 | + types.addAll(List.of(method.getParameterTypes())); |
| 57 | + } |
| 58 | + for (Constructor<?> constructor : c.getDeclaredConstructors()) { |
| 59 | + types.addAll(List.of(constructor.getParameterTypes())); |
| 60 | + } |
| 61 | + } |
| 62 | + return types; |
| 63 | + } |
| 64 | +} |
0 commit comments