@Test
  public void testCreateJob() throws Exception {
    SalesforceConnector connector = new SalesforceConnector();
    BulkConnection bulkConnection = Mockito.mock(BulkConnection.class);
    connector.setBulkConnection(bulkConnection);
    ArgumentCaptor<JobInfo> expectedJobInfo = ArgumentCaptor.forClass(JobInfo.class);

    Mockito.when(bulkConnection.createJob(Mockito.isA(JobInfo.class)))
        .thenAnswer(
            new Answer<JobInfo>() {
              @Override
              public JobInfo answer(InvocationOnMock invocation) throws Throwable {
                Object[] args = invocation.getArguments();
                return (JobInfo) args[0];
              }
            });

    JobInfo actualJobInfo =
        connector.createJob(OperationEnum.upsert, "Account", "NewField", ContentType.CSV, null);
    Mockito.verify(bulkConnection).createJob(expectedJobInfo.capture());

    assertEquals(expectedJobInfo.getValue(), actualJobInfo);
    assertEquals(OperationEnum.upsert, expectedJobInfo.getValue().getOperation());
    assertEquals(actualJobInfo.getObject(), expectedJobInfo.getValue().getObject());
    assertEquals(
        actualJobInfo.getExternalIdFieldName(),
        expectedJobInfo.getValue().getExternalIdFieldName());
    assertEquals(actualJobInfo.getContentType(), expectedJobInfo.getValue().getContentType());
  }
  @Test
  public void testBatchResultsStream() throws Exception {
    SalesforceConnector connector = new SalesforceConnector();
    BatchInfo batchInfo = setupBulkConnection(connector);
    BulkConnection bulkConnection = connector.getBulkConnection();
    InputStream stream = Mockito.mock(InputStream.class);

    when(bulkConnection.getBatchResultStream(batchInfo.getJobId(), batchInfo.getId()))
        .thenReturn(stream);
    InputStream actualIs = connector.batchResultStream(batchInfo);
    assertEquals(stream, actualIs);
  }
  @Test
  public void testAbortJob() throws Exception {
    SalesforceConnector connector = new SalesforceConnector();
    BulkConnection bulkConnection = Mockito.mock(BulkConnection.class);
    connector.setBulkConnection(bulkConnection);
    JobInfo expectedJobInfo = new JobInfo();
    String jobId = "uVsd234k23neasd";

    Mockito.when(bulkConnection.abortJob(jobId)).thenReturn(expectedJobInfo);
    JobInfo actualJobInfo = connector.abortJob(jobId);

    assertEquals(expectedJobInfo, actualJobInfo);
  }
  @Test
  public void testQueryResultStreamNoResults() throws Exception {
    SalesforceConnector connector = new SalesforceConnector();
    BatchInfo batchInfo = setupBulkConnection(connector);
    QueryResultList queryResultList = Mockito.mock(QueryResultList.class);
    BulkConnection bulkConnection = connector.getBulkConnection();
    String[] queryResults = new String[0];

    when(bulkConnection.getQueryResultList(batchInfo.getJobId(), batchInfo.getId()))
        .thenReturn(queryResultList);
    when(queryResultList.getResult()).thenReturn(queryResults);
    InputStream actualIs = connector.queryResultStream(batchInfo);
    assertEquals(null, actualIs);
  }
  @Test(expected = ConnectionException.class)
  public void testCreateBatchWithConnectionException() throws Exception {
    SalesforceConnector connector = new SalesforceConnector();
    BulkConnection bulkConnection = Mockito.mock(BulkConnection.class);
    AsyncApiException exception = Mockito.mock(AsyncApiException.class);
    connector.setBulkConnection(bulkConnection);

    JobInfo jobInfo = new JobInfo();
    List<Map<String, Object>> objects = new ArrayList<Map<String, Object>>();

    Mockito.when(exception.getExceptionCode()).thenReturn(AsyncExceptionCode.InvalidSessionId);
    Mockito.when(bulkConnection.createBatch(jobInfo)).thenThrow(exception);
    connector.createBatch(jobInfo, objects);
  }
  @Test
  public void testCreateBatch() throws Exception {
    SalesforceConnector connector = new SalesforceConnector();
    BulkConnection bulkConnection = Mockito.mock(BulkConnection.class);
    BatchRequest batchRequest = Mockito.mock(BatchRequest.class);
    connector.setBulkConnection(bulkConnection);

    JobInfo jobInfo = new JobInfo();
    List<Map<String, Object>> objects = new ArrayList<Map<String, Object>>();

    BatchInfo expectedBatchInfo = new BatchInfo();
    Mockito.when(bulkConnection.createBatch(jobInfo)).thenReturn(batchRequest);
    Mockito.when(batchRequest.completeRequest()).thenReturn(expectedBatchInfo);
    BatchInfo actualBatchInfo = connector.createBatch(jobInfo, objects);

    assertEquals(expectedBatchInfo, actualBatchInfo);
    Mockito.verify(batchRequest).addSObjects(connector.toAsyncSObjectList(objects));
  }
  @Test(expected = ConnectionException.class)
  public void testCreateBatchForQueryWithConnectionException() throws Exception {
    SalesforceConnector connector = new SalesforceConnector();
    BulkConnection bulkConnection = Mockito.mock(BulkConnection.class);
    ArgumentCaptor<JobInfo> expectedJobInfo = ArgumentCaptor.forClass(JobInfo.class);
    AsyncApiException exception = Mockito.mock(AsyncApiException.class);
    connector.setBulkConnection(bulkConnection);

    JobInfo actualJobInfo = new JobInfo();
    String query = "SELECT Id FROM Contact";

    Mockito.when(exception.getExceptionCode()).thenReturn(AsyncExceptionCode.InvalidSessionId);
    Mockito.when(
            bulkConnection.createBatchFromStream(
                expectedJobInfo.capture(), Mockito.isA(InputStream.class)))
        .thenThrow(exception);
    connector.createBatchForQuery(actualJobInfo, query);
    assertEquals(expectedJobInfo.getValue(), actualJobInfo);
  }
  @Test
  public void testCreateBatchStream() throws Exception {
    SalesforceConnector connector = new SalesforceConnector();
    BulkConnection bulkConnection = Mockito.mock(BulkConnection.class);
    ArgumentCaptor<JobInfo> expectedJobInfo = ArgumentCaptor.forClass(JobInfo.class);
    connector.setBulkConnection(bulkConnection);

    JobInfo actualJobInfo = new JobInfo();
    InputStream stream = Mockito.mock(InputStream.class);

    BatchInfo expectedBatchInfo = new BatchInfo();
    Mockito.when(
            bulkConnection.createBatchFromStream(
                expectedJobInfo.capture(), Mockito.isA(InputStream.class)))
        .thenReturn(expectedBatchInfo);
    BatchInfo actualBatchInfo = connector.createBatchStream(actualJobInfo, stream);

    assertEquals(expectedBatchInfo, actualBatchInfo);
    assertEquals(expectedJobInfo.getValue(), actualJobInfo);
  }
  @Test
  public void testQueryResultStream() throws Exception {
    SalesforceConnector connector = new SalesforceConnector();
    BatchInfo batchInfo = setupBulkConnection(connector);
    QueryResultList queryResultList = Mockito.mock(QueryResultList.class);
    BulkConnection bulkConnection = connector.getBulkConnection();
    String[] queryResults = {"ID1", "ID2"};
    byte[] a = {'a'};
    byte[] b = {'b'};
    InputStream[] sourceIs = {new ByteArrayInputStream(a), new ByteArrayInputStream(b)};

    when(bulkConnection.getQueryResultList(batchInfo.getJobId(), batchInfo.getId()))
        .thenReturn(queryResultList);
    when(queryResultList.getResult()).thenReturn(queryResults);
    when(bulkConnection.getQueryResultStream(batchInfo.getJobId(), batchInfo.getId(), "ID1"))
        .thenReturn(sourceIs[0]);
    when(bulkConnection.getQueryResultStream(batchInfo.getJobId(), batchInfo.getId(), "ID2"))
        .thenReturn(sourceIs[1]);
    InputStream actualIs = connector.queryResultStream(batchInfo);
    assertTrue(actualIs instanceof SequenceInputStream);
    assertEquals(actualIs.read(), a[0]);
    assertEquals(actualIs.read(), b[0]);
  }
예제 #10
0
  public BatchInfo completeRequest() throws AsyncApiException {
    try {
      xmlStream.writeEndTag(BulkConnection.NAMESPACE, "sObjects");
      xmlStream.endDocument();
      xmlStream.close();
      InputStream in = transport.getContent();

      if (transport.isSuccessful()) {
        return loadBatchInfo(in);
      } else {
        BulkConnection.parseAndThrowException(in);
      }
    } catch (IOException e) {
      throw new AsyncApiException(
          "Failed to complete request", AsyncExceptionCode.ClientInputError, e);
    } catch (PullParserException e) {
      throw new AsyncApiException(
          "Failed to complete request", AsyncExceptionCode.ClientInputError, e);
    } catch (ConnectionException e) {
      throw new AsyncApiException(
          "Failed to complete request", AsyncExceptionCode.ClientInputError, e);
    }
    return null;
  }