Exemplo n.º 1
0
 @BeforeMethod(groups = "unit")
 public void init() {
   context = new ActivityContext();
   context.setCredentialStr(Credentials.getMock().toString());
   jobDescription = new JobDescription();
   jobDescription.setName(UUID.randomUUID().toString());
   jobDescription.setId(UUID.randomUUID().toString());
 }
  @Test(groups = "unit")
  public void getCredentials() {
    CredentialProvider credentialProvider = EasyMock.createMock(CredentialProvider.class);
    Credential credential = Credentials.getMock();

    CredentialCreationOptions options = new CredentialCreationOptions();
    EasyMock.expect(
            credentialProvider.getGlobusCredential(
                EasyMock.eq("moo"), EasyMock.eq("cow"), EasyMock.same(options)))
        .andReturn(credential);

    EasyMock.replay(credentialProvider);

    final StaticGlobusCredentialSupplierImpl supplier = new StaticGlobusCredentialSupplierImpl();
    supplier.setGlobusCredentialProvider(credentialProvider);
    supplier.setUserName("moo");
    supplier.setPassword("cow");
    supplier.setOptions(options);
    assert credential == supplier.get();
    EasyMock.verify(credentialProvider);
  }
  @Test(groups = "integration")
  public void ftpOps() {
    final GridFtpClient gridFtpClient =
        gridFtpClientFactory.getGridFtpClient(Credentials.get(hostProxySupplier.get()));
    final String randomDir =
        System.getProperty("java.io.tmpdir") + File.separator + UUID.randomUUID().toString();
    final String mooFile = randomDir + File.separator + "moo";
    try {
      assert !gridFtpClient.exists(randomDir);
      gridFtpClient.makeDir(randomDir);
      assert gridFtpClient.exists(randomDir);

      assert Iterables.isEmpty(gridFtpClient.list(randomDir));

      assert !gridFtpClient.exists(mooFile);
      gridFtpClient.put(mooFile, new ByteArrayInputStream("Hello World".getBytes()));
      assert gridFtpClient.exists(mooFile);

      assert Iterables.getOnlyElement(gridFtpClient.list(randomDir)).getPath().equals(mooFile);
      assert !Iterables.getOnlyElement(gridFtpClient.list(randomDir)).isDirectory();

      final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      gridFtpClient.get(mooFile, outputStream);
      assert new String(outputStream.toByteArray()).equals("Hello World");

      assert gridFtpClient.exists(mooFile);
      gridFtpClient.deleteFile(mooFile);
      assert !gridFtpClient.exists(mooFile);

      assert Iterables.isEmpty(gridFtpClient.list(randomDir));

      assert gridFtpClient.exists(randomDir);
      gridFtpClient.deleteDir(randomDir);
      assert !gridFtpClient.exists(randomDir);

    } finally {
      deleteFileQuitely(gridFtpClient, mooFile);
      deleteDirectoryQuitely(gridFtpClient, randomDir);
    }
  }
public class BaseRawExtractJobFactoryImplTest {
  protected static final String BASE = "file123base";
  protected static final String PATH = "/moo/cow/path";
  protected static final String PARAMS = "test params";
  private RawExtractJobFactoryImpl factory;
  private CredentialedStagingDirectoryFactory stagingDirectoryFactory;
  private StagingDirectory stagingDirectory;

  public StagingDirectory getStagingDirectory() {
    return stagingDirectory;
  }

  protected RawExtractJobFactoryImpl getFactory() {
    return factory;
  }

  public void setStagingDirectory(final StagingDirectory stagingDirectory) {
    this.stagingDirectory = stagingDirectory;
  }

  private DisposableResourceTracker tracker;
  private InputContext rawPopulator;
  private DTAToMzXMLConverter converter;
  private DTAToMzXMLOptions options;
  protected static final Credential PROXY = Credentials.getMock();
  private MockObjectCollection mockObjects;
  private RawExtractJobProcessorImpl job;

  protected void setJob(final RawExtractJobProcessorImpl job) {
    this.job = job;
  }

  protected DisposableResourceTracker getTracker() {
    return tracker;
  }

  protected DTAToMzXMLConverter getConverter() {
    return converter;
  }

  // Test specific fields
  private boolean doPreprocessing;
  private boolean completedNormally;

  protected DTAToMzXMLOptions getOptions() {
    return options;
  }

  protected void setDoPreprocessing(final boolean doPreprocessing) {
    this.doPreprocessing = doPreprocessing;
  }

  protected void setCompletedNormally(final boolean completedNormally) {
    this.completedNormally = completedNormally;
  }

  protected boolean isCompletedNormally() {
    return completedNormally;
  }

  protected boolean getDoPreprocessing() {
    return doPreprocessing;
  }

  public BaseRawExtractJobFactoryImplTest() {
    super();
  }

  @BeforeMethod(groups = "unit")
  public void init() {
    factory = new RawExtractJobFactoryImpl();
    stagingDirectoryFactory = EasyMock.createMock(CredentialedStagingDirectoryFactory.class);
    stagingDirectory = EasyMock.createMock(StagingDirectory.class);
    EasyMock.expect(stagingDirectory.getAbsolutePath()).andStubReturn(PATH);

    EasyMock.expect(stagingDirectory.getSep()).andStubReturn("/");
    final Supplier<DisposableResourceTracker> trackerSupplier = EasyMockUtils.createMockSupplier();
    tracker = createMock(DisposableResourceTracker.class);
    expect(trackerSupplier.get()).andReturn(tracker);
    replay(trackerSupplier);
    rawPopulator = EasyMock.createMock(InputContext.class);

    converter = EasyMock.createMock(DTAToMzXMLConverter.class);
    options = new DTAToMzXMLOptions();

    factory.setDtaToMzXMLConverter(converter);
    factory.setDtaToMxXMLOptions(options);
    factory.setDisposableResourceTrackerSupplier(trackerSupplier);
    factory.setCredentialedStagingDirectoryFactory(stagingDirectoryFactory);
    mockObjects =
        MockObjectCollection.fromObjects(
            stagingDirectoryFactory, stagingDirectory, tracker, rawPopulator, converter);
  }

  protected void expectPreprocessingIfNeeded() {
    if (doPreprocessing) {
      EasyMock.expect(stagingDirectoryFactory.get(PROXY)).andReturn(stagingDirectory);
      stagingDirectory.setup();
      final OutputContext rawContext = EasyMock.createMock(OutputContext.class);
      EasyMock.expect(stagingDirectory.getOutputContext(String.format("%s.RAW", BASE)))
          .andReturn(rawContext);
      rawPopulator.get(rawContext);
    } else {
      EasyMock.expect(stagingDirectoryFactory.get(PROXY, PATH)).andReturn(stagingDirectory);
    }
  }

  protected void expectCleanUp() {
    stagingDirectory.cleanUp();
  }

  protected void postProcessAndVerify() {
    try {
      job.postprocess(completedNormally);
    } finally {
      mockObjects.verifyAndReset();
    }
  }

  protected void expectPreprocessingAndReplayMocks() {
    expectPreprocessingIfNeeded();
    expectCleanUp();
    mockObjects.replay();
  }

  protected ExecutableJobDescription buildJobAndPreprocess() {
    final JobProcessorConfiguration configuration =
        JobProcessorConfigurationFactories.getInstance().get(PROXY);
    job = factory.buildJob(configuration, rawPopulator, PARAMS, BASE);
    final ExecutableJobDescription outputDescription = job.preprocess();
    return outputDescription;
  }
}