private static final int CORRECT_PACKAGE_VERSION_ID = 2;
public void testCanDownloadScript() throws Exception {
RhqFacade rhqFacade = mock(RhqFacade.class);
RepoManagerRemote repoManager = mock(RepoManagerRemote.class);
ContentManagerRemote contentManager = mock(ContentManagerRemote.class);
when(rhqFacade.getProxy(RepoManagerRemote.class)).thenReturn(repoManager);
when(rhqFacade.getProxy(ContentManagerRemote.class)).thenReturn(contentManager);
when(repoManager.findReposByCriteria(any(Subject.class), any(RepoCriteria.class))).then(
new Answer<List<Repo>>() {
@Override
public List<Repo> answer(InvocationOnMock invocation) throws Throwable {
RepoCriteria crit = (RepoCriteria) invocation.getArguments()[1];
//this is so wrong...
Field f = RepoCriteria.class.getDeclaredField("filterName");
f.setAccessible(true);
String name = (String) f.get(crit);
if (CORRECT_REPO_NAME.equals(name)) {
Repo repo = new Repo(CORRECT_REPO_NAME);
repo.setId(CORRECT_REPO_ID);
PageList<Repo> ret = new PageList<Repo>(PageControl.getUnlimitedInstance());
ret.add(repo);
return ret;
} else {
return new PageList<Repo>(PageControl.getUnlimitedInstance());
}
}
});
when(contentManager.findPackagesWithLatestVersion(any(Subject.class), any(PackageCriteria.class))).then(
new Answer<List<PackageAndLatestVersionComposite>>() {
@Override
public List<PackageAndLatestVersionComposite> answer(InvocationOnMock invocation) throws Throwable {
PackageCriteria crit = (PackageCriteria) invocation.getArguments()[1];
//this is so wrong...
Field f = PackageCriteria.class.getDeclaredField("filterName");
f.setAccessible(true);
String name = (String) f.get(crit);
if (CORRECT_PACKAGE_NAME.equals(name)) {
PackageAndLatestVersionComposite composite = new PackageAndLatestVersionComposite();
composite
.setGeneralPackage(new org.rhq.core.domain.content.Package(CORRECT_PACKAGE_NAME, null));
PackageVersion pv = new PackageVersion();
pv.setId(CORRECT_PACKAGE_VERSION_ID);
composite.setLatestPackageVersion(pv);
PageList<PackageAndLatestVersionComposite> ret = new PageList<PackageAndLatestVersionComposite>(
PageControl.getUnlimitedInstance());
ret.add(composite);
return ret;
} else {
return new PageList<PackageAndLatestVersionComposite>(PageControl.getUnlimitedInstance());
}
}
});
when(repoManager.getPackageVersionBytes(any(Subject.class), anyInt(), anyInt())).then(
new Answer<byte[]>() {
@Override
public byte[] answer(InvocationOnMock invocation) throws Throwable {
int repoId = (Integer) invocation.getArguments()[1];
int packageId = (Integer) invocation.getArguments()[2];