Search in sources :

Example 1 with SyncReportResults

use of org.apache.camel.component.salesforce.api.dto.analytics.reports.SyncReportResults in project camel by apache.

the class AnalyticsApiIntegrationTest method testReport.

@Theory
public void testReport(String reportName) throws Exception {
    log.info("Testing report {}...", reportName);
    // get Report Id
    final QueryRecordsReport reports = template().requestBody("direct:queryReport", "SELECT Id FROM Report WHERE DeveloperName='" + reportName + "'", QueryRecordsReport.class);
    assertNotNull("query", reports);
    final List<Report> reportsRecords = reports.getRecords();
    assertFalse("Report not found", reportsRecords.isEmpty());
    final String testReportId = reportsRecords.get(0).getId();
    assertNotNull(testReportId);
    // 1. getReportDescription
    final ReportDescription reportDescription = template().requestBody("direct:getReportDescription", testReportId, ReportDescription.class);
    assertNotNull("getReportDescriptions", reportDescription);
    LOG.debug("getReportDescriptions: {}", reportDescription);
    final ReportMetadata testReportMetadata = reportDescription.getReportMetadata();
    // 2. executeSyncReport
    // execute with no metadata
    SyncReportResults reportResults = template().requestBodyAndHeader("direct:executeSyncReport", testReportId, SalesforceEndpointConfig.INCLUDE_DETAILS, Boolean.TRUE, SyncReportResults.class);
    assertNotNull("executeSyncReport", reportResults);
    LOG.debug("executeSyncReport: {}", reportResults);
    // execute with metadata
    final Map<String, Object> headers = new HashMap<String, Object>();
    headers.put(SalesforceEndpointConfig.INCLUDE_DETAILS, Boolean.FALSE);
    Object body;
    if (!bodyMetadata) {
        headers.put(SalesforceEndpointConfig.REPORT_METADATA, testReportMetadata);
        body = testReportId;
    } else {
        body = testReportMetadata;
    }
    reportResults = template().requestBodyAndHeaders("direct:executeSyncReport", body, headers, SyncReportResults.class);
    assertNotNull("executeSyncReport with metadata", reportResults);
    LOG.debug("executeSyncReport with metadata: {}", reportResults);
    // 3. executeAsyncReport
    // execute with no metadata
    ReportInstance reportInstance = template().requestBodyAndHeader("direct:executeAsyncReport", testReportId, SalesforceEndpointConfig.INCLUDE_DETAILS, true, ReportInstance.class);
    assertNotNull("executeAsyncReport", reportInstance);
    LOG.debug("executeAsyncReport: {}", reportInstance);
    // execute with metadata
    headers.clear();
    headers.put(SalesforceEndpointConfig.INCLUDE_DETAILS, "true");
    if (!bodyMetadata) {
        headers.put(SalesforceEndpointConfig.REPORT_METADATA, testReportMetadata);
        body = testReportId;
        bodyMetadata = true;
    } else {
        body = testReportMetadata;
        bodyMetadata = false;
    }
    reportInstance = template().requestBodyAndHeaders("direct:executeAsyncReport", body, headers, ReportInstance.class);
    assertNotNull("executeAsyncReport with metadata", reportInstance);
    LOG.debug("executeAsyncReport with metadata: {}", reportInstance);
    final String testReportInstanceId = reportInstance.getId();
    // 4. getReportInstances
    final List reportInstances = template().requestBody("direct:getReportInstances", testReportId, List.class);
    assertNotNull("getReportInstances", reportInstances);
    assertFalse("getReportInstances empty", reportInstances.isEmpty());
    LOG.debug("getReportInstances: {}", reportInstances);
    // 5. getReportResults
    // wait for the report to complete
    boolean done = false;
    int tries = 0;
    AsyncReportResults asyncReportResults = null;
    while (!done) {
        asyncReportResults = template().requestBodyAndHeader("direct:getReportResults", testReportId, SalesforceEndpointConfig.INSTANCE_ID, testReportInstanceId, AsyncReportResults.class);
        done = asyncReportResults != null && (asyncReportResults.getAttributes().getStatus() == ReportStatusEnum.Success || asyncReportResults.getAttributes().getStatus() == ReportStatusEnum.Error);
        if (!done) {
            // avoid flooding calls
            Thread.sleep(RETRY_DELAY);
            if (++tries > REPORT_RESULT_RETRIES) {
                final long retrySeconds = TimeUnit.SECONDS.convert(tries * RETRY_DELAY, TimeUnit.MILLISECONDS);
                fail("Async report result not available in " + retrySeconds + " seconds");
            }
        }
    }
    assertNotNull("getReportResults", asyncReportResults);
    assertEquals("getReportResults status", ReportStatusEnum.Success, asyncReportResults.getAttributes().getStatus());
    LOG.debug("getReportResults: {}", asyncReportResults);
    // 6. SalesforceReportResultsConverter tests
    // defaults
    String convertResults = template.requestBody("direct:convertResults", asyncReportResults, String.class);
    assertNotNull("default convertResults", convertResults);
    LOG.debug("Default options", convertResults);
    LOG.debug("{}", convertResults);
    // permutations of include details, include headers, include summary
    final boolean[] values = new boolean[NUM_OPTIONS];
    final int nIterations = (int) Math.pow(2, NUM_OPTIONS);
    for (int i = 0; i < nIterations; i++) {
        // toggle options
        for (int j = 0; j < NUM_OPTIONS; j++) {
            if (i % POWERS[j] == 0) {
                values[j] = !values[j];
            }
        }
        log.debug("Options {} = {}", REPORT_OPTIONS, values);
        headers.clear();
        for (int j = 0; j < REPORT_OPTIONS.length; j++) {
            headers.put(REPORT_OPTIONS[j], values[j]);
        }
        convertResults = template.requestBodyAndHeaders("direct:convertResults", asyncReportResults, headers, String.class);
        assertNotNull("convertResults", convertResults);
        LOG.debug("{}", convertResults);
    }
}
Also used : Report(org.apache.camel.component.salesforce.api.dto.analytics.reports.Report) QueryRecordsReport(org.apache.camel.component.salesforce.api.dto.analytics.reports.QueryRecordsReport) HashMap(java.util.HashMap) QueryRecordsReport(org.apache.camel.component.salesforce.api.dto.analytics.reports.QueryRecordsReport) SyncReportResults(org.apache.camel.component.salesforce.api.dto.analytics.reports.SyncReportResults) ReportInstance(org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportInstance) AsyncReportResults(org.apache.camel.component.salesforce.api.dto.analytics.reports.AsyncReportResults) List(java.util.List) ReportDescription(org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportDescription) ReportMetadata(org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportMetadata) Theory(org.junit.experimental.theories.Theory)

Example 2 with SyncReportResults

use of org.apache.camel.component.salesforce.api.dto.analytics.reports.SyncReportResults in project camel by apache.

the class DefaultAnalyticsApiClient method executeSyncReport.

@Override
public void executeSyncReport(String reportId, Boolean includeDetails, ReportMetadata reportMetadata, final ReportResultsResponseCallback callback) {
    final boolean useGet = reportMetadata == null;
    final Request request = getRequest(useGet ? HttpMethod.GET : HttpMethod.POST, reportsUrl(reportId, includeDetails));
    // set POST data
    if (!useGet) {
        try {
            // wrap reportMetadata in a map
            final HashMap<String, Object> input = new HashMap<String, Object>();
            input.put("reportMetadata", reportMetadata);
            marshalRequest(input, request);
        } catch (SalesforceException e) {
            callback.onResponse(null, e);
            return;
        }
    }
    doHttpRequest(request, new ClientResponseCallback() {

        @Override
        public void onResponse(InputStream response, SalesforceException ex) {
            SyncReportResults reportResults = null;
            try {
                reportResults = unmarshalResponse(response, request, SyncReportResults.class);
            } catch (SalesforceException e) {
                ex = e;
            }
            callback.onResponse(reportResults, ex);
        }
    });
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) HashMap(java.util.HashMap) InputStream(java.io.InputStream) Request(org.eclipse.jetty.client.api.Request) SyncReportResults(org.apache.camel.component.salesforce.api.dto.analytics.reports.SyncReportResults)

Aggregations

HashMap (java.util.HashMap)2 SyncReportResults (org.apache.camel.component.salesforce.api.dto.analytics.reports.SyncReportResults)2 InputStream (java.io.InputStream)1 List (java.util.List)1 SalesforceException (org.apache.camel.component.salesforce.api.SalesforceException)1 AsyncReportResults (org.apache.camel.component.salesforce.api.dto.analytics.reports.AsyncReportResults)1 QueryRecordsReport (org.apache.camel.component.salesforce.api.dto.analytics.reports.QueryRecordsReport)1 Report (org.apache.camel.component.salesforce.api.dto.analytics.reports.Report)1 ReportDescription (org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportDescription)1 ReportInstance (org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportInstance)1 ReportMetadata (org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportMetadata)1 Request (org.eclipse.jetty.client.api.Request)1 Theory (org.junit.experimental.theories.Theory)1