Skip to content

Simplify entitlement rest test discovery (#125449) #126539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Map.entry;
import static org.elasticsearch.entitlement.qa.test.EntitlementTest.ExpectedAccess.ALWAYS_ALLOWED;
Expand All @@ -49,29 +48,36 @@ record CheckAction(
Integer fromJavaVersion
) {}

private static final Map<String, CheckAction> checkActions = Stream.of(
getTestEntries(FileCheckActions.class),
getTestEntries(FileStoreActions.class),
getTestEntries(JvmActions.class),
getTestEntries(LoadNativeLibrariesCheckActions.class),
getTestEntries(ManageThreadsActions.class),
getTestEntries(NativeActions.class),
getTestEntries(NetworkAccessCheckActions.class),
getTestEntries(NioChannelsActions.class),
getTestEntries(NioFilesActions.class),
getTestEntries(NioFileSystemActions.class),
getTestEntries(OperatingSystemActions.class),
getTestEntries(PathActions.class),
getTestEntries(SpiActions.class),
getTestEntries(SystemActions.class),
getTestEntries(URLConnectionFileActions.class),
getTestEntries(URLConnectionNetworkActions.class),
getTestEntries(VersionSpecificManageThreadsActions.class),
getTestEntries(VersionSpecificNioFileSystemActions.class)
)
.flatMap(Function.identity())
.filter(entry -> entry.getValue().fromJavaVersion() == null || Runtime.version().feature() >= entry.getValue().fromJavaVersion())
.collect(Collectors.toUnmodifiableMap(Entry::getKey, Entry::getValue));
private static final Map<String, CheckAction> checkActions = collectTests(
FileCheckActions.class,
FileStoreActions.class,
JvmActions.class,
LoadNativeLibrariesCheckActions.class,
ManageThreadsActions.class,
NativeActions.class,
NetworkAccessCheckActions.class,
NioChannelsActions.class,
NioFilesActions.class,
NioFileSystemActions.class,
OperatingSystemActions.class,
PathActions.class,
SpiActions.class,
SystemActions.class,
URLConnectionFileActions.class,
URLConnectionNetworkActions.class,
VersionSpecificManageThreadsActions.class,
VersionSpecificNioFileSystemActions.class
);

private static Map<String, CheckAction> collectTests(Class<?>... testClasses) {
List<Entry<String, CheckAction>> entries = new ArrayList<>();
for (Class<?> testClass : testClasses) {
getTestEntries(entries, testClass, a -> a.fromJavaVersion() == null || Runtime.version().feature() >= a.fromJavaVersion());
}
@SuppressWarnings({ "unchecked", "rawtypes" })
Entry<String, CheckAction>[] entriesArray = entries.toArray(new Entry[0]);
return Map.ofEntries(entriesArray);
}

private final Environment environment;

Expand All @@ -84,8 +90,7 @@ private static Method[] getDeclaredMethods(Class<?> clazz) {
return clazz.getDeclaredMethods();
}

private static Stream<Entry<String, CheckAction>> getTestEntries(Class<?> actionsClass) {
List<Entry<String, CheckAction>> entries = new ArrayList<>();
private static void getTestEntries(List<Entry<String, CheckAction>> entries, Class<?> actionsClass, Predicate<CheckAction> filter) {
for (var method : getDeclaredMethods(actionsClass)) {
var testAnnotation = method.getAnnotation(EntitlementTest.class);
if (testAnnotation == null) {
Expand All @@ -94,6 +99,9 @@ private static Stream<Entry<String, CheckAction>> getTestEntries(Class<?> action
if (Modifier.isStatic(method.getModifiers()) == false) {
throw new AssertionError("Entitlement test method [" + method + "] must be static");
}
if (Modifier.isPrivate(method.getModifiers())) {
throw new AssertionError("Entitlement test method [" + method + "] must not be private");
}
final CheckedConsumer<Environment, Exception> call = createConsumerForMethod(method);
CheckedConsumer<Environment, Exception> runnable = env -> {
try {
Expand All @@ -109,14 +117,16 @@ private static Stream<Entry<String, CheckAction>> getTestEntries(Class<?> action
}
};
Integer fromJavaVersion = testAnnotation.fromJavaVersion() == -1 ? null : testAnnotation.fromJavaVersion();
entries.add(
entry(
method.getName(),
new CheckAction(runnable, testAnnotation.expectedAccess(), testAnnotation.expectedExceptionIfDenied(), fromJavaVersion)
)
var checkAction = new CheckAction(
runnable,
testAnnotation.expectedAccess(),
testAnnotation.expectedExceptionIfDenied(),
fromJavaVersion
);
if (filter.test(checkAction)) {
entries.add(entry(method.getName(), checkAction));
}
}
return entries.stream();
}

private static CheckedConsumer<Environment, Exception> createConsumerForMethod(Method method) {
Expand Down