package au.com.rds.rdsc.datafetcher;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
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 java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Ke Lin
* @version $Id$
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
locations = "classpath:META-INF/spring/applicationContext-test.xml"
)
@TransactionConfiguration(transactionManager = "txManager", defaultRollback = true)
public class DataFetcherRepositoryTest
{
@Autowired
private DataFetcherRepository repository;
@Test
@Transactional
public void testFindOne()
{
DataFetcher fetcher = repository.findOne(1L);
Assert.assertNotNull("Should be able to find the data fetcher row with id 1", fetcher);
Assert.assertEquals(fetcher.getId(), Long.valueOf(1L));
}
@Test
@Transactional
public void testRunTheFirstFetcher()
{
DataFetcher fetcher = repository.findOne(1L);
List<Map<String, Object>> resultList = repository.executeDataFetcher(fetcher);
Assert.assertEquals("Should get a result list of size 1", resultList.size(), 1);
Map<String, Object> result = resultList.get(0);
Assert.assertEquals("Should have an ID property which equals to 1", BigDecimal.valueOf(1), result.get("ID"));
Assert.assertEquals("Should have a DESCRIPTION property 'return a single row, id = 1'", "return a single row, id = 1", result.get("DESCRIPTION"));
}
@Test
@Transactional
public void testRunTheSecondOneWhichRequiresParameters()
{
DataFetcher fetcher = repository.findOne(2L);
Map<String, Object> params = new HashMap<String, Object>();
params.put("id", 3);
List<Map<String, Object>> resultList = repository.executeDataFetcher(fetcher, params);
Assert.assertEquals("Should get a result list of size 1", resultList.size(), 1);
Map<String, Object> result = resultList.get(0);
Assert.assertEquals("Should have an ID property which equals to 3", BigDecimal.valueOf(3), result.get("ID"));
Assert.assertEquals("Should have a DESCRIPTION property 'return multiple rows'", "return multiple rows", result.get("DESCRIPTION"));
}
@Test
@Transactional
public void testRunTheThirdOneWhichReturnsMultipleRows()
{
DataFetcher fetcher = repository.findOne(3L);
List<Map<String, Object>> resultList = repository.executeDataFetcher(fetcher);
Assert.assertEquals("Should get a result list of size 3", resultList.size(), 3);
}
}
INSERT INTO rdsc_data_fetcher VALUES(1, 'select id, description from rdsc_data_fetcher where id = 1', 'test 1 desc', 'test security expression 1');
INSERT INTO rdsc_data_fetcher VALUES(2, 'select id, description from rdsc_data_fetcher where id = 2', 'test 2 desc', 'test security expression 2');