package it.restart.com.atlassian.jira.plugins.dvcs.github;

import com.atlassian.jira.plugins.dvcs.pageobjects.common.OAuth;
import com.atlassian.pageobjects.Page;
import com.atlassian.pageobjects.PageBinder;
import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.pageobjects.elements.PageElementFinder;
import com.atlassian.pageobjects.elements.query.Poller;
import com.atlassian.webdriver.utils.WebDriverUtil;
import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;

import java.util.List;
import javax.inject.Inject;

public class GithubOAuthApplicationPage implements Page
{
    @Inject
    private PageElementFinder pageElementFinder;

    @Inject
    private PageBinder pageBinder;

    private final String hostUrl;

    public GithubOAuthApplicationPage()
    {
        this("https://github.com");
    }

    public GithubOAuthApplicationPage(String hostUrl)
    {
        this.hostUrl = hostUrl;
    }

    @Override
    public String getUrl()
    {
        // appUri is not found in /settings/applications instead it is found in /settings/developers
        return hostUrl + "/settings/developers";
    }

    public void removeConsumer(final OAuth oAuth)
    {
        this.removeConsumer(StringUtils.removeStart(oAuth.applicationId, hostUrl));
    }

    public void removeConsumer(final String appUri)
    {
        removeConsumer(this, By.xpath("//a[@href='" + appUri + "']"));
    }

    public void removeConsumerForAppName(final String appName)
    {
        removeConsumer(this, By.linkText(appName));
    }

    public List<PageElement> findOAthApplications(final String partialLinkText)
    {
        return pageElementFinder.findAll(By.partialLinkText(partialLinkText));
    }

    private static void removeConsumer(final GithubOAuthApplicationPage page, final By by)
    {
        final PageElement link = page.pageElementFinder.find(by);
        if (link.isPresent() && link.isVisible())
        {
            link.click();
            page.pageBinder.bind(GithubOAuthPage.class).removeConsumer();
        }
        else
        {
            // get next link
            final PageElement nextPageLink = page.pageElementFinder.find(By.className("next_page"));
            if (nextPageLink.isPresent() && nextPageLink.isVisible() && nextPageLink.getTagName().equalsIgnoreCase("a")
                    && nextPageLink.isEnabled())
            {
                nextPageLink.click();
                Poller.waitUntilTrue(page.pageElementFinder.find(By.className("table-list-bordered")).timed().isPresent());
                final GithubOAuthApplicationPage nextPage = page.pageBinder.bind(GithubOAuthApplicationPage.class);
                removeConsumer(nextPage, by);
            }
            else
            {
                throw new RuntimeException("Can not find consumer application link");
            }
        }
    }
}

@@ -996,109 +996,8 @@
   %7B%0A
-        // appUri is not found in /settings/applications instead it is found in /settings/developers%0A
     
@@ -1032,17 +1032,19 @@
 ngs/
-developer
+application
 s%22;%0A
@@ -1278,22 +1278,16 @@
 onsumer(
-this, 
 By.xpath
@@ -1423,14 +1423,8 @@
 mer(
-this, 
 By.l
@@ -1439,16 +1439,1009 @@
 appName)
+);%0A    %7D%0A%0A    public void removeConsumer(By bySelector)%0A    %7B%0A        // try to find the developer tab and click on it%0A        // if it is found we are in github so we need to use developers tab instead of applications tab%0A        // if not we are in github enterprise we do not have developers tab%0A        // TODO change this test%0A        // it is better to use /settings/developers for github tests%0A        // and /settings/applications for github enterprise%0A        GithubOAuthApplicationPage page = this;%0A        PageElement developerTabLink = pageElementFinder.find(By.cssSelector(%22.tabnav-tabs a%5Bhref='/settings/developers'%5D%22));%0A        if (developerTabLink.isPresent() && developerTabLink.isEnabled())%0A        %7B%0A           developerTabLink.click();%0A           page = pageBinder.bind(GithubOAuthApplicationPage.class);%0A           Poller.waitUntilTrue(page.pageElementFinder.find(By.className(%22table-list-bordered%22)).timed().isPresent());%0A        %7D%0A        removeConsumer(page, bySelector
 );%0A    %7D
@@ -2703,16 +2703,24 @@
 al By by
+Selector
 )%0A    %7B%0A
@@ -2782,16 +2782,24 @@
 .find(by
+Selector
 );%0A     
@@ -3620,16 +3620,24 @@
 Page, by
+Selector
 );%0A     

fix githubEnterpriseTests#afterClass