Skip to content
Browse files

Merge pull request #1 from garyrussell/support-updates

Add create Method to SObjectOperations
  • Loading branch information...
2 parents 5e99ba4 + 7341718 commit cdfe7fbf8062c78846c45abaf79dc7231a64b4a5 @umut umut committed
View
5 .gitignore
@@ -1,4 +1,7 @@
target
*.iml
*.ipr
-.idea
+.idea
+.project
+.settings
+.classpath
View
2 src/main/java/org/springframework/social/salesforce/api/SObjectOperations.java
@@ -21,4 +21,6 @@
public InputStream getBlob(String name, String id, String field);
+ Map<?, ?> create(String name, Map<String, String> fields);
+
}
View
14 src/main/java/org/springframework/social/salesforce/api/impl/SObjectsTemplate.java
@@ -1,7 +1,10 @@
package org.springframework.social.salesforce.api.impl;
import org.codehaus.jackson.JsonNode;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
+import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.social.salesforce.api.SObjectDetail;
import org.springframework.social.salesforce.api.SObjectOperations;
@@ -26,7 +29,6 @@
private RestTemplate restTemplate;
-
public SObjectsTemplate(Salesforce api, RestTemplate restTemplate) {
super(api);
this.restTemplate = restTemplate;
@@ -75,4 +77,14 @@ public InputStream extractData(ClientHttpResponse response) throws IOException {
}, name, id, field);
}
+ @Override
+ @SuppressWarnings("rawtypes")
+ public Map<?, ?> create(String name, Map<String, String> fields) {
+ requireAuthorization();
+ HttpHeaders headers = new HttpHeaders();
+ headers.setContentType(MediaType.APPLICATION_JSON);
+ HttpEntity<Map> entity = new HttpEntity<Map>(fields, headers);
+ return restTemplate.postForObject(api.getBaseUrl() + "/v23.0/sobjects/{name}", entity, Map.class, name);
+ }
+
}
View
16 src/test/java/org/springframework/social/salesforce/api/impl/SObjectsTemplateTest.java
@@ -9,12 +9,14 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.springframework.http.HttpMethod.GET;
+import static org.springframework.http.HttpMethod.POST;
import static org.springframework.social.test.client.RequestMatchers.method;
import static org.springframework.social.test.client.RequestMatchers.requestTo;
import static org.springframework.social.test.client.ResponseCreators.withResponse;
@@ -78,4 +80,18 @@ public void getBlob() throws IOException {
assertEquals("does-not-matter", reader.readLine());
}
+ @Test
+ public void testCreate() throws IOException {
+ mockServer.expect(requestTo("https://na7.salesforce.com/services/data/v23.0/sobjects/Lead"))
+ .andExpect(method(POST))
+ .andRespond(withResponse(new ByteArrayResource("{\"Id\" : \"1234\"}".getBytes("UTF-8")), responseHeaders));
+ Map<String, String> fields = new HashMap<String, String>();
+ fields.put("LastName", "Doe");
+ fields.put("FirstName", "John");
+ fields.put("Company", "Acme, Inc.");
+ Map<?, ?> result = salesforce.sObjectsOperations().create("Lead", fields);
+ assertEquals(1, result.size());
+ assertEquals("1234", result.get("Id"));
+ }
+
}

0 comments on commit cdfe7fb

Please sign in to comment.
Something went wrong with that request. Please try again.