package gov.healthit.chpl;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;


@Configuration
@EnableWebSecurity
@ComponentScan(basePackages = {"gov.healthit.chpl.**"}, excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = Configuration.class)})
public class CHPLTestConfig  {
	
	
	public static final String DEFAULT_PROPERTIES_FILE = "environment.test.properties";
	
	protected Properties props;
	
	protected void loadProperties() throws IOException {
		InputStream in = this.getClass().getClassLoader().getResourceAsStream(DEFAULT_PROPERTIES_FILE);
		
		if (in == null)
		{
			props = null;
			throw new FileNotFoundException("Environment Properties File not found in class path.");
		}
		else
		{
			props = new Properties();
			props.load(in);
		}
	}
	
	@Bean
	public org.springframework.orm.jpa.LocalEntityManagerFactoryBean entityManagerFactory(){
		
		if (props == null){
			try {
				loadProperties();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		org.springframework.orm.jpa.LocalEntityManagerFactoryBean bean = new org.springframework.orm.jpa.LocalEntityManagerFactoryBean();
		bean.setPersistenceUnitName(props.getProperty("persistenceUnitName"));
		return bean;
	}
	
	@Bean
	public org.springframework.orm.jpa.JpaTransactionManager transactionManager(){
		org.springframework.orm.jpa.JpaTransactionManager bean = new org.springframework.orm.jpa.JpaTransactionManager();
		bean.setEntityManagerFactory(entityManagerFactory().getObject());
		return bean;
	}
	
	@Bean
	public org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor persistenceAnnotationBeanPostProcessor(){
		return new org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor();
	}
}

@@ -148,24 +148,56 @@
 perties;%0D%0A%0D%0A
+import javax.sql.DataSource;%0D%0A%0D%0A
 import org.s
@@ -420,16 +420,240 @@
 rType;%0D%0A
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;%0D%0Aimport org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;%0D%0Aimport org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;%0D%0A
 import o
@@ -1459,32 +1459,322 @@
 );%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%09%0D%0A
+%09%0D%0A%09@Bean%0D%0A%09public DataSource dataSource() %7B%0D%0A%0D%0A%09%09EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();%0D%0A%09%09EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.DERBY).addScript(%22db/sql/create-db.sql%22).addScript(%22db/sql/insert-data.sql%22).build();%0D%0A%09%09return db;%0D%0A%0D%0A%09%7D%0D%0A%09%0D%0A%09%0D%0A
 %09@Bean%0D%0A%09public 
@@ -1798,32 +1798,41 @@
 rk.orm.jpa.Local
+Container
 EntityManagerFac
@@ -2023,32 +2023,41 @@
 rk.orm.jpa.Local
+Container
 EntityManagerFac
@@ -2109,16 +2109,25 @@
 pa.Local
+Container
 EntityMa
@@ -2161,67 +2161,30 @@
 .set
-PersistenceUnitName(props.getProperty(%22persistenceUnitName%22
+DataSource(dataSource(
 ));%0D

work on test config.