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());
}
}
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());
}
}
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());
}
}
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());
}
Aggregations