public SalesforceCreateSObjectComponent() { super("salesforce-create-sobject", SalesforceCreateSObjectComponent.class.getName()); setAfterProducer( exchange -> { // map json response back to CreateSObjectResult POJO ObjectMapper mapper = JsonUtils.createObjectMapper(); if (!exchange.isFailed()) { Message out = exchange.getOut(); CreateSObjectResult result = mapper.readValue(out.getBody(String.class), CreateSObjectResult.class); out.setBody(result); } }); }
private void doTestCreateUpdateDelete(String suffix) throws Exception { Merchandise__c merchandise = new Merchandise__c(); merchandise.setName("Wee Wee Wee Plane"); merchandise.setDescription__c("Microlite plane"); merchandise.setPrice__c(2000.0); merchandise.setTotal_Inventory__c(50.0); CreateSObjectResult result = template().requestBody("direct:createSObject" + suffix, merchandise, CreateSObjectResult.class); assertNotNull(result); assertTrue("Create success", result.getSuccess()); LOG.debug("Create: " + result); // test JSON update // make the plane cheaper merchandise.setPrice__c(1500.0); // change inventory to half merchandise.setTotal_Inventory__c(25.0); // also need to set the Id merchandise.setId(result.getId()); assertNull(template().requestBodyAndHeader("direct:updateSObject" + suffix, merchandise, SalesforceEndpointConfig.SOBJECT_ID, result.getId())); LOG.debug("Update successful"); // delete the newly created SObject assertNull(template().requestBody("direct:deleteSObject" + suffix, result.getId())); LOG.debug("Delete successful"); }
private void doTestCreateUpdateDeleteWithId(String suffix) throws Exception { // get line item with Name 1 Line_Item__c lineItem = template().requestBody("direct:getSObjectWithId" + suffix, TEST_LINE_ITEM_ID, Line_Item__c.class); assertNotNull(lineItem); LOG.debug("GetWithId: {}", lineItem); // test insert with id // set the unit price and sold lineItem.setUnit_Price__c(1000.0); lineItem.setUnits_Sold__c(50.0); // update line item with Name NEW_LINE_ITEM_ID lineItem.setName(NEW_LINE_ITEM_ID); CreateSObjectResult result = template().requestBodyAndHeader("direct:upsertSObject" + suffix, lineItem, SalesforceEndpointConfig.SOBJECT_EXT_ID_VALUE, NEW_LINE_ITEM_ID, CreateSObjectResult.class); assertNotNull(result); assertTrue(result.getSuccess()); LOG.debug("CreateWithId: {}", result); // clear read only parent type fields lineItem.setInvoice_Statement__c(null); lineItem.setMerchandise__c(null); // change the units sold lineItem.setUnits_Sold__c(25.0); // update line item with Name NEW_LINE_ITEM_ID result = template().requestBodyAndHeader("direct:upsertSObject" + suffix, lineItem, SalesforceEndpointConfig.SOBJECT_EXT_ID_VALUE, NEW_LINE_ITEM_ID, CreateSObjectResult.class); assertNull(result); LOG.debug("UpdateWithId: {}", result); // delete the SObject with Name NEW_LINE_ITEM_ID assertNull(template().requestBody("direct:deleteSObjectWithId" + suffix, NEW_LINE_ITEM_ID)); LOG.debug("DeleteWithId successful"); }
private void doTestStatus400(String suffix) throws Exception { // clone test merchandise with same external Id if (testId == null) { // execute getBasicInfo to get test id from recent items doTestGetBasicInfo(""); } // get test merchandise // note that the header value overrides sObjectFields in endpoint Merchandise__c merchandise = template().requestBodyAndHeader("direct:getSObject" + suffix, testId, "sObjectFields", "Description__c,Price__c", Merchandise__c.class); assertNotNull(merchandise); assertNotNull(merchandise.getPrice__c()); assertNull(merchandise.getTotal_Inventory__c()); merchandise.clearBaseFields(); // required field Total_Inventory__c is missing CreateSObjectResult result = null; try { result = template().requestBody("direct:createSObject" + suffix, merchandise, CreateSObjectResult.class); fail("Expected SalesforceException with statusCode 400"); } catch (CamelExecutionException e) { assertTrue(e.getCause() instanceof SalesforceException); assertTrue(e.getCause().getCause() instanceof SalesforceException); final SalesforceException cause = (SalesforceException) e.getCause().getCause(); assertEquals(400, cause.getStatusCode()); assertEquals(1, cause.getErrors().size()); assertEquals("[Total_Inventory__c]", cause.getErrors().get(0).getFields().toString()); } finally { // delete the clone if created if (result != null) { template().requestBody("direct:deleteSObject" + suffix, result.getId()); } } }
@Override protected void processRequest(Exchange exchange) throws SalesforceException { switch (operationName) { case GET_VERSIONS: // handle in built response types exchange.setProperty(RESPONSE_TYPE, new TypeReference<List<Version>>() { }); break; case GET_RESOURCES: // handle in built response types exchange.setProperty(RESPONSE_CLASS, RestResources.class); break; case GET_GLOBAL_OBJECTS: // handle in built response types exchange.setProperty(RESPONSE_CLASS, GlobalObjects.class); break; case GET_BASIC_INFO: // handle in built response types exchange.setProperty(RESPONSE_CLASS, SObjectBasicInfo.class); break; case GET_DESCRIPTION: // handle in built response types exchange.setProperty(RESPONSE_CLASS, SObjectDescription.class); break; case CREATE_SOBJECT: // handle known response type exchange.setProperty(RESPONSE_CLASS, CreateSObjectResult.class); break; case UPSERT_SOBJECT: // handle known response type exchange.setProperty(RESPONSE_CLASS, CreateSObjectResult.class); break; case SEARCH: // handle known response type exchange.setProperty(RESPONSE_TYPE, new TypeReference<List<SearchResult>>() { }); break; default: // ignore, some operations do not require response class or type } }
@Override protected void processRequest(Exchange exchange) throws SalesforceException { switch (operationName) { case GET_VERSIONS: exchange.setProperty(RESPONSE_CLASS, Versions.class); break; case GET_RESOURCES: exchange.setProperty(RESPONSE_CLASS, RestResources.class); break; case GET_GLOBAL_OBJECTS: // handle in built response types exchange.setProperty(RESPONSE_CLASS, GlobalObjects.class); break; case GET_BASIC_INFO: // handle in built response types exchange.setProperty(RESPONSE_CLASS, SObjectBasicInfo.class); // need to add alias for Salesforce XML that uses SObject name as root element exchange.setProperty(RESPONSE_ALIAS, getParameter(SOBJECT_NAME, exchange, USE_BODY, NOT_OPTIONAL)); break; case GET_DESCRIPTION: // handle in built response types exchange.setProperty(RESPONSE_CLASS, SObjectDescription.class); // need to add alias for Salesforce XML that uses SObject name as root element exchange.setProperty(RESPONSE_ALIAS, getParameter(SOBJECT_NAME, exchange, USE_BODY, NOT_OPTIONAL)); break; case GET_SOBJECT: // need to add alias for Salesforce XML that uses SObject name as root element exchange.setProperty(RESPONSE_ALIAS, getParameter(SOBJECT_NAME, exchange, IGNORE_BODY, NOT_OPTIONAL)); break; case CREATE_SOBJECT: // handle known response type exchange.setProperty(RESPONSE_CLASS, CreateSObjectResult.class); break; case GET_SOBJECT_WITH_ID: // need to add alias for Salesforce XML that uses SObject name as root element exchange.setProperty(RESPONSE_ALIAS, getParameter(SOBJECT_NAME, exchange, IGNORE_BODY, NOT_OPTIONAL)); break; case UPSERT_SOBJECT: // handle known response type exchange.setProperty(RESPONSE_CLASS, CreateSObjectResult.class); break; case QUERY: case QUERY_MORE: // need to add alias for Salesforce XML that uses SObject name as root element exchange.setProperty(RESPONSE_ALIAS, "QueryResult"); break; case SEARCH: // handle known response type exchange.setProperty(RESPONSE_CLASS, SearchResults.class); break; case APEX_CALL: // need to add alias for Salesforce XML that uses SObject name as root element exchange.setProperty(RESPONSE_ALIAS, "response"); break; default: // ignore, some operations do not require alias or class exchange properties } }
private void doTestStatus300(String suffix) throws Exception { // clone test merchandise with same external Id if (testId == null) { // execute getBasicInfo to get test id from recent items doTestGetBasicInfo(""); } // get test merchandise // note that the header value overrides sObjectFields in endpoint Merchandise__c merchandise = template().requestBodyAndHeader("direct:getSObject" + suffix, testId, "sObjectFields", "Name,Description__c,Price__c,Total_Inventory__c", Merchandise__c.class); assertNotNull(merchandise); assertNotNull(merchandise.getName()); assertNotNull(merchandise.getPrice__c()); assertNotNull(merchandise.getTotal_Inventory__c()); CreateSObjectResult result = null; try { merchandise.clearBaseFields(); result = template().requestBody("direct:createSObject" + suffix, merchandise, CreateSObjectResult.class); assertNotNull(result); assertNotNull(result.getId()); LOG.debug("Clone SObject: {}", result); // look by external Id to cause 300 error // note that the request SObject overrides settings on the endpoint for LineItem__c try { template().requestBody("direct:getSObjectWithId" + suffix, merchandise, Merchandise__c.class); fail("Expected SalesforceException with statusCode 300"); } catch (CamelExecutionException e) { assertTrue(e.getCause() instanceof SalesforceException); assertTrue(e.getCause().getCause() instanceof SalesforceMultipleChoicesException); final SalesforceMultipleChoicesException cause = (SalesforceMultipleChoicesException) e.getCause().getCause(); assertEquals(300, cause.getStatusCode()); final List<String> choices = cause.getChoices(); assertNotNull(choices); assertFalse(choices.isEmpty()); LOG.debug("Multiple choices: {}", choices); } } finally { // delete the test clone if (result != null) { template().requestBody("direct:deleteSObject" + suffix, result.getId()); } } }
@Test public void testSubscribeAndReceive() throws Exception { MockEndpoint mock = getMockEndpoint("mock:CamelTestTopic"); mock.expectedMessageCount(1); // assert expected static headers mock.expectedHeaderReceived("CamelSalesforceTopicName", "CamelTestTopic"); mock.expectedHeaderReceived("CamelSalesforceChannel", "/topic/CamelTestTopic"); Merchandise__c merchandise = new Merchandise__c(); merchandise.setName("TestNotification"); merchandise.setDescription__c("Merchandise for testing Streaming API updated on " + new DateTime().toString()); merchandise.setPrice__c(9.99); merchandise.setTotal_Inventory__c(1000.0); CreateSObjectResult result = template().requestBody( "direct:upsertSObject", merchandise, CreateSObjectResult.class); assertTrue("Merchandise test record not created", result == null || result.getSuccess()); try { // wait for Salesforce notification mock.assertIsSatisfied(); final Message in = mock.getExchanges().get(0).getIn(); merchandise = in.getMandatoryBody(Merchandise__c.class); assertNotNull("Missing event body", merchandise); log.info("Merchandise notification: {}", merchandise.toString()); assertNotNull("Missing field Id", merchandise.getId()); assertNotNull("Missing field Name", merchandise.getName()); // validate dynamic message headers assertNotNull("Missing header CamelSalesforceClientId", in.getHeader("CamelSalesforceClientId")); assertNotNull("Missing header CamelSalesforceEventType", in.getHeader("CamelSalesforceEventType")); assertNotNull("Missing header CamelSalesforceCreatedDate", in.getHeader("CamelSalesforceCreatedDate")); } finally { // remove the test record assertNull(template().requestBody("direct:deleteSObjectWithId", merchandise)); // remove the test topic // find it using SOQL first QueryRecordsPushTopic records = template().requestBody("direct:query", null, QueryRecordsPushTopic.class); assertEquals("Test topic not found", 1, records.getTotalSize()); assertNull(template().requestBody("direct:deleteSObject", records.getRecords().get(0))); } }
private void doTestTypes(String suffix) { Account account = new Account(); account.setName("Camel Test Account"); account.setBillingCity("San Francisco"); account.setBillingCountry("USA"); account.setBillingPostalCode("94105"); account.setBillingState("CA"); account.setBillingStreet("1 Market St #300"); account.setBillingLatitude(37.793779); account.setBillingLongitude(-122.39448); account.setShippingCity("San Francisco"); account.setShippingCountry("USA"); account.setShippingPostalCode("94105"); account.setShippingState("CA"); account.setShippingStreet("1 Market St #300"); account.setShippingLatitude(37.793779); account.setShippingLongitude(-122.39448); account.setShipping_Location__Latitude__s(37.793779); account.setShipping_Location__Longitude__s(-122.39448); CreateSObjectResult result = template().requestBody("direct:createSObject" + suffix, account, CreateSObjectResult.class); assertNotNull(result); assertTrue("Create success", result.getSuccess()); LOG.debug("Create: " + result); try { // get account with compound fields account = template().requestBody("direct:getSObject" + suffix, result.getId(), Account.class); assertNotNull(account); assertNotNull("Billing Address", account.getBillingAddress()); assertNotNull("Shipping Address", account.getShippingAddress()); assertNotNull("Shipping Location", account.getShippingAddress()); LOG.debug("Retrieved fields billing address: {}, shipping location: {}", account.getBillingAddress(), account.getShipping_Location__c()); } finally { // delete the test SObject assertNull(template().requestBody("direct:deleteSObject" + suffix, result.getId())); LOG.debug("Delete successful"); } }