7 Replies Latest reply on Dec 10, 2008 10:00 AM by ssilvert

    Issue while getting teh value of session scoped variable whe

      While testing login.xhtml', i am facing issue.
      Code for login.xhtml is
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ig="http://www.infragistics.com/faces/netadvantage"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:fic="http://www.fairisaac.com/edm/cc/ui/components"
      xmlns:cc="http://www.fairisaac.com/edm/cc/ui/components/messages"
      xmlns:c="http://java.sun.com/jsp/jstl/core">


      <meta http-equiv="Pragma" content="no-cache"/>
      <meta http-equiv="Cache-Control" content="no-cache"/>

      Debt Manager 8.0
















      Don't have an account?
      <h2>Please sign into your account.</h2>Access to the Fair Isaac applications requires verification of your username and password.

      <f:view>
      <h:form id="loginForm">



      <h:outputLabel value="Login ID" for="username" disabled="#{loginMBean.authenticateDisabled}"/>
      <h:inputText id="username" value="#{loginMBean.username}" disabled="#{loginMBean.authenticateDisabled}"/>
      <h:outputText id="invalidloginid" value="#{loginMBean.invalidLoginid}"/>


      <h:outputLabel value="Password" for="password" disabled="#{loginMBean.authenticateDisabled}"/>
      <h:inputSecret id="password" value="#{loginMBean.password}" disabled="#{loginMBean.authenticateDisabled}" redisplay="true"/>
      <h:outputText id="invalidpassword" value="#{loginMBean.invalidPassword}"/>




      <h:outputText value="Forgot your Login ID or Password?" disabled="#{loginMBean.authenticateDisabled}"/>





      <h:commandButton value="Authenticate" id="authenticate" action="authenticate" actionListener="#{loginMBean.authenticate}" disabled="#{loginMBean.authenticateDisabled}"/>



      <h:outputLabel value="Tenant" for="pickTenant" disabled="#{loginMBean.loginDisabled}"/>

      <h:selectOneListbox id="pickTenant" value="#{loginMBean.currentTenant}" disabled="#{loginMBean.loginDisabled}" size="1">
      <f:selectItems value="#{loginMBean.tenantList}" />
      </h:selectOneListbox>



      <h:outputLabel value="Locale" for="pickLocale" disabled="#{loginMBean.loginDisabled}"/>

      <h:selectOneListbox id="pickLocale" value="#{tmsLocaleBean.localization}" disabled="#{loginMBean.loginDisabled}" size="1">
      <f:selectItems value="#{loginMBean.localeList}" />
      </h:selectOneListbox>








      <h:commandButton value="Log In" id="logIn" action="#{loginMBean.getHomepage}" actionListener="#{loginMBean.login}" disabled="#{loginMBean.loginDisabled}" />
      <h:commandButton value="Reset" id="reset" action="reset" actionListener="#{loginMBean.reset}" disabled="#{loginMBean.loginDisabled}" immediate="true"/>




      <h:messages globalOnly="false" />





      </h:form>
      </f:view>



      Copyright 2008 Fair Isaac Corporation. All rights reserved.
      <!-- end container-->





      The flow is:
      1) When the page is first loadced the 'Authenticate' button is not disabled and 'LogIn' and 'Reset' button is disabled.
      2) When the authentication is succesfull, the 'Authenticate' button is disabled and 'LogIn' and 'Reset' button is not disabled.
      and control comes to 'Login.xhml'.
      3) When user clicks 'Reset' button control comes to login.xhtml and 'Authenticate' button is not disabled and 'LogIn' and 'Reset' button is disabled.
      4) when user clicks 'LogIn' button flow redirected to soem different page.

      Scope of the LoginMbean is 'Session'

      Code for my test class is

      package com.fairisaac.dms.fidem.jsfunit.test;

      import java.io.IOException;
      import java.util.Map;

      import javax.faces.component.UIComponent;

      import junit.framework.Test;
      import junit.framework.TestSuite;

      import org.apache.cactus.ServletTestCase;
      import org.jboss.jsfunit.framework.JSFUnitWebConnection;
      import org.jboss.jsfunit.framework.WebClientSpec;
      import org.jboss.jsfunit.jsfsession.JSFClientSession;
      import org.jboss.jsfunit.jsfsession.JSFServerSession;
      import org.jboss.jsfunit.jsfsession.JSFSession;
      import org.w3c.dom.Element;
      import org.xml.sax.SAXException;

      import com.fairisaac.dms.fidem.jsfunit.listener.JsfUnitRequestListener;
      import com.gargoylesoftware.htmlunit.WebClient;
      import com.gargoylesoftware.htmlunit.html.ClickableElement;


      /**
      * JSFUnit test class to test login.xhtml
      * @author MritunjayKumar
      *
      */
      public class JSFUnitTestForLoginPage extends ServletTestCase {

      private JSFClientSession client;
      private JSFServerSession server;

      /**
      * JUnit suite() method
      * @return
      */
      public static Test suite()
      {
      return new TestSuite( JSFUnitTestForLoginPage.class );
      }

      /* (non-Javadoc)
      * @see junit.framework.TestCase#setUp()
      */
      public void setUp() throws IOException, SAXException
      {
      //set the initial page here
      WebClientSpec wcSpec = new WebClientSpec("/pages/cc/login.faces");
      //This portion of code is required to run JSFUnit test in web sphere server.
      InitialRequestStrategy requestStrategy = new InitialRequestStrategy();
      wcSpec.setInitialRequestStrategy(requestStrategy);
      WebClient webClient = wcSpec.getWebClient();
      JSFUnitWebConnection webConnection = (JSFUnitWebConnection)webClient.getWebConnection();
      webConnection.addListener(new JsfUnitRequestListener());
      JSFSession jsfSession = new JSFSession(wcSpec);
      client = jsfSession.getJSFClientSession();
      server = jsfSession.getJSFServerSession();
      }

      /**
      * method to validate input data for Login page.
      * @throws IOException
      * @throws SAXException
      */
      public void testInputValidation() throws IOException, SAXException
      {
      client.setValue("loginForm:username", "");
      client.setValue("loginForm:password", "");
      ClickableElement authenticateButton = (ClickableElement)client.getElement("loginForm:authenticate");
      authenticateButton.click();

      assertEquals("/pages/cc/login.xhtml", server.getCurrentViewID());
      assertEquals("Invalid login id",server.getManagedBeanValue("#{loginMBean.invalidLoginid}"));
      assertEquals("Invalid password",server.getManagedBeanValue("#{loginMBean.invalidPassword}"));
      checkComponentForDisabled("loginForm:pickTenant", true);
      checkComponentForDisabled("loginForm:pickLocale", true);
      checkComponentForDisabled("loginForm:authenticate", false);
      checkComponentForDisabled("loginForm:logIn", true);
      checkComponentForDisabled("loginForm:reset", true);
      }

      /**
      * method to test authenticate button
      * @throws IOException
      * @throws SAXException
      */
      public void testAuthenticate() throws IOException, SAXException
      {
      client.setValue("loginForm:username", "collector");
      client.setValue("loginForm:password", "collector");
      ClickableElement authenticateButton = (ClickableElement)client.getElement("loginForm:authenticate");
      authenticateButton.click();

      assertEquals("/pages/cc/login.xhtml", server.getCurrentViewID());
      if((Boolean)server.getManagedBeanValue("#{loginMBean.authenticateDisabled}")){
      checkComponentForDisabled("loginForm:authenticate", true);
      checkComponentForDisabled("loginForm:logIn", false);
      checkComponentForDisabled("loginForm:reset", false);
      }
      assertNotNull(server.getManagedBeanValue("#{loginMBean.username}"));
      assertNotNull(server.getManagedBeanValue("#{loginMBean.password}"));
      checkComponentForDisabled("loginForm:pickTenant", false);
      checkComponentForDisabled("loginForm:pickLocale", false);
      }

      /**
      * method to test LogIn button
      * @throws IOException
      * @throws SAXException
      */
      public void testLogIn() throws IOException, SAXException
      {
      client.setValue("loginForm:username", "collector");
      client.setValue("loginForm:password", "collector");
      Element tenantDropDown = client.getElement("loginForm:pickTenant");
      tenantDropDown.setNodeValue("tenant");
      Element localeDropDown = client.getElement("loginForm:pickLocale");
      localeDropDown.setNodeValue("en_US");
      ClickableElement logInButton = (ClickableElement)client.getElement("loginForm:logIn");
      logInButton.removeAttribute("disabled");
      logInButton.click();

      String userName = (String)server.getManagedBeanValue("#{loginMBean.username}");
      if(userName.startsWith("admin")){
      assertEquals("/pages/fidem/home/administratorHome.xhtml", server.getCurrentViewID());
      }
      else{
      assertEquals("/pages/fidem/home/collectorHome.xhtml", server.getCurrentViewID());
      }

      // FacesContext facesContext = server.getFacesContext();
      // Application application = facesContext.getApplication();
      // LoginMBean loginBean = (LoginMBean)application.createValueBinding("#{loginMBean}").getValue(facesContext);
      // assertEquals("collector", loginBean.getUsername());
      // assertEquals("collector", loginBean.getPassword());
      }

      /**
      * method to test reset button
      * @throws IOException
      * @throws SAXException
      */
      public void testReset() throws IOException, SAXException
      {
      ClickableElement clickable = (ClickableElement)client.getElement("loginForm:reset");
      clickable.click();

      assertEquals("/pages/cc/login.xhtml", server.getCurrentViewID());
      assertNull(server.getManagedBeanValue("#{loginMBean.username}"));
      assertNull(server.getManagedBeanValue("#{loginMBean.password}"));
      checkComponentForDisabled("loginForm:authenticate", false);
      checkComponentForDisabled("loginForm:logIn", true);
      checkComponentForDisabled("loginForm:reset", true);
      checkComponentForDisabled("loginForm:pickTenant", true);
      checkComponentForDisabled("loginForm:pickLocale", true);
      }

      /**
      * Check any component whther eit is disable or not.
      * @param componentName String name of the component to check for disable..
      * @param forDisable boolean, true if component is disabled.
      */
      private void checkComponentForDisabled(String componentName, boolean forDisable){
      UIComponent component = server.findComponent(componentName);
      Map componentAttributes = component.getAttributes();
      assertEquals(forDisable, componentAttributes.get("disabled"));
      }

      }


      Issues that i am facing is:
      1) All the test methods are running properly except 'testLogIn' method. Assert is failing for this test method. It is still expecting login.xhtml.
      If From the 'login.xhtm; if i removed the disabled attribute for the 'LogIn' button, i am able to run the this test method.
      2) I am not able to get the values of the attributes of LoginMbean, even though 'LoginMbean in session scope.
      FacesContext facesContext = server.getFacesContext();
      Application application = facesContext.getApplication();
      LoginMBean loginBean = (LoginMBean)application.createValueBinding("#{loginMBean}").getValue(facesContext);
      assertEquals("collector", loginBean.getUsername());
      assertEquals("collector", loginBean.getPassword());

      Value of the loginBean.getUsername() and loginBean.getPassword() is null.

      My application is deployed on web sphere process server.

      Please solve me solving this issue as soon as possible.

      Regards,
      Mritunjay


        • 1. Re: Issue while getting teh value of session scoped variable
          ssilvert

          I'm sorry I haven't been able to get to this. Usually, I'm able to answer quickly but this is a holiday week and I probably won't be able to think about your question until Monday.

          Stan

          • 2. Re: Issue while getting teh value of session scoped variable

            Stan,
            Thanks for your time. I will wait for your answer till monday.

            Regards,
            Mritunjay

            • 3. Re: Issue while getting teh value of session scoped variable
              ssilvert

              I think I see your problem if I understand your application.

              You say that first you must Authenticate then you log in, right?

              Note that each test method starts a new session by calling setUp(). JUnit does that for you.

              So it looks like you just need to press the Authenticate button at the beginning of your testLogIn() method.

              Stan

              • 4. Re: Issue while getting teh value of session scoped variable

                Yes Stan, first i need to press authenticate and then logIN. i modified the code and able to run my test case succesfully. SO my problem no 1 is resolved but still problem no 2 is unresolved.
                Problem 2 is
                I am not able to get the values of the attributes of LoginMbean, even though 'LoginMbean in session scope.
                FacesContext facesContext = server.getFacesContext();
                Application application = facesContext.getApplication();
                LoginMBean loginBean = (LoginMBean)application.createValueBinding("#{loginMBean}").getValue(facesContext);
                assertEquals("collector", loginBean.getUsername());
                assertEquals("collector", loginBean.getPassword());

                Regards,
                Mritunjay

                • 5. Re: Issue while getting teh value of session scoped variable
                  ssilvert

                  An easier way to do that is:

                  LoginMBean loginBean = (LoginMBean)server.getManagedBeanValue("#{loginMBean}");


                  This API provided by JSFUnit does exactly the same thing you were doing with the JSF API, so I'm pretty sure it won't work either. But it will make your code shorter.

                  My suggestion is to dump the contents of the session to make sure that the managed bean really got created.
                  System.out.println(facesContext.getExternalContext().getSessionMap());


                  BTW, how are you declaring your MBean? Please show that. Because of a bug in JSFUnit I would expect that getManagedBeanValue() would never return null as long as the managed bean is properly declared.

                  The other possibility is that the bean got created in a different session. As long as you use the same JSFSession within the scope of your test, that should not happen.

                  Stan


                  • 6. Re: Issue while getting teh value of session scoped variable

                    My managed bean is declared like
                    <managed-bean>
                    <managed-bean-name>loginMBean</managed-bean-name>
                    <managed-bean-class>
                    com.fairisaac.edm.cc.ui.login.managedbeans.LoginMBean
                    </managed-bean-class>
                    <managed-bean-scope>session</managed-bean-scope>
                    <managed-property>
                    <property-name>loaderMBean</property-name>
                    #{loaderMBean}
                    </managed-property>
                    </managed-bean>


                    • 7. Re: Issue while getting teh value of session scoped variable
                      ssilvert

                      Did you dump the session map? What did that show?

                      Stan