Search in sources :

Example 1 with OpenEjbExtension

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;
        }
    };
}
Also used : OpenEjbExtension(org.apache.openejb.junit5.OpenEjbExtension) ArrayList(java.util.ArrayList) EJBAccessException(jakarta.ejb.EJBAccessException) EJBAccessException(jakarta.ejb.EJBAccessException) OpenEjbExtension(org.apache.openejb.junit5.OpenEjbExtension)

Example 2 with OpenEjbExtension

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());
            }
        });
    }
}
Also used : TestSecurity(org.apache.openejb.junit.TestSecurity) OpenEjbExtension(org.apache.openejb.junit5.OpenEjbExtension) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)2 OpenEjbExtension (org.apache.openejb.junit5.OpenEjbExtension)2 EJBAccessException (jakarta.ejb.EJBAccessException)1 List (java.util.List)1 TestSecurity (org.apache.openejb.junit.TestSecurity)1