public class ProjectLinksPageTest {

  @ClassRule public static Orchestrator ORCHESTRATOR = Category1Suite.ORCHESTRATOR;

  @Rule public Navigation nav = Navigation.get(ORCHESTRATOR);

  private static WsClient wsClient;
  private long customLinkId;

  @BeforeClass
  public static void setUp() {
    wsClient = newAdminWsClient(ORCHESTRATOR);

    ORCHESTRATOR.resetData();
    ORCHESTRATOR.executeBuild(
        SonarScanner.create(projectDir("shared/xoo-sample"))
            .setProperty("sonar.links.homepage", "http://example.com"));
  }

  @Before
  public void prepare() {
    customLinkId = Long.parseLong(createCustomLink().getLink().getId());
  }

  @After
  public void clean() {
    deleteLink(customLinkId);
  }

  @Test
  public void should_list_links() {
    ProjectLinksPage page = openPage();

    page.getLinks().shouldHaveSize(2);

    List<ProjectLinkItem> links = page.getLinksAsItems();
    ProjectLinkItem homepageLink = links.get(0);
    ProjectLinkItem customLink = links.get(1);

    homepageLink.getName().should(hasText("Home"));
    homepageLink.getType().should(hasText("sonar.links.homepage"));
    homepageLink.getUrl().should(hasText("http://example.com"));
    homepageLink.getDeleteButton().shouldNot(Condition.present);

    customLink.getName().should(hasText("Custom"));
    customLink.getType().shouldNot(Condition.present);
    customLink.getUrl().should(hasText("http://example.org/custom"));
    customLink.getDeleteButton().shouldBe(Condition.visible);
  }

  @Test
  public void should_create_link() {
    ProjectLinksPage page = openPage();

    page.getLinks().shouldHaveSize(2);

    $("#create-project-link").click();
    $("#create-link-name").setValue("Test");
    $("#create-link-url").setValue("http://example.com/test");
    $("#create-link-confirm").click();

    page.getLinks().shouldHaveSize(3);

    ProjectLinkItem testLink = page.getLinksAsItems().get(2);

    testLink.getName().should(hasText("Test"));
    testLink.getType().shouldNot(Condition.present);
    testLink.getUrl().should(hasText("http://example.com/test"));
    testLink.getDeleteButton().shouldBe(Condition.visible);
  }

  @Test
  public void should_delete_link() {
    ProjectLinksPage page = openPage();

    page.getLinks().shouldHaveSize(2);

    List<ProjectLinkItem> links = page.getLinksAsItems();
    ProjectLinkItem customLink = links.get(1);

    customLink.getDeleteButton().click();
    $("#delete-link-confirm").click();

    page.getLinks().shouldHaveSize(1);
  }

  private CreateWsResponse createCustomLink() {
    return wsClient
        .projectLinks()
        .create(
            new CreateWsRequest()
                .setProjectKey("sample")
                .setName("Custom")
                .setUrl("http://example.org/custom"));
  }

  private void deleteLink(long id) {
    try {
      wsClient.projectLinks().delete(new DeleteWsRequest().setId(id));
    } catch (Exception e) {
      // fail silently
    }
  }

  private ProjectLinksPage openPage() {
    nav.logIn().submitCredentials("admin", "admin");
    return nav.openProjectLinks("sample");
  }
}
 private ProjectLinksPage openPage() {
   nav.logIn().submitCredentials("admin", "admin");
   return nav.openProjectLinks("sample");
 }