use of org.apache.camel.component.salesforce.api.dto.approval.ApprovalRequest in project camel by apache.
the class AbstractRestProcessor method processApproval.
final void processApproval(final Exchange exchange, final AsyncCallback callback) throws SalesforceException {
final TypeConverter converter = exchange.getContext().getTypeConverter();
final ApprovalRequest approvalRequestFromHeader = getParameter(SalesforceEndpointConfig.APPROVAL, exchange, IGNORE_BODY, IS_OPTIONAL, ApprovalRequest.class);
final boolean requestGivenInHeader = approvalRequestFromHeader != null;
// find if there is a ApprovalRequest as `approval` in the message header
final ApprovalRequest approvalHeader = Optional.ofNullable(approvalRequestFromHeader).orElse(new ApprovalRequest());
final Message incomingMessage = exchange.getIn();
final Map<String, Object> incomingHeaders = incomingMessage.getHeaders();
final boolean requestGivenInParametersInHeader = processApprovalHeaderValues(approvalHeader, incomingHeaders);
final boolean nothingInheader = !requestGivenInHeader && !requestGivenInParametersInHeader;
final Object approvalBody = incomingMessage.getBody();
final boolean bodyIsIterable = approvalBody instanceof Iterable;
final boolean bodyIsIterableButEmpty = bodyIsIterable && !((Iterable) approvalBody).iterator().hasNext();
// body contains nothing of interest if it's null, holds an empty iterable or cannot be converted to
// ApprovalRequest
final boolean nothingInBody = !(approvalBody != null && !bodyIsIterableButEmpty);
// we found nothing in the headers or the body
if (nothingInheader && nothingInBody) {
throw new SalesforceException("Missing " + SalesforceEndpointConfig.APPROVAL + " parameter in header or ApprovalRequest or List of ApprovalRequests body", 0);
}
// let's try to resolve the request body to send
final ApprovalRequests requestsBody;
if (nothingInBody) {
// nothing in body use the header values only
requestsBody = new ApprovalRequests(approvalHeader);
} else if (bodyIsIterable) {
// multiple ApprovalRequests are found
final Iterable<?> approvalRequests = (Iterable<?>) approvalBody;
// use header values as template and apply them to the body
final List<ApprovalRequest> requests = StreamSupport.stream(approvalRequests.spliterator(), false).map(value -> converter.convertTo(ApprovalRequest.class, value)).map(request -> request.applyTemplate(approvalHeader)).collect(Collectors.toList());
requestsBody = new ApprovalRequests(requests);
} else {
// we've looked at the body, and are expecting to see something resembling ApprovalRequest in there
// but lets see if that is so
final ApprovalRequest given = converter.tryConvertTo(ApprovalRequest.class, approvalBody);
final ApprovalRequest request = Optional.ofNullable(given).orElse(new ApprovalRequest()).applyTemplate(approvalHeader);
requestsBody = new ApprovalRequests(request);
}
final InputStream request = getRequestStream(requestsBody);
restClient.approval(request, (response, exception) -> processResponse(exchange, response, exception, callback));
}
use of org.apache.camel.component.salesforce.api.dto.approval.ApprovalRequest in project camel by apache.
the class AbstractRestProcessorApprovalTest method shouldApplyTemplateToRequestsFromBody.
@Test
public void shouldApplyTemplateToRequestsFromBody() throws SalesforceException {
final ApprovalRequest template = new ApprovalRequest();
template.setActionType(Action.Submit);
final ApprovalRequest approvalRequest1 = new ApprovalRequest();
approvalRequest1.setComments("it should be me first");
final ApprovalRequest approvalRequest2 = new ApprovalRequest();
approvalRequest2.setComments("it should be me second");
final TestRestProcessor processor = sendBodyAndHeader(Arrays.asList(approvalRequest1, approvalRequest2), template);
verify(processor).getRequestStream(new ApprovalRequests(Arrays.asList(approvalRequest1.applyTemplate(template), approvalRequest2.applyTemplate(template))));
}
use of org.apache.camel.component.salesforce.api.dto.approval.ApprovalRequest in project camel by apache.
the class AbstractRestProcessorApprovalTest method shouldHonorPriorities.
@Test
public void shouldHonorPriorities() throws SalesforceException {
final ApprovalRequest template = new ApprovalRequest();
template.setComments("third priority");
final ApprovalRequest body = new ApprovalRequest();
body.setComments("first priority");
final Map<String, Object> headers = Collections.singletonMap("approval.Comments", "second priority");
final TestRestProcessor processor1 = sendBodyAndHeaders(null, template, null);
verify(processor1).getRequestStream(new ApprovalRequests(requestWithComment("third priority")));
final TestRestProcessor processor2 = sendBodyAndHeaders(null, template, headers);
verify(processor2).getRequestStream(new ApprovalRequests(requestWithComment("second priority")));
final TestRestProcessor processor3 = sendBodyAndHeaders(body, template, headers);
verify(processor3).getRequestStream(new ApprovalRequests(requestWithComment("first priority")));
}
use of org.apache.camel.component.salesforce.api.dto.approval.ApprovalRequest in project camel by apache.
the class AbstractRestProcessorApprovalTest method shouldFetchApprovalRequestsFromMultiplePropertiesInMessageHeaders.
@Test
public void shouldFetchApprovalRequestsFromMultiplePropertiesInMessageHeaders() throws SalesforceException {
final Map<String, Object> headers = new HashMap<>();
headers.put("approval.ContextId", "contextId");
final TestRestProcessor processor = sendBodyAndHeaders(notUsed(), notUsed(), headers);
final ApprovalRequest request = new ApprovalRequest();
request.setContextId("contextId");
verify(processor).getRequestStream(new ApprovalRequests(request));
}
use of org.apache.camel.component.salesforce.api.dto.approval.ApprovalRequest in project camel by apache.
the class AbstractRestProcessorApprovalTest method shouldApplyTemplateToRequestFromBody.
@Test
public void shouldApplyTemplateToRequestFromBody() throws SalesforceException {
final ApprovalRequest template = new ApprovalRequest();
template.setActionType(Action.Submit);
final ApprovalRequest approvalRequest = new ApprovalRequest();
approvalRequest.setComments("it should be me");
final TestRestProcessor processor = sendBodyAndHeader(approvalRequest, template);
verify(processor).getRequestStream(new ApprovalRequests(approvalRequest.applyTemplate(template)));
}
Aggregations