use of org.eclipse.jgit.api.MergeCommand in project spring-cloud-config by spring-cloud.
the class JGitEnvironmentRepositoryTests method testFetchException.
@Test
public void testFetchException() throws Exception {
Git git = mock(Git.class);
CloneCommand cloneCommand = mock(CloneCommand.class);
MockGitFactory factory = new MockGitFactory(git, cloneCommand);
this.repository.setGitFactory(factory);
this.repository.setDeleteUntrackedBranches(true);
// refresh()->shouldPull
StatusCommand statusCommand = mock(StatusCommand.class);
Status status = mock(Status.class);
when(git.status()).thenReturn(statusCommand);
Repository repository = mock(Repository.class);
when(git.getRepository()).thenReturn(repository);
StoredConfig storedConfig = mock(StoredConfig.class);
when(repository.getConfig()).thenReturn(storedConfig);
when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
when(statusCommand.call()).thenReturn(status);
when(status.isClean()).thenReturn(true);
// refresh()->fetch
FetchCommand fetchCommand = mock(FetchCommand.class);
when(git.fetch()).thenReturn(fetchCommand);
when(fetchCommand.setRemote(anyString())).thenReturn(fetchCommand);
// here
when(fetchCommand.call()).thenThrow(new InvalidRemoteException("invalid mock remote"));
// is
// our
// exception
// we
// are
// testing
// refresh()->checkout
CheckoutCommand checkoutCommand = mock(CheckoutCommand.class);
// refresh()->checkout->containsBranch
ListBranchCommand listBranchCommand = mock(ListBranchCommand.class);
when(git.checkout()).thenReturn(checkoutCommand);
when(git.branchList()).thenReturn(listBranchCommand);
List<Ref> refs = new ArrayList<>();
Ref ref = mock(Ref.class);
refs.add(ref);
when(ref.getName()).thenReturn("/master");
when(listBranchCommand.call()).thenReturn(refs);
// refresh()->merge
MergeCommand mergeCommand = mock(MergeCommand.class);
when(git.merge()).thenReturn(mergeCommand);
// here
when(mergeCommand.call()).thenThrow(new NotMergedException());
// is
// our
// exception
// we
// are
// testing
// refresh()->return
// git.getRepository().findRef("HEAD").getObjectId().getName();
Ref headRef = mock(Ref.class);
when(repository.findRef(anyString())).thenReturn(headRef);
ObjectId newObjectId = ObjectId.fromRaw(new int[] { 1, 2, 3, 4, 5 });
when(headRef.getObjectId()).thenReturn(newObjectId);
SearchPathLocator.Locations locations = this.repository.getLocations("bar", "staging", null);
assertEquals(locations.getVersion(), newObjectId.getName());
verify(git, times(0)).branchDelete();
}
Aggregations