Skip to content

Commit

Permalink
[WFLY-5403] Adapt tests that register EJB client interceptors to the …
Browse files Browse the repository at this point in the history
…new EJB client API
  • Loading branch information
fjuma committed Jan 13, 2017
1 parent bf75f89 commit ab01c59
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 122 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@
@RunWith(Arquillian.class) @RunWith(Arquillian.class)
public class ContainerInterceptorsTestCase { public class ContainerInterceptorsTestCase {


private static final int CLIENT_INTERCEPTOR_ORDER = 0x99999;

private static final String EJB_JAR_NAME = "ejb-container-interceptors"; private static final String EJB_JAR_NAME = "ejb-container-interceptors";


@ArquillianResource @ArquillianResource
Expand Down Expand Up @@ -169,42 +167,41 @@ public void testInvocationOnRemoteView() throws Exception {
// force real remote invocation so that the RemotingConnectionEJBReceiver is used instead of a LocalEJBReceiver // force real remote invocation so that the RemotingConnectionEJBReceiver is used instead of a LocalEJBReceiver
@RunAsClient @RunAsClient
public void testDataPassingForContainerInterceptorsOnRemoteView() throws Exception { public void testDataPassingForContainerInterceptorsOnRemoteView() throws Exception {
// get hold of the EJBClientContext
final EJBClientContext ejbClientContext = EJBClientContext.requireCurrent();

// create some data that the client side interceptor will pass along during the EJB invocation // create some data that the client side interceptor will pass along during the EJB invocation
final Map<String, Object> interceptorData = new HashMap<String, Object>(); final Map<String, Object> interceptorData = new HashMap<String, Object>();
interceptorData.put(FlowTrackingBean.CONTEXT_DATA_KEY, ContainerInterceptorOne.class.getName()); interceptorData.put(FlowTrackingBean.CONTEXT_DATA_KEY, ContainerInterceptorOne.class.getName());
final SimpleEJBClientInterceptor clientInterceptor = new SimpleEJBClientInterceptor(interceptorData); final SimpleEJBClientInterceptor clientInterceptor = new SimpleEJBClientInterceptor(interceptorData);
// register the client side interceptor // get hold of the EJBClientContext and register the client side interceptor
ejbClientContext.registerInterceptor(CLIENT_INTERCEPTOR_ORDER, clientInterceptor); EJBClientContext ejbClientContext = EJBClientContext.requireCurrent().withAddedInterceptors(clientInterceptor);


final Hashtable<String, Object> jndiProps = new Hashtable<String, Object>(); final Hashtable<String, Object> jndiProps = new Hashtable<String, Object>();
jndiProps.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); jndiProps.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
final Context jndiCtx = new InitialContext(jndiProps); final Context jndiCtx = new InitialContext(jndiProps);
final FlowTracker bean = (FlowTracker) jndiCtx.lookup("ejb:/" + EJB_JAR_NAME + "/" ejbClientContext.runCallable(() -> {
+ FlowTrackingBean.class.getSimpleName() + "!" + FlowTracker.class.getName()); final FlowTracker bean = (FlowTracker) jndiCtx.lookup("ejb:/" + EJB_JAR_NAME + "/"
final String message = "foo"; + FlowTrackingBean.class.getSimpleName() + "!" + FlowTracker.class.getName());
// we passed ContainerInterceptorOne as the value of the context data for the invocation, which means that we want the ContainerInterceptorOne final String message = "foo";
// to be skipped, so except that interceptor, the rest should be invoked. // we passed ContainerInterceptorOne as the value of the context data for the invocation, which means that we want the ContainerInterceptorOne
final String expectedResultForFirstInvocation = NonContainerInterceptor.class.getName() + " " // to be skipped, so except that interceptor, the rest should be invoked.
+ FlowTrackingBean.class.getName() + " " + message; final String expectedResultForFirstInvocation = NonContainerInterceptor.class.getName() + " "
final String firstResult = bean.echo(message); + FlowTrackingBean.class.getName() + " " + message;
Assert.assertEquals("Unexpected result invoking on bean when passing context data via EJB client interceptor", final String firstResult = bean.echo(message);
expectedResultForFirstInvocation, firstResult); Assert.assertEquals("Unexpected result invoking on bean when passing context data via EJB client interceptor",

expectedResultForFirstInvocation, firstResult);
// Now try another invocation, this time skip a different interceptor
interceptorData.clear(); // Now try another invocation, this time skip a different interceptor
interceptorData.put(FlowTrackingBean.CONTEXT_DATA_KEY, NonContainerInterceptor.class.getName()); interceptorData.clear();
final String secondMessage = "bar"; interceptorData.put(FlowTrackingBean.CONTEXT_DATA_KEY, NonContainerInterceptor.class.getName());
// we passed NonContainerInterceptor as the value of the context data for the invocation, which means that we want the NonContainerInterceptor final String secondMessage = "bar";
// to be skipped, so except that interceptor, the rest should be invoked. // we passed NonContainerInterceptor as the value of the context data for the invocation, which means that we want the NonContainerInterceptor
final String expectedResultForSecondInvocation = ContainerInterceptorOne.class.getName() + " " // to be skipped, so except that interceptor, the rest should be invoked.
+ FlowTrackingBean.class.getName() + " " + secondMessage; final String expectedResultForSecondInvocation = ContainerInterceptorOne.class.getName() + " "
final String secondResult = bean.echo(secondMessage); + FlowTrackingBean.class.getName() + " " + secondMessage;
Assert.assertEquals("Unexpected result invoking on bean when passing context data via EJB client interceptor", final String secondResult = bean.echo(secondMessage);
expectedResultForSecondInvocation, secondResult); Assert.assertEquals("Unexpected result invoking on bean when passing context data via EJB client interceptor",

expectedResultForSecondInvocation, secondResult);
return null;
});
} }


/** /**
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -201,23 +201,22 @@ private void callUsingClientLoginModul(String userName, boolean hasRole1, boolea
loginContext.login(); loginContext.login();


// register the client side interceptor // register the client side interceptor
final Registration clientInterceptorHandler = EJBClientContext.requireCurrent().registerInterceptor(112567, final EJBClientContext ejbClientContext = EJBClientContext.requireCurrent().withAddedInterceptors(new ClientSecurityInterceptor());
new ClientSecurityInterceptor()); ejbClientContext.runCallable(() -> {

final Manage targetBean = EJBUtil.lookupEJB(TargetBean.class, Manage.class);
final Manage targetBean = EJBUtil.lookupEJB(TargetBean.class, Manage.class); final Manage bridgeBean = EJBUtil.lookupEJB(BridgeBean.class, Manage.class);
final Manage bridgeBean = EJBUtil.lookupEJB(BridgeBean.class, Manage.class);

//test direct access
//test direct access testMethodAccess(targetBean, ManageMethodEnum.ALLROLES, true);
testMethodAccess(targetBean, ManageMethodEnum.ALLROLES, true); testMethodAccess(targetBean, ManageMethodEnum.ROLE1, hasRole1);
testMethodAccess(targetBean, ManageMethodEnum.ROLE1, hasRole1); testMethodAccess(targetBean, ManageMethodEnum.ROLE2, hasRole2);
testMethodAccess(targetBean, ManageMethodEnum.ROLE2, hasRole2);

//test security context propagation
//test security context propagation testMethodAccess(bridgeBean, ManageMethodEnum.ALLROLES, true);
testMethodAccess(bridgeBean, ManageMethodEnum.ALLROLES, true); testMethodAccess(bridgeBean, ManageMethodEnum.ROLE1, hasRole1);
testMethodAccess(bridgeBean, ManageMethodEnum.ROLE1, hasRole1); testMethodAccess(bridgeBean, ManageMethodEnum.ROLE2, hasRole2);
testMethodAccess(bridgeBean, ManageMethodEnum.ROLE2, hasRole2); return null;

});
clientInterceptorHandler.remove();
} finally { } finally {
if (loginContext != null) { if (loginContext != null) {
loginContext.logout(); loginContext.logout();
Expand All @@ -235,24 +234,24 @@ private void callUsingSecurityContextAssociation(String userName, boolean hasRol
final ContextSelector<EJBClientContext> selector = new ConfigBasedEJBClientContextSelector(cc); final ContextSelector<EJBClientContext> selector = new ConfigBasedEJBClientContextSelector(cc);
EJBClientContext.setSelector(selector); EJBClientContext.setSelector(selector);
// register the client side interceptor // register the client side interceptor
final Registration clientInterceptorHandler = EJBClientContext.requireCurrent().registerInterceptor(112567, final EJBClientContext ejbClientContext = EJBClientContext.requireCurrent().withAddedInterceptors(new ClientSecurityInterceptor());
new ClientSecurityInterceptor());
SecurityContextAssociation.setPrincipal(new SimplePrincipal(userName)); SecurityContextAssociation.setPrincipal(new SimplePrincipal(userName));


final Manage targetBean = EJBUtil.lookupEJB(TargetBean.class, Manage.class); ejbClientContext.runCallable(() -> {
final Manage bridgeBean = EJBUtil.lookupEJB(BridgeBean.class, Manage.class); final Manage targetBean = EJBUtil.lookupEJB(TargetBean.class, Manage.class);

final Manage bridgeBean = EJBUtil.lookupEJB(BridgeBean.class, Manage.class);
//test direct access
testMethodAccess(targetBean, ManageMethodEnum.ALLROLES, true); //test direct access
testMethodAccess(targetBean, ManageMethodEnum.ROLE1, hasRole1); testMethodAccess(targetBean, ManageMethodEnum.ALLROLES, true);
testMethodAccess(targetBean, ManageMethodEnum.ROLE2, hasRole2); testMethodAccess(targetBean, ManageMethodEnum.ROLE1, hasRole1);

testMethodAccess(targetBean, ManageMethodEnum.ROLE2, hasRole2);
//test security context propagation
testMethodAccess(bridgeBean, ManageMethodEnum.ALLROLES, true); //test security context propagation
testMethodAccess(bridgeBean, ManageMethodEnum.ROLE1, hasRole1); testMethodAccess(bridgeBean, ManageMethodEnum.ALLROLES, true);
testMethodAccess(bridgeBean, ManageMethodEnum.ROLE2, hasRole2); testMethodAccess(bridgeBean, ManageMethodEnum.ROLE1, hasRole1);

testMethodAccess(bridgeBean, ManageMethodEnum.ROLE2, hasRole2);
clientInterceptorHandler.remove(); return null;
});
} finally { } finally {
SecurityContextAssociation.clearSecurityContext(); SecurityContextAssociation.clearSecurityContext();
} }
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -202,23 +202,23 @@ private void callUsingClientLoginModul(String userName, boolean hasRole1, boolea
loginContext.login(); loginContext.login();


// register the client side interceptor // register the client side interceptor
final Registration clientInterceptorHandler = EJBClientContext.requireCurrent().registerInterceptor(112567, final EJBClientContext ejbClientContext = EJBClientContext.requireCurrent().withAddedInterceptors(new ClientSecurityInterceptor());
new ClientSecurityInterceptor()); ejbClientContext.runCallable(() -> {
final Manage targetBean = EJBUtil.lookupEJB(TargetBean.class, Manage.class);
final Manage bridgeBean = EJBUtil.lookupEJB(BridgeBean.class, Manage.class);

//test direct access
testMethodAccess(targetBean, ManageMethodEnum.ALLROLES, true);
testMethodAccess(targetBean, ManageMethodEnum.ROLE1, hasRole1);
testMethodAccess(targetBean, ManageMethodEnum.ROLE2, hasRole2);

//test security context propagation
testMethodAccess(bridgeBean, ManageMethodEnum.ALLROLES, true);
testMethodAccess(bridgeBean, ManageMethodEnum.ROLE1, hasRole1);
testMethodAccess(bridgeBean, ManageMethodEnum.ROLE2, hasRole2);
return null;
});


final Manage targetBean = EJBUtil.lookupEJB(TargetBean.class, Manage.class);
final Manage bridgeBean = EJBUtil.lookupEJB(BridgeBean.class, Manage.class);

//test direct access
testMethodAccess(targetBean, ManageMethodEnum.ALLROLES, true);
testMethodAccess(targetBean, ManageMethodEnum.ROLE1, hasRole1);
testMethodAccess(targetBean, ManageMethodEnum.ROLE2, hasRole2);

//test security context propagation
testMethodAccess(bridgeBean, ManageMethodEnum.ALLROLES, true);
testMethodAccess(bridgeBean, ManageMethodEnum.ROLE1, hasRole1);
testMethodAccess(bridgeBean, ManageMethodEnum.ROLE2, hasRole2);

clientInterceptorHandler.remove();
} finally { } finally {
if (loginContext != null) { if (loginContext != null) {
loginContext.logout(); loginContext.logout();
Expand All @@ -236,24 +236,24 @@ private void callUsingSecurityContextAssociation(String userName, boolean hasRol
final ContextSelector<EJBClientContext> selector = new ConfigBasedEJBClientContextSelector(cc); final ContextSelector<EJBClientContext> selector = new ConfigBasedEJBClientContextSelector(cc);
EJBClientContext.setSelector(selector); EJBClientContext.setSelector(selector);
// register the client side interceptor // register the client side interceptor
final Registration clientInterceptorHandler = EJBClientContext.requireCurrent().registerInterceptor(112567, final EJBClientContext ejbClientContext = EJBClientContext.requireCurrent().withAddedInterceptors(new ClientSecurityInterceptor());
new ClientSecurityInterceptor());
SecurityContextAssociation.setPrincipal(new SimplePrincipal(userName)); SecurityContextAssociation.setPrincipal(new SimplePrincipal(userName));


final Manage targetBean = EJBUtil.lookupEJB(TargetBean.class, Manage.class); ejbClientContext.runCallable(() -> {
final Manage bridgeBean = EJBUtil.lookupEJB(BridgeBean.class, Manage.class); final Manage targetBean = EJBUtil.lookupEJB(TargetBean.class, Manage.class);

final Manage bridgeBean = EJBUtil.lookupEJB(BridgeBean.class, Manage.class);
//test direct access
testMethodAccess(targetBean, ManageMethodEnum.ALLROLES, true); //test direct access
testMethodAccess(targetBean, ManageMethodEnum.ROLE1, hasRole1); testMethodAccess(targetBean, ManageMethodEnum.ALLROLES, true);
testMethodAccess(targetBean, ManageMethodEnum.ROLE2, hasRole2); testMethodAccess(targetBean, ManageMethodEnum.ROLE1, hasRole1);

testMethodAccess(targetBean, ManageMethodEnum.ROLE2, hasRole2);
//test security context propagation
testMethodAccess(bridgeBean, ManageMethodEnum.ALLROLES, true); //test security context propagation
testMethodAccess(bridgeBean, ManageMethodEnum.ROLE1, hasRole1); testMethodAccess(bridgeBean, ManageMethodEnum.ALLROLES, true);
testMethodAccess(bridgeBean, ManageMethodEnum.ROLE2, hasRole2); testMethodAccess(bridgeBean, ManageMethodEnum.ROLE1, hasRole1);

testMethodAccess(bridgeBean, ManageMethodEnum.ROLE2, hasRole2);
clientInterceptorHandler.remove(); return null;
});
} finally { } finally {
SecurityContextAssociation.clearSecurityContext(); SecurityContextAssociation.clearSecurityContext();
} }
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
@RunWith(Arquillian.class) @RunWith(Arquillian.class)
public class EJBClientInterceptorTestCase { public class EJBClientInterceptorTestCase {


private static final int CLIENT_INTERCEPTOR_ORDER = 0x99999;

private static final String APP_NAME = ""; private static final String APP_NAME = "";
private static final String DISTINCT_NAME = ""; private static final String DISTINCT_NAME = "";
private static final String MODULE_NAME = "ejb-client-interceptor-test-module"; private static final String MODULE_NAME = "ejb-client-interceptor-test-module";
Expand All @@ -67,9 +65,6 @@ public static Archive createDeployment() {
@Test @Test
@RunAsClient // run as a truly remote client @RunAsClient // run as a truly remote client
public void testEJBClientInterceptionFromRemoteClient() throws Exception { public void testEJBClientInterceptionFromRemoteClient() throws Exception {
// get hold of the EJBClientContext
final EJBClientContext ejbClientContext = EJBClientContext.requireCurrent();

// create some data that the client side interceptor will pass along during the EJB invocation // create some data that the client side interceptor will pass along during the EJB invocation
final Map<String, Object> interceptorData = new HashMap<String, Object>(); final Map<String, Object> interceptorData = new HashMap<String, Object>();
final String keyOne = "foo"; final String keyOne = "foo";
Expand All @@ -81,26 +76,29 @@ public void testEJBClientInterceptionFromRemoteClient() throws Exception {
interceptorData.put(keyTwo, valueTwo); interceptorData.put(keyTwo, valueTwo);


final SimpleEJBClientInterceptor clientInterceptor = new SimpleEJBClientInterceptor(interceptorData); final SimpleEJBClientInterceptor clientInterceptor = new SimpleEJBClientInterceptor(interceptorData);
// register the client side interceptor // get hold of the EJBClientContext and register the client side interceptor
ejbClientContext.registerInterceptor(CLIENT_INTERCEPTOR_ORDER, clientInterceptor); final EJBClientContext ejbClientContext = EJBClientContext.requireCurrent().withAddedInterceptors(clientInterceptor);


final Hashtable props = new Hashtable(); final Hashtable props = new Hashtable();
props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
final Context jndiContext = new InitialContext(props); final Context jndiContext = new InitialContext(props);
final RemoteSFSB remoteSFSB = (RemoteSFSB) jndiContext.lookup("ejb:" + APP_NAME + "/" + MODULE_NAME + "/" + DISTINCT_NAME ejbClientContext.runCallable(() -> {
+ "/" + SimpleSFSB.class.getSimpleName() + "!" + RemoteSFSB.class.getName() + "?stateful"); final RemoteSFSB remoteSFSB = (RemoteSFSB) jndiContext.lookup("ejb:" + APP_NAME + "/" + MODULE_NAME + "/" + DISTINCT_NAME

+ "/" + SimpleSFSB.class.getSimpleName() + "!" + RemoteSFSB.class.getName() + "?stateful");
// invoke the bean and ask it for the invocation data that it saw on the server side
final Map<String, Object> valuesSeenOnServerSide = remoteSFSB.getInvocationData(keyOne, keyTwo); // invoke the bean and ask it for the invocation data that it saw on the server side
// make sure the server side bean was able to get the data which was passed on by the client side final Map<String, Object> valuesSeenOnServerSide = remoteSFSB.getInvocationData(keyOne, keyTwo);
// interceptor // make sure the server side bean was able to get the data which was passed on by the client side
Assert.assertNotNull("Server side context data was expected to be non-null", valuesSeenOnServerSide); // interceptor
Assert.assertFalse("Server side context data was expected to be non-empty", valuesSeenOnServerSide.isEmpty()); Assert.assertNotNull("Server side context data was expected to be non-null", valuesSeenOnServerSide);
for (final Map.Entry<String, Object> clientInterceptorDataEntry : interceptorData.entrySet()) { Assert.assertFalse("Server side context data was expected to be non-empty", valuesSeenOnServerSide.isEmpty());
final String key = clientInterceptorDataEntry.getKey(); for (final Map.Entry<String, Object> clientInterceptorDataEntry : interceptorData.entrySet()) {
final Object expectedValue = clientInterceptorDataEntry.getValue(); final String key = clientInterceptorDataEntry.getKey();
Assert.assertEquals("Unexpected value in bean, on server side, via InvocationContext.getContextData() for key " + key, expectedValue, valuesSeenOnServerSide.get(key)); final Object expectedValue = clientInterceptorDataEntry.getValue();
} Assert.assertEquals("Unexpected value in bean, on server side, via InvocationContext.getContextData() for key " + key, expectedValue, valuesSeenOnServerSide.get(key));
}
return null;
});


} }


Expand Down
Loading

0 comments on commit ab01c59

Please sign in to comment.