use of org.apache.openejb.junit5.OpenEjbExtension in project tomee by apache.
the class TestSecurityTemplateInvocationContextProvider method invocationContext.
private TestTemplateInvocationContext invocationContext(String role, boolean authorized) {
return new TestTemplateInvocationContext() {
private boolean ejbAccessThrown = false;
@Override
public String getDisplayName(int invocationIndex) {
if (role.equals(TestSecurity.UNAUTHENTICATED)) {
return "unauthenticated";
}
return role;
}
@Override
public List<Extension> getAdditionalExtensions() {
List<Extension> extensions = new ArrayList<>();
extensions.add(new TestExecutionExceptionHandler() {
@Override
public void handleTestExecutionException(ExtensionContext extensionContext, Throwable throwable) throws Throwable {
if (!authorized) {
if (throwable instanceof EJBAccessException) {
// ok - this would be expected here, do not fail the test!
ejbAccessThrown = true;
return;
} else {
throw throwable;
}
}
throw throwable;
}
});
extensions.add(new AfterEachCallback() {
@Override
public void afterEach(ExtensionContext extensionContext) throws Exception {
if (!authorized) {
if (!ejbAccessThrown) {
throw new RuntimeException("Expected 'EJBAccessException' but caught none.");
}
}
}
});
extensions.add(new OpenEjbExtension(role));
return extensions;
}
};
}
use of org.apache.openejb.junit5.OpenEjbExtension in project tomee by apache.
the class TestSecurityTemplateInvocationContextProvider method provideTestTemplateInvocationContexts.
@Override
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext extensionContext) {
TestSecurity testSecurity = null;
if (extensionContext.getTestMethod().isPresent() && extensionContext.getTestMethod().get().isAnnotationPresent(TestSecurity.class)) {
testSecurity = extensionContext.getTestMethod().get().getAnnotation(TestSecurity.class);
} else if (extensionContext.getTestClass().isPresent() && extensionContext.getTestClass().get().isAnnotationPresent(TestSecurity.class)) {
testSecurity = extensionContext.getTestClass().get().getAnnotation(TestSecurity.class);
}
if (testSecurity != null) {
String[] authorized = testSecurity.authorized();
String[] unauthorized = testSecurity.unauthorized();
List<TestTemplateInvocationContext> contexts = new ArrayList<>();
for (String role : authorized) {
contexts.add(invocationContext(role, true));
}
for (String role : unauthorized) {
contexts.add(invocationContext(role, false));
}
return contexts.stream();
} else {
return Stream.of(new TestTemplateInvocationContext() {
@Override
public List<Extension> getAdditionalExtensions() {
return Collections.singletonList(new OpenEjbExtension());
}
});
}
}
Aggregations