// Path: src/main/java/org/springframework/social/salesforce/api/Salesforce.java
// public interface Salesforce extends ApiBinding {
// 
//     public ApiOperations apiOperations();
// 
//     public ApiOperations apiOperations(String instanceUrl);
// 
//     public ChatterOperations chatterOperations();
// 
//     public ChatterOperations chatterOperations(String instanceUrl);
// 
//     public QueryOperations queryOperations();
// 
//     public QueryOperations queryOperations(String instanceUrl);
// 
//     public RecentOperations recentOperations();
// 
//     public RecentOperations recentOperations(String instanceUrl);
// 
//     public SearchOperations searchOperations();
// 
//     public SearchOperations searchOperations(String instanceUrl);
// 
//     public SObjectOperations sObjectsOperations();
// 
//     public SObjectOperations sObjectsOperations(String instanceUrl);
// 
//     public UserOperations userOperations();
// 
//     public UserOperations userOperations(String gatewayUrl);
// 
//     public LimitsOperations limitsOperations();
// 
//     public LimitsOperations limitsOperations(String instanceUrl);
// 
//     public <T> List<T> readList(JsonNode jsonNode, Class<T> type);
// 
//     public <T> T readObject(JsonNode jsonNode, Class<T> type);
// 
//     public String getBaseUrl();
// 
//     public String getInstanceUrl();
// 
//     public void setInstanceUrl(String instanceUrl);
// 
//     public String getUserInfoUrl();
// 
//     public String getAuthGatewayUrl();
// 
//     public void setAuthGatewayBaseUrl(String gatewayUrl);
// 
//     public ConnectOperations connectOperations();
// 
//     public ConnectOperations connectOperations(String instanceUrl);
//     
//     public CustomApiOperations customApiOperations();
// 
//     public CustomApiOperations customApiOperations(String instanceUrl);
// }
// 
// Path: src/main/java/org/springframework/social/salesforce/client/BaseSalesforceFactory.java
// public class BaseSalesforceFactory implements SalesforceFactory {
// 
//     private final static String DEFAULT_AUTH_URL = SalesforceServiceProvider.PRODUCTION_GATEWAY_URL + SalesforceServiceProvider.TOKEN_PATH;
//     
// 
//     private String clientId;
// 
//     private String clientSecret;
// 
//     private String authorizeUrl = DEFAULT_AUTH_URL;
// 
//     private RestTemplate restTemplate;
// 
// 
//     public BaseSalesforceFactory(String clientId, String clientSecret) {
//         this(clientId, clientSecret, createRestTemplate());
//     }
// 
//     public BaseSalesforceFactory(String clientId, String clientSecret, RestTemplate restTemplate) {
//         this.clientId = clientId;
//         this.clientSecret = clientSecret;
//         this.restTemplate = restTemplate;
//     }
// 
// 
//     public void setAuthorizeUrl(String authorizeUrl) {
//         this.authorizeUrl = authorizeUrl;
//     }
//     
//     public String getAuthorizeUrl() {
//       return (this.authorizeUrl == null) ? DEFAULT_AUTH_URL : this.authorizeUrl;
//     }
// 
//     @Override
//     @SuppressWarnings("unchecked")
//     public Salesforce create(String username, String password, String securityToken) {
//         MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
//         map.add("grant_type", "password");
//         map.add("client_id", this.clientId);
//         map.add("client_secret", this.clientSecret);
//         map.add("username", username);
//         map.add("password", password + (securityToken == null ? "" : securityToken));
// 
//         Map<String, String> token = restTemplate.postForObject(this.authorizeUrl, map, Map.class);
//         SalesforceTemplate template = new SalesforceTemplate(token.get("access_token"));
//         String instanceUrl = token.get("instance_url");
//         if (instanceUrl != null) {
//             template.setInstanceUrl(instanceUrl);
//         }
//         return template;
//     }
// 
//     private static RestTemplate createRestTemplate() {
//         RestTemplate restTemplate = new RestTemplate();
//         restTemplate.setRequestFactory(ClientHttpRequestFactorySelector.getRequestFactory());
//         restTemplate.setErrorHandler(new ErrorHandler());
//         return restTemplate;
//     }
// 
// }


import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.social.salesforce.api.Salesforce;
import org.springframework.social.salesforce.client.BaseSalesforceFactory;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestTemplate;
import static org.junit.Assert.assertNotNull;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.content;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
/**
 * Copyright (C) 2017 https://github.com/jottley/spring-social-salesforce
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.social.salesforce.api.client;



/**
 * @author Umut Utkan
 * @author Jared Ottley
 */
public class BaseSalesforceFactoryTest {

    @Test
    public void testClientLogin() {
        RestTemplate restTemplate = new RestTemplate();
        MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);

        mockServer.expect(requestTo("https://login.salesforce.com/services/oauth2/token"))
                .andExpect(method(POST))
                .andExpect(content().string("grant_type=password&client_id=client-id&client_secret=client-secret&username=my-username&password=my-passwordsecurity-token"))
                .andRespond(withStatus(HttpStatus.OK).body(loadResource("client-token.json")).headers(responseHeaders));

// Path: src/main/java/org/springframework/social/salesforce/api/Salesforce.java
// public interface Salesforce extends ApiBinding {
// 
//     public ApiOperations apiOperations();
// 
//     public ApiOperations apiOperations(String instanceUrl);
// 
//     public ChatterOperations chatterOperations();
// 
//     public ChatterOperations chatterOperations(String instanceUrl);
// 
//     public QueryOperations queryOperations();
// 
//     public QueryOperations queryOperations(String instanceUrl);
// 
//     public RecentOperations recentOperations();
// 
//     public RecentOperations recentOperations(String instanceUrl);
// 
//     public SearchOperations searchOperations();
// 
//     public SearchOperations searchOperations(String instanceUrl);
// 
//     public SObjectOperations sObjectsOperations();
// 
//     public SObjectOperations sObjectsOperations(String instanceUrl);
// 
//     public UserOperations userOperations();
// 
//     public UserOperations userOperations(String gatewayUrl);
// 
//     public LimitsOperations limitsOperations();
// 
//     public LimitsOperations limitsOperations(String instanceUrl);
// 
//     public <T> List<T> readList(JsonNode jsonNode, Class<T> type);
// 
//     public <T> T readObject(JsonNode jsonNode, Class<T> type);
// 
//     public String getBaseUrl();
// 
//     public String getInstanceUrl();
// 
//     public void setInstanceUrl(String instanceUrl);
// 
//     public String getUserInfoUrl();
// 
//     public String getAuthGatewayUrl();
// 
//     public void setAuthGatewayBaseUrl(String gatewayUrl);
// 
//     public ConnectOperations connectOperations();
// 
//     public ConnectOperations connectOperations(String instanceUrl);
//     
//     public CustomApiOperations customApiOperations();
// 
//     public CustomApiOperations customApiOperations(String instanceUrl);
// }
// 
// Path: src/main/java/org/springframework/social/salesforce/client/BaseSalesforceFactory.java
// public class BaseSalesforceFactory implements SalesforceFactory {
// 
//     private final static String DEFAULT_AUTH_URL = SalesforceServiceProvider.PRODUCTION_GATEWAY_URL + SalesforceServiceProvider.TOKEN_PATH;
//     
// 
//     private String clientId;
// 
//     private String clientSecret;
// 
//     private String authorizeUrl = DEFAULT_AUTH_URL;
// 
//     private RestTemplate restTemplate;
// 
// 
//     public BaseSalesforceFactory(String clientId, String clientSecret) {
//         this(clientId, clientSecret, createRestTemplate());
//     }
// 
//     public BaseSalesforceFactory(String clientId, String clientSecret, RestTemplate restTemplate) {
//         this.clientId = clientId;
//         this.clientSecret = clientSecret;
//         this.restTemplate = restTemplate;
//     }
// 
// 
//     public void setAuthorizeUrl(String authorizeUrl) {
//         this.authorizeUrl = authorizeUrl;
//     }
//     
//     public String getAuthorizeUrl() {
//       return (this.authorizeUrl == null) ? DEFAULT_AUTH_URL : this.authorizeUrl;
//     }
// 
//     @Override
//     @SuppressWarnings("unchecked")
//     public Salesforce create(String username, String password, String securityToken) {
//         MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
//         map.add("grant_type", "password");
//         map.add("client_id", this.clientId);
//         map.add("client_secret", this.clientSecret);
//         map.add("username", username);
//         map.add("password", password + (securityToken == null ? "" : securityToken));
// 
//         Map<String, String> token = restTemplate.postForObject(this.authorizeUrl, map, Map.class);
//         SalesforceTemplate template = new SalesforceTemplate(token.get("access_token"));
//         String instanceUrl = token.get("instance_url");
//         if (instanceUrl != null) {
//             template.setInstanceUrl(instanceUrl);
//         }
//         return template;
//     }
// 
//     private static RestTemplate createRestTemplate() {
//         RestTemplate restTemplate = new RestTemplate();
//         restTemplate.setRequestFactory(ClientHttpRequestFactorySelector.getRequestFactory());
//         restTemplate.setErrorHandler(new ErrorHandler());
//         return restTemplate;
//     }
// 
// }


// Path: src/test/java/org/springframework/social/salesforce/api/client/BaseSalesforceFactoryTest.java
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.social.salesforce.api.Salesforce;
import org.springframework.social.salesforce.client.BaseSalesforceFactory;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestTemplate;
import static org.junit.Assert.assertNotNull;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.content;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;

/**
 * Copyright (C) 2017 https://github.com/jottley/spring-social-salesforce
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.social.salesforce.api.client;



/**
 * @author Umut Utkan
 * @author Jared Ottley
 */
public class BaseSalesforceFactoryTest {

    @Test
    public void testClientLogin() {
        RestTemplate restTemplate = new RestTemplate();
        MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);

        mockServer.expect(requestTo("https://login.salesforce.com/services/oauth2/token"))
                .andExpect(method(POST))
                .andExpect(content().string("grant_type=password&client_id=client-id&client_secret=client-secret&username=my-username&password=my-passwordsecurity-token"))
                .andRespond(withStatus(HttpStatus.OK).body(loadResource("client-token.json")).headers(responseHeaders));

        Salesforce template = new BaseSalesforceFactory("client-id", "client-secret", restTemplate)