Struggling to test secure EJB: No mapping available for role reference
simonna Mar 22, 2013 10:13 AMHi All,
I'm trying to test an EJB secured with basic authentification.
I'm using Arquillian with Glassfish Embedded:
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.0.3.Final</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-glassfish-embedded-3.1</artifactId>
<version>1.0.0.CR3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.2</version>
<scope>provided</scope>
</dependency>
Test.java:
@RunWith(Arquillian.class)
public class GirafeBOFactoryLocalBeanTest {
@EJB(mappedName="ejb/GirafeBeanFactory")
private GirafeBOFactoryLocal girafeBOFactory;
public GirafeBOFactoryLocalBeanTest() {
}
@Deployment
public static JavaArchive createDeployment() {
JavaArchive myArchive = ShrinkWrap.create(JavaArchive.class)
.addPackage(GirafeBOFactoryLocalBean.class.getPackage())
.addPackage(Fault.class.getPackage())
.addAsManifestResource("META-INF/persistence.xml", "persistence.xml")
.addAsManifestResource("META-INF/sun-ejb-jar.xml", "sun-ejb-jar.xml")
.addAsManifestResource("META-INF/ejb-jar.xml", "ejb-jar.xml")
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
System.out.println(myArchive.toString(true));
return myArchive;
}
/**
* Test of getFault method, of class GirafeBOFactoryLocalBean.
*/
@Test
public void testGetFault_4args() throws Exception {
if (GlassFishTestHelper.loginFileUser("simonna", "xxxxx"))
{
System.out.println("login success");
Fault expResult = null;
Fault result = null;
System.out.println("context:" + this.girafeBOFactory.getSessionContext());
System.out.println("principal:" + this.girafeBOFactory.getSessionContext().getCallerPrincipal());
System.out.println("role Query:" + this.girafeBOFactory.getSessionContext().isCallerInRole("Query"));
assertEquals(expResult, result);
}
else
{
System.out.println("login failure");
fail("Login failed.");
}
}
The GlassfishTestHelper class simply connects the embedded server using the programmatic API:
ProgrammaticLogin pgLogin = new ProgrammaticLogin();
boolean res = pgLogin.login(username, password.toCharArray(), "CbsWebRealm", true);
The connection seems to work properly, however the test is failing when checking the user role:
Output:
INFO: test was successfully deployed in 4,820 milliseconds.
getFault
login success
context:GirafeBOFactoryLocalBean; id: [B@3f8949
principal:simonna
classLoader = WebappClassLoader (delegate=true; repositories=WEB-INF/classes/)
SharedSecrets.getJavaNetAccess()=java.net.URLClassLoader$7@1def16d
PlainTextActionReporterSUCCESSNo monitoring data to report.
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 8.315 sec <<< FAILURE!
Exception:
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 8.315 sec <<< FAILURE!
testGetFault_4args(com.telkom.girafe.beans.GirafeBOFactoryLocalBeanTest) Time elapsed: 0.436 sec <<< ERROR!
java.lang.IllegalStateException: No mapping available for role reference Query
at com.sun.ejb.containers.EJBContextImpl.isCallerInRole(EJBContextImpl.java:463)
at com.telkom.girafe.beans.GirafeBOFactoryLocalBeanTest.testGetFault_4args(GirafeBOFactoryLocalBeanTest.java:118)
It looks like my sun-ejb-jar.xml (where my role / group mapping is defined) is not taken into consideration.
I guess the way I add the file as a manifest resource with ShrinkWrap is incorrect, but I couldn't find the right way to do it?
Did somebody already had this problem and found a solution?
I could not find any complete example implementing secure EJB testing with Arquillian. This would definitely help.