View Javadoc

1   package org.kuali.student.r2.common.dao;
2   
3   
4   import org.junit.Before;
5   import org.junit.Test;
6   import org.junit.runner.RunWith;
7   import org.kuali.student.common.util.query.QueryUtil;
8   import org.kuali.student.r2.common.exceptions.DoesNotExistException;
9   import org.kuali.student.r2.core.class1.state.dao.StateDao;
10  import org.kuali.student.r2.core.class1.state.model.StateEntity;
11  import org.springframework.beans.factory.annotation.Autowired;
12  import org.springframework.test.context.ContextConfiguration;
13  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14  import org.springframework.test.context.transaction.TransactionConfiguration;
15  import org.springframework.transaction.annotation.Transactional;
16  
17  import javax.persistence.TypedQuery;
18  import java.util.ArrayList;
19  import java.util.Date;
20  import java.util.HashSet;
21  import java.util.List;
22  import java.util.Set;
23  
24  import static org.junit.Assert.*;
25  
26  @RunWith(SpringJUnit4ClassRunner.class)
27  @ContextConfiguration(locations = {"classpath:state-test-context.xml"})
28  @TransactionConfiguration(transactionManager = "JtaTxManager", defaultRollback = true)
29  @Transactional
30  public class TestGenericEntityDao {
31      private static final int MAXIMUM_ENTITIES = 400;
32  
33      @Autowired
34      private StateDao stateDao;
35  
36      private List<String> primaryKeys = new ArrayList<String>();
37  
38      private String queryString;
39  
40      private class TestStateDao extends GenericEntityDao<StateEntity> {
41          @Override
42          public List<StateEntity> findByIds(String primaryKeyMemberName, List<String> primaryKeys) throws DoesNotExistException {
43  
44              // fix for jira KSENROLL-2949
45              if (primaryKeys == null || primaryKeys.isEmpty())
46                  return new ArrayList<StateEntity>();
47  
48              Set<String>primaryKeySet = new HashSet<String>(primaryKeys.size());
49              // remove duplicates from the key list
50              primaryKeySet.addAll(primaryKeys);
51  
52              StringBuilder queryStringRef = new StringBuilder();
53              queryStringRef.append("from ").append(entityClass.getSimpleName()).append(" where ");
54  
55              TypedQuery<StateEntity> query = QueryUtil.buildQuery(em, maxInClauseElements, queryStringRef, null, primaryKeyMemberName, primaryKeys, StateEntity.class);
56  
57              List<StateEntity> resultList = query.getResultList();
58  
59              verifyResults(resultList, primaryKeySet);
60  
61              queryString = queryStringRef.toString();
62  
63              return resultList;
64          }
65      }
66  
67      private TestStateDao testStateDao = new TestStateDao();
68  
69      @Before
70      public void setUp() {
71  
72          testStateDao.setEm(stateDao.getEm());
73          //Loading data and preparing primaryKeys list.
74          for(int i = 0; i < MAXIMUM_ENTITIES; i++) {
75  
76              StateEntity stateEntity = new StateEntity();
77              stateEntity.setCreateId("SYSTEMLOADER");
78              stateEntity.setCreateTime(new Date());
79              stateEntity.setDescrFormatted("The time slot is active " + i);
80              stateEntity.setDescrPlain("The time slot is active"+i);
81              stateEntity.setId("kuali.scheduling.timeslot.state.active" + i);
82              stateEntity.setInitialState(false);
83              stateEntity.setLifecycleKey("kuali.scheduling.timeslot.lifecycle"+i);
84              stateEntity.setName("Active"+i);
85              stateEntity.setVersionNumber(0L);
86              testStateDao.persist(stateEntity);
87  
88              primaryKeys.add(stateEntity.getId());
89          }
90  
91          testStateDao.em.flush();
92          queryString = "";
93  
94      }
95  
96      @Test
97      public void testStateDao() {
98  
99          try {
100             List<StateEntity> result = testStateDao.findByIds("id", primaryKeys);
101             int queryCount = getSubstringCount(queryString, "IN");
102 
103             assertNotNull("Result should be non-null", result);
104             assertEquals(MAXIMUM_ENTITIES + " entities should be returned", MAXIMUM_ENTITIES, result.size());
105             assertEquals("We have asked for " + MAXIMUM_ENTITIES + " ids, so the query should have 4 'in' clauses", queryCount, 4);
106 
107             // set the DAO to not allow splitting up of query
108             testStateDao.setMaxInClauseElements(500);
109             // re-run the query
110             result = testStateDao.findByIds("id", primaryKeys);
111             queryCount = getSubstringCount(queryString, "IN");
112 
113             assertNotNull("Result should be non-null", result);
114             assertEquals(MAXIMUM_ENTITIES + " entities should be returned", MAXIMUM_ENTITIES, result.size());
115             assertEquals("Since we disabled splitting up of query, it should have only one 'in' clause", queryCount, 1);
116         } catch(Exception e) {
117             e.printStackTrace();
118         }
119 
120     }
121 
122     @Test
123     public void testStateDaoWithNullAndEmptyPrimaryKeys() throws Exception {
124         // run query with null primary keys
125         List<StateEntity> result = testStateDao.findByIds("id", null);
126         assertNotNull("Result should be non-null", result);
127         assertTrue("Result should be empty", result.isEmpty());
128         assertTrue("Since the query never ran, the query string should be empty", queryString.isEmpty());
129 
130         // re-run query with empty primary keys
131         testStateDao.findByIds("id", new ArrayList<String>());
132         assertNotNull("Result should be non-null", result);
133         assertTrue("Result should be empty", result.isEmpty());
134         assertTrue("Since the query never ran, the query string should be empty", queryString.isEmpty());
135     }
136 
137     private int getSubstringCount(String fullString, String subString){
138 
139         int lastIndex = 0;
140         int count =0;
141 
142         while(lastIndex != -1){
143 
144             lastIndex = fullString.indexOf(subString,lastIndex);
145 
146             if( lastIndex != -1){
147                 count ++;
148                 lastIndex+=subString.length();
149             }
150         }
151         return count;
152     }
153 }