1 Reply Latest reply on Nov 23, 2009 1:12 PM by Sergey Vidyuk

    How to use IdentityManager in tests?

    Sergey Vidyuk Newbie

      I was able to create/modify new users using EntityManager both in applicacation and in tests. When I start using IdentityManager to create new users and grant roles I start getting EntityManagerFactory not found in JNDI : java:/entityManager in tests while application deployed to jboss works fine.


      I've created new project with seam-gen to reproduce this issue in smales possible example.


      AuthenticatorBean:


      @Stateless
      @Name("authenticator")
      public class AuthenticatorBean implements Authenticator
      {
          @Logger private Log log;
      
          @In Identity identity;
          @In Credentials credentials;
          @In IdentityManager identityManager;
      
          public boolean authenticate()
          {
              if (identityManager.authenticate(credentials.getUsername(),credentials.getPassword()))
              {
                  for (String role: identityManager.getGrantedRoles(credentials.getUsername())) {
                      identity.addRole(role);
                  }
              }
              return false;
          }
      
      }
      



      AutheneticatorTest:


      public class AutheneticatorTest extends SeamTest {
              private static final String USERNAME = "admin@test.com";
              private static final String PASS = "12345678";
      
              @BeforeMethod
              public void init() throws Exception {
                      new ComponentTest() {
                              @Override
                              protected void testComponents() throws Exception {
                                      createUser();
                              }
                              
                              @Transactional
                              private void createUser() {
                                      Identity.setSecurityEnabled(false);
                                      IdentityManager.instance().createUser(USERNAME,PASS);
                                      Identity.setSecurityEnabled(true);
                              }
                      }.run();
              }
      
              @Test
              public void loginTest() throws Exception {
                      new FacesRequest("/login.xhtml") {
                              @Override
                              protected void processValidations() throws Exception {
                                      validateValue("#{credentials.username}", USERNAME);
                                      validateValue("#{credentials.password}", PASS);
                                      validateValue("#{identity.loggedIn}", true);
                                      assert !isValidationFailure();
                              }
                              
                              @Override
                              protected void updateModelValues() throws Exception {
                                      setValue("#{credentials.username}", USERNAME);
                                      setValue("#{credentials.password}", PASS);
                              }
                              
                              @Override
                              protected void invokeApplication() {
                          assert invokeMethod("#{identity.login}").equals("loggedIn");
                       }
                      }.run();
              }
      }
      



      User and Role classes are taken from the chapter 15.4.2.3.1. Minimal schema example of Seam 2.2.0.GA tutorial, components.xml modified to use this classes for identity management.


      when I run tests I got the following errors:


      [testng] [Parser] Running:
         [testng]   /home/vidyuk/Development/test/test-build/AutheneticatorTest.xml
         [testng]                                                                  
         [testng] WARN  [org.jboss.seam.security.permission.PersistentPermissionResolver] no permission store available - please install a PermissionStore with the name 'org.jboss.seam.security.jpaPermissionStore' if persistent permissions are required. 
         [testng] FAILED CONFIGURATION: @BeforeMethod init                                                                        
         [testng] org.jboss.seam.security.management.IdentityManagementException: Could not create account                        
         [testng]     at org.jboss.seam.security.management.JpaIdentityStore.createUser(JpaIdentityStore.java:246)                
         [testng]     at org.jboss.seam.security.management.IdentityManager.createUser(IdentityManager.java:100)                  
         [testng]     at org.jboss.seam.security.management.IdentityManager.createUser(IdentityManager.java:94)                   
         [testng]     at com.mydomain.test.tests.AutheneticatorTest$1.createUser(AutheneticatorTest.java:25)                      
         [testng]     at com.mydomain.test.tests.AutheneticatorTest$1.testComponents(AutheneticatorTest.java:19)                  
         [testng]     at org.jboss.seam.mock.AbstractSeamTest$ComponentTest.run(AbstractSeamTest.java:162)                        
         [testng]     at com.mydomain.test.tests.AutheneticatorTest.init(AutheneticatorTest.java:16)                              
         [testng] Caused by: java.lang.IllegalArgumentException: EntityManagerFactory not found in JNDI : java:/entityManager     
         [testng]     at org.jboss.seam.persistence.ManagedPersistenceContext.getEntityManagerFactoryFromJndiOrValueBinding(ManagedPersistenceContext.java:245)                                                                                               
         [testng]     at org.jboss.seam.persistence.ManagedPersistenceContext.initEntityManager(ManagedPersistenceContext.java:78)
         [testng]     at org.jboss.seam.persistence.ManagedPersistenceContext.getEntityManager(ManagedPersistenceContext.java:107)
         [testng]     at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)                                              
         [testng]     at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:144)                                      
         [testng]     at org.jboss.seam.Component.callComponentMethod(Component.java:2249)                                        
         [testng]     at org.jboss.seam.Component.unwrap(Component.java:2275)                                                     
         [testng]     at org.jboss.seam.Component.getInstance(Component.java:2041)                                                
         [testng]     at org.jboss.seam.Component.getInstance(Component.java:1983)                                                
         [testng]     at org.jboss.seam.Component.getInstance(Component.java:1977)                                                
         [testng]     at org.jboss.seam.Namespace.getComponentInstance(Namespace.java:55)                                         
         [testng]     at org.jboss.seam.Namespace.getComponentInstance(Namespace.java:50)                                         
         [testng]     at org.jboss.seam.el.SeamELResolver.resolveBase(SeamELResolver.java:148)                                    
         [testng]     at org.jboss.seam.el.SeamELResolver.getValue(SeamELResolver.java:51)                                        
         [testng]     at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:143)                                      
         [testng]     at org.jboss.el.parser.AstIdentifier.getValue(AstIdentifier.java:44)                                        
         [testng]     at org.jboss.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)                                  
         [testng]     at org.jboss.seam.core.Expressions$1.getValue(Expressions.java:112)                                         
         [testng]     at org.jboss.seam.security.management.JpaIdentityStore.lookupEntityManager(JpaIdentityStore.java:1051)      
         [testng]     at org.jboss.seam.security.management.JpaIdentityStore.lookupUser(JpaIdentityStore.java:860)                
         [testng]     at org.jboss.seam.security.management.JpaIdentityStore.userExists(JpaIdentityStore.java:635)                
         [testng]     at org.jboss.seam.security.management.JpaIdentityStore.createUser(JpaIdentityStore.java:208)                
         [testng]     ... 30 more                                                                                                 
         [testng] Caused by: javax.naming.NameNotFoundException: entityManager not bound                                          
         [testng]     at org.jnp.server.NamingServer.getBinding(NamingServer.java:542)                                            
         [testng]     at org.jnp.server.NamingServer.getBinding(NamingServer.java:550)
         [testng]     at org.jnp.server.NamingServer.getObject(NamingServer.java:556)
         [testng]     at org.jnp.server.NamingServer.lookup(NamingServer.java:296)
         [testng]     at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:669)
         [testng]     at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:629)
         [testng]     at javax.naming.InitialContext.lookup(InitialContext.java:392)
         [testng]     at org.jboss.seam.persistence.ManagedPersistenceContext.getEntityManagerFactoryFromJndiOrValueBinding(ManagedPersistenceContext.java:241)
         [testng]     ... 55 more
         [testng] ... Removed 28 stack frames
         [testng] SKIPPED: loginTest
         [testng]
         [testng] ===============================================
         [testng]     Login
         [testng]     Tests run: 1, Failures: 0, Skips: 1
         [testng]     Configuration Failures: 1, Skips: 0
         [testng] ===============================================
         [testng]
         [testng]
         [testng] ===============================================
         [testng] Autheneticator
         [testng] Total tests run: 1, Failures: 0, Skips: 1
         [testng] Configuration Failures: 1, Skips: 0
         [testng] ===============================================
         [testng]
      



      I've already spent 8 or 9 hours trying to solve this issue. How can I run actions that are call IdentityManager methods in SeamTest?