/*
 * Copyright (c) 2012-2018 Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Red Hat, Inc. - initial API and implementation
 */
package org.eclipse.che.selenium.dashboard;

import static org.eclipse.che.commons.lang.NameGenerator.generate;
import static org.eclipse.che.selenium.core.constant.TestStacksConstants.JAVA;
import static org.eclipse.che.selenium.core.constant.TestTimeoutsConstants.ELEMENT_TIMEOUT_SEC;
import static org.eclipse.che.selenium.pageobject.ProjectExplorer.FolderTypes.PROJECT_FOLDER;
import static org.eclipse.che.selenium.pageobject.dashboard.ProjectSourcePage.Sources.GITHUB;
import static org.testng.Assert.fail;
import static org.testng.AssertJUnit.assertTrue;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import org.eclipse.che.selenium.core.SeleniumWebDriver;
import org.eclipse.che.selenium.core.TestGroup;
import org.eclipse.che.selenium.core.client.TestWorkspaceServiceClient;
import org.eclipse.che.selenium.core.user.DefaultTestUser;
import org.eclipse.che.selenium.core.webdriver.SeleniumWebDriverHelper;
import org.eclipse.che.selenium.pageobject.Ide;
import org.eclipse.che.selenium.pageobject.ProjectExplorer;
import org.eclipse.che.selenium.pageobject.ToastLoader;
import org.eclipse.che.selenium.pageobject.dashboard.Dashboard;
import org.eclipse.che.selenium.pageobject.dashboard.NewWorkspace;
import org.eclipse.che.selenium.pageobject.dashboard.ProjectSourcePage;
import org.eclipse.che.selenium.pageobject.dashboard.workspaces.Workspaces;
import org.openqa.selenium.WebDriverException;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

@Test(groups = {TestGroup.GITHUB, TestGroup.OSIO})
public class ImportProjectFromGitHubTest {
  private static final String WORKSPACE = generate("ImtMvnPrjGitHub", 4);
  private static final String GITHUB_PROJECT_NAME = "AngularJS";

  private String projectName;
  private String ideWin;

  @Inject
  @Named("github.username")
  private String gitHubUsername;

  @Inject
  @Named("github.password")
  private String gitHubPassword;

  @Inject private Ide ide;
  @Inject private Dashboard dashboard;
  @Inject private Workspaces workspaces;
  @Inject private DefaultTestUser defaultTestUser;
  @Inject private ProjectExplorer projectExplorer;
  @Inject private NewWorkspace newWorkspace;
  @Inject private ToastLoader toastLoader;
  @Inject private ProjectSourcePage projectSourcePage;
  @Inject private SeleniumWebDriver seleniumWebDriver;
  @Inject private SeleniumWebDriverHelper seleniumWebDriverHelper;
  @Inject private TestWorkspaceServiceClient workspaceServiceClient;

  @BeforeClass
  public void setUp() {
    projectName = gitHubUsername + "-" + GITHUB_PROJECT_NAME;

    dashboard.open();
  }

  @AfterClass
  public void tearDown() throws Exception {
    workspaceServiceClient.delete(WORKSPACE, defaultTestUser.getName());
  }

  @Test
  public void checkAbilityImportProjectFromGithub() throws Exception {
    ideWin = seleniumWebDriver.getWindowHandle();

    dashboard.waitDashboardToolbarTitle();
    dashboard.selectWorkspacesItemOnDashboard();
    workspaces.clickOnAddWorkspaceBtn();
    newWorkspace.waitToolbar();
    // we are selecting 'Java' stack from the 'All Stack' tab for compatibility with OSIO
    newWorkspace.clickOnAllStacksTab();
    newWorkspace.selectStack(JAVA.getId());
    newWorkspace.typeWorkspaceName(WORKSPACE);

    projectSourcePage.clickOnAddOrImportProjectButton();
    projectSourcePage.selectSourceTab(GITHUB);

    if (projectSourcePage.isConnectGitHubAccountButtonVisible()) {
      connectGithubAccount();
    }

    assertTrue(projectSourcePage.isGithubProjectsListDisplayed());
    projectSourcePage.selectProjectFromList(GITHUB_PROJECT_NAME);
    projectSourcePage.clickOnAddProjectButton();
    newWorkspace.clickOnCreateButtonAndOpenInIDE();

    seleniumWebDriverHelper.switchToIdeFrameAndWaitAvailability(ELEMENT_TIMEOUT_SEC);
    toastLoader.waitToastLoaderAndClickStartButton();
    ide.waitOpenedWorkspaceIsReadyToUse();
    projectExplorer.waitItem(projectName);
    projectExplorer.waitDefinedTypeOfFolder(projectName, PROJECT_FOLDER);
  }

  private void connectGithubAccount() {
    projectSourcePage.clickOnConnectGithubAccountButton();
    seleniumWebDriverHelper.switchToNextWindow(ideWin);

    projectSourcePage.waitAuthorizationPageOpened();
    projectSourcePage.typeLogin(gitHubUsername);
    projectSourcePage.typePassword(gitHubPassword);
    projectSourcePage.clickOnSignInButton();
    seleniumWebDriver.switchTo().window(ideWin);

    if (!projectSourcePage.isGithubProjectsListDisplayed()) {
      seleniumWebDriverHelper.switchToNextWindow(ideWin);

      try {
        projectSourcePage.waitAuthorizeBtn();
      } catch (WebDriverException ex) {
        // remove try-catch block after issue has been resolved
        fail("Known issue https://github.com/redhat-developer/rh-che/issues/621", ex);
      }

      projectSourcePage.clickOnAuthorizeBtn();
      seleniumWebDriver.switchTo().window(ideWin);
    }
  }
}

@@ -992,16 +992,99 @@
 .Named;%0A
+import java.io.IOException;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;%0A
 import o
@@ -1179,16 +1179,82 @@
 tGroup;%0A
+import org.eclipse.che.selenium.core.client.TestGitHubRepository;%0A
 import o
@@ -2195,16 +2195,22 @@
 KSPACE =
+%0A     
  generat
@@ -2215,31 +2215,64 @@
 ate(
-%22
 Im
-tMvnPrjGitHub%22
+portProjectFromGitHubTest.class.getSimpleName()
 , 4);%0A
+%0A
   pr
@@ -2281,63 +2281,27 @@
 ate 
-static final String GITHUB_PROJECT_NAME = %22AngularJS%22;%0A
+String projectName;
 %0A  p
@@ -2306,39 +2306,40 @@
  private String 
-project
+testRepo
 Name;%0A  private 
@@ -3045,108 +3045,238 @@
 nt;%0A
-%0A  @BeforeClass%0A  public void setUp() %7B%0A    projectName = gitHubUsername + %22-%22 + GITHUB_PROJECT_NAME
+  @Inject private TestGitHubRepository testRepo;%0A%0A  @BeforeClass%0A  public void setUp() throws IOException %7B%0A    Path entryPath = Paths.get(getClass().getResource(%22/projects/testRepo%22).getPath());%0A    testRepo.addContent(entryPath)
 ;%0A%0A 
@@ -3512,24 +3512,136 @@
 Exception %7B%0A
+    testRepoName = testRepo.getName();%0A    projectName = String.format(%22%25s-%25s%22, gitHubUsername, testRepoName);%0A%0A
     ideWin =
@@ -4390,27 +4390,20 @@
 ist(
-GITHUB_PROJECT_NAME
+testRepoName
 );%0A 

Rework the 'ImportProjectFromGitHubTest' to add test repo on Github using the 'kohsuke Github' library (#9762)