Spring Custom Profiles Configuration and Testing

@ActiveProfiles for Junit

import static org.junit.Assert.*;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import com.example.config.HerokuDataSourceConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={JpaConfiguration.class, HerokuDataSourceConfiguration.class})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@ActiveProfiles(profiles={"heroku"})
@Transactional
public class CustomerJpaConfigTest {

	@PersistenceContext
	EntityManager entityManager;
	
	@Test
	public void testJpaConfiguration() {
		assertTrue(entityManager.isOpen());
	}

}

AnnotationConfigApplicationContext setActiveProfiles to active profile

AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
    applicationContext.getEnvironment().setActiveProfiles("heroku");
    applicationContext.scan(JdbcDoaImpl.class.getPackage().getName());
    applicationContext.refresh();

Maven run test for active profile

mvn jetty:run -Dspring.profiles.active="heroku"

Salesforce Integration JUnit Testing Using Spring

Spring Configuration to test integration to Salesforce using PartnerConnection, EnterpriseConnection and BulkConnection. Create a properties file called salesforcesync.properties with all the login details

salesforcesync.properties

salesforce.sandbox.username=***your username ***
salesforce.sandbox.password=***your password + security token ***
salesforce.sandbox.url=https://test.salesforce.com/services/Soap/u/29.0
salesforce.sandbox.enterprise.url=https://test.salesforce.com/services/Soap/c/29.0/
salesforce.sandbox.version=29.0

salesforce.production.username=***your username ***
salesforce.production.password=***your password + security token ***
salesforce.production.url=https://login.salesforce.com/services/Soap/u/29.0
salesforce.production.enterprise.url=https://login.salesforce.com/services/Soap/c/29.0
salesforce.production.version=29.0

Salesforce Authentication Spring Configuration

package com.sforce.authentication;

import javax.inject.Inject;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import com.sforce.async.AsyncApiException;
import com.sforce.async.BulkConnection;

import com.sforce.soap.partner.Connector;
import com.sforce.soap.partner.PartnerConnection;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;

/**
 * @author tmichels
 */
@PropertySource("classpath:/salesforcesync.properties")
@Configuration
public class SalesforceAuthenticationConfigImpl implements SalesforceAuthenticationConfig {

	@Inject
    private Environment environment;
		    
	private PartnerConnection partnerConnection = null;
	private BulkConnection bulkConnection = null;
	
    @Bean(name="loginToProductionSalesforce")
	public PartnerConnection loginToProductionSalesforce() {
    	try {
    		ConnectorConfig config = new ConnectorConfig();
    		config.setUsername(environment.getProperty("salesforce.production.username"));
    		config.setPassword(environment.getProperty("salesforce.production.password"));
			partnerConnection = Connector.newConnection(config);
		} catch (ConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return partnerConnection;
	}

	@Bean(name="loginToSandboxSalesforce")
	public PartnerConnection loginToSandboxSalesforce() {
		try {
			ConnectorConfig config = new ConnectorConfig();
			config.setUsername(environment.getProperty("salesforce.sandbox.username"));
			config.setPassword(environment.getProperty("salesforce.sandbox.password"));
			config.setAuthEndpoint(environment.getProperty("salesforce.sandbox.url"));
			partnerConnection = Connector.newConnection(config);
		} catch (ConnectionException ce) {
			ce.printStackTrace();
		}
		return partnerConnection;
	}

	
	@Bean(name="bulkLoginToProductionSalesforce")
	public BulkConnection bulkLoginToProductionSalesforce() {
		try {
			
			ConnectorConfig partnerConfig = new ConnectorConfig();
			partnerConfig.setUsername(environment.getProperty("salesforce.production.username"));
			partnerConfig.setPassword(environment.getProperty("salesforce.production.password"));
			partnerConfig.setAuthEndpoint(environment.getProperty("salesforce.production.url"));
			partnerConnection = Connector.newConnection(partnerConfig);
			
		    ConnectorConfig config = new ConnectorConfig();
		    config.setSessionId(partnerConfig.getSessionId());
		    String soapEndpoint = partnerConfig.getServiceEndpoint();
		    String restEndpoint = soapEndpoint.substring(0, soapEndpoint.indexOf("Soap/"))
		        + "async/" + environment.getProperty("salesforce.sandbox.version");
		    config.setRestEndpoint(restEndpoint);
		    // This should only be false when doing debugging.
		    config.setCompression(true);
		    // Set this to true to see HTTP requests and responses on stdout
		    config.setTraceMessage(false);
		    bulkConnection = new BulkConnection(config);
		} catch (AsyncApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bulkConnection;
	}

	@Bean(name="bulkLoginToSandboxSalesforce")
	public BulkConnection bulkLoginToSandboxSalesforce() {
		try {
			ConnectorConfig partnerConfig = new ConnectorConfig();
			partnerConfig.setUsername(environment.getProperty("salesforce.sandbox.username"));
			partnerConfig.setPassword(environment.getProperty("salesforce.sandbox.password"));
			partnerConfig.setAuthEndpoint(environment.getProperty("salesforce.sandbox.url"));
			partnerConnection = Connector.newConnection(partnerConfig);
			
		    ConnectorConfig bulkconfig = new ConnectorConfig();
		    bulkconfig.setSessionId(partnerConfig.getSessionId());
		    String soapEndpoint = partnerConfig.getServiceEndpoint();
		    String restEndpoint = soapEndpoint.substring(0, soapEndpoint.indexOf("Soap/"))
		        + "async/" + environment.getProperty("salesforce.sandbox.version");
		     bulkconfig.setRestEndpoint(restEndpoint);
		    // This should only be false when doing debugging.
		    bulkconfig.setCompression(true);
		    // Set this to true to see HTTP requests and responses on stdout
		    bulkconfig.setTraceMessage(false);
		    bulkConnection = new BulkConnection(bulkconfig);
		} catch (AsyncApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bulkConnection;
	}

	@Bean(name="loginToSalesforceSandboxEnterprise")
	public EnterpriseConnection loginToSandboxSalesforceEnterprise() {
		try {
			ConnectorConfig config = new ConnectorConfig();
			config.setUsername(environment.getProperty("salesforce.sandbox.username"));
			config.setPassword(environment.getProperty("salesforce.sandbox.password"));
			config.setAuthEndpoint(environment.getProperty("salesforce.sandbox.enterprise.url"));
			config.setValidateSchema(true);
			config.setCompression(false);
			config.setConnectionTimeout(360000);
			config.setTraceMessage(true);
			config.setPrettyPrintXml(true);
			enterpriseConnection = new EnterpriseConnection(config);
		} catch (ConnectionException ce) {
			ce.printStackTrace();
		}
		return enterpriseConnection;
	}

	@Bean(name="loginToSalesforceProductionEnterprise")
	public EnterpriseConnection loginToProductionSalesforceEnterprise() {
		try {
			ConnectorConfig config = new ConnectorConfig();
			config.setUsername(environment.getProperty("salesforce.production.username"));
			config.setPassword(environment.getProperty("salesforce.production.password"));
			config.setAuthEndpoint(environment.getProperty("salesforce.production.url"));
			config.setValidateSchema(true);
			config.setCompression(false);
			config.setConnectionTimeout(360000);
			config.setTraceMessage(true);
			config.setPrettyPrintXml(true);
		
			enterpriseConnection = new EnterpriseConnection(config);
		} catch (ConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return enterpriseConnection;
	}
}

Junit test class to assert login was successful

package com.sforce.authentication;

import static org.junit.Assert.*;

import javax.inject.Inject;
import javax.inject.Named;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;

import com.sforce.async.BulkConnection;
import com.sforce.authentication.SalesforceAuthenticationConfig;
import com.sforce.authentication.SalesforceAuthenticationConfigImpl;

import com.sforce.soap.partner.PartnerConnection;
import com.sforce.ws.ConnectionException;

/**
 * @author tmichels
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=AnnotationConfigContextLoader.class, classes={SalesforceAuthenticationConfigImpl.class})
public class SalesforceAuthenticationServiceTest {

	@Inject @Named("loginToProductionSalesforce") PartnerConnection loginToProductionPartner;
	@Inject @Named("loginToSandboxSalesforce") PartnerConnection loginToSandboxPartner;
	@Inject @Named("bulkLoginToProductionSalesforce") BulkConnection loginToProductionBulkConnection;
	@Inject @Named("bulkLoginToSandboxSalesforce") BulkConnection loginToSandboxBulkConnection;
	@Inject @Named("loginToSalesforceSandboxEnterprise") EnterpriseConnection loginToSalesforceEnterpriseConnection;

	@Test
	public void testLoginToProductionSalesforce() {
		assertNotNull(loginToProductionPartner.getConfig().getSessionId());
		try {
			loginToProductionPartner.logout();
		} catch (ConnectionException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testLoginToSandboxSalesforce(){
		assertNotNull(loginToSandboxPartner.getConfig().getSessionId());
		try {
			loginToSandboxPartner.logout();
		} catch (ConnectionException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testBulkLoginToProductionSalesforce(){
		assertNotNull(loginToProductionBulkConnection.getConfig().getSessionId());
	}
	
	@Test
	public void testBulkLoginToSandboxSalesforce(){
		assertNotNull(loginToSandboxBulkConnection.getConfig().getSessionId());
	}
	
	@Test
	public void testLoginToSalesforceEnterprise(){
		assertNotNull(loginToSalesforceEnterpriseConnection.getConfig().getSessionId());
	}
}

Spring Jpa Transaction Manager Junit Testing

Setup Jpa Transaction Manager

package com.sforce.jpa;

import javax.inject.Inject;
import javax.inject.Named;
import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.sforce.datasource.DataSourceConfigImpl;
import com.sforce.model.DWAccount;

@PropertySource("classpath:/salesforcesync.properties")
@Configuration
@EnableTransactionManagement
@Import( { DataSourceConfigImpl.class })
public class JpaConfigurationImpl {
	
	 @Inject private Environment environment;
	 @Inject @Named("testDwDataSource") private DataSource testDwDataSource;
		
	 @Bean
	 public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws Exception {
	        LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
	        localContainerEntityManagerFactoryBean.setDataSource(testDwDataSource);
	        localContainerEntityManagerFactoryBean.setPackagesToScan(DWAccount.class.getPackage().getName());
	       
	        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
	        jpaVendorAdapter.setGenerateDdl(false);
	        jpaVendorAdapter.setShowSql(true);
	        jpaVendorAdapter.setDatabasePlatform(environment.getProperty("dataSource.dialect"));
	        
	        localContainerEntityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
	        return localContainerEntityManagerFactoryBean;
	    }
	 
	 @Bean
	 public PlatformTransactionManager transactionManager() throws Exception {
		 JpaTransactionManager transactionManager = new JpaTransactionManager();
	     transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
	     return transactionManager;
	 }
}

Test Jpa Transaction Manager

package com.sforce.jpa;

import static org.junit.Assert.*;

import javax.inject.Inject;
import javax.inject.Named;


import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
@Transactional
@ContextConfiguration(classes={JpaConfigurationImpl.class})
public class JpaTransactionConfigTest {

	@PersistenceContext
        EntityManager entityManager;
	
	@Test
	public void testDWAccountInsert() {
		DWAccount newDWAccount = new DWAccount();
		newDWAccount.setId(new Long(1234));
		entityManager.persist(newDWAccount);
                assertNotNull(entityManager.find(DWAccount.class, new Long(1234));
	}
}

Datapower Deployment Scenarios

This article explains on the Datapower Deployment Scenarios and their specific functions inside an enterprise environment.

1. Lab environment – this is an isolated environment that allows testing of any major new firmware release features to be tested without any impact on ongoing development streams. This assist change management of new features and testing new feature before implementation.

2. Development environment – this is an very common practice to isolate Datapower service development to a dedicated environment. This will usually be an single appliance for developers as an black sandbox to develop as well as do project-specific configurations.

3. Testing environment – this is an isolated test environment from the development environment mentioned above. This environment is used to test all developed services. The appliance provide easy to service migration between appliances and domains.

4. Staging environment – the environment allows testing pre-releases, or rolling new releases into production. The environment is used to do performance testing to determine sizing and scaling of production appliances.

5. Production environment – appliances in the production environment can be deployed as a cluster in an active/passive configuration or active/active configuration. Appliances can balance traffic to target servers using the Application Optimization feature.

6. DR environment – Many organization require full data center failover to a second fully equipped site. The DR environment will provide failover appliance.

soapUI: Datapower XML Firewall and Multi protocol Gateway Performance Testing

soapUI has limited support for testing standard HTML Web interfaces on top of the existing REST testing support.

Set up your local endpoints for your XML Firewall and Multi Protocol Gateway in SOAPUI:

For more information on this please visit the following link:

http://www.soapui.org/Web-/-HTTP/getting-started.html