Skip to content

Commit

Permalink
Add create Method to SObjectOperations
Browse files Browse the repository at this point in the history
Allows creating objects such as Leads, etc.
  • Loading branch information
garyrussell committed May 10, 2012
1 parent 5e99ba4 commit 7341718
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .gitignore
@@ -1,4 +1,7 @@
target
*.iml
*.ipr
.idea
.idea
.project
.settings
.classpath
Expand Up @@ -21,4 +21,6 @@ public interface SObjectOperations {

public InputStream getBlob(String name, String id, String field);

Map<?, ?> create(String name, Map<String, String> fields);

}
@@ -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;
Expand All @@ -26,7 +29,6 @@ public class SObjectsTemplate extends AbstractSalesForceOperations<Salesforce> i

private RestTemplate restTemplate;


public SObjectsTemplate(Salesforce api, RestTemplate restTemplate) {
super(api);
this.restTemplate = restTemplate;
Expand Down Expand Up @@ -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);
}

}
Expand Up @@ -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;
Expand Down Expand Up @@ -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 7341718

Please sign in to comment.