use of org.kohsuke.github.PagedIterable 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.PagedIterable in project github-branch-source-plugin by jenkinsci.
the class GithubSCMSourceTagsTest method testExistingMultipleTagsGHFileNotFoundExceptionIterable.
@Test
public void testExistingMultipleTagsGHFileNotFoundExceptionIterable() throws IOException {
// Scenario: Requesting multiple tags but a FileNotFound is thrown
// on the first returning the iterator and then an IO error is thrown on the iterator creation
SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class);
Error expectedError = new Error("Bad Tag Request", new GHFileNotFoundException());
Error expectedError2 = new Error("Bad Tag Request IOError", new IOException());
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);
Mockito.when(iterableSpy.iterator()).thenThrow(expectedError).thenThrow(expectedError2);
// Expected: When initially getting multiple tags, there will then be a thrown filenotfound
// which returns an empty list
// Then for the second tag iterator created it returns an IO error
Iterator<GHRef> tags = new GitHubSCMSource.LazyTags(request, repoSpy).iterator();
assertEquals(Collections.emptyIterator(), tags);
try {
Iterator<GHRef> tags2 = new GitHubSCMSource.LazyTags(request, repoSpy).iterator();
fail("This should throw an exception");
} catch (Error e) {
assertEquals("Bad Tag Request IOError", e.getMessage());
}
}
use of org.kohsuke.github.PagedIterable in project github-branch-source-plugin by jenkinsci.
the class GithubSCMSourceTagsTest method testExistingMultipleTagsIteratorGHFileNotFoundExceptionOnHasNext.
@Test
public void testExistingMultipleTagsIteratorGHFileNotFoundExceptionOnHasNext() throws IOException {
// Scenario: multiple tags but returns a filenotfound on the first hasNext
// and returns a IO error on the second hasNext
SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class);
Error expectedError = new Error("Bad Tag Request", new GHFileNotFoundException());
Error expectedError2 = new Error("Bad Tag Request IOError", new IOException());
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);
Mockito.when(iteratorSpy.hasNext()).thenThrow(expectedError).thenThrow(expectedError2);
// Expected: When initially getting multiple tags, return a filenotfound on hasNext which means
// it will get an empty list
// Then return a IO error on the second hasNext
Iterator<GHRef> tags = new GitHubSCMSource.LazyTags(request, repoSpy).iterator();
Iterator<GHRef> tags2 = new GitHubSCMSource.LazyTags(request, repoSpy).iterator();
assertFalse(tags.hasNext());
try {
tags.hasNext();
fail("This should throw an exception");
} catch (Error e) {
assertEquals("Bad Tag Request IOError", e.getMessage());
}
}
use of org.kohsuke.github.PagedIterable in project jreleaser by jreleaser.
the class Github method findMilestoneByName.
Optional<GHMilestone> findMilestoneByName(String owner, String repo, String milestoneName) throws IOException {
logger.debug(RB.$("git.milestone.lookup"), milestoneName, owner, repo);
GHRepository repository = findRepository(owner, repo);
PagedIterable<GHMilestone> milestones = repository.listMilestones(GHIssueState.OPEN);
return StreamSupport.stream(milestones.spliterator(), false).filter(m -> milestoneName.equals(m.getTitle())).findFirst();
}
use of org.kohsuke.github.PagedIterable 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());
}
}
Aggregations