package org.kohsuke.github;

import org.junit.Test;
import org.kohsuke.github.GHRepository.Contributor;

import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author Kohsuke Kawaguchi
 */
public class RepositoryTest extends AbstractGitHubApiTestBase {
    @Test
    public void subscription() throws Exception {
        GHRepository r = getRepository();
        assertNull(r.getSubscription());

        GHSubscription s = r.subscribe(true, false);
        assertEquals(s.getRepository(), r);

        s.delete();

        assertNull(r.getSubscription());
    }

    @Test
    public void listContributors() throws IOException {
        GHRepository r = gitHub.getOrganization("stapler").getRepository("stapler");
        int i=0;
        boolean kohsuke = false;

        for (Contributor c : r.listContributors()) {
            System.out.println(c.getName());
            assertTrue(c.getContributions()>0);
            if (c.getLogin().equals("kohsuke"))
                kohsuke = true;
            if (i++ > 5)
                break;
        }

        assertTrue(kohsuke);
    }

    @Test
    public void getPermission() throws Exception {
        kohsuke();
        GHRepository r = gitHub.getRepository("github-api-test-org/test-permission");
        assertEquals(GHPermissionType.ADMIN, r.getPermission("kohsuke"));
        assertEquals(GHPermissionType.READ, r.getPermission("dude"));
        r = gitHub.getOrganization("apache").getRepository("groovy");
        try {
            r.getPermission("jglick");
            fail();
        } catch (HttpException x) {
            x.printStackTrace(); // good
            assertEquals(403, x.getResponseCode());
        }

        if (false) {
            // can't easily test this; there's no private repository visible to the test user
            r = gitHub.getOrganization("cloudbees").getRepository("private-repo-not-writable-by-me");
            try {
                r.getPermission("jglick");
                fail();
            } catch (FileNotFoundException x) {
                x.printStackTrace(); // good
            }
        }
    }

    private GHRepository getRepository() throws IOException {
        return gitHub.getOrganization("github-api-test-org").getRepository("jenkins");
    }

    @Test
    public void listLanguages() throws IOException {
        GHRepository r = gitHub.getRepository("kohsuke/github-api");
        String mainLanguage = r.getLanguage();
        assertTrue(r.listLanguages().containsKey(mainLanguage));
    }

    @Test // Issue #261
    public void listEmptyContributors() throws IOException {
        GitHub gh = GitHub.connect();
        for (Contributor c : gh.getRepository("github-api-test-org/empty").listContributors()) {
            System.out.println(c);
        }
    }
}

@@ -2113,24 +2113,645 @@
      %7D%0A    %7D
+%0A    %0A    %0A%09%0A%09@Test%0A%09public void LatestRepositoryExist() %7B%0A%09%09try %7B%0A%09%09%09// add the repository that have latest release%0A%09%09%09GHRelease release = gitHub.getRepository(%22kamontat/CheckIDNumber%22).getLatestRelease();%0A%09%09%09assertEquals(%22v3.0%22, release.getTagName());%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%09fail();%0A%09%09%7D%0A%09%7D%0A%09%0A%09@Test%0A%09public void LatestRepositoryNotExist() %7B%0A%09%09try %7B%0A%09%09%09// add the repository that %60NOT%60 have latest release%0A%09%09%09GHRelease release = gitHub.getRepository(%22kamontat/Java8Example%22).getLatestRelease();%0A%09%09%09assertNull(release);%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%09fail();%0A%09%09%7D%0A%09%7D
 %0A%0A    privat

add test