CloneGit extends AbstractCommand { @Override public void execute() throws Exception { final String packageName = getArgument(0); final String branch = getOptionalArgument(1).orElse("master"); logger.debug("Package name: {}, Branch: {}", packageName, branch); final String remoteUri = MessageFormat.format(URI_PATTERN, packageName); final InMemoryRepository repository = new InMemoryRepository(new DfsRepositoryDescription()); final Git git = new Git(repository); git.remoteAdd().setName("origin").setUri(new URIish(remoteUri)).call(); logger.trace("Git: {}", git); final FetchResult fetchResult = git.fetch() .setRemote("origin") .setRefSpecs("+refs/heads/" + branch + ":refs/remotes/origin/" + branch) .call(); logger.debug("Fetch result: {}", fetchResult); logger.debug("Refs: {}", fetchResult.getAdvertisedRefs()); final Ref fetchHead = fetchResult.getAdvertisedRef(HEAD); final ObjectId commitId = fetchHead.getObjectId(); logger.debug("Commit id: {}", commitId); try (final RevWalk revWalk = new RevWalk(repository)) { visit(repository, revWalk, commitId); } logger.debug("FileSet: {}", fileSet); }  @Override void execute(); Stream<FileContent> stream();  }
@Test @PrepareForTest({CloneGit.class, FetchResult.class, RevCommit.class}) public void testExecute() throws Exception { final byte[] fileContent = randomUUID().toString().getBytes(); final InMemoryRepository repository = mock(InMemoryRepository.class); final Git git = mock(Git.class); final RemoteAddCommand remoteAddCommand = mock(RemoteAddCommand.class); final FetchCommand fetchCommand = mock(FetchCommand.class); final FetchResult fetchResult = mock(FetchResult.class); final Ref headRef = mock(Ref.class); final ObjectId headId = mock(ObjectId.class); final RevWalk revWalk = mock(RevWalk.class); final RevCommit revCommit = mock(RevCommit.class); final RevTree revTree = mock(RevTree.class); final TreeWalk treeWalk = mock(TreeWalk.class); final ObjectId fileId = mock(ObjectId.class); final ObjectLoader objectLoader = mock(ObjectLoader.class); whenNew(InMemoryRepository.class).withAnyArguments().thenReturn(repository); whenNew(Git.class).withAnyArguments().thenReturn(git); when(git.remoteAdd()).thenReturn(remoteAddCommand); when(remoteAddCommand.setName(anyString())).thenReturn(remoteAddCommand); when(remoteAddCommand.setUri(any(URIish.class))).thenReturn(remoteAddCommand); when(git.fetch()).thenReturn(fetchCommand); when(fetchCommand.setRemote(anyString())).thenReturn(fetchCommand); when(fetchCommand.setRefSpecs(anyString())).thenReturn(fetchCommand); when(fetchCommand.call()).thenReturn(fetchResult); when(fetchResult.getAdvertisedRef(eq(HEAD))).thenReturn(headRef); when(headRef.getObjectId()).thenReturn(headId); whenNew(RevWalk.class).withAnyArguments().thenReturn(revWalk); when(revWalk.parseCommit(any(AnyObjectId.class))).thenReturn(revCommit); when(revCommit.getTree()).thenReturn(revTree); whenNew(TreeWalk.class).withAnyArguments().thenReturn(treeWalk); final AtomicInteger visitCounter = new AtomicInteger(0); when(treeWalk.next()).then(invocation -> 0 == visitCounter.getAndIncrement()); when(treeWalk.getObjectId(0)).thenReturn(fileId); when(treeWalk.getPathString()).thenReturn(randomUUID().toString()); when(repository.open(fileId)).thenReturn(objectLoader); doAnswer(invocation -> { try { ((OutputStream) invocation.getArgument(0)).write(fileContent); } catch (final IOException ex) { fail("Unexpected exception"); } return null; }).when(objectLoader).copyTo(any(OutputStream.class)); final String packageName = randomUUID().toString(); final CloneGit cloneGit = new CloneGit(); cloneGit.setArguments(asList(packageName)); cloneGit.execute(); cloneGit.stream().forEach(file -> { try { assertArrayEquals(fileContent, from(file.open())); } catch (final IOException e) { e.printStackTrace(); fail(); } }); }