Search in sources :

Example 1 with ApprovalRequest

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));
}
Also used : SOBJECT_CLASS(org.apache.camel.component.salesforce.SalesforceEndpointConfig.SOBJECT_CLASS) SOBJECT_EXT_ID_VALUE(org.apache.camel.component.salesforce.SalesforceEndpointConfig.SOBJECT_EXT_ID_VALUE) Message(org.apache.camel.Message) ApprovalRequests(org.apache.camel.component.salesforce.api.dto.approval.ApprovalRequests) ApprovalRequest(org.apache.camel.component.salesforce.api.dto.approval.ApprovalRequest) SOBJECT_BLOB_FIELD_NAME(org.apache.camel.component.salesforce.SalesforceEndpointConfig.SOBJECT_BLOB_FIELD_NAME) SOBJECT_ID(org.apache.camel.component.salesforce.SalesforceEndpointConfig.SOBJECT_ID) SOBJECT_SEARCH(org.apache.camel.component.salesforce.SalesforceEndpointConfig.SOBJECT_SEARCH) HashMap(java.util.HashMap) Exchange(org.apache.camel.Exchange) APEX_URL(org.apache.camel.component.salesforce.SalesforceEndpointConfig.APEX_URL) SOBJECT_FIELDS(org.apache.camel.component.salesforce.SalesforceEndpointConfig.SOBJECT_FIELDS) Matcher(java.util.regex.Matcher) SOBJECT_NAME(org.apache.camel.component.salesforce.SalesforceEndpointConfig.SOBJECT_NAME) SalesforceEndpoint(org.apache.camel.component.salesforce.SalesforceEndpoint) Map(java.util.Map) SOBJECT_QUERY(org.apache.camel.component.salesforce.SalesforceEndpointConfig.SOBJECT_QUERY) StreamSupport(java.util.stream.StreamSupport) APEX_QUERY_PARAM_PREFIX(org.apache.camel.component.salesforce.SalesforceEndpointConfig.APEX_QUERY_PARAM_PREFIX) Method(java.lang.reflect.Method) PayloadFormat(org.apache.camel.component.salesforce.internal.PayloadFormat) ServiceHelper(org.apache.camel.util.ServiceHelper) AbstractSObjectBase(org.apache.camel.component.salesforce.api.dto.AbstractSObjectBase) RestClient(org.apache.camel.component.salesforce.internal.client.RestClient) AsyncCallback(org.apache.camel.AsyncCallback) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) APEX_METHOD(org.apache.camel.component.salesforce.SalesforceEndpointConfig.APEX_METHOD) URLEncoder(java.net.URLEncoder) List(java.util.List) TypeConverter(org.apache.camel.TypeConverter) SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) SOBJECT_EXT_ID_NAME(org.apache.camel.component.salesforce.SalesforceEndpointConfig.SOBJECT_EXT_ID_NAME) DefaultRestClient(org.apache.camel.component.salesforce.internal.client.DefaultRestClient) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) SalesforceEndpointConfig(org.apache.camel.component.salesforce.SalesforceEndpointConfig) API_VERSION(org.apache.camel.component.salesforce.SalesforceEndpointConfig.API_VERSION) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InputStream(java.io.InputStream) Message(org.apache.camel.Message) InputStream(java.io.InputStream) ApprovalRequest(org.apache.camel.component.salesforce.api.dto.approval.ApprovalRequest) TypeConverter(org.apache.camel.TypeConverter) SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) ApprovalRequests(org.apache.camel.component.salesforce.api.dto.approval.ApprovalRequests) List(java.util.List)

Example 2 with ApprovalRequest

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))));
}
Also used : ApprovalRequests(org.apache.camel.component.salesforce.api.dto.approval.ApprovalRequests) ApprovalRequest(org.apache.camel.component.salesforce.api.dto.approval.ApprovalRequest) Test(org.junit.Test)

Example 3 with ApprovalRequest

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")));
}
Also used : ApprovalRequests(org.apache.camel.component.salesforce.api.dto.approval.ApprovalRequests) ApprovalRequest(org.apache.camel.component.salesforce.api.dto.approval.ApprovalRequest) Test(org.junit.Test)

Example 4 with ApprovalRequest

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));
}
Also used : ApprovalRequests(org.apache.camel.component.salesforce.api.dto.approval.ApprovalRequests) HashMap(java.util.HashMap) ApprovalRequest(org.apache.camel.component.salesforce.api.dto.approval.ApprovalRequest) Test(org.junit.Test)

Example 5 with ApprovalRequest

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)));
}
Also used : ApprovalRequests(org.apache.camel.component.salesforce.api.dto.approval.ApprovalRequests) ApprovalRequest(org.apache.camel.component.salesforce.api.dto.approval.ApprovalRequest) Test(org.junit.Test)

Aggregations

ApprovalRequest (org.apache.camel.component.salesforce.api.dto.approval.ApprovalRequest)12 ApprovalRequests (org.apache.camel.component.salesforce.api.dto.approval.ApprovalRequests)9 Test (org.junit.Test)9 HashMap (java.util.HashMap)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 ParallelParameterized (com.googlecode.junittoolbox.ParallelParameterized)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 URLEncoder (java.net.URLEncoder)1 Arrays (java.util.Arrays)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 StreamSupport (java.util.stream.StreamSupport)1 AsyncCallback (org.apache.camel.AsyncCallback)1 Exchange (org.apache.camel.Exchange)1