use of org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportInstance 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);
}
}
use of org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportInstance in project camel by apache.
the class AnalyticsApiProcessor method processExecuteAsyncReport.
private void processExecuteAsyncReport(final Exchange exchange, final AsyncCallback callback) throws SalesforceException {
String reportId;
final Boolean includeDetails = getParameter(INCLUDE_DETAILS, exchange, IGNORE_BODY, IS_OPTIONAL, Boolean.class);
// try getting report metadata from body first
ReportMetadata reportMetadata = exchange.getIn().getBody(ReportMetadata.class);
if (reportMetadata != null) {
reportId = reportMetadata.getId();
if (reportId == null) {
reportId = getParameter(REPORT_ID, exchange, IGNORE_BODY, NOT_OPTIONAL);
}
} else {
reportId = getParameter(REPORT_ID, exchange, USE_BODY, NOT_OPTIONAL);
reportMetadata = getParameter(REPORT_METADATA, exchange, IGNORE_BODY, IS_OPTIONAL, ReportMetadata.class);
}
analyticsClient.executeAsyncReport(reportId, includeDetails, reportMetadata, new AnalyticsApiClient.ReportInstanceResponseCallback() {
@Override
public void onResponse(ReportInstance reportInstance, SalesforceException ex) {
processResponse(exchange, reportInstance, ex, callback);
}
});
}
use of org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportInstance in project camel by apache.
the class DefaultAnalyticsApiClient method executeAsyncReport.
@Override
public void executeAsyncReport(String reportId, Boolean includeDetails, ReportMetadata reportMetadata, final ReportInstanceResponseCallback callback) {
final Request request = getRequest(HttpMethod.POST, reportInstancesUrl(reportId, includeDetails));
// set POST data
if (reportMetadata != null) {
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) {
ReportInstance reportInstance = null;
try {
reportInstance = unmarshalResponse(response, request, ReportInstance.class);
} catch (SalesforceException e) {
ex = e;
}
callback.onResponse(reportInstance, ex);
}
});
}
Aggregations