@SuppressWarnings("unchecked")
@Test
public void testPopulateIndex() throws Exception {

  // This service registry must return a list of jobs
  List<String> args = new ArrayList<String>();
  args.add(new DefaultOrganization().getId());
  List<Job> jobs = new ArrayList<Job>();
  for (long i = 0; i < 10; i++) {
    MediaPackage mediaPackage = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew();
    mediaPackage.setIdentifier(IdImpl.fromUUID());
    searchDatabase.storeMediaPackage(mediaPackage, acl, new Date());
    String payload = MediaPackageParser.getAsXml(mediaPackage);
    Job job = new JobImpl(i);
    job.setArguments(args);
    job.setPayload(payload);
    job.setStatus(Status.FINISHED);
    jobs.add(job);
  }
  ServiceRegistry serviceRegistry = EasyMock.createNiceMock(ServiceRegistry.class);
  EasyMock.expect(
          serviceRegistry.createJob((String) EasyMock.anyObject(), (String) EasyMock.anyObject(),
                  (List<String>) EasyMock.anyObject(), (String) EasyMock.anyObject(), EasyMock.anyBoolean()))
                  .andReturn(new JobImpl()).anyTimes();
  EasyMock.expect(serviceRegistry.updateJob((Job) EasyMock.anyObject())).andReturn(new JobImpl()).anyTimes();
  EasyMock.expect(serviceRegistry.getJobs((String) EasyMock.anyObject(), (Status) EasyMock.anyObject()))
  .andReturn(jobs).anyTimes();
  EasyMock.replay(serviceRegistry);
  service.setServiceRegistry(serviceRegistry);
  OrganizationDirectoryService orgDirectory = EasyMock.createNiceMock(OrganizationDirectoryService.class);
  EasyMock.expect(orgDirectory.getOrganization((String) EasyMock.anyObject())).andReturn(new DefaultOrganization())
  .anyTimes();
  EasyMock.replay(orgDirectory);
  service.setOrganizationDirectoryService(orgDirectory);
  // We should have nothing in the search index
  assertEquals(0, service.getByQuery(new SearchQuery()).size());
  service.populateIndex("System Admin");
  // This time we should have 10 results
  assertEquals(10, service.getByQuery(new SearchQuery()).size());
}