/*
 * Copyright (c) 2012-2017 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.projectexplorer.dependencies;

import static org.testng.Assert.fail;

import com.google.inject.Inject;
import java.net.URL;
import java.nio.file.Paths;
import java.util.Random;
import org.eclipse.che.selenium.core.client.TestProjectServiceClient;
import org.eclipse.che.selenium.core.project.ProjectTemplates;
import org.eclipse.che.selenium.core.user.TestUser;
import org.eclipse.che.selenium.core.workspace.TestWorkspace;
import org.eclipse.che.selenium.pageobject.CodenvyEditor;
import org.eclipse.che.selenium.pageobject.Ide;
import org.eclipse.che.selenium.pageobject.Loader;
import org.eclipse.che.selenium.pageobject.PopupDialogsBrowser;
import org.eclipse.che.selenium.pageobject.ProjectExplorer;
import org.openqa.selenium.TimeoutException;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

/**
 * @author Andrienko Alexander
 * @author Andrey Chizhikov
 */
public class TransitiveDependencyTest {

  private static final String PROJECT_NAME =
      TransitiveDependencyTest.class.getSimpleName() + new Random().nextInt(10);
  private static final String LIB_FOLDER = "External Libraries";
  private static final String MAIN_LIBRARY = "spring-webmvc-3.0.5.RELEASE.jar";
  private static final String TRANSITIVE_DEPENDENCY_FOR_MAIN_LIBRARY =
      "spring-core-3.0.5.RELEASE.jar";

  @Inject private TestWorkspace testWorkspace;
  @Inject private TestUser defaultTestUser;
  @Inject private Ide ide;
  @Inject private ProjectExplorer projectExplorer;
  @Inject private CodenvyEditor editor;
  @Inject private Loader loader;
  @Inject private PopupDialogsBrowser popupDialogsBrowser;
  @Inject private TestProjectServiceClient testProjectServiceClient;

  @BeforeClass
  public void setUp() throws Exception {
    URL resource = getClass().getResource("/projects/default-spring-project");
    testProjectServiceClient.importProject(
        testWorkspace.getId(),
        Paths.get(resource.toURI()),
        PROJECT_NAME,
        ProjectTemplates.MAVEN_SPRING);
    ide.open(testWorkspace);
  }

  @Test
  public void transitiveDependencyTest() throws Exception {
    projectExplorer.waitItem(PROJECT_NAME);
    projectExplorer.openItemByPath(PROJECT_NAME);
    loader.waitOnClosed();
    projectExplorer.waitProjectExplorer();
    projectExplorer.waitItem(PROJECT_NAME + "/pom.xml");
    projectExplorer.openItemByPath(PROJECT_NAME + "/pom.xml");
    projectExplorer.openItemByVisibleNameInExplorer(LIB_FOLDER);

    try {
      projectExplorer.waitLibraryIsPresent(MAIN_LIBRARY);
    } catch (TimeoutException ex) {
      // remove try-catch block after issue has been resolved
      fail("Known issue https://github.com/eclipse/che/issues/8017");
    }

    projectExplorer.waitLibraryIsPresent(TRANSITIVE_DEPENDENCY_FOR_MAIN_LIBRARY);
    projectExplorer.openItemByPath(PROJECT_NAME + "/pom.xml");
    loader.waitOnClosed();
    editor.waitActive();

    deleteDependency();

    popupDialogsBrowser.waitAlertClose();
    projectExplorer.waitLibraryIsNotPresent(MAIN_LIBRARY);
    projectExplorer.waitLibraryIsNotPresent(TRANSITIVE_DEPENDENCY_FOR_MAIN_LIBRARY);
  }

  private void deleteDependency() {
    editor.waitActive();
    loader.waitOnClosed();
    for (int i = 36; i <= 40; i++) {
      editor.setCursorToLine(i);
      editor.selectLineAndDelete();
    }
  }
}

@@ -427,47 +427,8 @@
 s;%0A%0A
-import static org.testng.Assert.fail;%0A%0A
 impo
@@ -600,16 +600,103 @@
 Client;%0A
+import org.eclipse.che.selenium.core.constant.TestProjectExplorerContextMenuConstants;%0A
 import o
@@ -1149,53 +1149,8 @@
 er;%0A
-import org.openqa.selenium.TimeoutException;%0A
 impo
@@ -2537,24 +2537,310 @@
 JECT_NAME);%0A
+    projectExplorer.openContextMenuByPathSelectedItem(PROJECT_NAME);%0A    projectExplorer.clickOnItemInContextMenu(TestProjectExplorerContextMenuConstants.MAVEN);%0A    projectExplorer.clickOnNewContextMenuItem(TestProjectExplorerContextMenuConstants.REIMPORT);%0A    loader.waitOnClosed();%0A
     projectE
@@ -3059,32 +3059,58 @@
  + %22/pom.xml%22);%0A
+    editor.waitActive();%0A%0A
     projectExplo
@@ -3162,21 +3162,8 @@
 R);%0A
-%0A    try %7B%0A  
     
@@ -3218,183 +3218,8 @@
 Y);%0A
-    %7D catch (TimeoutException ex) %7B%0A      // remove try-catch block after issue has been resolved%0A      fail(%22Known issue https://github.com/eclipse/che/issues/8017%22);%0A    %7D%0A%0A
     

Fix unexpected fail of TransitiveDependencyTest selenium test (#8216)