Search in sources :

Example 1 with GHException

use of org.kohsuke.github.GHException in project github-branch-source-plugin by jenkinsci.

the class GithubSCMSourceTagsTest method testExistingMultipleTagsIteratorGHExceptionOnHasNextAndHasAtLeastOne.

@Test
public void testExistingMultipleTagsIteratorGHExceptionOnHasNextAndHasAtLeastOne() throws IOException {
    // Scenario: multiple tags but returns a GHException and found at least one tag
    SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class);
    Exception expectedError = new GHException("Bad Tag Request");
    Mockito.when(mockSCMHeadObserver.getIncludes()).thenReturn(new HashSet<>(Arrays.asList(new GitHubTagSCMHead("existent-multiple-tags1", System.currentTimeMillis()), new GitHubTagSCMHead("existent-multiple-tags2", System.currentTimeMillis()))));
    GitHubSCMSourceContext context = new GitHubSCMSourceContext(null, mockSCMHeadObserver);
    context.wantTags(true);
    GitHubSCMSourceRequest request = context.newRequest(new GitHubSCMSource("cloudbeers", "yolo", null, false), null);
    GHRepository repoSpy = Mockito.spy(repo);
    PagedIterable<GHRef> iterableSpy = (PagedIterable<GHRef>) Mockito.mock(PagedIterable.class);
    Mockito.when(repoSpy.listRefs("tags")).thenReturn(iterableSpy);
    PagedIterator<GHRef> iteratorSpy = (PagedIterator<GHRef>) Mockito.mock(PagedIterator.class);
    Mockito.when(iterableSpy.iterator()).thenReturn(iteratorSpy);
    Iterator<GHRef> tags = new GitHubSCMSource.LazyTags(request, repoSpy).iterator();
    Mockito.when(iteratorSpy.hasNext()).thenReturn(true).thenThrow(expectedError);
    // Expected: First call to hasNext should work true and then will throw an error
    try {
        // First Call is fine
        tags.hasNext();
        // Second Call fails
        tags.hasNext();
        fail("This should throw an exception");
    } catch (GHException e) {
        assertEquals("Bad Tag Request", e.getMessage());
    }
}
Also used : GHRepository(org.kohsuke.github.GHRepository) SCMHeadObserver(jenkins.scm.api.SCMHeadObserver) IOException(java.io.IOException) GHFileNotFoundException(org.kohsuke.github.GHFileNotFoundException) FileNotFoundException(java.io.FileNotFoundException) GHException(org.kohsuke.github.GHException) NoSuchElementException(java.util.NoSuchElementException) PagedIterator(org.kohsuke.github.PagedIterator) PagedIterable(org.kohsuke.github.PagedIterable) GHRef(org.kohsuke.github.GHRef) GHException(org.kohsuke.github.GHException) Test(org.junit.Test)

Example 2 with GHException

use of org.kohsuke.github.GHException in project github-branch-source-plugin by jenkinsci.

the class GithubSCMSourceTagsTest method testExistingMultipleTagsIteratorGHExceptionOnHasNextAndDoesNotHaveOne.

@Test
public void testExistingMultipleTagsIteratorGHExceptionOnHasNextAndDoesNotHaveOne() throws IOException {
    // Scenario: multiple tags but returns a GHException on the first tag
    SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class);
    Exception expectedError = new GHException("Bad Tag Request");
    Mockito.when(mockSCMHeadObserver.getIncludes()).thenReturn(new HashSet<>(Arrays.asList(new GitHubTagSCMHead("existent-multiple-tags1", System.currentTimeMillis()), new GitHubTagSCMHead("existent-multiple-tags2", System.currentTimeMillis()))));
    GitHubSCMSourceContext context = new GitHubSCMSourceContext(null, mockSCMHeadObserver);
    context.wantTags(true);
    GitHubSCMSourceRequest request = context.newRequest(new GitHubSCMSource("cloudbeers", "yolo", null, false), null);
    GHRepository repoSpy = Mockito.spy(repo);
    PagedIterable<GHRef> iterableSpy = (PagedIterable<GHRef>) Mockito.mock(PagedIterable.class);
    Mockito.when(repoSpy.listRefs("tags")).thenReturn(iterableSpy).thenCallRealMethod();
    PagedIterator<GHRef> iteratorSpy = (PagedIterator<GHRef>) Mockito.mock(PagedIterator.class);
    Mockito.when(iterableSpy.iterator()).thenReturn(iteratorSpy);
    Iterator<GHRef> tags = new GitHubSCMSource.LazyTags(request, repoSpy).iterator();
    Mockito.when(iteratorSpy.hasNext()).thenThrow(expectedError);
    // Expected: First call to hasNext throws the GHException
    try {
        tags.hasNext();
        fail("This should throw an exception");
    } catch (GHException e) {
        assertEquals("Bad Tag Request", e.getMessage());
    }
}
Also used : GHRepository(org.kohsuke.github.GHRepository) SCMHeadObserver(jenkins.scm.api.SCMHeadObserver) IOException(java.io.IOException) GHFileNotFoundException(org.kohsuke.github.GHFileNotFoundException) FileNotFoundException(java.io.FileNotFoundException) GHException(org.kohsuke.github.GHException) NoSuchElementException(java.util.NoSuchElementException) PagedIterator(org.kohsuke.github.PagedIterator) PagedIterable(org.kohsuke.github.PagedIterable) GHRef(org.kohsuke.github.GHRef) GHException(org.kohsuke.github.GHException) Test(org.junit.Test)

Example 3 with GHException

use of org.kohsuke.github.GHException in project github-branch-source-plugin by jenkinsci.

the class GithubSCMSourceTagsTest method testExistingMultipleTagsIteratorGHExceptionOnHasNextButThrowsIOErrorOnGetRefs.

@Test
public void testExistingMultipleTagsIteratorGHExceptionOnHasNextButThrowsIOErrorOnGetRefs() throws IOException {
    // Scenario: making a request for a multiple tags but catches a GH exception on hasNext
    // and then throws on IO error getRefs
    SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class);
    Exception expectedError = new GHException("Bad Tag Request");
    Exception expectedGetRefError = new IOException("Bad Tag Ref Request");
    Mockito.when(mockSCMHeadObserver.getIncludes()).thenReturn(new HashSet<>(Arrays.asList(new GitHubTagSCMHead("existent-multiple-tags1", System.currentTimeMillis()), new GitHubTagSCMHead("existent-multiple-tags2", System.currentTimeMillis()))));
    GitHubSCMSourceContext context = new GitHubSCMSourceContext(null, mockSCMHeadObserver);
    context.wantTags(true);
    GitHubSCMSourceRequest request = context.newRequest(new GitHubSCMSource("cloudbeers", "yolo", null, false), null);
    GHRepository repoSpy = Mockito.spy(repo);
    PagedIterable<GHRef> iterableSpy = (PagedIterable<GHRef>) Mockito.mock(PagedIterable.class);
    Mockito.when(repoSpy.listRefs("tags")).thenReturn(iterableSpy);
    PagedIterator<GHRef> iteratorSpy = (PagedIterator<GHRef>) Mockito.mock(PagedIterator.class);
    Mockito.when(iterableSpy.iterator()).thenReturn(iteratorSpy);
    Iterator<GHRef> tags = new GitHubSCMSource.LazyTags(request, repoSpy).iterator();
    Mockito.when(iteratorSpy.hasNext()).thenThrow(expectedError);
    Mockito.when(repoSpy.getRefs("tags")).thenThrow(expectedGetRefError);
    // getRefs so it returns an error
    try {
        tags.hasNext();
        fail("This should throw an exception");
    } catch (GHException e) {
        // We suppress the new GetRef error and then throw the original one
        assertEquals("Bad Tag Request", e.getMessage());
    }
}
Also used : GHRepository(org.kohsuke.github.GHRepository) SCMHeadObserver(jenkins.scm.api.SCMHeadObserver) IOException(java.io.IOException) IOException(java.io.IOException) GHFileNotFoundException(org.kohsuke.github.GHFileNotFoundException) FileNotFoundException(java.io.FileNotFoundException) GHException(org.kohsuke.github.GHException) NoSuchElementException(java.util.NoSuchElementException) PagedIterator(org.kohsuke.github.PagedIterator) PagedIterable(org.kohsuke.github.PagedIterable) GHRef(org.kohsuke.github.GHRef) GHException(org.kohsuke.github.GHException) Test(org.junit.Test)

Example 4 with GHException

use of org.kohsuke.github.GHException in project github-branch-source-plugin by jenkinsci.

the class GithubSCMSourceTagsTest method testExistingMultipleTagsIteratorGHExceptionOnHasNextButThrowsFileNotFoundOnGetRefs.

@Test
public void testExistingMultipleTagsIteratorGHExceptionOnHasNextButThrowsFileNotFoundOnGetRefs() throws IOException {
    // Scenario: multiple tags but catches a GH exception on hasNext and then
    // FilenotFound on getRefs
    SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class);
    Exception expectedError = new GHException("Bad Tag Request");
    Exception expectedGetRefError = new FileNotFoundException("Bad Tag Ref Request");
    Mockito.when(mockSCMHeadObserver.getIncludes()).thenReturn(new HashSet<>(Arrays.asList(new GitHubTagSCMHead("existent-multiple-tags1", System.currentTimeMillis()), new GitHubTagSCMHead("existent-multiple-tags2", System.currentTimeMillis()))));
    GitHubSCMSourceContext context = new GitHubSCMSourceContext(null, mockSCMHeadObserver);
    context.wantTags(true);
    GitHubSCMSourceRequest request = context.newRequest(new GitHubSCMSource("cloudbeers", "yolo", null, false), null);
    GHRepository repoSpy = Mockito.spy(repo);
    PagedIterable<GHRef> iterableSpy = (PagedIterable<GHRef>) Mockito.mock(PagedIterable.class);
    Mockito.when(repoSpy.listRefs("tags")).thenReturn(iterableSpy);
    PagedIterator<GHRef> iteratorSpy = (PagedIterator<GHRef>) Mockito.mock(PagedIterator.class);
    Mockito.when(iterableSpy.iterator()).thenReturn(iteratorSpy);
    Iterator<GHRef> tags = new GitHubSCMSource.LazyTags(request, repoSpy).iterator();
    Mockito.when(iteratorSpy.hasNext()).thenThrow(expectedError);
    Mockito.when(repoSpy.getRefs("tags")).thenThrow(expectedGetRefError);
    // Expected: First call to hasNext throws a GHException and then returns a FileNotFound on
    // getRefs so it returns an empty list
    assertFalse(tags.hasNext());
}
Also used : GHRepository(org.kohsuke.github.GHRepository) GHFileNotFoundException(org.kohsuke.github.GHFileNotFoundException) FileNotFoundException(java.io.FileNotFoundException) SCMHeadObserver(jenkins.scm.api.SCMHeadObserver) IOException(java.io.IOException) GHFileNotFoundException(org.kohsuke.github.GHFileNotFoundException) FileNotFoundException(java.io.FileNotFoundException) GHException(org.kohsuke.github.GHException) NoSuchElementException(java.util.NoSuchElementException) PagedIterator(org.kohsuke.github.PagedIterator) PagedIterable(org.kohsuke.github.PagedIterable) GHRef(org.kohsuke.github.GHRef) GHException(org.kohsuke.github.GHException) Test(org.junit.Test)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)4 IOException (java.io.IOException)4 NoSuchElementException (java.util.NoSuchElementException)4 SCMHeadObserver (jenkins.scm.api.SCMHeadObserver)4 Test (org.junit.Test)4 GHException (org.kohsuke.github.GHException)4 GHFileNotFoundException (org.kohsuke.github.GHFileNotFoundException)4 GHRef (org.kohsuke.github.GHRef)4 GHRepository (org.kohsuke.github.GHRepository)4 PagedIterable (org.kohsuke.github.PagedIterable)4 PagedIterator (org.kohsuke.github.PagedIterator)4