private void prepareEnterAuthenticateOneTimeUser(boolean authenticated) throws Exception {

    when(mockHttpServletRequest.getParameter(ServletUtil.VERSION)).thenReturn(versionString);
    when(mockHttpServletRequest.getParameter(ServletUtil.LOGIN)).thenReturn(loginString);
    when(mockHttpServletRequest.getParameter(ServletUtil.PASSWD)).thenReturn(passwdString);

    spy(ConnectionUtil.class);

    doNothing().when(ConnectionUtil.class);
    ConnectionUtil.checkParameter(versionString);

    doNothing().when(ConnectionUtil.class);
    ConnectionUtil.checkVersion(versionString);

    prepareForOtpManagerHelper();
    when(otpManagerHelper.authenticateOneTimeUser(anyString(), (byte[]) any()))
        .thenReturn(authenticated);

    mockUserHelper = mock(UserHelper.class);
    whenNew(UserHelper.class).withNoArguments().thenReturn(mockUserHelper);

    User user = AdminFactory.eINSTANCE.createUser();
    user.setLogin("*****@*****.**");
    when(mockUserHelper.findByLogin(anyString())).thenReturn(user);
  }
  @Test
  public void testListProjectsActionByNormalUser() throws Exception {
    String actionString = Action.ListProjects.toString();

    when(mockHttpServletRequest.getParameter(ServletUtil.ACTION)).thenReturn(actionString);

    // set AuthenticateOneTimeUser false
    prepareEnterAuthenticateOneTimeUser(false);
    User user = stubbingCheckUser();

    String xmi = constructProjectsObjectsXmi();
    doThrow(
            new org.talend.gwtadministrator.server.remoteconnection.ConnectionUtil
                .ResponseException(xmi))
        .when(ConnectionUtil.class);
    ConnectionUtil.listAuthorizedProjects(user);

    // RUN...
    defaultConnectionStrategy.execute(mockHttpServletRequest, mockHttpServletResponse);

    verifyNecessaryChecks(actionString);

    verifyStatic(org.mockito.Mockito.times(1));
    ConnectionUtil.checkUser(loginString, password, false);

    verifyStatic(org.mockito.Mockito.times(1));
    ConnectionUtil.listAuthorizedProjects(user);

    PrintWriter writer = mockHttpServletResponse.getWriter();
    writer.flush();
    System.out.println(outStringWriter.toString());
    // ASSERT...
    assertThat(outStringWriter.toString(), is("RESPONSE:" + xmi));
  }
  @Test
  public void testLoginActionByAuthenticateOneTimeUser() throws Exception {
    String actionString = Action.Login.toString();
    when(mockHttpServletRequest.getParameter(ServletUtil.ACTION)).thenReturn(actionString);
    when(mockHttpServletRequest.getParameter(ServletUtil.PROJECT)).thenReturn(projectString);

    // set AuthenticateOneTimeUser true
    prepareEnterAuthenticateOneTimeUser(true);

    String xmi = constructProjectsObjectsXmi();
    doThrow(
            new org.talend.gwtadministrator.server.remoteconnection.ConnectionUtil
                .ResponseException(xmi))
        .when(ConnectionUtil.class);
    ConnectionUtil.returnProjectAndUsers(anyString(), (User) any(), anyBoolean());

    // RUN...
    defaultConnectionStrategy.execute(mockHttpServletRequest, mockHttpServletResponse);

    verifyNecessaryChecks(actionString);

    // verify called listAllProjects once
    verifyStatic(org.mockito.Mockito.times(1));
    ConnectionUtil.returnProjectAndUsers(anyString(), (User) any(), anyBoolean());

    PrintWriter writer = mockHttpServletResponse.getWriter();
    writer.flush();
    System.out.println(outStringWriter.toString());
    // ASSERT...
    assertThat(outStringWriter.toString(), is("RESPONSE:" + xmi));
  }
  private void verifyNecessaryChecks(String actionString) throws Exception {
    verifyStatic(org.mockito.Mockito.times(1));
    ConnectionUtil.checkParameter(actionString);

    verifyStatic(org.mockito.Mockito.times(1));
    ConnectionUtil.checkParameter(versionString);

    verifyStatic(org.mockito.Mockito.times(1));
    ConnectionUtil.checkVersion(versionString);
  }
  @Test
  public void testCloseSessionByNormalUser() throws Exception {
    String sessionIdString = "555";
    String actionString = Action.CloseSession.toString();
    long sessionIdLong = Long.parseLong(sessionIdString);
    when(mockHttpServletRequest.getParameter(ServletUtil.ACTION)).thenReturn(actionString);
    when(mockHttpServletRequest.getParameter(ServletUtil.SESSION_ID)).thenReturn(sessionIdString);

    // set AuthenticateOneTimeUser false
    prepareEnterAuthenticateOneTimeUser(false);
    User user = stubbingCheckUser();

    UsersCache userCacheHelper = mock(UsersCache.class);
    mockStatic(UsersCacheFactory.class);
    when(UsersCacheFactory.getInstance()).thenReturn(userCacheHelper);
    doNothing().when(userCacheHelper);
    userCacheHelper.remove(sessionIdLong);

    // RUN...
    defaultConnectionStrategy.execute(mockHttpServletRequest, mockHttpServletResponse);

    verifyNecessaryChecks(actionString);

    verifyStatic(org.mockito.Mockito.times(1));
    ConnectionUtil.checkUser(loginString, password, false);

    PrintWriter writer = mockHttpServletResponse.getWriter();
    writer.flush();
    System.out.println(outStringWriter.toString());
    // ASSERT...
    assertThat(outStringWriter.toString(), is("RESPONSE:" + String.valueOf(sessionIdLong)));
  }
  @Test
  public void testOpenSessionByNormalUser() throws Exception {
    long sessionLong = 555;
    String actionString = Action.OpenSession.toString();
    when(mockHttpServletRequest.getParameter(ServletUtil.ACTION)).thenReturn(actionString);
    when(mockHttpServletRequest.getParameter(ServletUtil.PROJECT)).thenReturn(projectString);

    // set AuthenticateOneTimeUser false
    prepareEnterAuthenticateOneTimeUser(false);
    User user = stubbingCheckUser();

    UsersCache userCacheHelper = mock(UsersCache.class);
    mockStatic(UsersCacheFactory.class);
    when(UsersCacheFactory.getInstance()).thenReturn(userCacheHelper);
    when(userCacheHelper.logWithProjectLabel(loginString, CLIENT_TYPE, projectString))
        .thenReturn(sessionLong);

    // RUN...
    defaultConnectionStrategy.execute(mockHttpServletRequest, mockHttpServletResponse);

    verifyNecessaryChecks(actionString);

    verifyStatic(org.mockito.Mockito.times(1));
    ConnectionUtil.checkUser(loginString, password, false);

    PrintWriter writer = mockHttpServletResponse.getWriter();
    writer.flush();
    System.out.println(outStringWriter.toString());
    // ASSERT...
    assertThat(outStringWriter.toString(), is("RESPONSE:" + String.valueOf(sessionLong)));
  }
  private User stubbingCheckUser() throws Exception {

    User user = AdminFactory.eINSTANCE.createUser();
    user.setLogin("*****@*****.**");

    doReturn(user).when(ConnectionUtil.class);
    ConnectionUtil.checkUser(loginString, password, false);

    return user;
  }
  @Test
  public void testListProjectsActionSentByCmdline() throws Exception {

    String actionString = Action.ListProjects.toString();
    String loginString = ServletUtil.COMMAND_LIST_PROJECTS_HOOK;
    String passwdString = "any";

    when(mockHttpServletRequest.getParameter(ServletUtil.ACTION)).thenReturn(actionString);
    when(mockHttpServletRequest.getParameter(ServletUtil.VERSION)).thenReturn(versionString);
    when(mockHttpServletRequest.getParameter(ServletUtil.LOGIN)).thenReturn(loginString);
    when(mockHttpServletRequest.getParameter(ServletUtil.PASSWD)).thenReturn(passwdString);

    spy(ConnectionUtil.class);

    doNothing().when(ConnectionUtil.class);
    ConnectionUtil.checkParameter(versionString);

    doNothing().when(ConnectionUtil.class);
    ConnectionUtil.checkVersion(versionString);

    String xmi = constructProjectsObjectsXmi();
    doThrow(
            new org.talend.gwtadministrator.server.remoteconnection.ConnectionUtil
                .ResponseException(xmi))
        .when(ConnectionUtil.class);
    ConnectionUtil.listAllProjects((User) any());

    // RUN...
    defaultConnectionStrategy.execute(mockHttpServletRequest, mockHttpServletResponse);

    verifyNecessaryChecks(actionString);

    verifyStatic(org.mockito.Mockito.times(1));
    ConnectionUtil.listAllProjects((User) any());

    PrintWriter writer = mockHttpServletResponse.getWriter();
    writer.flush();
    System.out.println(outStringWriter.toString());
    // ASSERT...
    assertThat(outStringWriter.toString(), is("RESPONSE:" + xmi));
  }
  /**
   * Test method for {@link
   * org.talend.gwtadministrator.server.remoteconnection.strategy.DefaultConnectionStrategy#execute(javax.servlet.http.HttpServletRequest,
   * javax.servlet.http.HttpServletResponse)} .
   */
  @Test
  public void testCheckVersionAction() throws Exception {

    String actionString = Action.CheckVersion.toString();
    String serverVersion = "5.1.0RC1.81648"; // serverVersion

    when(mockHttpServletRequest.getParameter(ServletUtil.ACTION)).thenReturn(actionString);
    when(mockHttpServletRequest.getParameter(ServletUtil.VERSION)).thenReturn(versionString);

    String cleanClientVersion = ConnectionUtil.removeUselessChars(versionString);
    String cleanServerVersion = ConnectionUtil.removeUselessChars(serverVersion);

    mockStatic(BrandingHelper.class);
    when(BrandingHelper.getVersionDotRelease()).thenReturn(serverVersion);

    spy(ConnectionUtil.class);

    // RUN...
    defaultConnectionStrategy.execute(mockHttpServletRequest, mockHttpServletResponse);

    verifyNecessaryChecks(actionString);

    PrintWriter writer = mockHttpServletResponse.getWriter();
    writer.flush();
    System.out.println(outStringWriter.toString());

    // ASSERT...
    if (cleanClientVersion.equals(cleanServerVersion)) {
      assertThat(outStringWriter.toString(), is("RESPONSE:" + ServletUtil.OK));
    } else {
      assertThat(
          outStringWriter.toString(),
          is(
              "ERROR:"
                  + "Client version "
                  + versionString
                  + " is not compatible with the server version "
                  + serverVersion));
    }
  }
  @Test
  public void testBestExecutionServerByNormalUser() throws Exception {
    String actionString = Action.BestExecutionServer.toString();
    String virtualServerString = "server01";
    when(mockHttpServletRequest.getParameter(ServletUtil.ACTION)).thenReturn(actionString);
    when(mockHttpServletRequest.getParameter(ServletUtil.VIRTUAL_SERVER))
        .thenReturn(virtualServerString);
    // set AuthenticateOneTimeUser false
    prepareEnterAuthenticateOneTimeUser(false);
    User user = stubbingCheckUser();

    doNothing().when(ConnectionUtil.class);
    ConnectionUtil.checkUserAuthorizationJobConductor(user);

    String xmi = constructServersObjectXmi();
    doThrow(
            new org.talend.gwtadministrator.server.remoteconnection.ConnectionUtil
                .ResponseException(xmi))
        .when(ConnectionUtil.class);
    ConnectionUtil.returnBestExecutionServer(virtualServerString);

    // RUN...
    defaultConnectionStrategy.execute(mockHttpServletRequest, mockHttpServletResponse);

    verifyNecessaryChecks(actionString);

    // verify called checkUserAuthorizationJobConductor once
    verifyStatic(org.mockito.Mockito.times(1));
    ConnectionUtil.checkUserAuthorizationJobConductor(user);

    verifyStatic(org.mockito.Mockito.times(1));
    ConnectionUtil.returnBestExecutionServer(virtualServerString);

    PrintWriter writer = mockHttpServletResponse.getWriter();
    writer.flush();
    System.out.println(outStringWriter.toString());
    // ASSERT...
    assertThat(outStringWriter.toString(), is("RESPONSE:" + xmi));
  }
  @Test
  public void testGetWarningAction() throws Exception {
    String actionString = Action.GetWarning.toString();
    String warningMsg = "It's a warning message";
    String encodedMsg = EncryptionHelper.encode64(warningMsg.getBytes());

    when(mockHttpServletRequest.getParameter(ServletUtil.ACTION)).thenReturn(actionString);
    when(mockHttpServletRequest.getParameter(ServletUtil.VERSION)).thenReturn(versionString);

    spy(ConnectionUtil.class);

    doNothing().when(ConnectionUtil.class);
    ConnectionUtil.checkParameter(versionString);

    doNothing().when(ConnectionUtil.class);
    ConnectionUtil.checkVersion(versionString);

    doThrow(
            new org.talend.gwtadministrator.server.remoteconnection.ConnectionUtil
                .ResponseException(encodedMsg))
        .when(ConnectionUtil.class);
    ConnectionUtil.getWarnings();

    // RUN...
    defaultConnectionStrategy.execute(mockHttpServletRequest, mockHttpServletResponse);

    verifyNecessaryChecks(actionString);

    verifyStatic(org.mockito.Mockito.times(1));
    ConnectionUtil.getWarnings();

    PrintWriter writer = mockHttpServletResponse.getWriter();
    writer.flush();
    System.out.println(outStringWriter.toString());
    // ASSERT...
    assertThat(outStringWriter.toString(), is("RESPONSE:" + encodedMsg));
  }
  /**
   * DOC Administrator Comment method "setUp".
   *
   * @throws java.lang.Exception
   */
  @Before
  public void setUp() throws Exception {
    mockHttpServletRequest = mock(HttpServletRequest.class);
    mockHttpServletResponse = mock(HttpServletResponse.class);

    defaultConnectionStrategy = DefaultConnectionStrategy.getInstance();

    mockStatic(LicenseUtil.class);
    when(LicenseUtil.getInstance()).thenReturn(mock(LicenseUtil.class));

    outStringWriter = new StringWriter();
    PrintWriter outPrintWriter = new PrintWriter(outStringWriter);
    when(mockHttpServletResponse.getWriter()).thenReturn(outPrintWriter);

    password = ConnectionUtil.computePassword(passwdString);
  }